Эх сурвалжийг харах

Possibly fix and/or break missed message backfilling

Tulir Asokan 6 жил өмнө
parent
commit
666194b066
3 өөрчлөгдсөн 14 нэмэгдсэн , 9 устгасан
  1. 2 2
      go.mod
  2. 2 0
      go.sum
  3. 10 7
      portal.go

+ 2 - 2
go.mod

@@ -3,7 +3,7 @@ module maunium.net/go/mautrix-whatsapp
 go 1.12
 
 require (
-	github.com/Rhymen/go-whatsapp v0.0.2-0.20190524185555-8d76e32a6d8e
+	github.com/Rhymen/go-whatsapp v0.0.2
 	github.com/lib/pq v1.1.1
 	github.com/mattn/go-sqlite3 v1.10.0
 	github.com/pkg/errors v0.8.1
@@ -18,4 +18,4 @@ require (
 
 replace gopkg.in/russross/blackfriday.v2 => github.com/russross/blackfriday/v2 v2.0.1
 
-replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.0.2-0.20190527104455-0b656104de79
+replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.0.2-0.20190528182350-fde573a2a73b

+ 2 - 0
go.sum

@@ -92,6 +92,8 @@ github.com/tulir/go-whatsapp v0.0.2-0.20190523194501-cc7603f853df h1:6nSb5zdpr38
 github.com/tulir/go-whatsapp v0.0.2-0.20190523194501-cc7603f853df/go.mod h1:u3Hdptbz3iB5y/NEoSKgsp9hBzUlm0A5OrLMVdENAX8=
 github.com/tulir/go-whatsapp v0.0.2-0.20190527104455-0b656104de79 h1:+oTtiHOAVwQ5mK5LAmdHhJgq1BxGp/YSZCMPz8DFH78=
 github.com/tulir/go-whatsapp v0.0.2-0.20190527104455-0b656104de79/go.mod h1:u3Hdptbz3iB5y/NEoSKgsp9hBzUlm0A5OrLMVdENAX8=
+github.com/tulir/go-whatsapp v0.0.2-0.20190528182350-fde573a2a73b h1:t82If9TiBNCyZHSNVlOGymJZ3ewvEcw8glHanyidKv0=
+github.com/tulir/go-whatsapp v0.0.2-0.20190528182350-fde573a2a73b/go.mod h1:u3Hdptbz3iB5y/NEoSKgsp9hBzUlm0A5OrLMVdENAX8=
 go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
 golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20190222235706-ffb98f73852f h1:qWFY9ZxP3tfI37wYIs/MnIAqK0vlXp1xnYEa5HxFSSY=

+ 10 - 7
portal.go

@@ -522,23 +522,22 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error
 	if lastMessage == nil {
 		return nil
 	}
-	if lastMessage.Timestamp <= lastMessageTime {
+	if lastMessage.Timestamp >= lastMessageTime {
 		portal.log.Debugln("Not backfilling: no new messages")
 		return nil
 	}
 
 	lastMessageID := lastMessage.JID
+	lastMessageFromMe := lastMessage.Sender == user.JID
 	portal.log.Infoln("Backfilling history since", lastMessageID, "for", user.MXID)
 	for len(lastMessageID) > 0 {
 		portal.log.Debugln("Backfilling history: 50 messages after", lastMessageID)
-		resp, err := user.Conn.LoadMessagesAfter(portal.Key.JID, lastMessageID, 50)
+		resp, err := user.Conn.LoadMessagesAfter(portal.Key.JID, lastMessageID, lastMessageFromMe, 50)
 		if err != nil {
 			return err
 		}
 		messages, ok := resp.Content.([]interface{})
-		if !ok {
-			return fmt.Errorf("history response not a list")
-		} else if len(messages) == 0 {
+		if !ok || len(messages) == 0 {
 			break
 		}
 
@@ -547,6 +546,7 @@ func (portal *Portal) BackfillHistory(user *User, lastMessageTime uint64) error
 		lastMessageProto, ok := messages[len(messages)-1].(*waProto.WebMessageInfo)
 		if ok {
 			lastMessageID = lastMessageProto.GetKey().GetId()
+			lastMessageFromMe = lastMessageProto.GetKey().GetFromMe()
 		}
 	}
 	portal.log.Infoln("Backfilling finished")
@@ -563,6 +563,7 @@ func (portal *Portal) FillInitialHistory(user *User) error {
 	portal.log.Infoln("Filling initial history, maximum", n, "messages")
 	var messages []interface{}
 	before := ""
+	fromMe := true
 	chunkNum := 1
 	for n > 0 {
 		count := 100
@@ -570,7 +571,7 @@ func (portal *Portal) FillInitialHistory(user *User) error {
 			count = n
 		}
 		portal.log.Debugfln("Fetching chunk %d (%d messages / %d cap) before message %s", chunkNum, count, n, before)
-		resp, err := user.Conn.LoadMessagesBefore(portal.Key.JID, before, count)
+		resp, err := user.Conn.LoadMessagesBefore(portal.Key.JID, before, fromMe, count)
 		if err != nil {
 			return err
 		}
@@ -587,7 +588,9 @@ func (portal *Portal) FillInitialHistory(user *User) error {
 		portal.log.Debugfln("Fetched chunk and received %d messages", len(chunk))
 
 		n -= len(chunk)
-		before = chunk[0].(*waProto.WebMessageInfo).GetKey().GetId()
+		key := chunk[0].(*waProto.WebMessageInfo).GetKey()
+		before = key.GetId()
+		fromMe = key.GetFromMe()
 		if len(before) == 0 {
 			portal.log.Infoln("No message ID for first message, starting handling of loaded messages")
 			break