Просмотр исходного кода

Add typing notification bridging in both directions

Tulir Asokan 3 лет назад
Родитель
Сommit
2cd6713cc4
5 измененных файлов с 37 добавлено и 11 удалено
  1. 6 7
      custompuppet.go
  2. 1 1
      go.mod
  3. 2 2
      go.sum
  4. 2 1
      puppet.go
  5. 26 0
      user.go

+ 6 - 7
custompuppet.go

@@ -1,5 +1,5 @@
 // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
-// Copyright (C) 2020 Tulir Asokan
+// Copyright (C) 2021 Tulir Asokan
 //
 // This program is free software: you can redistribute it and/or modify
 // it under the terms of the GNU Affero General Public License as published by
@@ -221,12 +221,11 @@ func (puppet *Puppet) handleReceiptEvent(portal *Portal, event *event.Event) {
 			puppet.customUser.log.Debugfln("Ignoring double puppeted read receipt %+v", event.Content.Raw)
 			// Ignore double puppeted read receipts.
 		} else if message := puppet.bridge.DB.Message.GetByMXID(eventID); message != nil {
-			// TODO reimplement
-			//puppet.customUser.log.Debugfln("Marking %s/%s in %s/%s as read", message.JID, message.MXID, portal.Key.JID, portal.MXID)
-			//_, err := puppet.customUser.Client.Read(portal.Key.JID, message.JID)
-			//if err != nil {
-			//	puppet.customUser.log.Warnln("Error marking read:", err)
-			//}
+			puppet.customUser.log.Debugfln("Marking %s/%s in %s/%s as read", message.JID, message.MXID, portal.Key.JID, portal.MXID)
+			err := puppet.customUser.Client.MarkRead([]types.MessageID{message.JID}, time.UnixMilli(receipt.Timestamp), portal.Key.JID, message.Sender)
+			if err != nil {
+				puppet.customUser.log.Warnln("Error marking read:", err)
+			}
 		}
 	}
 }

+ 1 - 1
go.mod

@@ -8,7 +8,7 @@ require (
 	github.com/mattn/go-sqlite3 v1.14.9
 	github.com/prometheus/client_golang v1.11.0
 	github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
-	go.mau.fi/whatsmeow v0.0.0-20211027155822-952712ccbde8
+	go.mau.fi/whatsmeow v0.0.0-20211027162736-a1c695172891
 	golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
 	google.golang.org/protobuf v1.27.1
 	gopkg.in/yaml.v2 v2.4.0

+ 2 - 2
go.sum

@@ -139,8 +139,8 @@ github.com/tidwall/sjson v1.2.3 h1:5+deguEhHSEjmuICXZ21uSSsXotWMA0orU783+Z7Cp8=
 github.com/tidwall/sjson v1.2.3/go.mod h1:5WdjKx3AQMvCJ4RG6/2UYT7dLrGvJUV1x4jdTAyGvZs=
 go.mau.fi/libsignal v0.0.0-20211024113310-f9fc6a1855f2 h1:xpQTMgJGGaF+c8jV/LA/FVXAPJxZbSAGeflOc+Ly6uQ=
 go.mau.fi/libsignal v0.0.0-20211024113310-f9fc6a1855f2/go.mod h1:3XlVlwOfp8f9Wri+C1D4ORqgUsN4ZvunJOoPjQMBhos=
-go.mau.fi/whatsmeow v0.0.0-20211027155822-952712ccbde8 h1:fWoAGUXPQFJ//jsGEAHCKrM1ExU+ZQtEwWDX7wQB0d4=
-go.mau.fi/whatsmeow v0.0.0-20211027155822-952712ccbde8/go.mod h1:ODEmmqeUn9eBDQHFc1S902YA3YFLtmaBujYRRFl53jI=
+go.mau.fi/whatsmeow v0.0.0-20211027162736-a1c695172891 h1:QemoafkIJVRrCr8mKA9AUb1ACrTiv0wa7R/VtQV3Xwo=
+go.mau.fi/whatsmeow v0.0.0-20211027162736-a1c695172891/go.mod h1:ODEmmqeUn9eBDQHFc1S902YA3YFLtmaBujYRRFl53jI=
 golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

+ 2 - 1
puppet.go

@@ -23,6 +23,7 @@ import (
 	"net/http"
 	"regexp"
 	"sync"
+	"time"
 
 	"go.mau.fi/whatsmeow"
 	"go.mau.fi/whatsmeow/types"
@@ -154,7 +155,7 @@ type Puppet struct {
 	log    log.Logger
 
 	typingIn id.RoomID
-	typingAt int64
+	typingAt time.Time
 
 	MXID id.UserID
 

+ 26 - 0
user.go

@@ -434,6 +434,8 @@ func (user *User) HandleEvent(event interface{}) {
 		go user.syncPuppet(v.JID)
 	case *events.Receipt:
 		go user.handleReceipt(v)
+	case *events.ChatPresence:
+		go user.handleChatPresence(v)
 	case *events.Message:
 		portal := user.GetPortalByJID(v.Info.Chat)
 		portal.messages <- PortalMessage{evt: v, source: user}
@@ -730,6 +732,30 @@ func (user *User) syncPuppet(jid types.JID) {
 //	}
 //}
 
+const WATypingTimeout = 15 * time.Second
+
+func (user *User) handleChatPresence(presence *events.ChatPresence) {
+	puppet := user.bridge.GetPuppetByJID(presence.Sender)
+	portal := user.GetPortalByJID(presence.Chat)
+	if puppet == nil || portal == nil || len(portal.MXID) == 0 {
+		return
+	}
+	if presence.State == types.ChatPresenceComposing {
+		if puppet.typingIn != "" && puppet.typingAt.Add(WATypingTimeout).Before(time.Now()) {
+			if puppet.typingIn == portal.MXID {
+				return
+			}
+			_, _ = puppet.IntentFor(portal).UserTyping(puppet.typingIn, false, 0)
+		}
+		_, _ = puppet.IntentFor(portal).UserTyping(portal.MXID, true, WATypingTimeout.Milliseconds())
+		puppet.typingIn = portal.MXID
+		puppet.typingAt = time.Now()
+	} else {
+		_, _ = puppet.IntentFor(portal).UserTyping(portal.MXID, false, 0)
+		puppet.typingIn = ""
+	}
+}
+
 func (user *User) handleReceipt(receipt *events.Receipt) {
 	if receipt.Type != events.ReceiptTypeRead {
 		return