refactor: update GMSL (#3058)

Sister PR to https://github.com/matrix-org/gomatrixserverlib/pull/364

Read this commit by commit to avoid going insane.
This commit is contained in:
kegsay 2023-04-19 15:50:33 +01:00 committed by GitHub
parent 9fa39263c0
commit 72285b2659
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
306 changed files with 2117 additions and 1934 deletions

View file

@ -26,11 +26,12 @@ import (
"github.com/matrix-org/dendrite/internal/caching"
"github.com/matrix-org/dendrite/internal/sqlutil"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
)
type Database struct {
DB *sql.DB
IsLocalServerName func(gomatrixserverlib.ServerName) bool
IsLocalServerName func(spec.ServerName) bool
Cache caching.FederationCache
Writer sqlutil.Writer
FederationQueuePDUs tables.FederationQueuePDUs
@ -102,7 +103,7 @@ func (d *Database) GetJoinedHosts(
// Returns an error if something goes wrong.
func (d *Database) GetAllJoinedHosts(
ctx context.Context,
) ([]gomatrixserverlib.ServerName, error) {
) ([]spec.ServerName, error) {
return d.FederationJoinedHosts.SelectAllJoinedHosts(ctx)
}
@ -111,7 +112,7 @@ func (d *Database) GetJoinedHostsForRooms(
roomIDs []string,
excludeSelf,
excludeBlacklisted bool,
) ([]gomatrixserverlib.ServerName, error) {
) ([]spec.ServerName, error) {
servers, err := d.FederationJoinedHosts.SelectJoinedHostsForRooms(ctx, roomIDs, excludeBlacklisted)
if err != nil {
return nil, err
@ -148,7 +149,7 @@ func (d *Database) StoreJSON(
}
func (d *Database) AddServerToBlacklist(
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationBlacklist.InsertBlacklist(context.TODO(), txn, serverName)
@ -156,7 +157,7 @@ func (d *Database) AddServerToBlacklist(
}
func (d *Database) RemoveServerFromBlacklist(
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationBlacklist.DeleteBlacklist(context.TODO(), txn, serverName)
@ -170,14 +171,14 @@ func (d *Database) RemoveAllServersFromBlacklist() error {
}
func (d *Database) IsServerBlacklisted(
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) (bool, error) {
return d.FederationBlacklist.SelectBlacklist(context.TODO(), nil, serverName)
}
func (d *Database) SetServerAssumedOffline(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationAssumedOffline.InsertAssumedOffline(ctx, txn, serverName)
@ -186,7 +187,7 @@ func (d *Database) SetServerAssumedOffline(
func (d *Database) RemoveServerAssumedOffline(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationAssumedOffline.DeleteAssumedOffline(ctx, txn, serverName)
@ -203,15 +204,15 @@ func (d *Database) RemoveAllServersAssumedOffline(
func (d *Database) IsServerAssumedOffline(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) (bool, error) {
return d.FederationAssumedOffline.SelectAssumedOffline(ctx, nil, serverName)
}
func (d *Database) P2PAddRelayServersForServer(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
relayServers []gomatrixserverlib.ServerName,
serverName spec.ServerName,
relayServers []spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationRelayServers.InsertRelayServers(ctx, txn, serverName, relayServers)
@ -220,15 +221,15 @@ func (d *Database) P2PAddRelayServersForServer(
func (d *Database) P2PGetRelayServersForServer(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
) ([]gomatrixserverlib.ServerName, error) {
serverName spec.ServerName,
) ([]spec.ServerName, error) {
return d.FederationRelayServers.SelectRelayServers(ctx, nil, serverName)
}
func (d *Database) P2PRemoveRelayServersForServer(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
relayServers []gomatrixserverlib.ServerName,
serverName spec.ServerName,
relayServers []spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationRelayServers.DeleteRelayServers(ctx, txn, serverName, relayServers)
@ -237,7 +238,7 @@ func (d *Database) P2PRemoveRelayServersForServer(
func (d *Database) P2PRemoveAllRelayServersForServer(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
return d.FederationRelayServers.DeleteAllRelayServers(ctx, txn, serverName)
@ -246,7 +247,7 @@ func (d *Database) P2PRemoveAllRelayServersForServer(
func (d *Database) AddOutboundPeek(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
roomID string,
peekID string,
renewalInterval int64,
@ -258,7 +259,7 @@ func (d *Database) AddOutboundPeek(
func (d *Database) RenewOutboundPeek(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
roomID string,
peekID string,
renewalInterval int64,
@ -270,7 +271,7 @@ func (d *Database) RenewOutboundPeek(
func (d *Database) GetOutboundPeek(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
roomID,
peekID string,
) (*types.OutboundPeek, error) {
@ -286,7 +287,7 @@ func (d *Database) GetOutboundPeeks(
func (d *Database) AddInboundPeek(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
roomID string,
peekID string,
renewalInterval int64,
@ -298,7 +299,7 @@ func (d *Database) AddInboundPeek(
func (d *Database) RenewInboundPeek(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
roomID string,
peekID string,
renewalInterval int64,
@ -310,7 +311,7 @@ func (d *Database) RenewInboundPeek(
func (d *Database) GetInboundPeek(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
roomID string,
peekID string,
) (*types.InboundPeek, error) {
@ -326,7 +327,7 @@ func (d *Database) GetInboundPeeks(
func (d *Database) UpdateNotaryKeys(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
serverKeys gomatrixserverlib.ServerKeys,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
@ -337,7 +338,7 @@ func (d *Database) UpdateNotaryKeys(
// https://spec.matrix.org/unstable/server-server-api/#querying-keys-through-another-server
weekIntoFuture := time.Now().Add(7 * 24 * time.Hour)
if weekIntoFuture.Before(validUntil.Time()) {
validUntil = gomatrixserverlib.AsTimestamp(weekIntoFuture)
validUntil = spec.AsTimestamp(weekIntoFuture)
}
notaryID, err := d.NotaryServerKeysJSON.InsertJSONResponse(ctx, txn, serverKeys, serverName, validUntil)
if err != nil {
@ -364,7 +365,7 @@ func (d *Database) UpdateNotaryKeys(
func (d *Database) GetNotaryKeys(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
optKeyIDs []gomatrixserverlib.KeyID,
) (sks []gomatrixserverlib.ServerKeys, err error) {
err = d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {

View file

@ -24,6 +24,7 @@ import (
"github.com/matrix-org/dendrite/federationapi/storage/shared/receipt"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
)
// defaultExpiry for EDUs if not listed below
@ -32,8 +33,8 @@ var defaultExpiry = time.Hour * 24
// defaultExpireEDUTypes contains EDUs which can/should be expired after a given time
// if the target server isn't reachable for some reason.
var defaultExpireEDUTypes = map[string]time.Duration{
gomatrixserverlib.MTyping: time.Minute,
gomatrixserverlib.MPresence: time.Minute * 10,
spec.MTyping: time.Minute,
spec.MPresence: time.Minute * 10,
}
// AssociateEDUWithDestination creates an association that the
@ -41,7 +42,7 @@ var defaultExpireEDUTypes = map[string]time.Duration{
// to which servers.
func (d *Database) AssociateEDUWithDestinations(
ctx context.Context,
destinations map[gomatrixserverlib.ServerName]struct{},
destinations map[spec.ServerName]struct{},
dbReceipt *receipt.Receipt,
eduType string,
expireEDUTypes map[string]time.Duration,
@ -49,14 +50,14 @@ func (d *Database) AssociateEDUWithDestinations(
if expireEDUTypes == nil {
expireEDUTypes = defaultExpireEDUTypes
}
expiresAt := gomatrixserverlib.AsTimestamp(time.Now().Add(defaultExpiry))
expiresAt := spec.AsTimestamp(time.Now().Add(defaultExpiry))
if duration, ok := expireEDUTypes[eduType]; ok {
// Keep EDUs for at least x minutes before deleting them
expiresAt = gomatrixserverlib.AsTimestamp(time.Now().Add(duration))
expiresAt = spec.AsTimestamp(time.Now().Add(duration))
}
// We forcibly set m.direct_to_device and m.device_list_update events
// to 0, as we always want them to be delivered. (required for E2EE)
if eduType == gomatrixserverlib.MDirectToDevice || eduType == gomatrixserverlib.MDeviceListUpdate {
if eduType == spec.MDirectToDevice || eduType == spec.MDeviceListUpdate {
expiresAt = 0
}
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
@ -79,7 +80,7 @@ func (d *Database) AssociateEDUWithDestinations(
// the next pending transaction, up to the limit specified.
func (d *Database) GetPendingEDUs(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
limit int,
) (
edus map[*receipt.Receipt]*gomatrixserverlib.EDU,
@ -126,7 +127,7 @@ func (d *Database) GetPendingEDUs(
// transaction was sent successfully.
func (d *Database) CleanEDUs(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
receipts []*receipt.Receipt,
) error {
if len(receipts) == 0 {
@ -169,7 +170,7 @@ func (d *Database) CleanEDUs(
// waiting to be sent.
func (d *Database) GetPendingEDUServerNames(
ctx context.Context,
) ([]gomatrixserverlib.ServerName, error) {
) ([]spec.ServerName, error) {
return d.FederationQueueEDUs.SelectQueueEDUServerNames(ctx, nil)
}
@ -177,7 +178,7 @@ func (d *Database) GetPendingEDUServerNames(
func (d *Database) DeleteExpiredEDUs(ctx context.Context) error {
var jsonNIDs []int64
err := d.Writer.Do(d.DB, nil, func(txn *sql.Tx) (err error) {
expiredBefore := gomatrixserverlib.AsTimestamp(time.Now())
expiredBefore := spec.AsTimestamp(time.Now())
jsonNIDs, err = d.FederationQueueEDUs.SelectExpiredEDUs(ctx, txn, expiredBefore)
if err != nil {
return err

View file

@ -20,6 +20,7 @@ import (
"database/sql"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
)
// FetcherName implements KeyFetcher
@ -30,7 +31,7 @@ func (d Database) FetcherName() string {
// FetchKeys implements gomatrixserverlib.KeyDatabase
func (d *Database) FetchKeys(
ctx context.Context,
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
requests map[gomatrixserverlib.PublicKeyLookupRequest]spec.Timestamp,
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
return d.ServerSigningKeys.BulkSelectServerKeys(ctx, nil, requests)
}

View file

@ -23,6 +23,7 @@ import (
"github.com/matrix-org/dendrite/federationapi/storage/shared/receipt"
"github.com/matrix-org/gomatrixserverlib"
"github.com/matrix-org/gomatrixserverlib/spec"
)
// AssociatePDUWithDestination creates an association that the
@ -30,7 +31,7 @@ import (
// to which servers.
func (d *Database) AssociatePDUWithDestinations(
ctx context.Context,
destinations map[gomatrixserverlib.ServerName]struct{},
destinations map[spec.ServerName]struct{},
dbReceipt *receipt.Receipt,
) error {
return d.Writer.Do(d.DB, nil, func(txn *sql.Tx) error {
@ -52,7 +53,7 @@ func (d *Database) AssociatePDUWithDestinations(
// the next pending transaction, up to the limit specified.
func (d *Database) GetPendingPDUs(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
limit int,
) (
events map[*receipt.Receipt]*gomatrixserverlib.HeaderedEvent,
@ -105,7 +106,7 @@ func (d *Database) GetPendingPDUs(
// successfully.
func (d *Database) CleanPDUs(
ctx context.Context,
serverName gomatrixserverlib.ServerName,
serverName spec.ServerName,
receipts []*receipt.Receipt,
) error {
if len(receipts) == 0 {
@ -148,6 +149,6 @@ func (d *Database) CleanPDUs(
// waiting to be sent.
func (d *Database) GetPendingPDUServerNames(
ctx context.Context,
) ([]gomatrixserverlib.ServerName, error) {
) ([]spec.ServerName, error) {
return d.FederationQueuePDUs.SelectQueuePDUServerNames(ctx, nil)
}