Send double puppeting identifier with read receipts if server supports it

This commit is contained in:
Tulir Asokan 2021-05-18 15:23:19 +03:00
parent 62e806c8c3
commit 66f0b04a31
4 changed files with 23 additions and 15 deletions

View file

@ -215,19 +215,19 @@ func (puppet *Puppet) handlePresenceEvent(event *event.Event) {
func (puppet *Puppet) handleReceiptEvent(portal *Portal, event *event.Event) { func (puppet *Puppet) handleReceiptEvent(portal *Portal, event *event.Event) {
for eventID, receipts := range *event.Content.AsReceipt() { for eventID, receipts := range *event.Content.AsReceipt() {
if _, ok := receipts.Read[puppet.CustomMXID]; !ok { if receipt, ok := receipts.Read[puppet.CustomMXID]; !ok {
continue // Ignore receipt events where this user isn't present.
} } else if isDoublePuppeted, _ := receipt.Extra["net.maunium.whatsapp.puppet"].(bool); isDoublePuppeted {
message := puppet.bridge.DB.Message.GetByMXID(eventID) puppet.customUser.log.Debugfln("Ignoring double puppeted read receipt %+v", event.Content.Raw)
if message == nil { // Ignore double puppeted read receipts.
continue } else if message := puppet.bridge.DB.Message.GetByMXID(eventID); message != nil {
}
puppet.customUser.log.Debugfln("Marking %s/%s in %s/%s as read", message.JID, message.MXID, portal.Key.JID, portal.MXID) puppet.customUser.log.Debugfln("Marking %s/%s in %s/%s as read", message.JID, message.MXID, portal.Key.JID, portal.MXID)
_, err := puppet.customUser.Conn.Read(portal.Key.JID, message.JID) _, err := puppet.customUser.Conn.Read(portal.Key.JID, message.JID)
if err != nil { if err != nil {
puppet.customUser.log.Warnln("Error marking read:", err) puppet.customUser.log.Warnln("Error marking read:", err)
} }
} }
}
} }
func (puppet *Puppet) handleTypingEvent(portal *Portal, evt *event.Event) { func (puppet *Puppet) handleTypingEvent(portal *Portal, evt *event.Event) {

2
go.mod
View file

@ -12,7 +12,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
maunium.net/go/mauflag v1.0.0 maunium.net/go/mauflag v1.0.0
maunium.net/go/maulogger/v2 v2.2.4 maunium.net/go/maulogger/v2 v2.2.4
maunium.net/go/mautrix v0.9.11 maunium.net/go/mautrix v0.9.12
) )
replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.5.2 replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.5.2

2
go.sum
View file

@ -670,6 +670,8 @@ maunium.net/go/maulogger/v2 v2.2.4 h1:oV2GDeM4fx1uRysdpDC0FcrPg+thFicSd9XzPcYMbV
maunium.net/go/maulogger/v2 v2.2.4/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= maunium.net/go/maulogger/v2 v2.2.4/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A=
maunium.net/go/mautrix v0.9.11 h1:WY6mLVLM8MAM2znmqgsSFGVlSG7b3vvPMcKNrBOtK/o= maunium.net/go/mautrix v0.9.11 h1:WY6mLVLM8MAM2znmqgsSFGVlSG7b3vvPMcKNrBOtK/o=
maunium.net/go/mautrix v0.9.11/go.mod h1:7IzKfWvpQtN+W2Lzxc0rLvIxFM3ryKX6Ys3S/ZoWbg8= maunium.net/go/mautrix v0.9.11/go.mod h1:7IzKfWvpQtN+W2Lzxc0rLvIxFM3ryKX6Ys3S/ZoWbg8=
maunium.net/go/mautrix v0.9.12 h1:iNsKOCyK/BqN6Q/7A1aLj1B0/lMbr81LIg+Lry1Vjuo=
maunium.net/go/mautrix v0.9.12/go.mod h1:7IzKfWvpQtN+W2Lzxc0rLvIxFM3ryKX6Ys3S/ZoWbg8=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o= sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM= sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=

12
user.go
View file

@ -30,6 +30,7 @@ import (
"github.com/skip2/go-qrcode" "github.com/skip2/go-qrcode"
log "maunium.net/go/maulogger/v2" log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix/appservice" "maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/pushrules" "maunium.net/go/mautrix/pushrules"
@ -744,6 +745,11 @@ func (user *User) updateChatTag(intent *appservice.IntentAPI, portal *Portal, ta
} }
} }
type CustomReadReceipt struct {
Timestamp int64 `json:"ts,omitempty"`
DoublePuppet bool `json:"net.maunium.whatsapp.puppet,omitempty"`
}
func (user *User) syncChatDoublePuppetDetails(doublePuppet *Puppet, chat Chat, justCreated bool) { func (user *User) syncChatDoublePuppetDetails(doublePuppet *Puppet, chat Chat, justCreated bool) {
if doublePuppet == nil || doublePuppet.CustomIntent() == nil { if doublePuppet == nil || doublePuppet.CustomIntent() == nil {
return return
@ -752,7 +758,7 @@ func (user *User) syncChatDoublePuppetDetails(doublePuppet *Puppet, chat Chat, j
if chat.UnreadCount == 0 && (justCreated || !user.bridge.Config.Bridge.MarkReadOnlyOnCreate) { if chat.UnreadCount == 0 && (justCreated || !user.bridge.Config.Bridge.MarkReadOnlyOnCreate) {
lastMessage := user.bridge.DB.Message.GetLastInChat(chat.Portal.Key) lastMessage := user.bridge.DB.Message.GetLastInChat(chat.Portal.Key)
if lastMessage != nil { if lastMessage != nil {
err := intent.MarkRead(chat.Portal.MXID, lastMessage.MXID) err := intent.MarkReadWithContent(chat.Portal.MXID, lastMessage.MXID, &CustomReadReceipt{DoublePuppet: true})
if err != nil { if err != nil {
user.log.Warnln("Failed to mark %s in %s as read after backfill: %v", lastMessage.MXID, chat.Portal.MXID, err) user.log.Warnln("Failed to mark %s in %s as read after backfill: %v", lastMessage.MXID, chat.Portal.MXID, err)
} }
@ -1225,7 +1231,7 @@ func (user *User) HandleMsgInfo(info whatsapp.JSONMsgInfo) {
continue continue
} }
err := intent.MarkRead(portal.MXID, msg.MXID) err := intent.MarkReadWithContent(portal.MXID, msg.MXID, &CustomReadReceipt{DoublePuppet: intent.IsCustomPuppet})
if err != nil { if err != nil {
user.log.Warnln("Failed to mark message %s as read by %s: %v", msg.MXID, info.SenderJID, err) user.log.Warnln("Failed to mark message %s as read by %s: %v", msg.MXID, info.SenderJID, err)
} }
@ -1276,7 +1282,7 @@ func (user *User) markSelfRead(jid, messageID string) {
} }
user.log.Debugfln("User read message %s/%s in %s/%s in WhatsApp mobile", message.JID, message.MXID, portal.Key.JID, portal.MXID) user.log.Debugfln("User read message %s/%s in %s/%s in WhatsApp mobile", message.JID, message.MXID, portal.Key.JID, portal.MXID)
} }
err := intent.MarkRead(portal.MXID, message.MXID) err := intent.MarkReadWithContent(portal.MXID, message.MXID, &CustomReadReceipt{DoublePuppet: true})
if err != nil { if err != nil {
user.log.Warnfln("Failed to bridge own read receipt in %s: %v", jid, err) user.log.Warnfln("Failed to bridge own read receipt in %s: %v", jid, err)
} }