2020-02-13 11:27:33 -06:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-08-05 04:00:35 -05:00
|
|
|
"strings"
|
2020-02-13 11:27:33 -06:00
|
|
|
|
2021-11-24 04:45:23 -06:00
|
|
|
"github.com/matrix-org/dendrite/federationapi/types"
|
2020-05-21 08:40:13 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2020-06-12 08:55:57 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-02-13 11:27:33 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
CREATE TABLE IF NOT EXISTS federationsender_joined_hosts (
|
|
|
|
-- 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
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS federatonsender_joined_hosts_event_id_idx
|
|
|
|
ON federationsender_joined_hosts (event_id);
|
|
|
|
|
|
|
|
CREATE INDEX IF NOT EXISTS federatonsender_joined_hosts_room_id_idx
|
|
|
|
ON federationsender_joined_hosts (room_id)
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertJoinedHostsSQL = "" +
|
2021-04-14 05:11:54 -05:00
|
|
|
"INSERT OR IGNORE INTO federationsender_joined_hosts (room_id, event_id, server_name)" +
|
2020-02-13 11:27:33 -06:00
|
|
|
" VALUES ($1, $2, $3)"
|
|
|
|
|
|
|
|
const deleteJoinedHostsSQL = "" +
|
|
|
|
"DELETE FROM federationsender_joined_hosts WHERE event_id = $1"
|
|
|
|
|
2020-10-22 04:39:16 -05:00
|
|
|
const deleteJoinedHostsForRoomSQL = "" +
|
|
|
|
"DELETE FROM federationsender_joined_hosts WHERE room_id = $1"
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
const selectJoinedHostsSQL = "" +
|
|
|
|
"SELECT event_id, server_name FROM federationsender_joined_hosts" +
|
|
|
|
" WHERE room_id = $1"
|
|
|
|
|
2020-07-16 07:52:08 -05:00
|
|
|
const selectAllJoinedHostsSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts"
|
|
|
|
|
2020-08-04 05:32:14 -05:00
|
|
|
const selectJoinedHostsForRoomsSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts WHERE room_id IN ($1)"
|
|
|
|
|
2022-11-15 11:21:16 -06:00
|
|
|
const selectJoinedHostsForRoomsExcludingBlacklistedSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts j WHERE room_id IN ($1) AND NOT EXISTS (" +
|
|
|
|
" SELECT server_name FROM federationsender_blacklist WHERE j.server_name = server_name" +
|
|
|
|
");"
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
type joinedHostsStatements struct {
|
2020-10-22 04:39:16 -05:00
|
|
|
db *sql.DB
|
|
|
|
insertJoinedHostsStmt *sql.Stmt
|
|
|
|
deleteJoinedHostsStmt *sql.Stmt
|
|
|
|
deleteJoinedHostsForRoomStmt *sql.Stmt
|
|
|
|
selectJoinedHostsStmt *sql.Stmt
|
|
|
|
selectAllJoinedHostsStmt *sql.Stmt
|
2020-08-05 04:00:35 -05:00
|
|
|
// selectJoinedHostsForRoomsStmt *sql.Stmt - prepared at runtime due to variadic
|
2022-11-15 11:21:16 -06:00
|
|
|
// selectJoinedHostsForRoomsExcludingBlacklistedStmt *sql.Stmt - prepared at runtime due to variadic
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func NewSQLiteJoinedHostsTable(db *sql.DB) (s *joinedHostsStatements, err error) {
|
|
|
|
s = &joinedHostsStatements{
|
2020-08-21 04:42:08 -05:00
|
|
|
db: db,
|
2020-07-20 10:55:20 -05:00
|
|
|
}
|
2020-02-13 11:27:33 -06:00
|
|
|
_, err = db.Exec(joinedHostsSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 07:52:53 -05:00
|
|
|
|
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.insertJoinedHostsStmt, insertJoinedHostsSQL},
|
|
|
|
{&s.deleteJoinedHostsStmt, deleteJoinedHostsSQL},
|
|
|
|
{&s.deleteJoinedHostsForRoomStmt, deleteJoinedHostsForRoomSQL},
|
|
|
|
{&s.selectJoinedHostsStmt, selectJoinedHostsSQL},
|
|
|
|
{&s.selectAllJoinedHostsStmt, selectAllJoinedHostsSQL},
|
|
|
|
}.Prepare(db)
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *joinedHostsStatements) InsertJoinedHosts(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomID, eventID string,
|
2023-04-19 09:50:33 -05:00
|
|
|
serverName spec.ServerName,
|
2020-02-13 11:27:33 -06:00
|
|
|
) error {
|
2020-08-21 04:42:08 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertJoinedHostsStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, roomID, eventID, serverName)
|
|
|
|
return err
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *joinedHostsStatements) DeleteJoinedHosts(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) error {
|
2020-08-21 04:42:08 -05:00
|
|
|
for _, eventID := range eventIDs {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteJoinedHostsStmt)
|
|
|
|
if _, err := stmt.ExecContext(ctx, eventID); err != nil {
|
|
|
|
return err
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
2020-08-21 04:42:08 -05:00
|
|
|
}
|
|
|
|
return nil
|
2020-02-13 11:27:33 -06:00
|
|
|
}
|
|
|
|
|
2020-10-22 04:39:16 -05: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 10:55:20 -05:00
|
|
|
func (s *joinedHostsStatements) SelectJoinedHostsWithTx(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
2020-06-12 08:55:57 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectJoinedHostsStmt)
|
2020-02-13 11:27:33 -06:00
|
|
|
return joinedHostsFromStmt(ctx, stmt, roomID)
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *joinedHostsStatements) SelectJoinedHosts(
|
2020-02-13 11:27:33 -06:00
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
|
|
|
return joinedHostsFromStmt(ctx, s.selectJoinedHostsStmt, roomID)
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *joinedHostsStatements) SelectAllJoinedHosts(
|
2020-07-16 07:52:08 -05:00
|
|
|
ctx context.Context,
|
2023-04-19 09:50:33 -05:00
|
|
|
) ([]spec.ServerName, error) {
|
2020-07-16 07:52:08 -05:00
|
|
|
rows, err := s.selectAllJoinedHostsStmt.QueryContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectAllJoinedHosts: rows.close() failed")
|
|
|
|
|
2023-04-19 09:50:33 -05:00
|
|
|
var result []spec.ServerName
|
2020-07-16 07:52:08 -05:00
|
|
|
for rows.Next() {
|
|
|
|
var serverName string
|
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-19 09:50:33 -05:00
|
|
|
result = append(result, spec.ServerName(serverName))
|
2020-07-16 07:52:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
2020-08-04 05:32:14 -05:00
|
|
|
func (s *joinedHostsStatements) SelectJoinedHostsForRooms(
|
2022-11-15 11:21:16 -06:00
|
|
|
ctx context.Context, roomIDs []string, excludingBlacklisted bool,
|
2023-04-19 09:50:33 -05:00
|
|
|
) ([]spec.ServerName, error) {
|
2020-08-04 05:32:14 -05:00
|
|
|
iRoomIDs := make([]interface{}, len(roomIDs))
|
|
|
|
for i := range roomIDs {
|
|
|
|
iRoomIDs[i] = roomIDs[i]
|
|
|
|
}
|
2022-11-15 11:21:16 -06:00
|
|
|
query := selectJoinedHostsForRoomsSQL
|
|
|
|
if excludingBlacklisted {
|
|
|
|
query = selectJoinedHostsForRoomsExcludingBlacklistedSQL
|
|
|
|
}
|
|
|
|
sql := strings.Replace(query, "($1)", sqlutil.QueryVariadic(len(iRoomIDs)), 1)
|
2020-08-05 04:00:35 -05:00
|
|
|
rows, err := s.db.QueryContext(ctx, sql, iRoomIDs...)
|
2020-08-04 05:32:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "selectJoinedHostsForRoomsStmt: rows.close() failed")
|
|
|
|
|
2023-04-19 09:50:33 -05:00
|
|
|
var result []spec.ServerName
|
2020-08-04 05:32:14 -05:00
|
|
|
for rows.Next() {
|
|
|
|
var serverName string
|
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-19 09:50:33 -05:00
|
|
|
result = append(result, spec.ServerName(serverName))
|
2020-08-04 05:32:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:27:33 -06:00
|
|
|
func joinedHostsFromStmt(
|
|
|
|
ctx context.Context, stmt *sql.Stmt, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
|
|
|
rows, err := stmt.QueryContext(ctx, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 08:40:13 -05:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "joinedHostsFromStmt: rows.close() failed")
|
2020-02-13 11:27:33 -06: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,
|
2023-04-19 09:50:33 -05:00
|
|
|
ServerName: spec.ServerName(serverName),
|
2020-02-13 11:27:33 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|