diff --git a/pkg/hicli/database/room.go b/pkg/hicli/database/room.go index 92fcd0e..40d44e2 100644 --- a/pkg/hicli/database/room.go +++ b/pkg/hicli/database/room.go @@ -21,7 +21,7 @@ import ( const ( getRoomBaseQuery = ` - SELECT room_id, creation_content, name, name_quality, avatar, explicit_avatar, topic, canonical_alias, + SELECT room_id, creation_content, tombstone_content, name, name_quality, avatar, explicit_avatar, topic, canonical_alias, lazy_load_summary, encryption_event, has_member_list, preview_event_rowid, sorting_timestamp, unread_highlights, unread_notifications, unread_messages, prev_batch FROM room @@ -35,21 +35,22 @@ const ( upsertRoomFromSyncQuery = ` UPDATE room SET creation_content = COALESCE(room.creation_content, $2), - name = COALESCE($3, room.name), - name_quality = CASE WHEN $3 IS NOT NULL THEN $4 ELSE room.name_quality END, - avatar = COALESCE($5, room.avatar), - explicit_avatar = CASE WHEN $5 IS NOT NULL THEN $6 ELSE room.explicit_avatar END, - topic = COALESCE($7, room.topic), - canonical_alias = COALESCE($8, room.canonical_alias), - lazy_load_summary = COALESCE($9, room.lazy_load_summary), - encryption_event = COALESCE($10, room.encryption_event), - has_member_list = room.has_member_list OR $11, - preview_event_rowid = COALESCE($12, room.preview_event_rowid), - sorting_timestamp = COALESCE($13, room.sorting_timestamp), - unread_highlights = COALESCE($14, room.unread_highlights), - unread_notifications = COALESCE($15, room.unread_notifications), - unread_messages = COALESCE($16, room.unread_messages), - prev_batch = COALESCE($17, room.prev_batch) + tombstone_content = COALESCE(room.tombstone_content, $3), + name = COALESCE($4, room.name), + name_quality = CASE WHEN $4 IS NOT NULL THEN $5 ELSE room.name_quality END, + avatar = COALESCE($6, room.avatar), + explicit_avatar = CASE WHEN $6 IS NOT NULL THEN $7 ELSE room.explicit_avatar END, + topic = COALESCE($8, room.topic), + canonical_alias = COALESCE($9, room.canonical_alias), + lazy_load_summary = COALESCE($10, room.lazy_load_summary), + encryption_event = COALESCE($11, room.encryption_event), + has_member_list = room.has_member_list OR $12, + preview_event_rowid = COALESCE($13, room.preview_event_rowid), + sorting_timestamp = COALESCE($14, room.sorting_timestamp), + unread_highlights = COALESCE($15, room.unread_highlights), + unread_notifications = COALESCE($16, room.unread_notifications), + unread_messages = COALESCE($17, room.unread_messages), + prev_batch = COALESCE($18, room.prev_batch) WHERE room_id = $1 ` setRoomPrevBatchQuery = ` @@ -130,8 +131,9 @@ const ( const PrevBatchPaginationComplete = "fi.mau.gomuks.pagination_complete" type Room struct { - ID id.RoomID `json:"room_id"` - CreationContent *event.CreateEventContent `json:"creation_content,omitempty"` + ID id.RoomID `json:"room_id"` + CreationContent *event.CreateEventContent `json:"creation_content,omitempty"` + Tombstone *event.TombstoneEventContent `json:"tombstone,omitempty"` Name *string `json:"name,omitempty"` NameQuality NameQuality `json:"name_quality"` @@ -153,6 +155,14 @@ type Room struct { } func (r *Room) CheckChangesAndCopyInto(other *Room) (hasChanges bool) { + if r.CreationContent != nil { + other.CreationContent = r.CreationContent + hasChanges = true + } + if r.Tombstone != nil { + other.Tombstone = r.Tombstone + hasChanges = true + } if r.Name != nil && r.NameQuality >= other.NameQuality { other.Name = r.Name other.NameQuality = r.NameQuality @@ -216,6 +226,7 @@ func (r *Room) Scan(row dbutil.Scannable) (*Room, error) { err := row.Scan( &r.ID, dbutil.JSON{Data: &r.CreationContent}, + dbutil.JSON{Data: &r.Tombstone}, &r.Name, &r.NameQuality, &r.Avatar, @@ -245,6 +256,7 @@ func (r *Room) sqlVariables() []any { return []any{ r.ID, dbutil.JSONPtr(r.CreationContent), + dbutil.JSONPtr(r.Tombstone), r.Name, r.NameQuality, r.Avatar, diff --git a/pkg/hicli/database/upgrades/00-latest-revision.sql b/pkg/hicli/database/upgrades/00-latest-revision.sql index 0808a6e..78520d3 100644 --- a/pkg/hicli/database/upgrades/00-latest-revision.sql +++ b/pkg/hicli/database/upgrades/00-latest-revision.sql @@ -1,4 +1,4 @@ --- v0 -> v3 (compatible with v1+): Latest revision +-- v0 -> v4 (compatible with v1+): Latest revision CREATE TABLE account ( user_id TEXT NOT NULL PRIMARY KEY, device_id TEXT NOT NULL, @@ -11,6 +11,7 @@ CREATE TABLE account ( CREATE TABLE room ( room_id TEXT NOT NULL PRIMARY KEY, creation_content TEXT, + tombstone_content TEXT, name TEXT, name_quality INTEGER NOT NULL DEFAULT 0, diff --git a/pkg/hicli/database/upgrades/04-room-tombstone-cache.sql b/pkg/hicli/database/upgrades/04-room-tombstone-cache.sql new file mode 100644 index 0000000..bebc46f --- /dev/null +++ b/pkg/hicli/database/upgrades/04-room-tombstone-cache.sql @@ -0,0 +1,5 @@ +-- v4 (compatible with v1+): Store tombstone event in room table +ALTER TABLE room ADD COLUMN tombstone_content TEXT; +UPDATE room SET tombstone_content=( + SELECT content FROM event WHERE type='m.room.tombstone' AND state_key='' AND event.room_id=room.room_id +); diff --git a/pkg/hicli/sync.go b/pkg/hicli/sync.go index 5679481..d32a24a 100644 --- a/pkg/hicli/sync.go +++ b/pkg/hicli/sync.go @@ -816,7 +816,8 @@ func processImportantEvent(ctx context.Context, evt *event.Event, existingRoomDa return } switch evt.Type { - case event.StateCreate, event.StateRoomName, event.StateCanonicalAlias, event.StateRoomAvatar, event.StateTopic, event.StateEncryption: + case event.StateCreate, event.StateTombstone, event.StateRoomName, event.StateCanonicalAlias, + event.StateRoomAvatar, event.StateTopic, event.StateEncryption: if *evt.StateKey != "" { return } @@ -834,6 +835,8 @@ func processImportantEvent(ctx context.Context, evt *event.Event, existingRoomDa switch evt.Type { case event.StateCreate: updatedRoom.CreationContent, _ = evt.Content.Parsed.(*event.CreateEventContent) + case event.StateTombstone: + updatedRoom.Tombstone, _ = evt.Content.Parsed.(*event.TombstoneEventContent) case event.StateEncryption: newEncryption, _ := evt.Content.Parsed.(*event.EncryptionEventContent) if existingRoomData.EncryptionEvent == nil || existingRoomData.EncryptionEvent.Algorithm == newEncryption.Algorithm {