From fdb9e7dc09599fd982cff4ea90903971e368983e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 27 Jun 2024 16:43:53 +0300 Subject: [PATCH] v2: fix handling disappearing timer changes --- msgconv/from-signal.go | 16 ++++++++++------ msgconv/msgconv.go | 3 ++- pkg/connector/connector.go | 17 ++++++++++++++++- pkg/connector/handlesignal.go | 17 +++++++++++++---- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/msgconv/from-signal.go b/msgconv/from-signal.go index 8a4b5d14..918c749c 100644 --- a/msgconv/from-signal.go +++ b/msgconv/from-signal.go @@ -173,12 +173,16 @@ func (mc *MessageConverter) ConvertDisappearingTimerChangeToMatrix(ctx context.C if timer == 0 { part.Content.Body = "Disappearing messages disabled" } - if updatePortal && !mc.NoUpdateDisappearing { - portal := mc.GetData(ctx) - portal.ExpirationTime = timer - err := portal.Update(ctx) - if err != nil { - zerolog.Ctx(ctx).Err(err).Msg("Failed to update portal disappearing timer in database") + if updatePortal { + if mc.UpdateDisappearing != nil { + mc.UpdateDisappearing(ctx, time.Duration(timer)*time.Second) + } else { + portal := mc.GetData(ctx) + portal.ExpirationTime = timer + err := portal.Update(ctx) + if err != nil { + zerolog.Ctx(ctx).Err(err).Msg("Failed to update portal disappearing timer in database") + } } } return part diff --git a/msgconv/msgconv.go b/msgconv/msgconv.go index 33ac4a5b..ee3ff559 100644 --- a/msgconv/msgconv.go +++ b/msgconv/msgconv.go @@ -18,6 +18,7 @@ package msgconv import ( "context" + "time" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" @@ -54,7 +55,7 @@ type MessageConverter struct { ConvertGIFToAPNG bool MaxFileSize int64 AsyncFiles bool - NoUpdateDisappearing bool + UpdateDisappearing func(ctx context.Context, newTimer time.Duration) LocationFormat string } diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index 8aec917a..dcb7ada0 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -20,10 +20,13 @@ import ( "context" "fmt" "text/template" + "time" "github.com/google/uuid" + "github.com/rs/zerolog" "go.mau.fi/util/dbutil" "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/id" @@ -124,7 +127,19 @@ func (s *SignalConnector) Init(bridge *bridgev2.Bridge) { MaxFileSize: 50 * 1024 * 1024, AsyncFiles: true, LocationFormat: s.Config.LocationFormat, - NoUpdateDisappearing: true, + UpdateDisappearing: func(ctx context.Context, newTimer time.Duration) { + portal := ctx.Value(msgconvContextKey).(*msgconvContext).Portal + portal.Metadata.DisappearTimer = newTimer + if newTimer == 0 { + portal.Metadata.DisappearType = "" + } else { + portal.Metadata.DisappearType = database.DisappearingTypeAfterRead + } + err := portal.Save(ctx) + if err != nil { + zerolog.Ctx(ctx).Err(err).Msg("Failed to update portal disappearing timer in database") + } + }, } } diff --git a/pkg/connector/handlesignal.go b/pkg/connector/handlesignal.go index 6af25ab2..5d10716e 100644 --- a/pkg/connector/handlesignal.go +++ b/pkg/connector/handlesignal.go @@ -23,10 +23,12 @@ import ( "github.com/google/uuid" "github.com/rs/zerolog" + "go.mau.fi/util/exfmt" "go.mau.fi/util/exzerolog" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/database" "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/event" "go.mau.fi/mautrix-signal/pkg/signalmeow/events" signalpb "go.mau.fi/mautrix-signal/pkg/signalmeow/protobuf" @@ -235,13 +237,20 @@ func (evt *Bv2ChatEvent) ConvertMessage(ctx context.Context, portal *bridgev2.Po if portal.Metadata.DisappearTimer != disappear.Timer { portal.Metadata.DisappearType = disappear.Type portal.Metadata.DisappearTimer = disappear.Timer - // TODO if the message doesn't have the DataMessage_EXPIRATION_TIMER_UPDATE, - // we should send a message to the portal notifying of the implicit update err := portal.Save(ctx) if err != nil { - zerolog.Ctx(ctx).Err(err).Msg("Failed to save portal metadata after updating disappearing timer") + zerolog.Ctx(ctx).Err(err).Msg("Failed to save portal metadata after implicitly updating disappearing timer") } else { - zerolog.Ctx(ctx).Debug().Dur("new_timer", portal.Metadata.DisappearTimer).Msg("Updated disappearing timer in portal") + zerolog.Ctx(ctx).Debug().Dur("new_timer", portal.Metadata.DisappearTimer).Msg("Implicitly updated disappearing timer in portal") + } + _, err = portal.Bridge.Bot.SendMessage(ctx, portal.MXID, event.EventMessage, &event.Content{ + Parsed: &event.MessageEventContent{ + MsgType: event.MsgNotice, + Body: fmt.Sprintf("Automatically enabled disappearing message timer (%s) because incoming message is disappearing", exfmt.Duration(disappear.Timer)), + }, + }, time.UnixMilli(int64(dataMsg.GetTimestamp()))) + if err != nil { + zerolog.Ctx(ctx).Err(err).Msg("Failed to send notice about disappearing message timer changing implicitly") } } }