Update room version descriptors, add error handling (#906)
* Update room version descriptors, add error handling * Fix database queries * Drop Get from version package * Fix database wrapping, add comments for version descriptions * Don't set default room_version value in SQLmain
parent
dfd8b93d93
commit
452f393dd7
2
go.mod
2
go.mod
|
@ -10,7 +10,7 @@ require (
|
||||||
github.com/matrix-org/go-http-js-libp2p v0.0.0-20200310180544-7f3fad43b51c
|
github.com/matrix-org/go-http-js-libp2p v0.0.0-20200310180544-7f3fad43b51c
|
||||||
github.com/matrix-org/go-sqlite3-js v0.0.0-20200304164012-aa524245b658
|
github.com/matrix-org/go-sqlite3-js v0.0.0-20200304164012-aa524245b658
|
||||||
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26
|
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20200306154041-df6bb9a3e424
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200316144058-cc6847798a3f
|
||||||
github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1
|
github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1
|
||||||
github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7
|
github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7
|
||||||
github.com/mattn/go-sqlite3 v2.0.2+incompatible
|
github.com/mattn/go-sqlite3 v2.0.2+incompatible
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -138,6 +138,10 @@ github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5 h1:km
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI=
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200124100636-0c2ec91d1df5/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI=
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20200306154041-df6bb9a3e424 h1:H61lT6ckUFIDl9qb636qNomfo0B52lFt73ecioiqF10=
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200306154041-df6bb9a3e424 h1:H61lT6ckUFIDl9qb636qNomfo0B52lFt73ecioiqF10=
|
||||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20200306154041-df6bb9a3e424/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI=
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200306154041-df6bb9a3e424/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI=
|
||||||
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200316100021-ac4a53d49690 h1:aQPPypOdoIsquJHhoalRYvKtDoiJfSyyJqOEn6R7yTY=
|
||||||
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200316100021-ac4a53d49690/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI=
|
||||||
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200316144058-cc6847798a3f h1:JXSvzG4movqXT1KcdX+XZ9HM61m1FW4rIMFKUM9j/Dk=
|
||||||
|
github.com/matrix-org/gomatrixserverlib v0.0.0-20200316144058-cc6847798a3f/go.mod h1:FsKa2pWE/bpQql9H7U4boOPXFoJX/QcqaZZ6ijLkaZI=
|
||||||
github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1 h1:osLoFdOy+ChQqVUn2PeTDETFftVkl4w9t/OW18g3lnk=
|
github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1 h1:osLoFdOy+ChQqVUn2PeTDETFftVkl4w9t/OW18g3lnk=
|
||||||
github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1/go.mod h1:cXoYQIENbdWIQHt1SyCo6Bl3C3raHwJ0wgVrXHSqf+A=
|
github.com/matrix-org/naffka v0.0.0-20200127221512-0716baaabaf1/go.mod h1:cXoYQIENbdWIQHt1SyCo6Bl3C3raHwJ0wgVrXHSqf+A=
|
||||||
github.com/matrix-org/util v0.0.0-20171127121716-2e2df66af2f5 h1:W7l5CP4V7wPyPb4tYE11dbmeAOwtFQBTW0rf4OonOS8=
|
github.com/matrix-org/util v0.0.0-20171127121716-2e2df66af2f5 h1:W7l5CP4V7wPyPb4tYE11dbmeAOwtFQBTW0rf4OonOS8=
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
@ -733,10 +732,10 @@ func (r *RoomserverQueryAPI) QueryRoomVersionCapabilities(
|
||||||
request *api.QueryRoomVersionCapabilitiesRequest,
|
request *api.QueryRoomVersionCapabilitiesRequest,
|
||||||
response *api.QueryRoomVersionCapabilitiesResponse,
|
response *api.QueryRoomVersionCapabilitiesResponse,
|
||||||
) error {
|
) error {
|
||||||
response.DefaultRoomVersion = strconv.Itoa(int(version.GetDefaultRoomVersion()))
|
response.DefaultRoomVersion = string(version.DefaultRoomVersion())
|
||||||
response.AvailableRoomVersions = make(map[string]string)
|
response.AvailableRoomVersions = make(map[string]string)
|
||||||
for v, desc := range version.GetSupportedRoomVersions() {
|
for v, desc := range version.SupportedRoomVersions() {
|
||||||
sv := strconv.Itoa(int(v))
|
sv := string(v)
|
||||||
if desc.Stable {
|
if desc.Stable {
|
||||||
response.AvailableRoomVersions[sv] = "stable"
|
response.AvailableRoomVersions[sv] = "stable"
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -45,5 +45,5 @@ type Database interface {
|
||||||
GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderUserID string) (membershipEventNID types.EventNID, stillInRoom bool, err error)
|
GetMembership(ctx context.Context, roomNID types.RoomNID, requestSenderUserID string) (membershipEventNID types.EventNID, stillInRoom bool, err error)
|
||||||
GetMembershipEventNIDsForRoom(ctx context.Context, roomNID types.RoomNID, joinOnly bool) ([]types.EventNID, error)
|
GetMembershipEventNIDsForRoom(ctx context.Context, roomNID types.RoomNID, joinOnly bool) ([]types.EventNID, error)
|
||||||
EventsFromIDs(ctx context.Context, eventIDs []string) ([]types.Event, error)
|
EventsFromIDs(ctx context.Context, eventIDs []string) ([]types.Event, error)
|
||||||
GetRoomVersionForRoom(ctx context.Context, roomNID types.RoomNID) (int64, error)
|
GetRoomVersionForRoom(ctx context.Context, roomNID types.RoomNID) (gomatrixserverlib.RoomVersion, error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const roomsSchema = `
|
const roomsSchema = `
|
||||||
|
@ -42,13 +43,13 @@ CREATE TABLE IF NOT EXISTS roomserver_rooms (
|
||||||
state_snapshot_nid BIGINT NOT NULL DEFAULT 0,
|
state_snapshot_nid BIGINT NOT NULL DEFAULT 0,
|
||||||
-- The version of the room, which will assist in determining the state resolution
|
-- The version of the room, which will assist in determining the state resolution
|
||||||
-- algorithm, event ID format, etc.
|
-- algorithm, event ID format, etc.
|
||||||
room_version BIGINT NOT NULL DEFAULT 1
|
room_version TEXT NOT NULL
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
|
||||||
// Same as insertEventTypeNIDSQL
|
// Same as insertEventTypeNIDSQL
|
||||||
const insertRoomNIDSQL = "" +
|
const insertRoomNIDSQL = "" +
|
||||||
"INSERT INTO roomserver_rooms (room_id) VALUES ($1)" +
|
"INSERT INTO roomserver_rooms (room_id, room_version) VALUES ($1, $2)" +
|
||||||
" ON CONFLICT ON CONSTRAINT roomserver_room_id_unique" +
|
" ON CONFLICT ON CONSTRAINT roomserver_room_id_unique" +
|
||||||
" DO NOTHING RETURNING (room_nid)"
|
" DO NOTHING RETURNING (room_nid)"
|
||||||
|
|
||||||
|
@ -92,11 +93,12 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomStatements) insertRoomNID(
|
func (s *roomStatements) insertRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx, roomID string,
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||||
) (types.RoomNID, error) {
|
) (types.RoomNID, error) {
|
||||||
var roomNID int64
|
var roomNID int64
|
||||||
stmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
stmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
||||||
err := stmt.QueryRowContext(ctx, roomID).Scan(&roomNID)
|
err := stmt.QueryRowContext(ctx, roomID, roomVersion).Scan(&roomNID)
|
||||||
return types.RoomNID(roomNID), err
|
return types.RoomNID(roomNID), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,8 +167,8 @@ func (s *roomStatements) updateLatestEventNIDs(
|
||||||
|
|
||||||
func (s *roomStatements) selectRoomVersionForRoomNID(
|
func (s *roomStatements) selectRoomVersionForRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||||
) (int64, error) {
|
) (gomatrixserverlib.RoomVersion, error) {
|
||||||
var roomVersion int64
|
var roomVersion gomatrixserverlib.RoomVersion
|
||||||
stmt := common.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
stmt := common.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
||||||
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
||||||
return roomVersion, err
|
return roomVersion, err
|
||||||
|
|
|
@ -68,7 +68,8 @@ func (d *Database) StoreEvent(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID()); err != nil {
|
// TODO: Room version here
|
||||||
|
if roomNID, err = d.assignRoomNID(ctx, nil, event.RoomID(), "1"); err != nil {
|
||||||
return 0, types.StateAtEvent{}, err
|
return 0, types.StateAtEvent{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,13 +122,14 @@ func (d *Database) StoreEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) assignRoomNID(
|
func (d *Database) assignRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx, roomID string,
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||||
) (types.RoomNID, error) {
|
) (types.RoomNID, error) {
|
||||||
// Check if we already have a numeric ID in the database.
|
// Check if we already have a numeric ID in the database.
|
||||||
roomNID, err := d.statements.selectRoomNID(ctx, txn, roomID)
|
roomNID, err := d.statements.selectRoomNID(ctx, txn, roomID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
// We don't have a numeric ID so insert one into the database.
|
// We don't have a numeric ID so insert one into the database.
|
||||||
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID)
|
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID, roomVersion)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
// We raced with another insert so run the select again.
|
// We raced with another insert so run the select again.
|
||||||
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||||
|
@ -494,7 +496,8 @@ func (d *Database) MembershipUpdater(
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
roomNID, err := d.assignRoomNID(ctx, txn, roomID)
|
// TODO: Room version here
|
||||||
|
roomNID, err := d.assignRoomNID(ctx, txn, roomID, "1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -699,7 +702,7 @@ func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]type
|
||||||
|
|
||||||
func (d *Database) GetRoomVersionForRoom(
|
func (d *Database) GetRoomVersionForRoom(
|
||||||
ctx context.Context, roomNID types.RoomNID,
|
ctx context.Context, roomNID types.RoomNID,
|
||||||
) (int64, error) {
|
) (gomatrixserverlib.RoomVersion, error) {
|
||||||
return d.statements.selectRoomVersionForRoomNID(
|
return d.statements.selectRoomVersionForRoomNID(
|
||||||
ctx, nil, roomNID,
|
ctx, nil, roomNID,
|
||||||
)
|
)
|
||||||
|
|
|
@ -22,6 +22,7 @@ import (
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/roomserver/types"
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
const roomsSchema = `
|
const roomsSchema = `
|
||||||
|
@ -31,13 +32,13 @@ const roomsSchema = `
|
||||||
latest_event_nids TEXT NOT NULL DEFAULT '[]',
|
latest_event_nids TEXT NOT NULL DEFAULT '[]',
|
||||||
last_event_sent_nid INTEGER NOT NULL DEFAULT 0,
|
last_event_sent_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
state_snapshot_nid INTEGER NOT NULL DEFAULT 0,
|
state_snapshot_nid INTEGER NOT NULL DEFAULT 0,
|
||||||
room_version INTEGER NOT NULL DEFAULT 1
|
room_version TEXT NOT NULL
|
||||||
);
|
);
|
||||||
`
|
`
|
||||||
|
|
||||||
// Same as insertEventTypeNIDSQL
|
// Same as insertEventTypeNIDSQL
|
||||||
const insertRoomNIDSQL = `
|
const insertRoomNIDSQL = `
|
||||||
INSERT INTO roomserver_rooms (room_id) VALUES ($1)
|
INSERT INTO roomserver_rooms (room_id, room_version) VALUES ($1, $2)
|
||||||
ON CONFLICT DO NOTHING;
|
ON CONFLICT DO NOTHING;
|
||||||
`
|
`
|
||||||
|
|
||||||
|
@ -81,11 +82,12 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomStatements) insertRoomNID(
|
func (s *roomStatements) insertRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx, roomID string,
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||||
) (types.RoomNID, error) {
|
) (types.RoomNID, error) {
|
||||||
var err error
|
var err error
|
||||||
insertStmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
insertStmt := common.TxStmt(txn, s.insertRoomNIDStmt)
|
||||||
if _, err = insertStmt.ExecContext(ctx, roomID); err == nil {
|
if _, err = insertStmt.ExecContext(ctx, roomID, roomVersion); err == nil {
|
||||||
return s.selectRoomNID(ctx, txn, roomID)
|
return s.selectRoomNID(ctx, txn, roomID)
|
||||||
} else {
|
} else {
|
||||||
return types.RoomNID(0), err
|
return types.RoomNID(0), err
|
||||||
|
@ -157,8 +159,8 @@ func (s *roomStatements) updateLatestEventNIDs(
|
||||||
|
|
||||||
func (s *roomStatements) selectRoomVersionForRoomNID(
|
func (s *roomStatements) selectRoomVersionForRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
ctx context.Context, txn *sql.Tx, roomNID types.RoomNID,
|
||||||
) (int64, error) {
|
) (gomatrixserverlib.RoomVersion, error) {
|
||||||
var roomVersion int64
|
var roomVersion gomatrixserverlib.RoomVersion
|
||||||
stmt := common.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
stmt := common.TxStmt(txn, s.selectRoomVersionForRoomNIDStmt)
|
||||||
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
err := stmt.QueryRowContext(ctx, roomNID).Scan(&roomVersion)
|
||||||
return roomVersion, err
|
return roomVersion, err
|
||||||
|
|
|
@ -90,7 +90,8 @@ func (d *Database) StoreEvent(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if roomNID, err = d.assignRoomNID(ctx, txn, event.RoomID()); err != nil {
|
// TODO: Room version here
|
||||||
|
if roomNID, err = d.assignRoomNID(ctx, txn, event.RoomID(), "1"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,13 +151,14 @@ func (d *Database) StoreEvent(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Database) assignRoomNID(
|
func (d *Database) assignRoomNID(
|
||||||
ctx context.Context, txn *sql.Tx, roomID string,
|
ctx context.Context, txn *sql.Tx,
|
||||||
|
roomID string, roomVersion gomatrixserverlib.RoomVersion,
|
||||||
) (roomNID types.RoomNID, err error) {
|
) (roomNID types.RoomNID, err error) {
|
||||||
// Check if we already have a numeric ID in the database.
|
// Check if we already have a numeric ID in the database.
|
||||||
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
// We don't have a numeric ID so insert one into the database.
|
// We don't have a numeric ID so insert one into the database.
|
||||||
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID)
|
roomNID, err = d.statements.insertRoomNID(ctx, txn, roomID, roomVersion)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Now get the numeric ID back out of the database
|
// Now get the numeric ID back out of the database
|
||||||
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
roomNID, err = d.statements.selectRoomNID(ctx, txn, roomID)
|
||||||
|
@ -630,7 +632,8 @@ func (d *Database) MembershipUpdater(
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
roomNID, err := d.assignRoomNID(ctx, txn, roomID)
|
// TODO: Room version here
|
||||||
|
roomNID, err := d.assignRoomNID(ctx, txn, roomID, "1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -853,7 +856,7 @@ func (d *Database) EventsFromIDs(ctx context.Context, eventIDs []string) ([]type
|
||||||
|
|
||||||
func (d *Database) GetRoomVersionForRoom(
|
func (d *Database) GetRoomVersionForRoom(
|
||||||
ctx context.Context, roomNID types.RoomNID,
|
ctx context.Context, roomNID types.RoomNID,
|
||||||
) (int64, error) {
|
) (gomatrixserverlib.RoomVersion, error) {
|
||||||
return d.statements.selectRoomVersionForRoomNID(
|
return d.statements.selectRoomVersionForRoomNID(
|
||||||
ctx, nil, roomNID,
|
ctx, nil, roomNID,
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,85 +15,64 @@
|
||||||
package version
|
package version
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/roomserver/state"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
)
|
|
||||||
|
|
||||||
type RoomVersionID int
|
|
||||||
type EventFormatID int
|
|
||||||
|
|
||||||
const (
|
|
||||||
RoomVersionV1 RoomVersionID = iota + 1
|
|
||||||
RoomVersionV2
|
|
||||||
RoomVersionV3
|
|
||||||
RoomVersionV4
|
|
||||||
RoomVersionV5
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
EventFormatV1 EventFormatID = iota + 1 // original event ID formatting
|
|
||||||
EventFormatV2 // event ID is event hash
|
|
||||||
EventFormatV3 // event ID is URL-safe base64 event hash
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RoomVersionDescription contains information about a room version,
|
||||||
|
// namely whether it is marked as supported or stable in this server
|
||||||
|
// version.
|
||||||
|
// A version is supported if the server has some support for rooms
|
||||||
|
// that are this version. A version is marked as stable or unstable
|
||||||
|
// in order to hint whether the version should be used to clients
|
||||||
|
// calling the /capabilities endpoint.
|
||||||
|
// https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-capabilities
|
||||||
type RoomVersionDescription struct {
|
type RoomVersionDescription struct {
|
||||||
Supported bool
|
Supported bool
|
||||||
Stable bool
|
Stable bool
|
||||||
StateResolution state.StateResolutionVersion
|
|
||||||
EventFormat EventFormatID
|
|
||||||
EnforceSigningKeyValidity bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var roomVersions = map[RoomVersionID]RoomVersionDescription{
|
var roomVersions = map[gomatrixserverlib.RoomVersion]RoomVersionDescription{
|
||||||
RoomVersionV1: RoomVersionDescription{
|
gomatrixserverlib.RoomVersionV1: RoomVersionDescription{
|
||||||
Supported: true,
|
Supported: true,
|
||||||
Stable: true,
|
Stable: true,
|
||||||
StateResolution: state.StateResolutionAlgorithmV1,
|
|
||||||
EventFormat: EventFormatV1,
|
|
||||||
EnforceSigningKeyValidity: false,
|
|
||||||
},
|
},
|
||||||
RoomVersionV2: RoomVersionDescription{
|
gomatrixserverlib.RoomVersionV2: RoomVersionDescription{
|
||||||
Supported: false,
|
Supported: false,
|
||||||
Stable: true,
|
Stable: false,
|
||||||
StateResolution: state.StateResolutionAlgorithmV2,
|
|
||||||
EventFormat: EventFormatV1,
|
|
||||||
EnforceSigningKeyValidity: false,
|
|
||||||
},
|
},
|
||||||
RoomVersionV3: RoomVersionDescription{
|
gomatrixserverlib.RoomVersionV3: RoomVersionDescription{
|
||||||
Supported: false,
|
Supported: false,
|
||||||
Stable: true,
|
Stable: false,
|
||||||
StateResolution: state.StateResolutionAlgorithmV2,
|
|
||||||
EventFormat: EventFormatV2,
|
|
||||||
EnforceSigningKeyValidity: false,
|
|
||||||
},
|
},
|
||||||
RoomVersionV4: RoomVersionDescription{
|
gomatrixserverlib.RoomVersionV4: RoomVersionDescription{
|
||||||
Supported: false,
|
Supported: false,
|
||||||
Stable: true,
|
Stable: false,
|
||||||
StateResolution: state.StateResolutionAlgorithmV2,
|
|
||||||
EventFormat: EventFormatV3,
|
|
||||||
EnforceSigningKeyValidity: false,
|
|
||||||
},
|
},
|
||||||
RoomVersionV5: RoomVersionDescription{
|
gomatrixserverlib.RoomVersionV5: RoomVersionDescription{
|
||||||
Supported: false,
|
Supported: false,
|
||||||
Stable: true,
|
Stable: false,
|
||||||
StateResolution: state.StateResolutionAlgorithmV2,
|
|
||||||
EventFormat: EventFormatV3,
|
|
||||||
EnforceSigningKeyValidity: true,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDefaultRoomVersion() RoomVersionID {
|
// DefaultRoomVersion contains the room version that will, by
|
||||||
return RoomVersionV1
|
// default, be used to create new rooms on this server.
|
||||||
|
func DefaultRoomVersion() gomatrixserverlib.RoomVersion {
|
||||||
|
return gomatrixserverlib.RoomVersionV1
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRoomVersions() map[RoomVersionID]RoomVersionDescription {
|
// RoomVersions returns a map of all known room versions to this
|
||||||
|
// server.
|
||||||
|
func RoomVersions() map[gomatrixserverlib.RoomVersion]RoomVersionDescription {
|
||||||
return roomVersions
|
return roomVersions
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSupportedRoomVersions() map[RoomVersionID]RoomVersionDescription {
|
// SupportedRoomVersions returns a map of descriptions for room
|
||||||
versions := make(map[RoomVersionID]RoomVersionDescription)
|
// versions that are supported by this homeserver.
|
||||||
for id, version := range GetRoomVersions() {
|
func SupportedRoomVersions() map[gomatrixserverlib.RoomVersion]RoomVersionDescription {
|
||||||
|
versions := make(map[gomatrixserverlib.RoomVersion]RoomVersionDescription)
|
||||||
|
for id, version := range RoomVersions() {
|
||||||
if version.Supported {
|
if version.Supported {
|
||||||
versions[id] = version
|
versions[id] = version
|
||||||
}
|
}
|
||||||
|
@ -101,12 +80,46 @@ func GetSupportedRoomVersions() map[RoomVersionID]RoomVersionDescription {
|
||||||
return versions
|
return versions
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSupportedRoomVersion(version RoomVersionID) (desc RoomVersionDescription, err error) {
|
// RoomVersion returns information about a specific room version.
|
||||||
|
// An UnknownVersionError is returned if the version is not known
|
||||||
|
// to the server.
|
||||||
|
func RoomVersion(version gomatrixserverlib.RoomVersion) (RoomVersionDescription, error) {
|
||||||
if version, ok := roomVersions[version]; ok {
|
if version, ok := roomVersions[version]; ok {
|
||||||
desc = version
|
return version, nil
|
||||||
}
|
}
|
||||||
if !desc.Supported {
|
return RoomVersionDescription{}, UnknownVersionError{version}
|
||||||
err = errors.New("unsupported room version")
|
}
|
||||||
}
|
|
||||||
return
|
// SupportedRoomVersion returns information about a specific room
|
||||||
|
// version. An UnknownVersionError is returned if the version is not
|
||||||
|
// known to the server, or an UnsupportedVersionError is returned if
|
||||||
|
// the version is known but specifically marked as unsupported.
|
||||||
|
func SupportedRoomVersion(version gomatrixserverlib.RoomVersion) (RoomVersionDescription, error) {
|
||||||
|
result, err := RoomVersion(version)
|
||||||
|
if err != nil {
|
||||||
|
return RoomVersionDescription{}, err
|
||||||
|
}
|
||||||
|
if !result.Supported {
|
||||||
|
return RoomVersionDescription{}, UnsupportedVersionError{version}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnknownVersionError is caused when the room version is not known.
|
||||||
|
type UnknownVersionError struct {
|
||||||
|
Version gomatrixserverlib.RoomVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e UnknownVersionError) Error() string {
|
||||||
|
return fmt.Sprintf("room version '%s' is not known", e.Version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnsupportedVersionError is caused when the room version is specifically
|
||||||
|
// marked as unsupported.
|
||||||
|
type UnsupportedVersionError struct {
|
||||||
|
Version gomatrixserverlib.RoomVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e UnsupportedVersionError) Error() string {
|
||||||
|
return fmt.Sprintf("room version '%s' is marked as unsupported", e.Version)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue