浏览代码

Add hacky option to mute all guild channels on create

Tulir Asokan 2 年之前
父节点
当前提交
e2bd89bd97
共有 5 个文件被更改,包括 32 次插入5 次删除
  1. 1 0
      config/bridge.go
  2. 1 0
      config/upgrade.go
  3. 3 0
      example-config.yaml
  4. 3 0
      puppet.go
  5. 24 5
      user.go

+ 1 - 0
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"`

+ 1 - 0
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")

+ 3 - 0
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.

+ 3 - 0
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
 }
 

+ 24 - 5
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) syncChatDoublePuppetDetails(portal *Portal, justCreated bool) {
-	doublePuppet := portal.bridge.GetPuppetByCustomMXID(user.MXID)
-	if doublePuppet == nil {
+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)
+	}
+}
 
-	if doublePuppet == nil || doublePuppet.CustomIntent() == nil || portal.MXID == "" {
+func (user *User) syncChatDoublePuppetDetails(portal *Portal, justCreated bool) {
+	doublePuppetIntent := portal.bridge.GetPuppetByCustomMXID(user.MXID).CustomIntent()
+	if doublePuppetIntent == nil || portal.MXID == "" {
 		return
 	}
 
-	// TODO sync mute status
+	// TODO sync mute status properly
+	if portal.GuildID != "" && user.bridge.Config.Bridge.MuteChannelsOnCreate {
+		go user.mutePortal(doublePuppetIntent, portal, false)
+	}
 }
 
 func (user *User) Login(token string) error {