Only fetch room tags once when syncing

This commit is contained in:
Tulir Asokan 2023-07-19 22:35:19 +03:00
parent ecae11f6b2
commit f57735901f

31
user.go
View file

@ -764,29 +764,21 @@ type CustomTagEventContent struct {
Tags map[string]CustomTagData `json:"tags"` Tags map[string]CustomTagData `json:"tags"`
} }
func (user *User) updateChatTag(portal *Portal, tag string, active bool) { func (user *User) updateChatTag(portal *Portal, tag string, active bool, existingTags CustomTagEventContent) {
intent := user.DoublePuppetIntent var err error
if intent == nil || len(portal.MXID) == 0 {
return
}
var existingTags CustomTagEventContent
err := intent.GetTagsWithCustomData(portal.MXID, &existingTags)
if err != nil && !errors.Is(err, mautrix.MNotFound) {
user.log.Warnfln("Failed to get tags of %s: %v", portal.MXID, err)
}
currentTag, ok := existingTags.Tags[tag] currentTag, ok := existingTags.Tags[tag]
if active && !ok { if active && !ok {
user.log.Debugln("Adding tag", tag, "to", portal.MXID) user.zlog.Debug().Str("tag", tag).Str("room_id", portal.MXID.String()).Msg("Adding room tag")
data := CustomTagData{Order: "0.5", DoublePuppet: user.bridge.Name} data := CustomTagData{Order: "0.5", DoublePuppet: user.bridge.Name}
err = intent.AddTagWithCustomData(portal.MXID, tag, &data) err = user.DoublePuppetIntent.AddTagWithCustomData(portal.MXID, tag, &data)
} else if !active && ok && currentTag.DoublePuppet == user.bridge.Name { } else if !active && ok && currentTag.DoublePuppet == user.bridge.Name {
user.log.Debugln("Removing tag", tag, "from", portal.MXID) user.zlog.Debug().Str("tag", tag).Str("room_id", portal.MXID.String()).Msg("Removing room tag")
err = intent.RemoveTag(portal.MXID, tag) err = user.DoublePuppetIntent.RemoveTag(portal.MXID, tag)
} else { } else {
err = nil err = nil
} }
if err != nil { if err != nil {
user.log.Warnfln("Failed to update tag %s for %s through double puppet: %v", tag, portal.MXID, err) user.zlog.Warn().Err(err).Str("room_id", portal.MXID.String()).Msg("Failed to update room tag")
} }
} }
@ -839,8 +831,13 @@ func (user *User) syncChatDoublePuppetDetails(portal *Portal, conv *gmproto.Conv
return return
} }
if justCreated || !user.bridge.Config.Bridge.TagOnlyOnCreate { if justCreated || !user.bridge.Config.Bridge.TagOnlyOnCreate {
user.updateChatTag(portal, user.bridge.Config.Bridge.ArchiveTag, conv.Status == gmproto.ConvUpdateTypes_ARCHIVED) var existingTags CustomTagEventContent
user.updateChatTag(portal, user.bridge.Config.Bridge.PinnedTag, conv.Pinned) err := user.DoublePuppetIntent.GetTagsWithCustomData(portal.MXID, &existingTags)
if err != nil && !errors.Is(err, mautrix.MNotFound) {
user.zlog.Warn().Err(err).Str("room_id", portal.MXID.String()).Msg("Failed to get existing room tags")
}
user.updateChatTag(portal, user.bridge.Config.Bridge.ArchiveTag, conv.Status == gmproto.ConvUpdateTypes_ARCHIVED, existingTags)
user.updateChatTag(portal, user.bridge.Config.Bridge.PinnedTag, conv.Pinned, existingTags)
} }
} }