From 59e2015fa79ebd41194c87a20d1dd58458c87f34 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 15 Jun 2020 20:28:04 +0300 Subject: [PATCH] Add option to resend bridge info to all portals --- config/bridge.go | 1 + example-config.yaml | 4 ++++ go.mod | 2 +- go.sum | 2 ++ main.go | 17 +++++++++++++++++ portal.go | 5 +++++ 6 files changed, 30 insertions(+), 1 deletion(-) diff --git a/config/bridge.go b/config/bridge.go index b18e118..f948369 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -62,6 +62,7 @@ type BridgeConfig struct { InviteOwnPuppetForBackfilling bool `yaml:"invite_own_puppet_for_backfilling"` PrivateChatPortalMeta bool `yaml:"private_chat_portal_meta"` + ResendBridgeInfo bool `yaml:"resend_bridge_info"` WhatsappThumbnail bool `yaml:"whatsapp_thumbnail"` diff --git a/example-config.yaml b/example-config.yaml index 3953e85..09fc99c 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -141,6 +141,10 @@ bridge: # chat portal rooms. This can be useful if the previous field works fine, # but causes room avatar/name bugs. private_chat_portal_meta: false + # Set this to true to tell the bridge to re-send m.bridge events to all rooms on the next run. + # This field will automatically be changed back to false after it, + # except if the config file is not writable. + resend_bridge_info: false # Whether or not thumbnails from WhatsApp should be sent. # They're disabled by default due to very low resolution. diff --git a/go.mod b/go.mod index a902f09..507079a 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( gopkg.in/yaml.v2 v2.3.0 maunium.net/go/mauflag v1.0.0 maunium.net/go/maulogger/v2 v2.1.1 - maunium.net/go/mautrix v0.5.0-rc.3 + maunium.net/go/mautrix v0.5.0 ) replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.2.8 diff --git a/go.sum b/go.sum index fa7586d..a7f9c59 100644 --- a/go.sum +++ b/go.sum @@ -69,3 +69,5 @@ maunium.net/go/mautrix v0.5.0-rc.2 h1:ohx+dprvMS6Txm+suMx5pbjl0rjDpfftFxgXhx/+Us maunium.net/go/mautrix v0.5.0-rc.2/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY= maunium.net/go/mautrix v0.5.0-rc.3 h1:ltb6mF6pck1YzEkuC13V4UtSGDIxaq+XqzIdSg7vgMI= maunium.net/go/mautrix v0.5.0-rc.3/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY= +maunium.net/go/mautrix v0.5.0 h1:lEVvdrF5leCon005PDs8yO8VTLgEFZ8lWATaRmwCj0M= +maunium.net/go/mautrix v0.5.0/go.mod h1:LnkFnB1yjCbb8V+upoEHDGvI/F38NHSTWYCe2RRJgSY= diff --git a/main.go b/main.go index 7833d95..490f58b 100644 --- a/main.go +++ b/main.go @@ -283,6 +283,23 @@ func (bridge *Bridge) Start() { go bridge.Crypto.Start() } go bridge.StartUsers() + + if bridge.Config.Bridge.ResendBridgeInfo { + go bridge.ResendBridgeInfo() + } +} + +func (bridge *Bridge) ResendBridgeInfo() { + bridge.Config.Bridge.ResendBridgeInfo = false + err := bridge.Config.Save(*configPath) + if err != nil { + bridge.Log.Errorln("Failed to save config after setting resend_bridge_info to false:", err) + } + bridge.Log.Infoln("Re-sending bridge info state event to all portals") + for _, portal := range bridge.GetAllPortals() { + portal.UpdateBridgeInfo() + } + bridge.Log.Infoln("Finished re-sending bridge info state events") } func (bridge *Bridge) LoadRelaybot() { diff --git a/portal.go b/portal.go index ba401d9..e58b732 100644 --- a/portal.go +++ b/portal.go @@ -835,6 +835,11 @@ func (portal *Portal) getBridgeInfo() (string, event.Content) { } func (portal *Portal) UpdateBridgeInfo() { + if len(portal.MXID) == 0 { + portal.log.Debugln("Not updating bridge info: no Matrix room created") + return + } + portal.log.Debugln("Updating bridge info...") stateKey, content := portal.getBridgeInfo() _, err := portal.MainIntent().SendStateEvent(portal.MXID, StateBridgeInfo, stateKey, content) if err != nil {