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 15:10:17 +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-06-28 15:10:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-18 13:15:17 +00:00
|
|
|
"context"
|
2017-06-28 15:10:17 +00:00
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/lib/pq"
|
2017-08-21 16:20:23 +00:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-06-28 15:10:17 +00:00
|
|
|
"github.com/matrix-org/dendrite/federationsender/types"
|
|
|
|
"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 10:51:46 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS federationsender_joined_hosts (
|
2017-06-28 15:10:17 +00: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 10:51:46 +00:00
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS federatonsender_joined_hosts_event_id_idx
|
|
|
|
ON federationsender_joined_hosts (event_id);
|
2017-06-28 15:10:17 +00:00
|
|
|
|
2017-08-07 10:51:46 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS federatonsender_joined_hosts_room_id_idx
|
|
|
|
ON federationsender_joined_hosts (room_id)
|
2017-06-28 15:10:17 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertJoinedHostsSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"INSERT INTO federationsender_joined_hosts (room_id, event_id, server_name)" +
|
2017-06-28 15:10:17 +00:00
|
|
|
" VALUES ($1, $2, $3)"
|
|
|
|
|
|
|
|
const deleteJoinedHostsSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"DELETE FROM federationsender_joined_hosts WHERE event_id = ANY($1)"
|
2017-06-28 15:10:17 +00:00
|
|
|
|
|
|
|
const selectJoinedHostsSQL = "" +
|
2017-08-07 10:51:46 +00:00
|
|
|
"SELECT event_id, server_name FROM federationsender_joined_hosts" +
|
2017-06-28 15:10:17 +00:00
|
|
|
" WHERE room_id = $1"
|
|
|
|
|
|
|
|
type joinedHostsStatements struct {
|
|
|
|
insertJoinedHostsStmt *sql.Stmt
|
|
|
|
deleteJoinedHostsStmt *sql.Stmt
|
|
|
|
selectJoinedHostsStmt *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *joinedHostsStatements) prepare(db *sql.DB) (err error) {
|
|
|
|
_, err = db.Exec(joinedHostsSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.insertJoinedHostsStmt, err = db.Prepare(insertJoinedHostsSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.deleteJoinedHostsStmt, err = db.Prepare(deleteJoinedHostsSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s.selectJoinedHostsStmt, err = db.Prepare(selectJoinedHostsSQL); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *joinedHostsStatements) insertJoinedHosts(
|
2017-09-18 13:15:17 +00:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
roomID, eventID string,
|
|
|
|
serverName gomatrixserverlib.ServerName,
|
2017-06-28 15:10:17 +00:00
|
|
|
) error {
|
2017-09-18 13:15:17 +00:00
|
|
|
stmt := common.TxStmt(txn, s.insertJoinedHostsStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, roomID, eventID, serverName)
|
2017-06-28 15:10:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-18 13:15:17 +00:00
|
|
|
func (s *joinedHostsStatements) deleteJoinedHosts(
|
|
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
|
|
) error {
|
|
|
|
stmt := common.TxStmt(txn, s.deleteJoinedHostsStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, pq.StringArray(eventIDs))
|
2017-06-28 15:10:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:26:57 +00:00
|
|
|
func (s *joinedHostsStatements) selectJoinedHostsWithTx(
|
2017-09-18 13:15:17 +00:00
|
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
2017-06-28 15:10:17 +00:00
|
|
|
) ([]types.JoinedHost, error) {
|
2017-09-18 13:15:17 +00:00
|
|
|
stmt := common.TxStmt(txn, s.selectJoinedHostsStmt)
|
2018-08-10 15:26:57 +00:00
|
|
|
return joinedHostsFromStmt(ctx, stmt, roomID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *joinedHostsStatements) selectJoinedHosts(
|
|
|
|
ctx context.Context, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
|
|
|
return joinedHostsFromStmt(ctx, s.selectJoinedHostsStmt, roomID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinedHostsFromStmt(
|
|
|
|
ctx context.Context, stmt *sql.Stmt, roomID string,
|
|
|
|
) ([]types.JoinedHost, error) {
|
2017-09-18 13:15:17 +00:00
|
|
|
rows, err := stmt.QueryContext(ctx, roomID)
|
2017-06-28 15:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-20 09:59:19 +00:00
|
|
|
defer rows.Close() // nolint: errcheck
|
2017-06-28 15:10:17 +00: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 15:26:57 +00:00
|
|
|
|
2017-06-28 15:10:17 +00:00
|
|
|
return result, nil
|
|
|
|
}
|