Skip to content

Commit

Permalink
fixup! ws commands: add ability to lock backfill for a specific local…
Browse files Browse the repository at this point in the history
… device ID

Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Aug 4, 2023
1 parent 909e242 commit ff24bbe
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,35 +347,45 @@ func (mx *WebsocketCommandHandler) HandleSyncProxyError(syncErr *mautrix.RespErr
mx.bridge.RequestStartSync()
}

type LockBackfillForDeviceRequest struct {
type LockBackfillForDeviceReq struct {
DeviceID id.DeviceID `json:"device_id"`
}

type LockBackfillForDeviceResp struct {
Success bool `json:"success"`
BackfillDeviceID id.DeviceID `json:"backfill_device_id"`
}

var lockBackfillForDeviceLock sync.Mutex

func (mx *WebsocketCommandHandler) lockBackfillToForDevice(cmd appservice.WebsocketCommand) (bool, any) {
lockBackfillForDeviceLock.Lock()
defer lockBackfillForDeviceLock.Unlock()

var req LockBackfillForDeviceRequest
var req LockBackfillForDeviceReq
if err := json.Unmarshal(cmd.Data, &req); err != nil {
return false, fmt.Errorf("failed to parse request: %w", err)
}

deviceID, err := mx.bridge.DB.Backfill.GetLocalBackfillDeviceID(cmd.Ctx, mx.bridge.user.MXID)
if err != nil {
return false, fmt.Errorf("failed to get local backfill device ID")
return false, fmt.Errorf("failed to get local backfill device ID: %w", err)
} else if deviceID != "" {
if deviceID != req.DeviceID {
return false, fmt.Errorf("backfill is already locked to another device")
return true, LockBackfillForDeviceResp{
Success: false,
BackfillDeviceID: deviceID,
}
}
return true, nil
} else {
err := mx.bridge.DB.Backfill.SetLocalBackfillDeviceID(cmd.Ctx, mx.bridge.user.MXID, req.DeviceID)
if err != nil {
return false, fmt.Errorf("failed to set local backfill device ID: %w", err)
}
return true, nil
}
return true, LockBackfillForDeviceResp{
Success: true,
BackfillDeviceID: req.DeviceID,
}
}

Expand Down

0 comments on commit ff24bbe

Please sign in to comment.