diff --git a/pkg/hicli/database/room.go b/pkg/hicli/database/room.go index 40d44e2..de01edb 100644 --- a/pkg/hicli/database/room.go +++ b/pkg/hicli/database/room.go @@ -56,6 +56,9 @@ const ( setRoomPrevBatchQuery = ` UPDATE room SET prev_batch = $2 WHERE room_id = $1 ` + deleteRoomQuery = ` + DELETE FROM room WHERE room_id = $1 + ` updateRoomPreviewIfLaterOnTimelineQuery = ` UPDATE room SET preview_event_rowid = $2 @@ -95,6 +98,10 @@ func (rq *RoomQuery) Upsert(ctx context.Context, room *Room) error { return rq.Exec(ctx, upsertRoomFromSyncQuery, room.sqlVariables()...) } +func (rq *RoomQuery) Delete(ctx context.Context, roomID id.RoomID) error { + return rq.Exec(ctx, deleteRoomQuery, roomID) +} + func (rq *RoomQuery) CreateRow(ctx context.Context, roomID id.RoomID) error { return rq.Exec(ctx, ensureRoomExistsQuery, roomID) } diff --git a/pkg/hicli/events.go b/pkg/hicli/events.go index df46afd..86b303c 100644 --- a/pkg/hicli/events.go +++ b/pkg/hicli/events.go @@ -28,7 +28,8 @@ type SyncNotification struct { } type SyncComplete struct { - Rooms map[id.RoomID]*SyncRoom `json:"rooms"` + Rooms map[id.RoomID]*SyncRoom `json:"rooms"` + LeftRooms []id.RoomID `json:"left_rooms"` } func (c *SyncComplete) IsEmpty() bool { diff --git a/pkg/hicli/init.go b/pkg/hicli/init.go index 3b6175a..55c5598 100644 --- a/pkg/hicli/init.go +++ b/pkg/hicli/init.go @@ -62,7 +62,8 @@ func (h *HiClient) GetInitialSync(ctx context.Context, batchSize int) iter.Seq[* return } payload := SyncComplete{ - Rooms: make(map[id.RoomID]*SyncRoom, len(rooms)-1), + Rooms: make(map[id.RoomID]*SyncRoom, len(rooms)-1), + LeftRooms: make([]id.RoomID, 0), } for _, room := range rooms { if room.SortingTimestamp == rooms[len(rooms)-1].SortingTimestamp { diff --git a/pkg/hicli/sync.go b/pkg/hicli/sync.go index 3249f25..5e194a8 100644 --- a/pkg/hicli/sync.go +++ b/pkg/hicli/sync.go @@ -224,15 +224,14 @@ func (h *HiClient) processSyncJoinedRoom(ctx context.Context, roomID id.RoomID, } func (h *HiClient) processSyncLeftRoom(ctx context.Context, roomID id.RoomID, room *mautrix.SyncLeftRoom) error { - existingRoomData, err := h.DB.Room.Get(ctx, roomID) + zerolog.Ctx(ctx).Debug().Stringer("room_id", roomID).Msg("Deleting left room") + err := h.DB.Room.Delete(ctx, roomID) if err != nil { - return fmt.Errorf("failed to get room data: %w", err) - } else if existingRoomData == nil { - return nil + return fmt.Errorf("failed to delete room: %w", err) } - // TODO delete room + payload := ctx.Value(syncContextKey).(*syncContext).evt + payload.LeftRooms = append(payload.LeftRooms, roomID) return nil - //return h.processStateAndTimeline(ctx, existingRoomData, &room.State, &room.Timeline, &room.Summary, nil, nil) } func isDecryptionErrorRetryable(err error) bool { diff --git a/pkg/hicli/syncwrap.go b/pkg/hicli/syncwrap.go index 1383720..966196d 100644 --- a/pkg/hicli/syncwrap.go +++ b/pkg/hicli/syncwrap.go @@ -27,7 +27,10 @@ const ( func (h *hiSyncer) ProcessResponse(ctx context.Context, resp *mautrix.RespSync, since string) error { c := (*HiClient)(h) - ctx = context.WithValue(ctx, syncContextKey, &syncContext{evt: &SyncComplete{Rooms: make(map[id.RoomID]*SyncRoom, len(resp.Rooms.Join))}}) + ctx = context.WithValue(ctx, syncContextKey, &syncContext{evt: &SyncComplete{ + Rooms: make(map[id.RoomID]*SyncRoom, len(resp.Rooms.Join)), + LeftRooms: make([]id.RoomID, 0, len(resp.Rooms.Leave)), + }}) err := c.preProcessSyncResponse(ctx, resp, since) if err != nil { return err diff --git a/web/src/api/statestore/main.ts b/web/src/api/statestore/main.ts index f43bc9f..73c825c 100644 --- a/web/src/api/statestore/main.ts +++ b/web/src/api/statestore/main.ts @@ -146,6 +146,10 @@ export class StateStore { } } } + for (const roomID of sync.left_rooms) { + this.rooms.delete(roomID) + changedRoomListEntries.set(roomID, null) + } let updatedRoomList: RoomListEntry[] | undefined if (resyncRoomList) { diff --git a/web/src/api/types/hievents.ts b/web/src/api/types/hievents.ts index e12541b..7244cd2 100644 --- a/web/src/api/types/hievents.ts +++ b/web/src/api/types/hievents.ts @@ -80,6 +80,7 @@ export interface SyncNotification { export interface SyncCompleteData { rooms: Record + left_rooms: RoomID[] } export interface SyncCompleteEvent extends RPCCommand {