From 2afdf80937ffee83f3691bf6bf9dea2dc5e7d889 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Mon, 25 Apr 2022 19:46:05 -0600 Subject: [PATCH 1/2] backfill: don't create room if the user is not a participant This can happen if the user gets kicked from a chat. --- portal.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/portal.go b/portal.go index a687187..cea3c77 100644 --- a/portal.go +++ b/portal.go @@ -1221,7 +1221,13 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i } else { if groupInfo == nil || !isFullInfo { foundInfo, err := user.Client.GetGroupInfo(portal.Key.JID) - if err != nil { + + // Ensure that the user is actually a participant in the conversation + // before creating the matrix room + if errors.Is(err, whatsmeow.ErrNotInGroup) { + user.log.Debugfln("Skipping creating matrix room for %s because the user is not a participant", portal.Key.JID) + return err + } else if err != nil { portal.log.Warnfln("Failed to get group info through %s: %v", user.JID, err) } else { groupInfo = foundInfo From caf0a363c91237cba6b957e18f0b1ccd43a661c2 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Wed, 27 Apr 2022 08:29:15 -0600 Subject: [PATCH 2/2] backfill: delete messages for rooms the user is not in If the chat turns out to be not bridgable due to the user not being in the room, delete the messages, and stop trying to backfill it. --- database/backfillqueue.go | 10 ++++++++++ database/historysync.go | 10 ++++++++++ portal.go | 2 ++ 3 files changed, 22 insertions(+) diff --git a/database/backfillqueue.go b/database/backfillqueue.go index 4da6123..b39604e 100644 --- a/database/backfillqueue.go +++ b/database/backfillqueue.go @@ -106,6 +106,16 @@ func (bq *BackfillQuery) DeleteAll(userID id.UserID) error { return err } +func (bq *BackfillQuery) DeleteAllForPortal(userID id.UserID, portalKey PortalKey) error { + _, err := bq.db.Exec(` + DELETE FROM backfill_queue + WHERE user_mxid=$1 + AND portal_jid=$2 + AND portal_receiver=$3 + `, userID, portalKey.JID, portalKey.Receiver) + return err +} + type Backfill struct { db *Database log log.Logger diff --git a/database/historysync.go b/database/historysync.go index d2c166a..eba4f8d 100644 --- a/database/historysync.go +++ b/database/historysync.go @@ -314,3 +314,13 @@ func (hsq *HistorySyncQuery) DeleteAllMessages(userID id.UserID) error { _, err := hsq.db.Exec("DELETE FROM history_sync_message WHERE user_mxid=$1", userID) return err } + +func (hsq *HistorySyncQuery) DeleteAllMessagesForPortal(userID id.UserID, portalKey PortalKey) error { + _, err := hsq.db.Exec(` + DELETE FROM history_sync_message + WHERE user_mxid=$1 + AND portal_jid=$2 + AND portal_receiver=$3 + `, userID, portalKey.JID, portalKey.Receiver) + return err +} diff --git a/portal.go b/portal.go index cea3c77..e646906 100644 --- a/portal.go +++ b/portal.go @@ -1226,6 +1226,8 @@ func (portal *Portal) CreateMatrixRoom(user *User, groupInfo *types.GroupInfo, i // before creating the matrix room if errors.Is(err, whatsmeow.ErrNotInGroup) { user.log.Debugfln("Skipping creating matrix room for %s because the user is not a participant", portal.Key.JID) + user.bridge.DB.BackfillQuery.DeleteAllForPortal(user.MXID, portal.Key) + user.bridge.DB.HistorySyncQuery.DeleteAllMessagesForPortal(user.MXID, portal.Key) return err } else if err != nil { portal.log.Warnfln("Failed to get group info through %s: %v", user.JID, err)