aboutsummaryrefslogtreecommitdiff
path: root/roomserver/storage/postgres/storage.go
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2020-03-17 15:12:01 +0000
committerGitHub <noreply@github.com>2020-03-17 15:12:01 +0000
commita66c701b29255a5cb7ec68b970948fb77f1f31af (patch)
tree799a8b973b820741beb52ec1c597ba1c716b519a /roomserver/storage/postgres/storage.go
parentaebf347a79102597a359dcd59bb45593c8352780 (diff)
Add room version to room create request, persist in storage (#915)
* Add room version into createRoomReq * Extract room version from m.room.create event when persisting * Reduce cyclomatic complexity * Update whitelist, gomatrixserverlib, tweaks to roomserver * Update sytest-whitelist again
Diffstat (limited to 'roomserver/storage/postgres/storage.go')
-rw-r--r--roomserver/storage/postgres/storage.go43
1 files changed, 41 insertions, 2 deletions
diff --git a/roomserver/storage/postgres/storage.go b/roomserver/storage/postgres/storage.go
index ee1a1cd4..b2b4159c 100644
--- a/roomserver/storage/postgres/storage.go
+++ b/roomserver/storage/postgres/storage.go
@@ -18,6 +18,9 @@ package postgres
import (
"context"
"database/sql"
+ "encoding/json"
+
+ roomserverVersion "github.com/matrix-org/dendrite/roomserver/version"
// Import the postgres database driver.
_ "github.com/lib/pq"
@@ -68,8 +71,21 @@ func (d *Database) StoreEvent(
}
}
- // TODO: Room version here
- if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID(), "1"); err != nil {
+ // TODO: Here we should aim to have two different code paths for new rooms
+ // vs existing ones.
+
+ // Get the default room version. If the client doesn't supply a room_version
+ // then we will use our configured default to create the room.
+ // https://matrix.org/docs/spec/client_server/r0.6.0#post-matrix-client-r0-createroom
+ // Note that the below logic depends on the m.room.create event being the
+ // first event that is persisted to the database when creating or joining a
+ // room.
+ var roomVersion gomatrixserverlib.RoomVersion
+ if roomVersion, err = extractRoomVersionFromCreateEvent(event); err != nil {
+ return 0, types.StateAtEvent{}, err
+ }
+
+ if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID(), roomVersion); err != nil {
return 0, types.StateAtEvent{}, err
}
@@ -121,6 +137,29 @@ func (d *Database) StoreEvent(
}, nil
}
+func extractRoomVersionFromCreateEvent(event gomatrixserverlib.Event) (
+ gomatrixserverlib.RoomVersion, error,
+) {
+ var err error
+ var roomVersion gomatrixserverlib.RoomVersion
+ // Look for m.room.create events.
+ if event.Type() != gomatrixserverlib.MRoomCreate {
+ return gomatrixserverlib.RoomVersion(""), nil
+ }
+ roomVersion = roomserverVersion.DefaultRoomVersion()
+ var createContent gomatrixserverlib.CreateContent
+ // The m.room.create event contains an optional "room_version" key in
+ // the event content, so we need to unmarshal that first.
+ if err = json.Unmarshal(event.Content(), &createContent); err != nil {
+ return gomatrixserverlib.RoomVersion(""), err
+ }
+ // A room version was specified in the event content?
+ if createContent.RoomVersion != nil {
+ roomVersion = *createContent.RoomVersion
+ }
+ return roomVersion, err
+}
+
func (d *Database) assignRoomNID(
ctx context.Context, txn *sql.Tx,
roomID string, roomVersion gomatrixserverlib.RoomVersion,