From 7f636e6aefff6681cd9ddf449f9e85dc995248e3 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 7 Jan 2022 15:05:09 +0200 Subject: [PATCH] Update whatsmeow to read group disappearing message timer --- go.mod | 2 +- go.sum | 4 ++-- portal.go | 42 +++++++++++++++++++++++++++++++++++------- user.go | 2 ++ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 2493f79..0b554d3 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.10 github.com/prometheus/client_golang v1.11.0 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e - go.mau.fi/whatsmeow v0.0.0-20211231085557-666a92779bf5 + go.mau.fi/whatsmeow v0.0.0-20220107125553-190be349f174 golang.org/x/image v0.0.0-20211028202545-6944b10bf410 google.golang.org/protobuf v1.27.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b diff --git a/go.sum b/go.sum index 94cbf5b..3755cc2 100644 --- a/go.sum +++ b/go.sum @@ -139,8 +139,8 @@ github.com/tidwall/sjson v1.2.3 h1:5+deguEhHSEjmuICXZ21uSSsXotWMA0orU783+Z7Cp8= github.com/tidwall/sjson v1.2.3/go.mod h1:5WdjKx3AQMvCJ4RG6/2UYT7dLrGvJUV1x4jdTAyGvZs= go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910 h1:9FFhG0OmkuMau5UEaTgiUQ+7cSbtbOQ7hiWKdN8OI3I= go.mau.fi/libsignal v0.0.0-20211109153248-a67163214910/go.mod h1:AufGrvVh+00Nc07Jm4hTquh7yleZyn20tKJI2wCPAKg= -go.mau.fi/whatsmeow v0.0.0-20211231085557-666a92779bf5 h1:tzQ3IRQtZJi1U3JpNCvvm0cL4SGd9PbyJUe2XodfqCs= -go.mau.fi/whatsmeow v0.0.0-20211231085557-666a92779bf5/go.mod h1:8jUjOAi3xtGubxcZgG8uSHpAdyQXBRbWAfxkctX/4y4= +go.mau.fi/whatsmeow v0.0.0-20220107125553-190be349f174 h1:RJNCmrezldkWTttNO1yyORKizd7uEmQMSW/4TpWe1jU= +go.mau.fi/whatsmeow v0.0.0-20220107125553-190be349f174/go.mod h1:8jUjOAi3xtGubxcZgG8uSHpAdyQXBRbWAfxkctX/4y4= golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/portal.go b/portal.go index f0c6af5..1b700f6 100644 --- a/portal.go +++ b/portal.go @@ -417,17 +417,11 @@ func (portal *Portal) convertMessage(intent *appservice.IntentAPI, source *User, case waMsg.ProtocolMessage != nil && waMsg.ProtocolMessage.GetType() == waProto.ProtocolMessage_EPHEMERAL_SETTING: portal.ExpirationTime = waMsg.ProtocolMessage.GetEphemeralExpiration() portal.Update() - var msg string - if portal.ExpirationTime == 0 { - msg = "Turned off disappearing messages" - } else { - msg = fmt.Sprintf("Set the disappearing message timer to %s", formatDuration(time.Duration(portal.ExpirationTime)*time.Second)) - } return &ConvertedMessage{ Intent: intent, Type: event.EventMessage, Content: &event.MessageEventContent{ - Body: msg, + Body: portal.formatDisappearingMessageNotice(), MsgType: event.MsgNotice, }, } @@ -436,6 +430,36 @@ func (portal *Portal) convertMessage(intent *appservice.IntentAPI, source *User, } } +func (portal *Portal) UpdateGroupDisappearingMessages(sender *types.JID, timestamp time.Time, timer uint32) { + portal.ExpirationTime = timer + portal.Update() + intent := portal.MainIntent() + if sender != nil { + intent = portal.bridge.GetPuppetByJID(sender.ToNonAD()).IntentFor(portal) + } else { + sender = &types.EmptyJID + } + _, err := portal.sendMessage(intent, event.EventMessage, &event.MessageEventContent{ + Body: portal.formatDisappearingMessageNotice(), + MsgType: event.MsgNotice, + }, nil, timestamp.UnixMilli()) + if err != nil { + portal.log.Warnfln("Failed to notify portal about disappearing message timer change by %s to %d", *sender, timer) + } +} + +func (portal *Portal) formatDisappearingMessageNotice() string { + if portal.ExpirationTime == 0 { + return "Turned off disappearing messages" + } else { + msg := fmt.Sprintf("Set the disappearing message timer to %s", formatDuration(time.Duration(portal.ExpirationTime)*time.Second)) + if !portal.bridge.Config.Bridge.DisappearingMessagesInGroups && portal.IsGroupChat() { + msg += ". However, this bridge is not configured to disappear messages in group chats." + } + return msg + } +} + const UndecryptableMessageNotice = "Decrypting message from WhatsApp failed, waiting for sender to re-send... " + "([learn more](https://faq.whatsapp.com/general/security-and-privacy/seeing-waiting-for-this-message-this-may-take-a-while))" @@ -887,6 +911,10 @@ func (portal *Portal) UpdateMetadata(user *User, groupInfo *types.GroupInfo) boo update := false update = portal.UpdateName(groupInfo.Name, groupInfo.NameSetBy, false) || update update = portal.UpdateTopic(groupInfo.Topic, groupInfo.TopicSetBy, false) || update + if portal.ExpirationTime != groupInfo.DisappearingTimer { + update = true + portal.ExpirationTime = groupInfo.DisappearingTimer + } portal.RestrictMessageSending(groupInfo.IsAnnounce) portal.RestrictMetadataChanges(groupInfo.IsLocked) diff --git a/user.go b/user.go index a059818..a4353c8 100644 --- a/user.go +++ b/user.go @@ -937,6 +937,8 @@ func (user *User) handleGroupUpdate(evt *events.GroupInfo) { portal.ChangeAdminStatus(evt.Promote, true) case evt.Demote != nil: portal.ChangeAdminStatus(evt.Demote, false) + case evt.Ephemeral != nil: + portal.UpdateGroupDisappearingMessages(evt.Sender, evt.Timestamp, evt.Ephemeral.DisappearingTimer) } }