Mark Matrix room as read if WhatsApp chat is read after backfill

This commit is contained in:
Tulir Asokan 2021-03-22 14:16:04 +02:00
parent 24d172fd55
commit 9f64b357e0

12
user.go
View file

@ -440,6 +440,7 @@ func (user *User) Login(ce *CommandEvent) {
type Chat struct { type Chat struct {
Portal *Portal Portal *Portal
LastMessageTime uint64 LastMessageTime uint64
MarkAsRead bool
Contact whatsapp.Contact Contact whatsapp.Contact
} }
@ -679,6 +680,7 @@ func (user *User) syncPortals(chatMap map[string]whatsapp.Chat, createAll bool)
Portal: portal, Portal: portal,
Contact: user.Conn.Store.Contacts[chat.JID], Contact: user.Conn.Store.Contacts[chat.JID],
LastMessageTime: ts, LastMessageTime: ts,
MarkAsRead: chat.Unread == "0",
}) })
var inCommunity, ok bool var inCommunity, ok bool
if inCommunity, ok = existingKeys[portal.Key]; !ok || !inCommunity { if inCommunity, ok = existingKeys[portal.Key]; !ok || !inCommunity {
@ -706,6 +708,7 @@ func (user *User) syncPortals(chatMap map[string]whatsapp.Chat, createAll bool)
} }
now := uint64(time.Now().Unix()) now := uint64(time.Now().Unix())
user.log.Infoln("Syncing portals") user.log.Infoln("Syncing portals")
doublePuppet := user.bridge.GetPuppetByCustomMXID(user.MXID)
for i, chat := range chats { for i, chat := range chats {
if chat.LastMessageTime+user.bridge.Config.Bridge.SyncChatMaxAge < now { if chat.LastMessageTime+user.bridge.Config.Bridge.SyncChatMaxAge < now {
break break
@ -720,6 +723,15 @@ func (user *User) syncPortals(chatMap map[string]whatsapp.Chat, createAll bool)
if err != nil { if err != nil {
chat.Portal.log.Errorln("Error backfilling history:", err) chat.Portal.log.Errorln("Error backfilling history:", err)
} }
if chat.MarkAsRead && doublePuppet != nil && doublePuppet.CustomIntent() != nil {
lastMessage := user.bridge.DB.Message.GetLastInChat(chat.Portal.Key)
if lastMessage != nil {
err = doublePuppet.CustomIntent().MarkRead(chat.Portal.MXID, lastMessage.MXID)
if err != nil {
user.log.Warnln("Failed to mark %s in %s as read after backfill: %v", lastMessage.MXID, chat.Portal.MXID, err)
}
}
}
} }
} }
if user.Conn != connAtStart { if user.Conn != connAtStart {