From 0ed0f61cedfccd60feed68e9a3f47e45ac0a8b59 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 15 May 2022 14:01:23 +0300 Subject: [PATCH] Add command to change disappearing timer --- commands.go | 41 ++++++++++++++++++++++++++++++++++++++++- go.mod | 2 +- go.sum | 4 ++-- portal.go | 3 +++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index 255d22e..b287c23 100644 --- a/commands.go +++ b/commands.go @@ -87,6 +87,17 @@ func (ce *CommandEvent) Reply(msg string, args ...interface{}) { } } +func (ce *CommandEvent) React(key string) { + intent := ce.Bot + if ce.Portal != nil && ce.Portal.IsPrivateChat() { + intent = ce.Portal.MainIntent() + } + _, err := intent.SendReaction(ce.RoomID, ce.EventID, key) + if err != nil { + ce.Handler.log.Warnfln("Failed to react to command from %s: %v", ce.User.MXID, err) + } +} + // Handle handles messages to the bridge func (handler *CommandHandler) Handle(roomID id.RoomID, eventID id.EventID, user *User, message string, replyTo id.EventID) { args := strings.Fields(message) @@ -143,7 +154,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandLogout(ce) case "toggle": handler.CommandToggle(ce) - case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "resolve", "resolve-link", "join", "create", "accept", "backfill": + case "set-relay", "unset-relay", "login-matrix", "sync", "list", "search", "open", "pm", "invite-link", "resolve", + "resolve-link", "join", "create", "accept", "backfill", "disappearing-timer": if !ce.User.HasSession() { ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.") return @@ -181,6 +193,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandAccept(ce) case "backfill": handler.CommandBackfill(ce) + case "disappearing-timer": + handler.CommandDisappearingTimer(ce) } default: ce.Reply("Unknown command, use the `help` command for help.") @@ -750,6 +764,7 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { cmdPrefix + cmdResolveLinkHelp, cmdPrefix + cmdJoinHelp, cmdPrefix + cmdCreateHelp, + cmdPrefix + cmdDisappearingTimerHelp, cmdPrefix + cmdSetPowerLevelHelp, cmdPrefix + cmdDeletePortalHelp, cmdPrefix + cmdDeleteAllPortalsHelp, @@ -1211,3 +1226,27 @@ func (handler *CommandHandler) CommandLogoutMatrix(ce *CommandEvent) { } ce.Reply("Successfully removed custom puppet") } + +const cmdDisappearingTimerHelp = `disappearing-timer - Set future messages in the room to disappear after the given time.` + +func (handler *CommandHandler) CommandDisappearingTimer(ce *CommandEvent) { + duration, ok := whatsmeow.ParseDisappearingTimerString(ce.Args[0]) + if !ok { + ce.Reply("Invalid timer '%s'", ce.Args[0]) + return + } + prevExpirationTime := ce.Portal.ExpirationTime + ce.Portal.ExpirationTime = uint32(duration.Seconds()) + err := ce.User.Client.SetDisappearingTimer(ce.Portal.Key.JID, duration) + if err != nil { + ce.Reply("Failed to set disappearing timer: %v", err) + ce.Portal.ExpirationTime = prevExpirationTime + return + } + ce.Portal.Update() + if !ce.Portal.IsPrivateChat() && !ce.Bridge.Config.Bridge.DisappearingMessagesInGroups { + ce.Reply("Disappearing timer changed successfully, but this bridge is not configured to disappear messages in group chats.") + } else { + ce.React("✅") + } +} diff --git a/go.mod b/go.mod index c0ca244..12f70d8 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/prometheus/client_golang v1.12.2-0.20220514081015-5d584e2717ef github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/tidwall/gjson v1.14.1 - go.mau.fi/whatsmeow v0.0.0-20220515081149-cea8dab19fc9 + go.mau.fi/whatsmeow v0.0.0-20220515110043-a0a87653cff0 golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 golang.org/x/net v0.0.0-20220513224357-95641704303c google.golang.org/protobuf v1.28.0 diff --git a/go.sum b/go.sum index a8e29e6..89b198c 100644 --- a/go.sum +++ b/go.sum @@ -62,8 +62,8 @@ github.com/yuin/goldmark v1.4.12 h1:6hffw6vALvEDqJ19dOJvJKOoAOKe4NDaTqvd2sktGN0= github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mau.fi/libsignal v0.0.0-20220425070825-c40c839ee6a0 h1:3IQF2bgAyibdo77hTejwuJe4jlypj9QaE4xCQuxrThM= go.mau.fi/libsignal v0.0.0-20220425070825-c40c839ee6a0/go.mod h1:kBOXTvYyDG/q1Ihgvd4J6WenGPh7wtEGvPKF6vmf5ak= -go.mau.fi/whatsmeow v0.0.0-20220515081149-cea8dab19fc9 h1:mYmfIeKdlvWHwOMz2SIkC4N35JDc5TXIxLHhdISZZ1I= -go.mau.fi/whatsmeow v0.0.0-20220515081149-cea8dab19fc9/go.mod h1:iUBgOLNaqShLrR17u0kIiRptIGFH+nbT1tRhaWBEX/c= +go.mau.fi/whatsmeow v0.0.0-20220515110043-a0a87653cff0 h1:VtmnDmyjGRaKP6i8cjvn8rHq+qolMdToXNQMEEtheM0= +go.mau.fi/whatsmeow v0.0.0-20220515110043-a0a87653cff0/go.mod h1:iUBgOLNaqShLrR17u0kIiRptIGFH+nbT1tRhaWBEX/c= golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c= golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= diff --git a/portal.go b/portal.go index d5c8a84..bc3915a 100644 --- a/portal.go +++ b/portal.go @@ -497,6 +497,9 @@ func (portal *Portal) convertMessage(intent *appservice.IntentAPI, source *User, } func (portal *Portal) UpdateGroupDisappearingMessages(sender *types.JID, timestamp time.Time, timer uint32) { + if portal.ExpirationTime == timer { + return + } portal.ExpirationTime = timer portal.Update() intent := portal.MainIntent()