Add QueryServerJoinedToRoom to federation sender API

This commit is contained in:
Neil Alexander 2021-11-04 12:15:48 +00:00
parent b9a575919a
commit 73c0c91bbd
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
9 changed files with 108 additions and 0 deletions

View file

@ -66,6 +66,9 @@ const selectAllJoinedHostsSQL = "" +
const selectJoinedHostsForRoomsSQL = "" +
"SELECT DISTINCT server_name FROM federationsender_joined_hosts WHERE room_id = ANY($1)"
const selectServerJoinedToRoomSQL = "" +
"SELECT COUNT(*) FROM federation_sender_joined_hosts WHERE server_name = $1 AND room_id = $2"
type joinedHostsStatements struct {
db *sql.DB
insertJoinedHostsStmt *sql.Stmt
@ -74,6 +77,7 @@ type joinedHostsStatements struct {
selectJoinedHostsStmt *sql.Stmt
selectAllJoinedHostsStmt *sql.Stmt
selectJoinedHostsForRoomsStmt *sql.Stmt
selectServerJoinedToRoomStmt *sql.Stmt
}
func NewPostgresJoinedHostsTable(db *sql.DB) (s *joinedHostsStatements, err error) {
@ -102,6 +106,9 @@ func NewPostgresJoinedHostsTable(db *sql.DB) (s *joinedHostsStatements, err erro
if s.selectJoinedHostsForRoomsStmt, err = s.db.Prepare(selectJoinedHostsForRoomsSQL); err != nil {
return
}
if s.selectServerJoinedToRoomStmt, err = s.db.Prepare(selectServerJoinedToRoomSQL); err != nil {
return
}
return
}
@ -145,6 +152,20 @@ func (s *joinedHostsStatements) SelectJoinedHosts(
return joinedHostsFromStmt(ctx, s.selectJoinedHostsStmt, roomID)
}
func (s *joinedHostsStatements) SelectServerJoinedToRoom(
ctx context.Context, serverName gomatrixserverlib.ServerName, roomID string,
) (bool, error) {
row := s.selectServerJoinedToRoomStmt.QueryRowContext(ctx, serverName, roomID)
if err := row.Err(); err != nil {
return false, err
}
var count int
if err := row.Scan(&count); err != nil {
return false, err
}
return count > 0, nil
}
func (s *joinedHostsStatements) SelectAllJoinedHosts(
ctx context.Context,
) ([]gomatrixserverlib.ServerName, error) {