From e2bd89bd97c8aea88f1e203f906e7159348cb6b5 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 20 Jan 2023 15:07:18 +0200 Subject: [PATCH] Add hacky option to mute all guild channels on create --- config/bridge.go | 1 + config/upgrade.go | 1 + example-config.yaml | 3 +++ puppet.go | 3 +++ user.go | 31 +++++++++++++++++++++++++------ 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index b7e97cf..f8678ab 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -42,6 +42,7 @@ type BridgeConfig struct { MessageErrorNotices bool `yaml:"message_error_notices"` RestrictedRooms bool `yaml:"restricted_rooms"` AutojoinThreadOnOpen bool `yaml:"autojoin_thread_on_open"` + MuteChannelsOnCreate bool `yaml:"mute_channels_on_create"` SyncDirectChatList bool `yaml:"sync_direct_chat_list"` ResendBridgeInfo bool `yaml:"resend_bridge_info"` DeletePortalOnChannelDelete bool `yaml:"delete_portal_on_channel_delete"` diff --git a/config/upgrade.go b/config/upgrade.go index d620bcd..a33ad3b 100644 --- a/config/upgrade.go +++ b/config/upgrade.go @@ -37,6 +37,7 @@ func DoUpgrade(helper *up.Helper) { helper.Copy(up.Bool, "bridge", "message_error_notices") helper.Copy(up.Bool, "bridge", "restricted_rooms") helper.Copy(up.Bool, "bridge", "autojoin_thread_on_open") + helper.Copy(up.Bool, "bridge", "mute_channels_on_create") helper.Copy(up.Bool, "bridge", "sync_direct_chat_list") helper.Copy(up.Bool, "bridge", "resend_bridge_info") helper.Copy(up.Bool, "bridge", "delete_portal_on_channel_delete") diff --git a/example-config.yaml b/example-config.yaml index dda0737..3eced6c 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -115,6 +115,9 @@ bridge: # Should the bridge automatically join the user to threads on Discord when the thread is opened on Matrix? # This only works with clients that support thread read receipts (MSC3771 added in Matrix v1.4). autojoin_thread_on_open: true + # Should guild channels be muted when the portal is created? This only meant for single-user instances, + # it won't mute it for all users if there are multiple Matrix users in the same Discord guild. + mute_channels_on_create: false # Should the bridge update the m.direct account data event when double puppeting is enabled. # Note that updating the m.direct event is not atomic (except with mautrix-asmux) # and is therefore prone to race conditions. diff --git a/puppet.go b/puppet.go index 2b8f82d..1fe6552 100644 --- a/puppet.go +++ b/puppet.go @@ -178,6 +178,9 @@ func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI { } func (puppet *Puppet) CustomIntent() *appservice.IntentAPI { + if puppet == nil { + return nil + } return puppet.customIntent } diff --git a/user.go b/user.go index 3e86cee..875c07c 100644 --- a/user.go +++ b/user.go @@ -22,6 +22,7 @@ import ( "maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" + "maunium.net/go/mautrix/pushrules" "go.mau.fi/mautrix-discord/database" ) @@ -392,17 +393,35 @@ func (user *User) ViewingChannel(portal *Portal) bool { return false } +func (user *User) mutePortal(intent *appservice.IntentAPI, portal *Portal, unmute bool) { + if len(portal.MXID) == 0 || !user.bridge.Config.Bridge.MuteChannelsOnCreate { + return + } + var err error + if unmute { + user.log.Debugfln("Unmuting portal %s", portal.MXID) + err = intent.DeletePushRule("global", pushrules.RoomRule, string(portal.MXID)) + } else { + user.log.Debugfln("Muting portal %s", portal.MXID) + err = intent.PutPushRule("global", pushrules.RoomRule, string(portal.MXID), &mautrix.ReqPutPushRule{ + Actions: []pushrules.PushActionType{pushrules.ActionDontNotify}, + }) + } + if err != nil && !errors.Is(err, mautrix.MNotFound) { + user.log.Warnfln("Failed to update push rule for %s through double puppet: %v", portal.MXID, err) + } +} + func (user *User) syncChatDoublePuppetDetails(portal *Portal, justCreated bool) { - doublePuppet := portal.bridge.GetPuppetByCustomMXID(user.MXID) - if doublePuppet == nil { + doublePuppetIntent := portal.bridge.GetPuppetByCustomMXID(user.MXID).CustomIntent() + if doublePuppetIntent == nil || portal.MXID == "" { return } - if doublePuppet == nil || doublePuppet.CustomIntent() == nil || portal.MXID == "" { - return + // TODO sync mute status properly + if portal.GuildID != "" && user.bridge.Config.Bridge.MuteChannelsOnCreate { + go user.mutePortal(doublePuppetIntent, portal, false) } - - // TODO sync mute status } func (user *User) Login(token string) error {