2020-07-20 17:55:20 +02:00
// Copyright 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 tables
import (
"context"
"database/sql"
2021-11-24 11:45:23 +01:00
"github.com/matrix-org/dendrite/federationapi/types"
2020-07-20 17:55:20 +02:00
"github.com/matrix-org/gomatrixserverlib"
2023-04-19 16:50:33 +02:00
"github.com/matrix-org/gomatrixserverlib/spec"
2020-07-20 17:55:20 +02:00
)
2021-07-15 18:45:37 +02:00
type NotaryID int64
2021-11-24 11:45:23 +01:00
type FederationQueuePDUs interface {
2023-04-19 16:50:33 +02:00
InsertQueuePDU ( ctx context . Context , txn * sql . Tx , transactionID gomatrixserverlib . TransactionID , serverName spec . ServerName , nid int64 ) error
DeleteQueuePDUs ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , jsonNIDs [ ] int64 ) error
2020-07-20 17:55:20 +02:00
SelectQueuePDUReferenceJSONCount ( ctx context . Context , txn * sql . Tx , jsonNID int64 ) ( int64 , error )
2023-04-19 16:50:33 +02:00
SelectQueuePDUs ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , limit int ) ( [ ] int64 , error )
SelectQueuePDUServerNames ( ctx context . Context , txn * sql . Tx ) ( [ ] spec . ServerName , error )
2020-07-20 17:55:20 +02:00
}
2021-11-24 11:45:23 +01:00
type FederationQueueEDUs interface {
2023-04-19 16:50:33 +02:00
InsertQueueEDU ( ctx context . Context , txn * sql . Tx , eduType string , serverName spec . ServerName , nid int64 , expiresAt spec . Timestamp ) error
DeleteQueueEDUs ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , jsonNIDs [ ] int64 ) error
SelectQueueEDUs ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , limit int ) ( [ ] int64 , error )
2020-07-20 17:55:20 +02:00
SelectQueueEDUReferenceJSONCount ( ctx context . Context , txn * sql . Tx , jsonNID int64 ) ( int64 , error )
2023-04-19 16:50:33 +02:00
SelectQueueEDUServerNames ( ctx context . Context , txn * sql . Tx ) ( [ ] spec . ServerName , error )
SelectExpiredEDUs ( ctx context . Context , txn * sql . Tx , expiredBefore spec . Timestamp ) ( [ ] int64 , error )
DeleteExpiredEDUs ( ctx context . Context , txn * sql . Tx , expiredBefore spec . Timestamp ) error
2022-08-09 11:15:58 +02:00
Prepare ( ) error
2020-07-20 17:55:20 +02:00
}
2021-11-24 11:45:23 +01:00
type FederationQueueJSON interface {
2020-07-20 17:55:20 +02:00
InsertQueueJSON ( ctx context . Context , txn * sql . Tx , json string ) ( int64 , error )
DeleteQueueJSON ( ctx context . Context , txn * sql . Tx , nids [ ] int64 ) error
SelectQueueJSON ( ctx context . Context , txn * sql . Tx , jsonNIDs [ ] int64 ) ( map [ int64 ] [ ] byte , error )
}
2023-01-23 18:55:12 +01:00
type FederationQueueTransactions interface {
2023-04-19 16:50:33 +02:00
InsertQueueTransaction ( ctx context . Context , txn * sql . Tx , transactionID gomatrixserverlib . TransactionID , serverName spec . ServerName , nid int64 ) error
DeleteQueueTransactions ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , jsonNIDs [ ] int64 ) error
SelectQueueTransactions ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , limit int ) ( [ ] int64 , error )
SelectQueueTransactionCount ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) ( int64 , error )
2023-01-23 18:55:12 +01:00
}
type FederationTransactionJSON interface {
InsertTransactionJSON ( ctx context . Context , txn * sql . Tx , json string ) ( int64 , error )
DeleteTransactionJSON ( ctx context . Context , txn * sql . Tx , nids [ ] int64 ) error
SelectTransactionJSON ( ctx context . Context , txn * sql . Tx , jsonNIDs [ ] int64 ) ( map [ int64 ] [ ] byte , error )
}
2021-11-24 11:45:23 +01:00
type FederationJoinedHosts interface {
2023-04-19 16:50:33 +02:00
InsertJoinedHosts ( ctx context . Context , txn * sql . Tx , roomID , eventID string , serverName spec . ServerName ) error
2020-07-20 17:55:20 +02:00
DeleteJoinedHosts ( ctx context . Context , txn * sql . Tx , eventIDs [ ] string ) error
2020-10-22 11:39:16 +02:00
DeleteJoinedHostsForRoom ( ctx context . Context , txn * sql . Tx , roomID string ) error
2020-07-20 17:55:20 +02:00
SelectJoinedHostsWithTx ( ctx context . Context , txn * sql . Tx , roomID string ) ( [ ] types . JoinedHost , error )
SelectJoinedHosts ( ctx context . Context , roomID string ) ( [ ] types . JoinedHost , error )
2023-04-19 16:50:33 +02:00
SelectAllJoinedHosts ( ctx context . Context ) ( [ ] spec . ServerName , error )
SelectJoinedHostsForRooms ( ctx context . Context , roomIDs [ ] string , excludingBlacklisted bool ) ( [ ] spec . ServerName , error )
2020-07-20 17:55:20 +02:00
}
2021-11-24 11:45:23 +01:00
type FederationBlacklist interface {
2023-04-19 16:50:33 +02:00
InsertBlacklist ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) error
SelectBlacklist ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) ( bool , error )
DeleteBlacklist ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) error
2021-05-24 12:43:24 +02:00
DeleteAllBlacklist ( ctx context . Context , txn * sql . Tx ) error
2020-07-22 18:01:29 +02:00
}
2021-01-22 15:55:08 +01:00
2023-01-23 18:55:12 +01:00
type FederationAssumedOffline interface {
2023-04-19 16:50:33 +02:00
InsertAssumedOffline ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) error
SelectAssumedOffline ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) ( bool , error )
DeleteAssumedOffline ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) error
2023-01-23 18:55:12 +01:00
DeleteAllAssumedOffline ( ctx context . Context , txn * sql . Tx ) error
}
type FederationRelayServers interface {
2023-04-19 16:50:33 +02:00
InsertRelayServers ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , relayServers [ ] spec . ServerName ) error
SelectRelayServers ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) ( [ ] spec . ServerName , error )
DeleteRelayServers ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , relayServers [ ] spec . ServerName ) error
DeleteAllRelayServers ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName ) error
2023-01-23 18:55:12 +01:00
}
2021-11-24 11:45:23 +01:00
type FederationOutboundPeeks interface {
2023-04-19 16:50:33 +02:00
InsertOutboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string , renewalInterval int64 ) ( err error )
RenewOutboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string , renewalInterval int64 ) ( err error )
SelectOutboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string ) ( outboundPeek * types . OutboundPeek , err error )
2021-01-22 15:55:08 +01:00
SelectOutboundPeeks ( ctx context . Context , txn * sql . Tx , roomID string ) ( outboundPeeks [ ] types . OutboundPeek , err error )
2023-04-19 16:50:33 +02:00
DeleteOutboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string ) ( err error )
2021-01-22 15:55:08 +01:00
DeleteOutboundPeeks ( ctx context . Context , txn * sql . Tx , roomID string ) ( err error )
}
2021-11-24 11:45:23 +01:00
type FederationInboundPeeks interface {
2023-04-19 16:50:33 +02:00
InsertInboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string , renewalInterval int64 ) ( err error )
RenewInboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string , renewalInterval int64 ) ( err error )
SelectInboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string ) ( inboundPeek * types . InboundPeek , err error )
2021-01-22 15:55:08 +01:00
SelectInboundPeeks ( ctx context . Context , txn * sql . Tx , roomID string ) ( inboundPeeks [ ] types . InboundPeek , err error )
2023-04-19 16:50:33 +02:00
DeleteInboundPeek ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , roomID , peekID string ) ( err error )
2021-01-22 15:55:08 +01:00
DeleteInboundPeeks ( ctx context . Context , txn * sql . Tx , roomID string ) ( err error )
}
2021-07-15 18:45:37 +02:00
2021-11-24 11:45:23 +01:00
// FederationNotaryServerKeysJSON contains the byte-for-byte responses from servers which contain their keys and is signed by them.
type FederationNotaryServerKeysJSON interface {
// InsertJSONResponse inserts a new response JSON. Useless on its own, needs querying via FederationNotaryServerKeysMetadata
2021-07-15 18:45:37 +02:00
// `validUntil` should be the value of `valid_until_ts` with the 7-day check applied from:
// "Servers MUST use the lesser of this field and 7 days into the future when determining if a key is valid.
// This is to avoid a situation where an attacker publishes a key which is valid for a significant amount of time
// without a way for the homeserver owner to revoke it.""
2023-04-19 16:50:33 +02:00
InsertJSONResponse ( ctx context . Context , txn * sql . Tx , keyQueryResponseJSON gomatrixserverlib . ServerKeys , serverName spec . ServerName , validUntil spec . Timestamp ) ( NotaryID , error )
2021-07-15 18:45:37 +02:00
}
2021-11-24 11:45:23 +01:00
// FederationNotaryServerKeysMetadata persists the metadata for FederationNotaryServerKeysJSON
type FederationNotaryServerKeysMetadata interface {
2021-07-15 18:45:37 +02:00
// UpsertKey updates or inserts a (server_name, key_id) tuple, pointing it via NotaryID at the the response which has the longest valid_until_ts
// `newNotaryID` and `newValidUntil` should be the notary ID / valid_until which has this (server_name, key_id) tuple already, e.g one you just inserted.
2023-04-19 16:50:33 +02:00
UpsertKey ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , keyID gomatrixserverlib . KeyID , newNotaryID NotaryID , newValidUntil spec . Timestamp ) ( NotaryID , error )
2021-07-15 18:45:37 +02:00
// SelectKeys returns the signed JSON objects which contain the given key IDs. This will be at most the length of `keyIDs` and at least 1 (assuming
// the keys exist in the first place). If `keyIDs` is empty, the signed JSON object with the longest valid_until_ts will be returned.
2023-04-19 16:50:33 +02:00
SelectKeys ( ctx context . Context , txn * sql . Tx , serverName spec . ServerName , keyIDs [ ] gomatrixserverlib . KeyID ) ( [ ] gomatrixserverlib . ServerKeys , error )
2021-11-24 11:45:23 +01:00
// DeleteOldJSONResponses removes all responses which are not referenced in FederationNotaryServerKeysMetadata
2021-07-15 18:45:37 +02:00
DeleteOldJSONResponses ( ctx context . Context , txn * sql . Tx ) error
}
2021-11-24 11:45:23 +01:00
type FederationServerSigningKeys interface {
2023-04-19 16:50:33 +02:00
BulkSelectServerKeys ( ctx context . Context , txn * sql . Tx , requests map [ gomatrixserverlib . PublicKeyLookupRequest ] spec . Timestamp ) ( map [ gomatrixserverlib . PublicKeyLookupRequest ] gomatrixserverlib . PublicKeyLookupResult , error )
2021-11-24 11:45:23 +01:00
UpsertServerKeys ( ctx context . Context , txn * sql . Tx , request gomatrixserverlib . PublicKeyLookupRequest , key gomatrixserverlib . PublicKeyLookupResult ) error
}