2020-01-03 14:07:05 +00:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-07-28 10:31:43 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-01-03 14:07:05 +00:00
|
|
|
package postgres
|
2017-07-28 10:31:43 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 15:30:19 +00:00
|
|
|
"context"
|
2017-07-28 10:31:43 +00:00
|
|
|
"database/sql"
|
2020-03-18 10:17:18 +00:00
|
|
|
|
2020-05-21 13:40:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-08-20 15:24:33 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-27 10:03:47 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
2020-05-27 08:36:09 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-07-28 10:31:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const roomAliasesSchema = `
|
|
|
|
-- Stores room aliases and room IDs they refer to
|
2017-08-07 10:51:46 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_room_aliases (
|
2017-07-28 10:31:43 +00:00
|
|
|
-- Alias of the room
|
|
|
|
alias TEXT NOT NULL PRIMARY KEY,
|
|
|
|
-- Room ID the alias refers to
|
2019-08-07 03:00:58 +00:00
|
|
|
room_id TEXT NOT NULL,
|
|
|
|
-- User ID of the creator of this alias
|
|
|
|
creator_id TEXT NOT NULL
|
2017-07-28 10:31:43 +00:00
|
|
|
);
|
|
|
|
|
2017-08-22 10:12:51 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS roomserver_room_id_idx ON roomserver_room_aliases(room_id);
|
2017-07-28 10:31:43 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertRoomAliasSQL = "" +
|
2019-08-07 03:00:58 +00:00
|
|
|
"INSERT INTO roomserver_room_aliases (alias, room_id, creator_id) VALUES ($1, $2, $3)"
|
2017-07-28 10:31:43 +00:00
|
|
|
|
|
|
|
const selectRoomIDFromAliasSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"SELECT room_id FROM roomserver_room_aliases WHERE alias = $1"
|
2017-07-28 10:31:43 +00:00
|
|
|
|
|
|
|
const selectAliasesFromRoomIDSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"SELECT alias FROM roomserver_room_aliases WHERE room_id = $1"
|
2017-07-28 10:31:43 +00:00
|
|
|
|
2019-08-07 03:00:58 +00:00
|
|
|
const selectCreatorIDFromAliasSQL = "" +
|
|
|
|
"SELECT creator_id FROM roomserver_room_aliases WHERE alias = $1"
|
|
|
|
|
2017-07-28 10:31:43 +00:00
|
|
|
const deleteRoomAliasSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"DELETE FROM roomserver_room_aliases WHERE alias = $1"
|
2017-07-28 10:31:43 +00:00
|
|
|
|
|
|
|
type roomAliasesStatements struct {
|
2019-08-07 03:00:58 +00:00
|
|
|
insertRoomAliasStmt *sql.Stmt
|
|
|
|
selectRoomIDFromAliasStmt *sql.Stmt
|
|
|
|
selectAliasesFromRoomIDStmt *sql.Stmt
|
|
|
|
selectCreatorIDFromAliasStmt *sql.Stmt
|
|
|
|
deleteRoomAliasStmt *sql.Stmt
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 12:25:57 +00:00
|
|
|
func createRoomAliasesTable(db *sql.DB) error {
|
2020-05-27 08:36:09 +00:00
|
|
|
_, err := db.Exec(roomAliasesSchema)
|
2021-04-26 12:25:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareRoomAliasesTable(db *sql.DB) (tables.RoomAliases, error) {
|
|
|
|
s := &roomAliasesStatements{}
|
|
|
|
|
2020-05-27 10:03:47 +00:00
|
|
|
return s, shared.StatementList{
|
2017-07-28 10:31:43 +00:00
|
|
|
{&s.insertRoomAliasStmt, insertRoomAliasSQL},
|
|
|
|
{&s.selectRoomIDFromAliasStmt, selectRoomIDFromAliasSQL},
|
|
|
|
{&s.selectAliasesFromRoomIDStmt, selectAliasesFromRoomIDSQL},
|
2019-08-07 03:00:58 +00:00
|
|
|
{&s.selectCreatorIDFromAliasStmt, selectCreatorIDFromAliasSQL},
|
2017-07-28 10:31:43 +00:00
|
|
|
{&s.deleteRoomAliasStmt, deleteRoomAliasSQL},
|
2020-05-27 10:03:47 +00:00
|
|
|
}.Prepare(db)
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *roomAliasesStatements) InsertRoomAlias(
|
2020-08-20 15:24:33 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, alias string, roomID string, creatorUserID string,
|
2017-09-13 15:30:19 +00:00
|
|
|
) (err error) {
|
2020-08-20 15:24:33 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertRoomAliasStmt)
|
|
|
|
_, err = stmt.ExecContext(ctx, alias, roomID, creatorUserID)
|
2017-07-28 10:31:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *roomAliasesStatements) SelectRoomIDFromAlias(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context, alias string,
|
|
|
|
) (roomID string, err error) {
|
|
|
|
err = s.selectRoomIDFromAliasStmt.QueryRowContext(ctx, alias).Scan(&roomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *roomAliasesStatements) SelectAliasesFromRoomID(
|
2017-09-13 15:30:19 +00:00
|
|
|
ctx context.Context, roomID string,
|
2020-02-11 14:12:21 +00:00
|
|
|
) ([]string, error) {
|
2017-09-13 15:30:19 +00:00
|
|
|
rows, err := s.selectAliasesFromRoomIDStmt.QueryContext(ctx, roomID)
|
2017-07-28 10:31:43 +00:00
|
|
|
if err != nil {
|
2020-02-11 14:12:21 +00:00
|
|
|
return nil, err
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
2020-05-21 13:40:13 +00:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAliasesFromRoomID: rows.close() failed")
|
2017-07-28 10:31:43 +00:00
|
|
|
|
2020-02-11 14:12:21 +00:00
|
|
|
var aliases []string
|
2017-07-28 10:31:43 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var alias string
|
|
|
|
if err = rows.Scan(&alias); err != nil {
|
2020-02-11 14:12:21 +00:00
|
|
|
return nil, err
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aliases = append(aliases, alias)
|
|
|
|
}
|
2020-02-11 14:12:21 +00:00
|
|
|
return aliases, rows.Err()
|
2017-07-28 10:31:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *roomAliasesStatements) SelectCreatorIDFromAlias(
|
2019-08-07 03:00:58 +00:00
|
|
|
ctx context.Context, alias string,
|
|
|
|
) (creatorID string, err error) {
|
|
|
|
err = s.selectCreatorIDFromAliasStmt.QueryRowContext(ctx, alias).Scan(&creatorID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 08:36:09 +00:00
|
|
|
func (s *roomAliasesStatements) DeleteRoomAlias(
|
2020-08-20 15:24:33 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, alias string,
|
2017-09-13 15:30:19 +00:00
|
|
|
) (err error) {
|
2020-08-20 15:24:33 +00:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteRoomAliasStmt)
|
|
|
|
_, err = stmt.ExecContext(ctx, alias)
|
2017-07-28 10:31:43 +00:00
|
|
|
return
|
|
|
|
}
|