From 180c8996a8ce3b61092b13d26b9fe3680560d2a7 Mon Sep 17 00:00:00 2001 From: vurpo Date: Mon, 18 Jul 2022 16:08:52 +0300 Subject: [PATCH] Implement "Delete for me" support for messages (#530) --- portal.go | 22 ++++++++++++++++++++++ user.go | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/portal.go b/portal.go index 9b46154..9c48b8c 100644 --- a/portal.go +++ b/portal.go @@ -1664,6 +1664,28 @@ func (portal *Portal) HandleMessageRevoke(user *User, info *types.MessageInfo, k return true } +func (portal *Portal) deleteForMe(user *User, content *events.DeleteForMe) bool { + matrixUsers, err := portal.GetMatrixUsers() + if err != nil { + portal.log.Errorln("Failed to get Matrix users in portal to see if DeleteForMe should be handled:", err) + return false + } + if len(matrixUsers) == 1 && matrixUsers[0] == user.MXID { + msg := portal.bridge.DB.Message.GetByJID(portal.Key, content.MessageID) + if msg == nil || msg.IsFakeMXID() { + return false + } + _, err := portal.MainIntent().RedactEvent(portal.MXID, msg.MXID) + if err != nil { + portal.log.Errorln("Failed to redact %s: %v", msg.JID, err) + } else { + msg.Delete() + } + return true + } + return false +} + func (portal *Portal) sendMainIntentMessage(content *event.MessageEventContent) (*mautrix.RespSendEvent, error) { return portal.sendMessage(portal.MainIntent(), event.EventMessage, content, nil, 0) } diff --git a/user.go b/user.go index 2c17ee7..ab1d511 100644 --- a/user.go +++ b/user.go @@ -921,6 +921,11 @@ func (user *User) HandleEvent(event interface{}) { if user.bridge.Config.Bridge.SyncManualMarkedUnread { user.markUnread(user.GetPortalByJID(v.JID), !v.Action.GetRead()) } + case *events.DeleteForMe: + portal := user.GetPortalByJID(v.ChatJID) + if portal != nil { + portal.deleteForMe(user, v) + } default: user.log.Debugfln("Unknown type of event in HandleEvent: %T", v) }