diff --git a/database/message.go b/database/message.go index 847f946..7464e35 100644 --- a/database/message.go +++ b/database/message.go @@ -21,6 +21,7 @@ import ( "database/sql" "encoding/json" "strings" + "time" "github.com/Rhymen/go-whatsapp" waProto "github.com/Rhymen/go-whatsapp/binary/proto" @@ -65,9 +66,13 @@ func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message { } func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message { + return mq.GetLastInChatBefore(chat, time.Now().Unix()+60) +} + +func (mq *MessageQuery) GetLastInChatBefore(chat PortalKey, maxTimestamp int64) *Message { msg := mq.get("SELECT chat_jid, chat_receiver, jid, mxid, sender, timestamp, sent, content "+ - "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND sent=true ORDER BY timestamp DESC LIMIT 1", - chat.JID, chat.Receiver) + "FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND timestamp<=$3 AND sent=true ORDER BY timestamp DESC LIMIT 1", + chat.JID, chat.Receiver, maxTimestamp) if msg == nil || msg.Timestamp == 0 { // Old db, we don't know what the last message is. return nil diff --git a/go.mod b/go.mod index 9dc2316..fd75404 100644 --- a/go.mod +++ b/go.mod @@ -15,4 +15,4 @@ require ( 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.3 diff --git a/go.sum b/go.sum index dda3ea6..d4e4f56 100644 --- a/go.sum +++ b/go.sum @@ -468,6 +468,8 @@ github.com/tulir/go-whatsapp v0.5.1 h1:nJ+ggQrO4kpNdE7VfM6y7J4xezoIHivrAlUjDpQSO github.com/tulir/go-whatsapp v0.5.1/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A= github.com/tulir/go-whatsapp v0.5.2 h1:CJcFSAoD/vXybamd9Hfa67Otg11G4EfoIgciQakunps= github.com/tulir/go-whatsapp v0.5.2/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A= +github.com/tulir/go-whatsapp v0.5.3 h1:e3bPLCmbBF146l8tnswqxnVLtGCWOEruJSR4luySh6I= +github.com/tulir/go-whatsapp v0.5.3/go.mod h1:7J3IIL3bEQiBJGtiZst1N4PgXHlWIartdVQLe6lcx9A= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= diff --git a/user.go b/user.go index 453c81d..6188d02 100644 --- a/user.go +++ b/user.go @@ -756,7 +756,7 @@ func (user *User) syncChatDoublePuppetDetails(doublePuppet *Puppet, chat Chat, j } intent := doublePuppet.CustomIntent() if chat.UnreadCount == 0 && (justCreated || !user.bridge.Config.Bridge.MarkReadOnlyOnCreate) { - lastMessage := user.bridge.DB.Message.GetLastInChat(chat.Portal.Key) + lastMessage := user.bridge.DB.Message.GetLastInChatBefore(chat.Portal.Key, chat.ReceivedAt.Unix()) if lastMessage != nil { err := intent.MarkReadWithContent(chat.Portal.MXID, lastMessage.MXID, &CustomReadReceipt{DoublePuppet: true}) if err != nil {