Skip to content

Commit

Permalink
v2: fix handling disappearing timer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 27, 2024
1 parent 5b1941c commit fdb9e7d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
16 changes: 10 additions & 6 deletions msgconv/from-signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion msgconv/msgconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package msgconv

import (
"context"
"time"

"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
}
},
}
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/connector/handlesignal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
}
}
Expand Down

0 comments on commit fdb9e7d

Please sign in to comment.