Skip to content

Commit a95101e

Browse files
committed
bridgev2/backfill: add optional done callback to fetch response
1 parent 830136b commit a95101e

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

bridgev2/networkinterface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ type FetchMessagesResponse struct {
495495
ApproxRemainingCount int
496496
// Approximate total number of messages in the chat.
497497
ApproxTotalCount int
498+
499+
// An optional function that is called after the backfill batch has been sent.
500+
CompleteCallback func()
498501
}
499502

500503
// BackfillingNetworkAPI is an optional interface that network connectors can implement to support backfilling message history.

bridgev2/portalbackfill.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (portal *Portal) doForwardBackfill(ctx context.Context, source *UserLogin,
7474
log.Warn().Msg("No messages left to backfill after cutting off old messages")
7575
return
7676
}
77-
portal.sendBackfill(ctx, source, resp.Messages, true, resp.MarkRead, false)
77+
portal.sendBackfill(ctx, source, resp.Messages, true, resp.MarkRead, false, resp.CompleteCallback)
7878
}
7979

8080
func (portal *Portal) DoBackwardsBackfill(ctx context.Context, source *UserLogin, task *database.BackfillTask) error {
@@ -134,7 +134,7 @@ func (portal *Portal) DoBackwardsBackfill(ctx context.Context, source *UserLogin
134134
if len(resp.Messages) == 0 {
135135
return fmt.Errorf("no messages left to backfill after cutting off too new messages")
136136
}
137-
portal.sendBackfill(ctx, source, resp.Messages, false, resp.MarkRead, false)
137+
portal.sendBackfill(ctx, source, resp.Messages, false, resp.MarkRead, false, resp.CompleteCallback)
138138
if len(resp.Messages) > 0 {
139139
task.OldestMessageID = resp.Messages[0].ID
140140
}
@@ -182,7 +182,7 @@ func (portal *Portal) doThreadBackfill(ctx context.Context, source *UserLogin, t
182182
}
183183
resp := portal.fetchThreadBackfill(ctx, source, anchorMessage)
184184
if resp != nil {
185-
portal.sendBackfill(ctx, source, resp.Messages, true, resp.MarkRead, true)
185+
portal.sendBackfill(ctx, source, resp.Messages, true, resp.MarkRead, true, resp.CompleteCallback)
186186
}
187187
}
188188

@@ -257,7 +257,15 @@ func (portal *Portal) cutoffMessages(ctx context.Context, messages []*BackfillMe
257257
return messages
258258
}
259259

260-
func (portal *Portal) sendBackfill(ctx context.Context, source *UserLogin, messages []*BackfillMessage, forceForward, markRead, inThread bool) {
260+
func (portal *Portal) sendBackfill(
261+
ctx context.Context,
262+
source *UserLogin,
263+
messages []*BackfillMessage,
264+
forceForward,
265+
markRead,
266+
inThread bool,
267+
done func(),
268+
) {
261269
canBatchSend := portal.Bridge.Matrix.GetCapabilities().BatchSending
262270
unreadThreshold := time.Duration(portal.Bridge.Config.Backfill.UnreadHoursThreshold) * time.Hour
263271
forceMarkRead := unreadThreshold > 0 && time.Since(messages[len(messages)-1].Timestamp) > unreadThreshold
@@ -272,6 +280,9 @@ func (portal *Portal) sendBackfill(ctx context.Context, source *UserLogin, messa
272280
} else {
273281
portal.sendLegacyBackfill(ctx, source, messages, markRead || forceMarkRead)
274282
}
283+
if done != nil {
284+
done()
285+
}
275286
zerolog.Ctx(ctx).Debug().Msg("Backfill finished")
276287
if !canBatchSend && !inThread && portal.Bridge.Config.Backfill.Threads.MaxInitialMessages > 0 {
277288
for _, msg := range messages {

bridgev2/portalinternal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ func (portal *PortalInternals) UpdateUserLocalInfo(ctx context.Context, info *Us
269269
(*Portal)(portal).updateUserLocalInfo(ctx, info, source, didJustCreate)
270270
}
271271

272-
func (portal *PortalInternals) UpdateParent(ctx context.Context, newParent networkid.PortalID, source *UserLogin) bool {
273-
return (*Portal)(portal).updateParent(ctx, newParent, source)
272+
func (portal *PortalInternals) UpdateParent(ctx context.Context, newParentID networkid.PortalID, source *UserLogin) bool {
273+
return (*Portal)(portal).updateParent(ctx, newParentID, source)
274274
}
275275

276276
func (portal *PortalInternals) LockedUpdateInfoFromGhost(ctx context.Context, ghost *Ghost) {
@@ -309,8 +309,8 @@ func (portal *PortalInternals) CutoffMessages(ctx context.Context, messages []*B
309309
return (*Portal)(portal).cutoffMessages(ctx, messages, aggressiveDedup, forward, lastMessage)
310310
}
311311

312-
func (portal *PortalInternals) SendBackfill(ctx context.Context, source *UserLogin, messages []*BackfillMessage, forceForward, markRead, inThread bool) {
313-
(*Portal)(portal).sendBackfill(ctx, source, messages, forceForward, markRead, inThread)
312+
func (portal *PortalInternals) SendBackfill(ctx context.Context, source *UserLogin, messages []*BackfillMessage, forceForward, markRead, inThread bool, done func()) {
313+
(*Portal)(portal).sendBackfill(ctx, source, messages, forceForward, markRead, inThread, done)
314314
}
315315

316316
func (portal *PortalInternals) CompileBatchMessage(ctx context.Context, source *UserLogin, msg *BackfillMessage, out *compileBatchOutput, inThread bool) {

0 commit comments

Comments
 (0)