From a2f00425365ed8951f41963a342f0d94c35ba7ac Mon Sep 17 00:00:00 2001 From: Annie Elequin Date: Fri, 17 Sep 2021 12:41:14 -0400 Subject: [PATCH] okay try some different things --- portal.go | 30 +++++++++++++++++++++++------- user.go | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/portal.go b/portal.go index d8be64f..8db23dd 100644 --- a/portal.go +++ b/portal.go @@ -190,16 +190,32 @@ type Portal struct { const MaxMessageAgeToCreatePortal = 5 * 60 // 5 minutes +func (portal *Portal) syncDoublePuppetDetailsAfterCreate(source *User) { + doublePuppet := portal.bridge.GetPuppetByCustomMXID(source.MXID) + if doublePuppet == nil { + return + } + source.Conn.Store.ChatsLock.RLock() + chat, ok := source.Conn.Store.Chats[portal.Key.JID] + source.Conn.Store.ChatsLock.RUnlock() + if !ok { + portal.log.Debugln("Not syncing chat mute/tags with %s: chat info not found", source.MXID) + return + } + source.syncChatDoublePuppetDetails(doublePuppet, Chat{ + Chat: chat, + Portal: portal, + }, true) +} + + func (portal *Portal) handleMessageLoop() { for msg := range portal.messages { if len(portal.MXID) == 0 { if msg.timestamp+MaxMessageAgeToCreatePortal < uint64(time.Now().Unix()) { portal.log.Debugln("Not creating portal room for incoming message: message is too old") continue - } else if !portal.shouldCreateRoom(msg) { - portal.log.Debugln("Not creating portal room for incoming message: message is not a chat message") - continue - } + } portal.log.Debugln("Creating Matrix room from incoming message") err := portal.CreateMatrixRoom(msg.source) if err != nil { @@ -214,13 +230,13 @@ func (portal *Portal) handleMessageLoop() { } } -func (portal *Portal) handleMessage(msg PortalMessage) { +func (portal *Portal) handleMessage(msg PortalMessage, isBackfill bool) { if len(portal.MXID) == 0 { portal.log.Warnln("handleMessage called even though portal.MXID is empty") return } portal.HandleTextMessage(msg.source, msg.data) - portal.handleReaction(msg.data.ID.String(), msg.data.FavoritedBy) + // portal.handleReaction(msg.data.ID.String(), msg.data.FavoritedBy) } func (portal *Portal) isRecentlyHandled(id groupme.ID) bool { @@ -968,7 +984,7 @@ func (portal *Portal) CreateMatrixRoom(user *User) error { } else { portal.log.Debugln("else: it's not a private chat") var err error - metadata, err = user.Conn.GetGroupMetaData(portal.Key.JID) + metadata, err = user.Client.ShowGroup(context.TODO(), groupme.ID(portal.Key.JID)) if err == nil && metadata.Status == 0 { portal.Name = metadata.Name portal.Topic = metadata.Topic diff --git a/user.go b/user.go index 0e589a7..ccc0928 100644 --- a/user.go +++ b/user.go @@ -677,6 +677,29 @@ func (user *User) getDirectChats() map[id.UserID][]id.RoomID { return res } +func (user *User) syncChatDoublePuppetDetails(doublePuppet *Puppet, chat Chat, justCreated bool) { + if doublePuppet == nil || doublePuppet.CustomIntent() == nil || len(chat.Portal.MXID) == 0 { + return + } + intent := doublePuppet.CustomIntent() + if chat.UnreadCount == 0 && (justCreated || !user.bridge.Config.Bridge.MarkReadOnlyOnCreate) { + lastMessage := user.bridge.DB.Message.GetLastInChatBefore(chat.Portal.Key, chat.ReceivedAt.Unix()) + if lastMessage != nil { + err := intent.MarkReadWithContent(chat.Portal.MXID, lastMessage.MXID, &CustomReadReceipt{DoublePuppet: true}) + if err != nil { + user.log.Warnfln("Failed to mark %s in %s as read after backfill: %v", lastMessage.MXID, chat.Portal.MXID, err) + } + } + } else if chat.UnreadCount == -1 { + user.log.Debugfln("Invalid unread count (missing field?) in chat info %+v", chat.Source) + } + if justCreated || !user.bridge.Config.Bridge.TagOnlyOnCreate { + user.updateChatMute(intent, chat.Portal, chat.MutedUntil) + user.updateChatTag(intent, chat.Portal, user.bridge.Config.Bridge.ArchiveTag, chat.IsArchived) + user.updateChatTag(intent, chat.Portal, user.bridge.Config.Bridge.PinnedTag, chat.IsPinned) + } +} + func (user *User) UpdateDirectChats(chats map[id.UserID][]id.RoomID) { if !user.bridge.Config.Bridge.SyncDirectChatList { return