Skip to content

Commit

Permalink
Add option to mark old chats as read on backfill
Browse files Browse the repository at this point in the history
[skip cd]
  • Loading branch information
tulir committed Jan 29, 2024
1 parent dd8d225 commit ce6e7b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ func (portal *Portal) convertAndSendBackfill(ctx context.Context, source *User,
}
}
}
if unreadHoursThreshold := portal.bridge.Config.Bridge.Backfill.UnreadHoursThreshold; unreadHoursThreshold > 0 && !markRead && len(messages) > 0 {
markRead = messages[len(messages)-1].TimestampMs < time.Now().Add(-time.Duration(unreadHoursThreshold)*time.Hour).UnixMilli()
}
if portal.bridge.SpecVersions.Supports(mautrix.BeeperFeatureBatchSending) {
log.Info().Int("event_count", len(events)).Msg("Sending events to Matrix using Beeper batch sending")
portal.sendBackfillBeeper(ctx, source, events, metas, markRead, forward)
Expand Down Expand Up @@ -583,7 +586,7 @@ func (portal *Portal) sendBackfillLegacy(ctx context.Context, source *User, even

func (portal *Portal) sendBackfillBeeper(ctx context.Context, source *User, events []*event.Event, metas []*BackfillPartMetadata, markRead, forward bool) {
var markReadBy id.UserID
if markRead {
if markRead && forward {
markReadBy = source.MXID
}
resp, err := portal.MainIntent().BeeperBatchSend(ctx, portal.MXID, &mautrix.ReqBeeperBatchSend{
Expand Down
11 changes: 6 additions & 5 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ type BridgeConfig struct {
CommandPrefix string `yaml:"command_prefix"`

Backfill struct {
Enabled bool `yaml:"enabled"`
InboxFetchPages int `yaml:"inbox_fetch_pages"`
HistoryFetchPages int `yaml:"history_fetch_pages"`
CatchupFetchPages int `yaml:"catchup_fetch_pages"`
Queue struct {
Enabled bool `yaml:"enabled"`
InboxFetchPages int `yaml:"inbox_fetch_pages"`
HistoryFetchPages int `yaml:"history_fetch_pages"`
CatchupFetchPages int `yaml:"catchup_fetch_pages"`
UnreadHoursThreshold int `yaml:"unread_hours_threshold"`
Queue struct {
PagesAtOnce int `yaml:"pages_at_once"`
MaxPages int `yaml:"max_pages"`
SleepBetweenTasks time.Duration `yaml:"sleep_between_tasks"`
Expand Down
1 change: 1 addition & 0 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func DoUpgrade(helper *up.Helper) {
helper.Copy(up.Int, "bridge", "backfill", "inbox_fetch_pages")
helper.Copy(up.Int, "bridge", "backfill", "history_fetch_pages")
helper.Copy(up.Int, "bridge", "backfill", "catchup_fetch_pages")
helper.Copy(up.Int, "bridge", "backfill", "unread_hours_threshold")
helper.Copy(up.Int, "bridge", "backfill", "queue", "pages_at_once")
helper.Copy(up.Int, "bridge", "backfill", "queue", "max_pages")
helper.Copy(up.Str, "bridge", "backfill", "queue", "sleep_between_tasks")
Expand Down
3 changes: 3 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ bridge:
history_fetch_pages: 0
# Same as above, but for catchup backfills (i.e. when the bridge is restarted).
catchup_fetch_pages: 5
# Maximum age of chats to leave as unread when backfilling. 0 means all chats can be left as unread.
# If non-zero, chats that are older than this will be marked as read, even if they're still unread on Meta.
unread_hours_threshold: 0
# Backfill queue settings. Only relevant for Beeper, because standard Matrix servers
# don't support inserting messages into room history.
queue:
Expand Down

0 comments on commit ce6e7b9

Please sign in to comment.