Skip to content

Commit

Permalink
fixup! backfill: implement loop
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Aug 4, 2023
1 parent ff24bbe commit f452582
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions backfillqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,34 @@ func (user *User) EnqueueDeferredBackfills(txn dbutil.Execable, portals []*Porta
func (user *User) runOnlyBackfillMode() {
log := user.bridge.ZLog.With().Str("mode", "backfill_only").Logger()
ctx := log.WithContext(context.Background())
client := user.CustomIntent().Client

// Lock the backfill to this device.
var lockBackfillResp LockBackfillForDeviceResp
url := client.BuildURL(mautrix.BaseURLPath{
"_matrix", "asmux", "mxauth", "appservice", user.MXID.Localpart(), "imessagecloud",
"exec", "lock_backfill_for_device"})
_, err := client.MakeRequest("POST", url, LockBackfillForDeviceReq{
DeviceID: id.DeviceID(user.bridge.Config.Bridge.DeviceID),
}, &lockBackfillResp)
if err != nil {
// TODO do something more intelligent here
log.Fatal().Err(err).Msg("Error locking backfill for device")
} else if !lockBackfillResp.Success {
// TODO do something more intelligent here
log.Fatal().
Str("device_id", lockBackfillResp.BackfillDeviceID.String()).
Msg("Backfill already locked for a different device")
}

err := user.bridge.Crypto.ShareOneTimeKeys(context.Background())
err = user.bridge.Crypto.ShareOneTimeKeys(context.Background())
if err != nil {
// TODO do something more intelligent here
log.Fatal().Err(err).Msg("Error sharing keys")
}

if !user.bridge.SpecVersions.Supports(mautrix.BeeperFeatureBatchSending) {
// TODO do something more intelligent here
log.Fatal().Msg("The homeserver does not support Beeper's batch send endpoint, are you sure you connected to the correct endpoint?")
}

Expand All @@ -116,6 +136,7 @@ func (user *User) runOnlyBackfillMode() {
user.handleHistorySyncsLoop()

if count, err := user.bridge.DB.Backfill.Count(ctx); err != nil {
// TODO do something more intelligent here
log.Fatal().Err(err).Msg("Error retrieving backfill queue count")
} else if count > 0 {
// If there are already items in the backfill queue, then we just need to
Expand All @@ -141,8 +162,7 @@ func (user *User) runOnlyBackfillMode() {
}

// Ask the cloud bridge to create room IDs for every one of the chats.
client := user.CustomIntent().Client
url := client.BuildURL(mautrix.BaseURLPath{
url = client.BuildURL(mautrix.BaseURLPath{
"_matrix", "asmux", "mxauth", "appservice", user.MXID.Localpart(), "imessagecloud",
"exec", "create_rooms_for_backfill"})
_, err = client.MakeRequest("POST", url, NewRoomBackfillRequest{
Expand All @@ -153,30 +173,30 @@ func (user *User) runOnlyBackfillMode() {
}

// Wait for the rooms to be created.
var resp RoomInfoForBackfillResponse
var roomInfoResp RoomInfoForBackfillResponse
for {
url = client.BuildURL(mautrix.BaseURLPath{
"_matrix", "asmux", "mxauth", "appservice", user.MXID.Localpart(), "imessagecloud",
"exec", "get_room_info_for_backfill"})
_, err = client.MakeRequest("POST", url, RoomInfoForBackfillRequest{
ChatGUIDs: chatGUIDs,
}, &resp)
}, &roomInfoResp)
if err != nil {
// TODO do something more intelligent here
log.Fatal().Err(err).Msg("Error requesting backfill room info")
}

if resp.Status == RoomCreationForBackfillStatusDone {
if roomInfoResp.Status == RoomCreationForBackfillStatusDone {
break
} else if resp.Status == RoomCreationForBackfillStatusError {
} else if roomInfoResp.Status == RoomCreationForBackfillStatusError {
// TODO do something more intelligent here
log.Fatal().Str("error", resp.Error).Msg("Error requesting backfill room IDs")
} else if resp.Status == RoomCreationForBackfillStatusInProgress {
log.Fatal().Str("error", roomInfoResp.Error).Msg("Error requesting backfill room IDs")
} else if roomInfoResp.Status == RoomCreationForBackfillStatusInProgress {
log.Info().Msg("Backfill room creation still in progress, waiting 5 seconds")
time.Sleep(5 * time.Second)
} else {
// TODO do something more intelligent here
log.Fatal().Err(err).Str("status", string(resp.Status)).Msg("Unknown status")
log.Fatal().Err(err).Str("status", string(roomInfoResp.Status)).Msg("Unknown status")
}
}

Expand All @@ -186,7 +206,7 @@ func (user *User) runOnlyBackfillMode() {
txn, err := user.bridge.DB.Begin()
{
var i int
for guid, roomInfo := range resp.Rooms {
for guid, roomInfo := range roomInfoResp.Rooms {
portal := user.bridge.GetPortalByGUIDWithTransaction(txn, guid)
portal.MXID = roomInfo.RoomID
portal.Update(txn)
Expand Down

0 comments on commit f452582

Please sign in to comment.