From 666194b06674176b9677c1ff9da43f24be85efbd Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 28 May 2019 21:30:39 +0300 Subject: [PATCH] Possibly fix and/or break missed message backfilling --- go.mod | 4 ++-- go.sum | 2 ++ portal.go | 17 ++++++++++------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 61646d1..05fa25d 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 4f0f286..97aea9e 100644 --- a/go.sum +++ b/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= diff --git a/portal.go b/portal.go index 1e3c268..36263c1 100644 --- a/portal.go +++ b/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