diff options
author | Kegsay <kegan@matrix.org> | 2020-09-01 12:40:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-01 12:40:49 +0100 |
commit | 6d79f043541f522207f298d0f585c01e74e1177d (patch) | |
tree | 7509c9f1407bd9592de75f1b76d199cc9a2c9839 /roomserver/storage/sqlite3 | |
parent | 0ab5bccd11eea6063968c60fbdf5b36ade22da81 (diff) |
Add RoomInfo metadata struct (#1367)
* Add RoomInfo struct
* Remove RoomNID and replace with RoomInfo
* Bugfix and remove another needless query
* nil guard
Diffstat (limited to 'roomserver/storage/sqlite3')
-rw-r--r-- | roomserver/storage/sqlite3/rooms_table.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/roomserver/storage/sqlite3/rooms_table.go b/roomserver/storage/sqlite3/rooms_table.go index 6541cc0c..fc1bcf22 100644 --- a/roomserver/storage/sqlite3/rooms_table.go +++ b/roomserver/storage/sqlite3/rooms_table.go @@ -64,6 +64,9 @@ const selectRoomVersionForRoomIDSQL = "" + const selectRoomVersionForRoomNIDSQL = "" + "SELECT room_version FROM roomserver_rooms WHERE room_nid = $1" +const selectRoomInfoSQL = "" + + "SELECT room_version, room_nid, state_snapshot_nid, latest_event_nids FROM roomserver_rooms WHERE room_id = $1" + type roomStatements struct { db *sql.DB insertRoomNIDStmt *sql.Stmt @@ -73,6 +76,7 @@ type roomStatements struct { updateLatestEventNIDsStmt *sql.Stmt selectRoomVersionForRoomIDStmt *sql.Stmt selectRoomVersionForRoomNIDStmt *sql.Stmt + selectRoomInfoStmt *sql.Stmt } func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) { @@ -91,9 +95,30 @@ func NewSqliteRoomsTable(db *sql.DB) (tables.Rooms, error) { {&s.updateLatestEventNIDsStmt, updateLatestEventNIDsSQL}, {&s.selectRoomVersionForRoomIDStmt, selectRoomVersionForRoomIDSQL}, {&s.selectRoomVersionForRoomNIDStmt, selectRoomVersionForRoomNIDSQL}, + {&s.selectRoomInfoStmt, selectRoomInfoSQL}, }.Prepare(db) } +func (s *roomStatements) SelectRoomInfo(ctx context.Context, roomID string) (*types.RoomInfo, error) { + var info types.RoomInfo + var latestNIDsJSON string + err := s.selectRoomInfoStmt.QueryRowContext(ctx, roomID).Scan( + &info.RoomVersion, &info.RoomNID, &info.StateSnapshotNID, &latestNIDsJSON, + ) + if err != nil { + if err == sql.ErrNoRows { + return nil, nil + } + return nil, err + } + var latestNIDs []int64 + if err = json.Unmarshal([]byte(latestNIDsJSON), &latestNIDs); err != nil { + return nil, err + } + info.IsStub = len(latestNIDs) == 0 + return &info, err +} + func (s *roomStatements) InsertRoomNID( ctx context.Context, txn *sql.Tx, roomID string, roomVersion gomatrixserverlib.RoomVersion, |