From 89131bd20c4b1aa83233384a91c67ed478338b20 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 29 Apr 2022 16:59:27 -0600 Subject: [PATCH 1/2] backfill: add notification of disappearing messages at correct timestamp This makes it so that the timestamp of the chat in Matrix looks correct, even though the message is not there to be bridged since it has disappeared. --- historysync.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/historysync.go b/historysync.go index e7c8c83..61e6285 100644 --- a/historysync.go +++ b/historysync.go @@ -158,7 +158,19 @@ func (user *User) backfillInChunks(req *database.Backfill, conv *database.Histor } } allMsgs := user.bridge.DB.HistorySyncQuery.GetMessagesBetween(user.MXID, conv.ConversationID, req.TimeStart, req.TimeEnd, req.MaxTotalEvents) - if len(allMsgs) == 0 { + + sendDisappearedNotice := false + // If expired messages are on, and a notice has not been sent to this chat + // about it having disappeared messages at the conversation timestamp, send + // a notice indicating so. + if len(allMsgs) == 0 && conv.EphemeralExpiration != nil && *conv.EphemeralExpiration > 0 { + lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key) + if lastMessage == nil || !conv.LastMessageTimestamp.Equal(lastMessage.Timestamp) { + sendDisappearedNotice = true + } + } + + if !sendDisappearedNotice && len(allMsgs) == 0 { user.log.Debugfln("Not backfilling %s: no bridgeable messages found", portal.Key.JID) return } @@ -172,6 +184,28 @@ func (user *User) backfillInChunks(req *database.Backfill, conv *database.Histor } } + if sendDisappearedNotice { + user.log.Debugfln("Sending notice to %s that there are disappeared messages ending at %v", portal.Key.JID, conv.LastMessageTimestamp) + resp, err := portal.sendMessage(portal.MainIntent(), event.EventMessage, &event.MessageEventContent{ + MsgType: event.MsgNotice, + Body: portal.formatDisappearingMessageNotice(), + }, nil, conv.LastMessageTimestamp.UnixMilli()) + + if err != nil { + portal.log.Errorln("Error sending disappearing messages notice event") + return + } + + msg := portal.bridge.DB.Message.New() + msg.Chat = portal.Key + msg.MXID = resp.EventID + msg.JID = types.MessageID(resp.EventID) + msg.Timestamp = conv.LastMessageTimestamp + msg.Sent = true + msg.Insert() + return + } + user.log.Infofln("Backfilling %d messages in %s, %d messages at a time (queue ID: %d)", len(allMsgs), portal.Key.JID, req.MaxBatchEvents, req.QueueID) toBackfill := allMsgs[0:] var insertionEventIds []id.EventID From 3096786454d868dd8a21bc861991b9ce6dd2bc85 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sat, 30 Apr 2022 01:03:53 -0600 Subject: [PATCH 2/2] backfill queue: interrupt sending to deferred channel on queue re-check If a queue re-check is requested, interrupt sending the backfill request to the deferred channel so that immediate backfills can happen ASAP. --- backfillqueue.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backfillqueue.go b/backfillqueue.go index 4a1c651..57ad1a6 100644 --- a/backfillqueue.go +++ b/backfillqueue.go @@ -40,7 +40,14 @@ func (bq *BackfillQueue) RunLoop(user *User) { if backfill.BackfillType == database.BackfillImmediate || backfill.BackfillType == database.BackfillForward { bq.ImmediateBackfillRequests <- backfill } else { - bq.DeferredBackfillRequests <- backfill + select { + case <-bq.ReCheckQueue: + // If a queue re-check is requested, interrupt sending the + // backfill request to the deferred channel so that + // immediate backfills can happen ASAP. + continue + case bq.DeferredBackfillRequests <- backfill: + } } backfill.MarkDone() } else {