mirror of
https://github.com/matrix-org/dendrite
synced 2024-11-09 11:21:08 +01:00
e5208c2ec9
Squashed commit of the following:
commit 86c2388e13ffdbabdd50cea205652dccc40e1860
Merge: b0a3ee6c f5e7e751
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Thu Jul 16 13:47:10 2020 +0100
Merge branch 'master' into neilalexander/yggbarequic
commit b0a3ee6c5c063962384bb91c59ec753ddc8cfe5f
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Thu Jul 16 13:42:22 2020 +0100
Add support for broadcasting wake-up EDUs to known hosts
commit 8a5c2020b3a4b705b5d5686a9e71990a49e6d471
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Thu Jul 16 13:42:10 2020 +0100
Bare QUIC demo working
commit d3939b3d6568cf4262c0391486a5203873b68bfc
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Wed Jul 15 11:42:43 2020 +0100
Support bare Yggdrasil sessions with encrypted QUIC
168 lines
5 KiB
Go
168 lines
5 KiB
Go
// 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"
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/types"
|
|
"github.com/matrix-org/dendrite/internal"
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
"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.
|
|
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 = "" +
|
|
"INSERT INTO federationsender_joined_hosts (room_id, event_id, server_name)" +
|
|
" VALUES ($1, $2, $3)"
|
|
|
|
const deleteJoinedHostsSQL = "" +
|
|
"DELETE FROM federationsender_joined_hosts WHERE event_id = $1"
|
|
|
|
const selectJoinedHostsSQL = "" +
|
|
"SELECT event_id, server_name FROM federationsender_joined_hosts" +
|
|
" WHERE room_id = $1"
|
|
|
|
const selectAllJoinedHostsSQL = "" +
|
|
"SELECT DISTINCT server_name FROM federationsender_joined_hosts"
|
|
|
|
type joinedHostsStatements struct {
|
|
insertJoinedHostsStmt *sql.Stmt
|
|
deleteJoinedHostsStmt *sql.Stmt
|
|
selectJoinedHostsStmt *sql.Stmt
|
|
selectAllJoinedHostsStmt *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
|
|
}
|
|
if s.selectAllJoinedHostsStmt, err = db.Prepare(selectAllJoinedHostsSQL); err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *joinedHostsStatements) insertJoinedHosts(
|
|
ctx context.Context,
|
|
txn *sql.Tx,
|
|
roomID, eventID string,
|
|
serverName gomatrixserverlib.ServerName,
|
|
) error {
|
|
stmt := sqlutil.TxStmt(txn, s.insertJoinedHostsStmt)
|
|
_, err := stmt.ExecContext(ctx, roomID, eventID, serverName)
|
|
return err
|
|
}
|
|
|
|
func (s *joinedHostsStatements) deleteJoinedHosts(
|
|
ctx context.Context, txn *sql.Tx, eventIDs []string,
|
|
) error {
|
|
for _, eventID := range eventIDs {
|
|
stmt := sqlutil.TxStmt(txn, s.deleteJoinedHostsStmt)
|
|
if _, err := stmt.ExecContext(ctx, eventID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *joinedHostsStatements) selectJoinedHostsWithTx(
|
|
ctx context.Context, txn *sql.Tx, roomID string,
|
|
) ([]types.JoinedHost, error) {
|
|
stmt := sqlutil.TxStmt(txn, s.selectJoinedHostsStmt)
|
|
return joinedHostsFromStmt(ctx, stmt, roomID)
|
|
}
|
|
|
|
func (s *joinedHostsStatements) selectJoinedHosts(
|
|
ctx context.Context, roomID string,
|
|
) ([]types.JoinedHost, error) {
|
|
return joinedHostsFromStmt(ctx, s.selectJoinedHostsStmt, roomID)
|
|
}
|
|
|
|
func (s *joinedHostsStatements) selectAllJoinedHosts(
|
|
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()
|
|
}
|
|
|
|
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
|
|
}
|
|
defer internal.CloseAndLogIfError(ctx, rows, "joinedHostsFromStmt: rows.close() failed")
|
|
|
|
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),
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|