mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-12 01:13:10 -06:00
Use NIDs instead of IDs for alias and room retrieval
This commit is contained in:
parent
a99f7de70f
commit
9a077b0440
|
|
@ -23,21 +23,30 @@ import (
|
||||||
"github.com/matrix-org/dendrite/common"
|
"github.com/matrix-org/dendrite/common"
|
||||||
"github.com/matrix-org/dendrite/common/config"
|
"github.com/matrix-org/dendrite/common/config"
|
||||||
"github.com/matrix-org/dendrite/roomserver/api"
|
"github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
"github.com/matrix-org/gomatrixserverlib"
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
"github.com/matrix-org/util"
|
"github.com/matrix-org/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RoomserverAliasAPIDatabase has the storage APIs needed to implement the alias API.
|
// RoomserverAliasAPIDatabase has the storage APIs needed to implement the alias API.
|
||||||
type RoomserverAliasAPIDatabase interface {
|
type RoomserverAliasAPIDatabase interface {
|
||||||
// Save a given room alias with the room ID it refers to.
|
// Lookup the numeric ID for the room.
|
||||||
|
// Returns 0 if the room doesn't exists.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
SetRoomAlias(alias string, roomID string) error
|
RoomNID(roomID string) (types.RoomNID, error)
|
||||||
// Lookup the room ID a given alias refers to.
|
// Lookup the ID of a room identified by a given numeric ID.
|
||||||
|
// Returns an error if there was a problem talking to the database or if no
|
||||||
|
// room matches the given numeric ID.
|
||||||
|
RoomID(roomNID types.RoomNID) (string, error)
|
||||||
|
// Save a given room alias with the room numeric ID it refers to.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
GetRoomIDFromAlias(alias string) (string, error)
|
SetRoomAlias(alias string, roomNID types.RoomNID) error
|
||||||
// Lookup all aliases referring to a given room ID.
|
// Lookup the room numeric ID a given alias refers to.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
GetAliasesFromRoomID(roomID string) ([]string, error)
|
GetRoomNIDFromAlias(alias string) (types.RoomNID, error)
|
||||||
|
// Lookup all aliases referring to a given room numeric ID.
|
||||||
|
// Returns an error if there was a problem talking to the database.
|
||||||
|
GetAliasesFromRoomNID(roomNID types.RoomNID) ([]string, error)
|
||||||
// Remove a given room alias.
|
// Remove a given room alias.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
RemoveRoomAlias(alias string) error
|
RemoveRoomAlias(alias string) error
|
||||||
|
|
@ -57,24 +66,28 @@ func (r *RoomserverAliasAPI) SetRoomAlias(
|
||||||
response *api.SetRoomAliasResponse,
|
response *api.SetRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Check if the alias isn't already referring to a room
|
// Check if the alias isn't already referring to a room
|
||||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
roomNID, err := r.DB.GetRoomNIDFromAlias(request.Alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(roomID) > 0 {
|
if roomNID != 0 {
|
||||||
// If the alias already exists, stop the process
|
// If the alias already exists, stop the process
|
||||||
response.AliasExists = true
|
response.AliasExists = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
response.AliasExists = false
|
response.AliasExists = false
|
||||||
|
|
||||||
|
roomNID, err = r.DB.RoomNID(request.RoomID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// Save the new alias
|
// Save the new alias
|
||||||
if err := r.DB.SetRoomAlias(request.Alias, request.RoomID); err != nil {
|
if err := r.DB.SetRoomAlias(request.Alias, roomNID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send a m.room.aliases event with the updated list of aliases for this room
|
// Send a m.room.aliases event with the updated list of aliases for this room
|
||||||
if err := r.sendUpdatedAliasesEvent(request.UserID, request.RoomID); err != nil {
|
if err := r.sendUpdatedAliasesEvent(request.UserID, request.RoomID, roomNID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,7 +100,12 @@ func (r *RoomserverAliasAPI) GetAliasRoomID(
|
||||||
response *api.GetAliasRoomIDResponse,
|
response *api.GetAliasRoomIDResponse,
|
||||||
) error {
|
) error {
|
||||||
// Lookup the room ID in the database
|
// Lookup the room ID in the database
|
||||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
roomNID, err := r.DB.GetRoomNIDFromAlias(request.Alias)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
roomID, err := r.DB.RoomID(roomNID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -102,7 +120,12 @@ func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
||||||
response *api.RemoveRoomAliasResponse,
|
response *api.RemoveRoomAliasResponse,
|
||||||
) error {
|
) error {
|
||||||
// Lookup the room ID in the database
|
// Lookup the room ID in the database
|
||||||
roomID, err := r.DB.GetRoomIDFromAlias(request.Alias)
|
roomNID, err := r.DB.GetRoomNIDFromAlias(request.Alias)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
roomID, err := r.DB.RoomID(roomNID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +136,7 @@ func (r *RoomserverAliasAPI) RemoveRoomAlias(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send an updated m.room.aliases event
|
// Send an updated m.room.aliases event
|
||||||
if err := r.sendUpdatedAliasesEvent(request.UserID, roomID); err != nil {
|
if err := r.sendUpdatedAliasesEvent(request.UserID, roomID, roomNID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -126,7 +149,7 @@ type roomAliasesContent struct {
|
||||||
|
|
||||||
// Build the updated m.room.aliases event to send to the room after addition or
|
// Build the updated m.room.aliases event to send to the room after addition or
|
||||||
// removal of an alias
|
// removal of an alias
|
||||||
func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(userID string, roomID string) error {
|
func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(userID string, roomID string, roomNID types.RoomNID) error {
|
||||||
serverName := string(r.Cfg.Matrix.ServerName)
|
serverName := string(r.Cfg.Matrix.ServerName)
|
||||||
|
|
||||||
builder := gomatrixserverlib.EventBuilder{
|
builder := gomatrixserverlib.EventBuilder{
|
||||||
|
|
@ -138,7 +161,7 @@ func (r *RoomserverAliasAPI) sendUpdatedAliasesEvent(userID string, roomID strin
|
||||||
|
|
||||||
// Retrieve the updated list of aliases, marhal it and set it as the
|
// Retrieve the updated list of aliases, marhal it and set it as the
|
||||||
// event's content
|
// event's content
|
||||||
aliases, err := r.DB.GetAliasesFromRoomID(roomID)
|
aliases, err := r.DB.GetAliasesFromRoomNID(roomNID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,23 +33,27 @@ type RoomserverPublicRoomAPIDatabase interface {
|
||||||
// Returns 0 if the room doesn't exists.
|
// Returns 0 if the room doesn't exists.
|
||||||
// Returns an error if there was a problem talking to the database.
|
// Returns an error if there was a problem talking to the database.
|
||||||
RoomNID(roomID string) (types.RoomNID, error)
|
RoomNID(roomID string) (types.RoomNID, error)
|
||||||
|
// Lookup the room IDs matching a batch of room numeric IDs, ordered by
|
||||||
|
// numeric ID (ie map[roomNID] = roomID).
|
||||||
|
// Returns an error if the retrieval failed.
|
||||||
|
RoomIDs([]types.RoomNID) (map[types.RoomNID]string, error)
|
||||||
// Checks the visibility of the room identified by the given numeric ID.
|
// Checks the visibility of the room identified by the given numeric ID.
|
||||||
// Returns true if the room is publicly visible, returns false if not.
|
// Returns true if the room is publicly visible, returns false if not.
|
||||||
// If there's no room matching this numeric ID, or if the retrieval failed,
|
// If there's no room matching this numeric ID, or if the retrieval failed,
|
||||||
// returns an error.
|
// returns an error.
|
||||||
IsRoomPublic(roomNID types.RoomNID) (bool, error)
|
IsRoomPublic(roomNID types.RoomNID) (bool, error)
|
||||||
// Returns an array of string containing the room IDs of the rooms that are
|
// Returns an array of string containing the room numeric IDs of the rooms
|
||||||
// publicly visible.
|
// that are publicly visible.
|
||||||
// Returns an error if the retrieval failed.
|
// Returns an error if the retrieval failed.
|
||||||
GetPublicRoomIDs() ([]string, error)
|
GetPublicRoomNIDs() ([]types.RoomNID, error)
|
||||||
// Updates the visibility for a room to the given value: true means that the
|
// Updates the visibility for a room to the given value: true means that the
|
||||||
// room is publicly visible, false means that the room isn't publicly visible.
|
// room is publicly visible, false means that the room isn't publicly visible.
|
||||||
// Returns an error if the update failed.
|
// Returns an error if the update failed.
|
||||||
UpdateRoomVisibility(roomNID types.RoomNID, visibility bool) error
|
UpdateRoomVisibility(roomNID types.RoomNID, visibility bool) error
|
||||||
// Returns a map of the aliases bound to a given set of room IDs, ordered
|
// Returns a map of the aliases bound to a given set of room numeric IDs,
|
||||||
// by room ID (ie map[roomID] = []alias)
|
// ordered by room NID (ie map[roomNID] = []alias)
|
||||||
// Returns an error if the retrieval failed
|
// Returns an error if the retrieval failed
|
||||||
GetAliasesFromRoomIDs(roomIDs []string) (map[string][]string, error)
|
GetAliasesFromRoomNIDs(roomNIDs []types.RoomNID) (map[types.RoomNID][]string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomserverPublicRoomAPI is an implementation of api.RoomserverPublicRoomAPI
|
// RoomserverPublicRoomAPI is an implementation of api.RoomserverPublicRoomAPI
|
||||||
|
|
@ -120,12 +124,16 @@ func (r *RoomserverPublicRoomAPI) GetPublicRooms(
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
roomIDs, err := r.DB.GetPublicRoomIDs()
|
roomNIDs, err := r.DB.GetPublicRoomNIDs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
aliases, err := r.DB.GetAliasesFromRoomIDs(roomIDs)
|
aliases, err := r.DB.GetAliasesFromRoomNIDs(roomNIDs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
roomIDs, err := r.DB.RoomIDs(roomNIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -133,9 +141,9 @@ func (r *RoomserverPublicRoomAPI) GetPublicRooms(
|
||||||
chunks := []api.PublicRoomsChunk{}
|
chunks := []api.PublicRoomsChunk{}
|
||||||
// Iterate over the array of aliases instead of the array of rooms, because
|
// Iterate over the array of aliases instead of the array of rooms, because
|
||||||
// a room must have at least one alias to be listed
|
// a room must have at least one alias to be listed
|
||||||
for room, as := range aliases {
|
for roomNID, as := range aliases {
|
||||||
chunk := api.PublicRoomsChunk{
|
chunk := api.PublicRoomsChunk{
|
||||||
RoomID: room,
|
RoomID: roomIDs[roomNID],
|
||||||
Aliases: as,
|
Aliases: as,
|
||||||
NumJoinedMembers: 0,
|
NumJoinedMembers: 0,
|
||||||
WorldReadable: true,
|
WorldReadable: true,
|
||||||
|
|
@ -144,13 +152,14 @@ func (r *RoomserverPublicRoomAPI) GetPublicRooms(
|
||||||
chunks = append(chunks, chunk)
|
chunks = append(chunks, chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
if limit == 0 {
|
chunks = chunks[offset:]
|
||||||
// If limit is 0, don't limit the results
|
|
||||||
response.Chunks = chunks[offset:]
|
if len(chunks) >= int(limit) {
|
||||||
} else {
|
chunks = chunks[offset:limit]
|
||||||
response.Chunks = chunks[offset:limit]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.Chunks = chunks
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
|
"github.com/matrix-org/dendrite/roomserver/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const roomAliasesSchema = `
|
const roomAliasesSchema = `
|
||||||
|
|
@ -25,33 +26,33 @@ const roomAliasesSchema = `
|
||||||
CREATE TABLE IF NOT EXISTS roomserver_room_aliases (
|
CREATE TABLE IF NOT EXISTS roomserver_room_aliases (
|
||||||
-- Alias of the room
|
-- Alias of the room
|
||||||
alias TEXT NOT NULL PRIMARY KEY,
|
alias TEXT NOT NULL PRIMARY KEY,
|
||||||
-- Room ID the alias refers to
|
-- Room numeric ID the alias refers to
|
||||||
room_id TEXT NOT NULL
|
room_nid TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS roomserver_room_id_idx ON roomserver_room_aliases(room_id);
|
CREATE UNIQUE INDEX IF NOT EXISTS roomserver_room_nid_idx ON roomserver_room_aliases(room_nid);
|
||||||
`
|
`
|
||||||
|
|
||||||
const insertRoomAliasSQL = "" +
|
const insertRoomAliasSQL = "" +
|
||||||
"INSERT INTO roomserver_room_aliases (alias, room_id) VALUES ($1, $2)"
|
"INSERT INTO roomserver_room_aliases (alias, room_nid) VALUES ($1, $2)"
|
||||||
|
|
||||||
const selectRoomIDFromAliasSQL = "" +
|
const selectRoomNIDFromAliasSQL = "" +
|
||||||
"SELECT room_id FROM roomserver_room_aliases WHERE alias = $1"
|
"SELECT room_nid FROM roomserver_room_aliases WHERE alias = $1"
|
||||||
|
|
||||||
const selectAliasesFromRoomIDSQL = "" +
|
const selectAliasesFromRoomNIDSQL = "" +
|
||||||
"SELECT alias FROM roomserver_room_aliases WHERE room_id = $1"
|
"SELECT alias FROM roomserver_room_aliases WHERE room_nid = $1"
|
||||||
|
|
||||||
const selectAliasesFromRoomIDsSQL = "" +
|
const selectAliasesFromRoomNIDsSQL = "" +
|
||||||
"SELECT alias, room_id FROM roomserver_room_aliases WHERE room_id = ANY($1)"
|
"SELECT alias, room_nid FROM roomserver_room_aliases WHERE room_nid = ANY($1)"
|
||||||
|
|
||||||
const deleteRoomAliasSQL = "" +
|
const deleteRoomAliasSQL = "" +
|
||||||
"DELETE FROM roomserver_room_aliases WHERE alias = $1"
|
"DELETE FROM roomserver_room_aliases WHERE alias = $1"
|
||||||
|
|
||||||
type roomAliasesStatements struct {
|
type roomAliasesStatements struct {
|
||||||
insertRoomAliasStmt *sql.Stmt
|
insertRoomAliasStmt *sql.Stmt
|
||||||
selectRoomIDFromAliasStmt *sql.Stmt
|
selectRoomNIDFromAliasStmt *sql.Stmt
|
||||||
selectAliasesFromRoomIDStmt *sql.Stmt
|
selectAliasesFromRoomNIDStmt *sql.Stmt
|
||||||
selectAliasesFromRoomIDsStmt *sql.Stmt
|
selectAliasesFromRoomNIDsStmt *sql.Stmt
|
||||||
deleteRoomAliasStmt *sql.Stmt
|
deleteRoomAliasStmt *sql.Stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,29 +63,29 @@ func (s *roomAliasesStatements) prepare(db *sql.DB) (err error) {
|
||||||
}
|
}
|
||||||
return statementList{
|
return statementList{
|
||||||
{&s.insertRoomAliasStmt, insertRoomAliasSQL},
|
{&s.insertRoomAliasStmt, insertRoomAliasSQL},
|
||||||
{&s.selectRoomIDFromAliasStmt, selectRoomIDFromAliasSQL},
|
{&s.selectRoomNIDFromAliasStmt, selectRoomNIDFromAliasSQL},
|
||||||
{&s.selectAliasesFromRoomIDStmt, selectAliasesFromRoomIDSQL},
|
{&s.selectAliasesFromRoomNIDStmt, selectAliasesFromRoomNIDSQL},
|
||||||
{&s.selectAliasesFromRoomIDsStmt, selectAliasesFromRoomIDsSQL},
|
{&s.selectAliasesFromRoomNIDsStmt, selectAliasesFromRoomNIDsSQL},
|
||||||
{&s.deleteRoomAliasStmt, deleteRoomAliasSQL},
|
{&s.deleteRoomAliasStmt, deleteRoomAliasSQL},
|
||||||
}.prepare(db)
|
}.prepare(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomAliasesStatements) insertRoomAlias(alias string, roomID string) (err error) {
|
func (s *roomAliasesStatements) insertRoomAlias(alias string, roomNID types.RoomNID) (err error) {
|
||||||
_, err = s.insertRoomAliasStmt.Exec(alias, roomID)
|
_, err = s.insertRoomAliasStmt.Exec(alias, roomNID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomAliasesStatements) selectRoomIDFromAlias(alias string) (roomID string, err error) {
|
func (s *roomAliasesStatements) selectRoomNIDFromAlias(alias string) (roomNID types.RoomNID, err error) {
|
||||||
err = s.selectRoomIDFromAliasStmt.QueryRow(alias).Scan(&roomID)
|
err = s.selectRoomNIDFromAliasStmt.QueryRow(alias).Scan(&roomNID)
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return "", nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomAliasesStatements) selectAliasesFromRoomID(roomID string) (aliases []string, err error) {
|
func (s *roomAliasesStatements) selectAliasesFromRoomNID(roomNID types.RoomNID) (aliases []string, err error) {
|
||||||
aliases = []string{}
|
aliases = []string{}
|
||||||
rows, err := s.selectAliasesFromRoomIDStmt.Query(roomID)
|
rows, err := s.selectAliasesFromRoomNIDStmt.Query(roomNID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -101,23 +102,31 @@ func (s *roomAliasesStatements) selectAliasesFromRoomID(roomID string) (aliases
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomAliasesStatements) selectAliasesFromRoomIDs(roomIDs []string) (aliases map[string][]string, err error) {
|
func (s *roomAliasesStatements) selectAliasesFromRoomNIDs(roomNIDs []types.RoomNID) (aliases map[types.RoomNID][]string, err error) {
|
||||||
aliases = make(map[string][]string)
|
aliases = make(map[types.RoomNID][]string)
|
||||||
rows, err := s.selectAliasesFromRoomIDsStmt.Query(pq.StringArray(roomIDs))
|
// Convert the array of numeric IDs into one that can be used in the query
|
||||||
|
nIDs := []int64{}
|
||||||
|
for _, roomNID := range roomNIDs {
|
||||||
|
nIDs = append(nIDs, int64(roomNID))
|
||||||
|
}
|
||||||
|
rows, err := s.selectAliasesFromRoomNIDsStmt.Query(pq.Int64Array(nIDs))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var alias, roomID string
|
var alias string
|
||||||
if err = rows.Scan(&alias, &roomID); err != nil {
|
var roomNID types.RoomNID
|
||||||
|
|
||||||
|
if err = rows.Scan(&alias, &roomNID); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(aliases[roomID]) > 0 {
|
if len(aliases[roomNID]) > 0 {
|
||||||
aliases[roomID] = append(aliases[roomID], alias)
|
aliases[roomNID] = append(aliases[roomNID], alias)
|
||||||
} else {
|
} else {
|
||||||
aliases[roomID] = []string{alias}
|
aliases[roomNID] = []string{alias}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,12 @@ const insertRoomNIDSQL = "" +
|
||||||
const selectRoomNIDSQL = "" +
|
const selectRoomNIDSQL = "" +
|
||||||
"SELECT room_nid FROM roomserver_rooms WHERE room_id = $1"
|
"SELECT room_nid FROM roomserver_rooms WHERE room_id = $1"
|
||||||
|
|
||||||
|
const selectRoomIDSQL = "" +
|
||||||
|
"SELECT room_id FROM roomserver_rooms WHERE room_nid = $1"
|
||||||
|
|
||||||
|
const selectRoomIDsSQL = "" +
|
||||||
|
"SELECT room_nid, room_id FROM roomserver_rooms WHERE room_nid = ANY($1)"
|
||||||
|
|
||||||
const selectLatestEventNIDsSQL = "" +
|
const selectLatestEventNIDsSQL = "" +
|
||||||
"SELECT latest_event_nids, state_snapshot_nid FROM roomserver_rooms WHERE room_nid = $1"
|
"SELECT latest_event_nids, state_snapshot_nid FROM roomserver_rooms WHERE room_nid = $1"
|
||||||
|
|
||||||
|
|
@ -63,7 +69,7 @@ const selectVisibilityForRoomNIDSQL = "" +
|
||||||
"SELECT visibility FROM roomserver_rooms WHERE room_nid = $1"
|
"SELECT visibility FROM roomserver_rooms WHERE room_nid = $1"
|
||||||
|
|
||||||
const selectPublicRoomIDsSQL = "" +
|
const selectPublicRoomIDsSQL = "" +
|
||||||
"SELECT room_id FROM roomserver_rooms WHERE visibility = true"
|
"SELECT room_nid FROM roomserver_rooms WHERE visibility = true"
|
||||||
|
|
||||||
const updateLatestEventNIDsSQL = "" +
|
const updateLatestEventNIDsSQL = "" +
|
||||||
"UPDATE roomserver_rooms SET latest_event_nids = $2, last_event_sent_nid = $3, state_snapshot_nid = $4 WHERE room_nid = $1"
|
"UPDATE roomserver_rooms SET latest_event_nids = $2, last_event_sent_nid = $3, state_snapshot_nid = $4 WHERE room_nid = $1"
|
||||||
|
|
@ -74,6 +80,8 @@ const updateVisibilityForRoomNIDSQL = "" +
|
||||||
type roomStatements struct {
|
type roomStatements struct {
|
||||||
insertRoomNIDStmt *sql.Stmt
|
insertRoomNIDStmt *sql.Stmt
|
||||||
selectRoomNIDStmt *sql.Stmt
|
selectRoomNIDStmt *sql.Stmt
|
||||||
|
selectRoomIDStmt *sql.Stmt
|
||||||
|
selectRoomIDsStmt *sql.Stmt
|
||||||
selectLatestEventNIDsStmt *sql.Stmt
|
selectLatestEventNIDsStmt *sql.Stmt
|
||||||
selectLatestEventNIDsForUpdateStmt *sql.Stmt
|
selectLatestEventNIDsForUpdateStmt *sql.Stmt
|
||||||
selectVisibilityForRoomNIDStmt *sql.Stmt
|
selectVisibilityForRoomNIDStmt *sql.Stmt
|
||||||
|
|
@ -90,6 +98,8 @@ func (s *roomStatements) prepare(db *sql.DB) (err error) {
|
||||||
return statementList{
|
return statementList{
|
||||||
{&s.insertRoomNIDStmt, insertRoomNIDSQL},
|
{&s.insertRoomNIDStmt, insertRoomNIDSQL},
|
||||||
{&s.selectRoomNIDStmt, selectRoomNIDSQL},
|
{&s.selectRoomNIDStmt, selectRoomNIDSQL},
|
||||||
|
{&s.selectRoomIDStmt, selectRoomIDSQL},
|
||||||
|
{&s.selectRoomIDsStmt, selectRoomIDsSQL},
|
||||||
{&s.selectLatestEventNIDsStmt, selectLatestEventNIDsSQL},
|
{&s.selectLatestEventNIDsStmt, selectLatestEventNIDsSQL},
|
||||||
{&s.selectLatestEventNIDsForUpdateStmt, selectLatestEventNIDsForUpdateSQL},
|
{&s.selectLatestEventNIDsForUpdateStmt, selectLatestEventNIDsForUpdateSQL},
|
||||||
{&s.selectVisibilityForRoomNIDStmt, selectVisibilityForRoomNIDSQL},
|
{&s.selectVisibilityForRoomNIDStmt, selectVisibilityForRoomNIDSQL},
|
||||||
|
|
@ -111,6 +121,39 @@ func (s *roomStatements) selectRoomNID(roomID string) (types.RoomNID, error) {
|
||||||
return types.RoomNID(roomNID), err
|
return types.RoomNID(roomNID), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) selectRoomID(roomNID types.RoomNID) (string, error) {
|
||||||
|
var roomID string
|
||||||
|
err := s.selectRoomIDStmt.QueryRow(roomNID).Scan(&roomID)
|
||||||
|
return roomID, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *roomStatements) selectRoomIDs(roomNIDs []types.RoomNID) (map[types.RoomNID]string, error) {
|
||||||
|
roomIDs := make(map[types.RoomNID]string)
|
||||||
|
|
||||||
|
nIDs := []int64{}
|
||||||
|
for _, roomNID := range roomNIDs {
|
||||||
|
nIDs = append(nIDs, int64(roomNID))
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.selectRoomIDsStmt.Query(pq.Int64Array(nIDs))
|
||||||
|
if err != nil {
|
||||||
|
return roomIDs, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
var roomNID types.RoomNID
|
||||||
|
var roomID string
|
||||||
|
|
||||||
|
if err := rows.Scan(&roomNID, &roomID); err != nil {
|
||||||
|
return roomIDs, err
|
||||||
|
}
|
||||||
|
|
||||||
|
roomIDs[roomNID] = roomID
|
||||||
|
}
|
||||||
|
|
||||||
|
return roomIDs, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *roomStatements) selectLatestEventNIDs(roomNID types.RoomNID) ([]types.EventNID, types.StateSnapshotNID, error) {
|
func (s *roomStatements) selectLatestEventNIDs(roomNID types.RoomNID) ([]types.EventNID, types.StateSnapshotNID, error) {
|
||||||
var nids pq.Int64Array
|
var nids pq.Int64Array
|
||||||
var stateSnapshotNID int64
|
var stateSnapshotNID int64
|
||||||
|
|
@ -148,22 +191,22 @@ func (s *roomStatements) selectVisibilityForRoomNID(roomNID types.RoomNID) (bool
|
||||||
return visibility, err
|
return visibility, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomStatements) selectPublicRoomIDs() ([]string, error) {
|
func (s *roomStatements) selectPublicRoomNIDs() ([]types.RoomNID, error) {
|
||||||
roomIDs := []string{}
|
roomNIDs := []types.RoomNID{}
|
||||||
rows, err := s.selectPublicRoomIDsStmt.Query()
|
rows, err := s.selectPublicRoomIDsStmt.Query()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return roomIDs, err
|
return roomNIDs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var roomID string
|
var roomID types.RoomNID
|
||||||
if err = rows.Scan(&roomID); err != nil {
|
if err = rows.Scan(&roomID); err != nil {
|
||||||
return roomIDs, err
|
return roomNIDs, err
|
||||||
}
|
}
|
||||||
roomIDs = append(roomIDs, roomID)
|
roomNIDs = append(roomNIDs, roomID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return roomIDs, nil
|
return roomNIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *roomStatements) updateVisibilityForRoomNID(roomNID types.RoomNID, visibility bool) error {
|
func (s *roomStatements) updateVisibilityForRoomNID(roomNID types.RoomNID, visibility bool) error {
|
||||||
|
|
|
||||||
|
|
@ -353,24 +353,29 @@ func (d *Database) LatestEventIDs(roomNID types.RoomNID) ([]gomatrixserverlib.Ev
|
||||||
return references, currentStateSnapshotNID, depth, nil
|
return references, currentStateSnapshotNID, depth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RoomID implements alias.RoomserverAliasAPIDB
|
||||||
|
func (d *Database) RoomID(roomNID types.RoomNID) (string, error) {
|
||||||
|
return d.statements.selectRoomID(roomNID)
|
||||||
|
}
|
||||||
|
|
||||||
// SetRoomAlias implements alias.RoomserverAliasAPIDB
|
// SetRoomAlias implements alias.RoomserverAliasAPIDB
|
||||||
func (d *Database) SetRoomAlias(alias string, roomID string) error {
|
func (d *Database) SetRoomAlias(alias string, roomNID types.RoomNID) error {
|
||||||
return d.statements.insertRoomAlias(alias, roomID)
|
return d.statements.insertRoomAlias(alias, roomNID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRoomIDFromAlias implements alias.RoomserverAliasAPIDB
|
// GetRoomNIDFromAlias implements alias.RoomserverAliasAPIDB
|
||||||
func (d *Database) GetRoomIDFromAlias(alias string) (string, error) {
|
func (d *Database) GetRoomNIDFromAlias(alias string) (types.RoomNID, error) {
|
||||||
return d.statements.selectRoomIDFromAlias(alias)
|
return d.statements.selectRoomNIDFromAlias(alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasesFromRoomID implements alias.RoomserverAliasAPIDB
|
// GetAliasesFromRoomNID implements alias.RoomserverAliasAPIDB
|
||||||
func (d *Database) GetAliasesFromRoomID(roomID string) ([]string, error) {
|
func (d *Database) GetAliasesFromRoomNID(roomNID types.RoomNID) ([]string, error) {
|
||||||
return d.statements.selectAliasesFromRoomID(roomID)
|
return d.statements.selectAliasesFromRoomNID(roomNID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAliasesFromRoomIDs implements publicroom.RoomserverPublicRoomAPIDB
|
// GetAliasesFromRoomNIDs implements publicroom.RoomserverPublicRoomAPIDB
|
||||||
func (d *Database) GetAliasesFromRoomIDs(roomIDs []string) (map[string][]string, error) {
|
func (d *Database) GetAliasesFromRoomNIDs(roomNIDs []types.RoomNID) (map[types.RoomNID][]string, error) {
|
||||||
return d.statements.selectAliasesFromRoomIDs(roomIDs)
|
return d.statements.selectAliasesFromRoomNIDs(roomNIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveRoomAlias implements alias.RoomserverAliasAPIDB
|
// RemoveRoomAlias implements alias.RoomserverAliasAPIDB
|
||||||
|
|
@ -385,14 +390,19 @@ func (d *Database) StateEntriesForTuples(
|
||||||
return d.statements.bulkSelectFilteredStateBlockEntries(stateBlockNIDs, stateKeyTuples)
|
return d.statements.bulkSelectFilteredStateBlockEntries(stateBlockNIDs, stateKeyTuples)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RoomIDs implements publicroom.RoomserverPublicRoomAPIDB
|
||||||
|
func (d *Database) RoomIDs(roomNIDs []types.RoomNID) (map[types.RoomNID]string, error) {
|
||||||
|
return d.statements.selectRoomIDs(roomNIDs)
|
||||||
|
}
|
||||||
|
|
||||||
// IsRoomPublic implements publicroom.RoomserverPublicRoomAPIDB
|
// IsRoomPublic implements publicroom.RoomserverPublicRoomAPIDB
|
||||||
func (d *Database) IsRoomPublic(roomNID types.RoomNID) (bool, error) {
|
func (d *Database) IsRoomPublic(roomNID types.RoomNID) (bool, error) {
|
||||||
return d.statements.selectVisibilityForRoomNID(roomNID)
|
return d.statements.selectVisibilityForRoomNID(roomNID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPublicRoomIDs implements publicroom.RoomserverPublicRoomAPIDB
|
// GetPublicRoomNIDs implements publicroom.RoomserverPublicRoomAPIDB
|
||||||
func (d *Database) GetPublicRoomIDs() ([]string, error) {
|
func (d *Database) GetPublicRoomNIDs() ([]types.RoomNID, error) {
|
||||||
return d.statements.selectPublicRoomIDs()
|
return d.statements.selectPublicRoomNIDs()
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRoomVisibility implements publicroom.RoomserverPublicRoomAPIDB
|
// UpdateRoomVisibility implements publicroom.RoomserverPublicRoomAPIDB
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue