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-06-28 16:10:17 +01: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-06-28 16:10:17 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-18 14:15:17 +01:00
|
|
|
"context"
|
2017-06-28 16:10:17 +01:00
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/lib/pq"
|
2021-11-24 10:45:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/types"
|
2020-05-21 14:40:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 14:55:57 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2017-06-28 16:10:17 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
const joinedHostsSchema = `
|
|
|
|
-- The joined_hosts table stores a list of m.room.member event ids in the
|
|
|
|
-- current state for each room where the membership is "join".
|
|
|
|
-- There will be an entry for every user that is joined to the room.
|
2017-08-07 11:51:46 +01:00
|
|
|
CREATE TABLE IF NOT EXISTS federationsender_joined_hosts (
|
2017-06-28 16:10:17 +01:00
|
|
|
-- The string ID of the room.
|
|
|
|
room_id TEXT NOT NULL,
|
|
|
|
-- The event ID of the m.room.member join event.
|
|
|
|
event_id TEXT NOT NULL,
|
|
|
|
-- The domain part of the user ID the m.room.member event is for.
|
|
|
|
server_name TEXT NOT NULL
|
|
|
|
);
|
|
|
|
|
2017-08-07 11:51:46 +01:00
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS federatonsender_joined_hosts_event_id_idx
|
|
|
|
ON federationsender_joined_hosts (event_id);
|
2017-06-28 16:10:17 +01:00
|
|
|
|
2017-08-07 11:51:46 +01:00
|
|
|
CREATE INDEX IF NOT EXISTS federatonsender_joined_hosts_room_id_idx
|
|
|
|
ON federationsender_joined_hosts (room_id)
|
2017-06-28 16:10:17 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertJoinedHostsSQL = "" +
|
2017-08-07 11:51:46 +01:00
|
|
|
"INSERT INTO federationsender_joined_hosts (room_id, event_id, server_name)" +
|
2021-04-14 11:11:54 +01:00
|
|
|
" VALUES ($1, $2, $3) ON CONFLICT DO NOTHING"
|
2017-06-28 16:10:17 +01:00
|
|
|
|
|
|
|
const deleteJoinedHostsSQL = "" +
|
2017-08-07 11:51:46 +01:00
|
|
|
"DELETE FROM federationsender_joined_hosts WHERE event_id = ANY($1)"
|
2017-06-28 16:10:17 +01:00
|
|
|
|
2020-10-22 10:39:16 +01:00
|
|
|
const deleteJoinedHostsForRoomSQL = "" +
|
|
|
|
"DELETE FROM federationsender_joined_hosts WHERE room_id = $1"
|
|
|
|
|
2017-06-28 16:10:17 +01:00
|
|
|
const selectJoinedHostsSQL = "" +
|
2017-08-07 11:51:46 +01:00
|
|
|
"SELECT event_id, server_name FROM federationsender_joined_hosts" +
|
2017-06-28 16:10:17 +01:00
|
|
|
" WHERE room_id = $1"
|
|
|
|
|
2020-07-16 13:52:08 +01:00
|
|
|
const selectAllJoinedHostsSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts"
|
|
|
|
|
2020-08-04 11:32:14 +01:00
|
|
|
const selectJoinedHostsForRoomsSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts WHERE room_id = ANY($1)"
|
|
|
|
|
2022-11-15 17:21:16 +00:00
|
|
|
const selectJoinedHostsForRoomsExcludingBlacklistedSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts j WHERE room_id = ANY($1) AND NOT EXISTS (" +
|
|
|
|
" SELECT server_name FROM federationsender_blacklist WHERE j.server_name = server_name" +
|
|
|
|
");"
|
|
|
|
|
2017-06-28 16:10:17 +01:00
|
|
|
type joinedHostsStatements struct {
|
2022-11-15 17:21:16 +00:00
|
|
|
db *sql.DB
|
|
|
|
insertJoinedHostsStmt *sql.Stmt
|
|
|
|
deleteJoinedHostsStmt *sql.Stmt
|
|
|
|
deleteJoinedHostsForRoomStmt *sql.Stmt
|
|
|
|
selectJoinedHostsStmt *sql.Stmt
|
|
|
|
selectAllJoinedHostsStmt *sql.Stmt
|
|
|
|
selectJoinedHostsForRoomsStmt *sql.Stmt
|
|
|
|
selectJoinedHostsForRoomsExcludingBlacklistedStmt *sql.Stmt
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:55:20 +01:00
|
|
|
func NewPostgresJoinedHostsTable(db *sql.DB) (s *joinedHostsStatements, err error) {
|
|
|
|
s = &joinedHostsStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
_, err = s.db.Exec(joinedHostsSchema)
|
2017-06-28 16:10:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 12:52:53 +00:00
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.insertJoinedHostsStmt, insertJoinedHostsSQL},
|
|
|
|
{&s.deleteJoinedHostsStmt, deleteJoinedHostsSQL},
|
|
|
|
{&s.deleteJoinedHostsForRoomStmt, deleteJoinedHostsForRoomSQL},
|
|
|
|
{&s.selectJoinedHostsStmt, selectJoinedHostsSQL},
|
|
|
|
{&s.selectAllJoinedHostsStmt, selectAllJoinedHostsSQL},
|
|
|
|
{&s.selectJoinedHostsForRoomsStmt, selectJoinedHostsForRoomsSQL},
|
|
|
|
{&s.selectJoinedHostsForRoomsExcludingBlacklistedStmt, selectJoinedHostsForRoomsExcludingBlacklistedSQL},
|
|
|
|
}.Prepare(db)
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:55:20 +01:00
|
|
|
func (s *joinedHostsStatements) InsertJoinedHosts(
|
2017-09-18 14:15:17 +01:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomID, eventID string,
|
|
|
|
serverName gomatrixserverlib.ServerName,
|
2017-06-28 16:10:17 +01:00
|
|
|
) error {
|
2020-06-12 14:55:57 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertJoinedHostsStmt)
|
2017-09-18 14:15:17 +01:00
|
|
|
_, err := stmt.ExecContext(ctx, roomID, eventID, serverName)
|
2017-06-28 16:10:17 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:55:20 +01:00
|
|
|
func (s *joinedHostsStatements) DeleteJoinedHosts(
|
2017-09-18 14:15:17 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) error {
|
2020-06-12 14:55:57 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteJoinedHostsStmt)
|
2017-09-18 14:15:17 +01:00
|
|
|
_, err := stmt.ExecContext(ctx, pq.StringArray(eventIDs))
|
2017-06-28 16:10:17 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:39:16 +01:00
|
|
|
func (s *joinedHostsStatements) DeleteJoinedHostsForRoom(
|
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
|
|
) error {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteJoinedHostsForRoomStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, roomID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:55:20 +01:00
|
|
|
func (s *joinedHostsStatements) SelectJoinedHostsWithTx(
|
2017-09-18 14:15:17 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
2017-06-28 16:10:17 +01:00
|
|
|
) ([]types.JoinedHost, error) {
|
2020-06-12 14:55:57 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectJoinedHostsStmt)
|
2018-08-10 16:26:57 +01:00
|
|
|
return joinedHostsFromStmt(ctx, stmt, roomID)
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:55:20 +01:00
|
|
|
func (s *joinedHostsStatements) SelectJoinedHosts(
|
2018-08-10 16:26:57 +01:00
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
|
|
|
return joinedHostsFromStmt(ctx, s.selectJoinedHostsStmt, roomID)
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:55:20 +01:00
|
|
|
func (s *joinedHostsStatements) SelectAllJoinedHosts(
|
2020-07-16 13:52:08 +01:00
|
|
|
ctx context.Context,
|
|
|
|
) ([]gomatrixserverlib.ServerName, error) {
|
|
|
|
rows, err := s.selectAllJoinedHostsStmt.QueryContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAllJoinedHosts: rows.close() failed")
|
|
|
|
|
|
|
|
var result []gomatrixserverlib.ServerName
|
|
|
|
for rows.Next() {
|
|
|
|
var serverName string
|
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, gomatrixserverlib.ServerName(serverName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:32:14 +01:00
|
|
|
func (s *joinedHostsStatements) SelectJoinedHostsForRooms(
|
2022-11-15 17:21:16 +00:00
|
|
|
ctx context.Context, roomIDs []string, excludingBlacklisted bool,
|
2020-08-04 11:32:14 +01:00
|
|
|
) ([]gomatrixserverlib.ServerName, error) {
|
2022-11-15 17:21:16 +00:00
|
|
|
stmt := s.selectJoinedHostsForRoomsStmt
|
|
|
|
if excludingBlacklisted {
|
|
|
|
stmt = s.selectJoinedHostsForRoomsExcludingBlacklistedStmt
|
|
|
|
}
|
|
|
|
rows, err := stmt.QueryContext(ctx, pq.StringArray(roomIDs))
|
2020-08-04 11:32:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectJoinedHostsForRoomsStmt: rows.close() failed")
|
|
|
|
|
|
|
|
var result []gomatrixserverlib.ServerName
|
|
|
|
for rows.Next() {
|
|
|
|
var serverName string
|
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, gomatrixserverlib.ServerName(serverName))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:26:57 +01:00
|
|
|
func joinedHostsFromStmt(
|
|
|
|
ctx context.Context, stmt *sql.Stmt, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
2017-09-18 14:15:17 +01:00
|
|
|
rows, err := stmt.QueryContext(ctx, roomID)
|
2017-06-28 16:10:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 14:40:13 +01:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "joinedHostsFromStmt: rows.close() failed")
|
2017-06-28 16:10:17 +01:00
|
|
|
|
|
|
|
var result []types.JoinedHost
|
|
|
|
for rows.Next() {
|
|
|
|
var eventID, serverName string
|
|
|
|
if err = rows.Scan(&eventID, &serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, types.JoinedHost{
|
|
|
|
MemberEventID: eventID,
|
|
|
|
ServerName: gomatrixserverlib.ServerName(serverName),
|
|
|
|
})
|
|
|
|
}
|
2018-08-10 16:26:57 +01:00
|
|
|
|
2020-02-11 14:12:21 +00:00
|
|
|
return result, rows.Err()
|
2017-06-28 16:10:17 +01:00
|
|
|
}
|