hicli/database: store tombstone content in room table

This commit is contained in:
Tulir Asokan 2024-10-20 12:28:09 +03:00
parent ba30026cdc
commit 4b9e28c644
4 changed files with 41 additions and 20 deletions

View file

@ -21,7 +21,7 @@ import (
const ( const (
getRoomBaseQuery = ` 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, lazy_load_summary, encryption_event, has_member_list, preview_event_rowid, sorting_timestamp,
unread_highlights, unread_notifications, unread_messages, prev_batch unread_highlights, unread_notifications, unread_messages, prev_batch
FROM room FROM room
@ -35,21 +35,22 @@ const (
upsertRoomFromSyncQuery = ` upsertRoomFromSyncQuery = `
UPDATE room UPDATE room
SET creation_content = COALESCE(room.creation_content, $2), SET creation_content = COALESCE(room.creation_content, $2),
name = COALESCE($3, room.name), tombstone_content = COALESCE(room.tombstone_content, $3),
name_quality = CASE WHEN $3 IS NOT NULL THEN $4 ELSE room.name_quality END, name = COALESCE($4, room.name),
avatar = COALESCE($5, room.avatar), name_quality = CASE WHEN $4 IS NOT NULL THEN $5 ELSE room.name_quality END,
explicit_avatar = CASE WHEN $5 IS NOT NULL THEN $6 ELSE room.explicit_avatar END, avatar = COALESCE($6, room.avatar),
topic = COALESCE($7, room.topic), explicit_avatar = CASE WHEN $6 IS NOT NULL THEN $7 ELSE room.explicit_avatar END,
canonical_alias = COALESCE($8, room.canonical_alias), topic = COALESCE($8, room.topic),
lazy_load_summary = COALESCE($9, room.lazy_load_summary), canonical_alias = COALESCE($9, room.canonical_alias),
encryption_event = COALESCE($10, room.encryption_event), lazy_load_summary = COALESCE($10, room.lazy_load_summary),
has_member_list = room.has_member_list OR $11, encryption_event = COALESCE($11, room.encryption_event),
preview_event_rowid = COALESCE($12, room.preview_event_rowid), has_member_list = room.has_member_list OR $12,
sorting_timestamp = COALESCE($13, room.sorting_timestamp), preview_event_rowid = COALESCE($13, room.preview_event_rowid),
unread_highlights = COALESCE($14, room.unread_highlights), sorting_timestamp = COALESCE($14, room.sorting_timestamp),
unread_notifications = COALESCE($15, room.unread_notifications), unread_highlights = COALESCE($15, room.unread_highlights),
unread_messages = COALESCE($16, room.unread_messages), unread_notifications = COALESCE($16, room.unread_notifications),
prev_batch = COALESCE($17, room.prev_batch) unread_messages = COALESCE($17, room.unread_messages),
prev_batch = COALESCE($18, room.prev_batch)
WHERE room_id = $1 WHERE room_id = $1
` `
setRoomPrevBatchQuery = ` setRoomPrevBatchQuery = `
@ -130,8 +131,9 @@ const (
const PrevBatchPaginationComplete = "fi.mau.gomuks.pagination_complete" const PrevBatchPaginationComplete = "fi.mau.gomuks.pagination_complete"
type Room struct { type Room struct {
ID id.RoomID `json:"room_id"` ID id.RoomID `json:"room_id"`
CreationContent *event.CreateEventContent `json:"creation_content,omitempty"` CreationContent *event.CreateEventContent `json:"creation_content,omitempty"`
Tombstone *event.TombstoneEventContent `json:"tombstone,omitempty"`
Name *string `json:"name,omitempty"` Name *string `json:"name,omitempty"`
NameQuality NameQuality `json:"name_quality"` NameQuality NameQuality `json:"name_quality"`
@ -153,6 +155,14 @@ type Room struct {
} }
func (r *Room) CheckChangesAndCopyInto(other *Room) (hasChanges bool) { 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 { if r.Name != nil && r.NameQuality >= other.NameQuality {
other.Name = r.Name other.Name = r.Name
other.NameQuality = r.NameQuality other.NameQuality = r.NameQuality
@ -216,6 +226,7 @@ func (r *Room) Scan(row dbutil.Scannable) (*Room, error) {
err := row.Scan( err := row.Scan(
&r.ID, &r.ID,
dbutil.JSON{Data: &r.CreationContent}, dbutil.JSON{Data: &r.CreationContent},
dbutil.JSON{Data: &r.Tombstone},
&r.Name, &r.Name,
&r.NameQuality, &r.NameQuality,
&r.Avatar, &r.Avatar,
@ -245,6 +256,7 @@ func (r *Room) sqlVariables() []any {
return []any{ return []any{
r.ID, r.ID,
dbutil.JSONPtr(r.CreationContent), dbutil.JSONPtr(r.CreationContent),
dbutil.JSONPtr(r.Tombstone),
r.Name, r.Name,
r.NameQuality, r.NameQuality,
r.Avatar, r.Avatar,

View file

@ -1,4 +1,4 @@
-- v0 -> v3 (compatible with v1+): Latest revision -- v0 -> v4 (compatible with v1+): Latest revision
CREATE TABLE account ( CREATE TABLE account (
user_id TEXT NOT NULL PRIMARY KEY, user_id TEXT NOT NULL PRIMARY KEY,
device_id TEXT NOT NULL, device_id TEXT NOT NULL,
@ -11,6 +11,7 @@ CREATE TABLE account (
CREATE TABLE room ( CREATE TABLE room (
room_id TEXT NOT NULL PRIMARY KEY, room_id TEXT NOT NULL PRIMARY KEY,
creation_content TEXT, creation_content TEXT,
tombstone_content TEXT,
name TEXT, name TEXT,
name_quality INTEGER NOT NULL DEFAULT 0, name_quality INTEGER NOT NULL DEFAULT 0,

View file

@ -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
);

View file

@ -816,7 +816,8 @@ func processImportantEvent(ctx context.Context, evt *event.Event, existingRoomDa
return return
} }
switch evt.Type { 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 != "" { if *evt.StateKey != "" {
return return
} }
@ -834,6 +835,8 @@ func processImportantEvent(ctx context.Context, evt *event.Event, existingRoomDa
switch evt.Type { switch evt.Type {
case event.StateCreate: case event.StateCreate:
updatedRoom.CreationContent, _ = evt.Content.Parsed.(*event.CreateEventContent) updatedRoom.CreationContent, _ = evt.Content.Parsed.(*event.CreateEventContent)
case event.StateTombstone:
updatedRoom.Tombstone, _ = evt.Content.Parsed.(*event.TombstoneEventContent)
case event.StateEncryption: case event.StateEncryption:
newEncryption, _ := evt.Content.Parsed.(*event.EncryptionEventContent) newEncryption, _ := evt.Content.Parsed.(*event.EncryptionEventContent)
if existingRoomData.EncryptionEvent == nil || existingRoomData.EncryptionEvent.Algorithm == newEncryption.Algorithm { if existingRoomData.EncryptionEvent == nil || existingRoomData.EncryptionEvent.Algorithm == newEncryption.Algorithm {