2022-01-27 14:29:14 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-01-27 14:29:14 +00:00
|
|
|
)
|
|
|
|
|
2023-04-27 01:43:46 +01:00
|
|
|
const defaultTimeout = time.Second * 30
|
|
|
|
|
2022-01-27 14:29:14 +00:00
|
|
|
// Functions here are "proxying" calls to the gomatrixserverlib federation
|
|
|
|
// client.
|
|
|
|
|
2023-04-27 01:43:46 +01:00
|
|
|
func (a *FederationInternalAPI) MakeJoin(
|
|
|
|
ctx context.Context, origin, s spec.ServerName, roomID, userID string,
|
|
|
|
) (res gomatrixserverlib.MakeJoinResponse, err error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ires, err := a.federation.MakeJoin(ctx, origin, s, roomID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return &fclient.RespMakeJoin{}, err
|
|
|
|
}
|
|
|
|
return &ires, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) SendJoin(
|
2023-05-02 15:03:16 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, event gomatrixserverlib.PDU,
|
2023-04-27 01:43:46 +01:00
|
|
|
) (res gomatrixserverlib.SendJoinResponse, err error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
ires, err := a.federation.SendJoin(ctx, origin, s, event)
|
|
|
|
if err != nil {
|
|
|
|
return &fclient.RespSendJoin{}, err
|
|
|
|
}
|
|
|
|
return &ires, nil
|
|
|
|
}
|
|
|
|
|
2022-01-27 14:29:14 +00:00
|
|
|
func (a *FederationInternalAPI) GetEventAuth(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName,
|
2022-01-27 14:29:14 +00:00
|
|
|
roomVersion gomatrixserverlib.RoomVersion, roomID, eventID string,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (res fclient.RespEventAuth, err error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.GetEventAuth(ctx, origin, s, roomVersion, roomID, eventID)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespEventAuth{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.RespEventAuth), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) GetUserDevices(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, userID string,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (fclient.RespUserDevices, error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.GetUserDevices(ctx, origin, s, userID)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespUserDevices{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.RespUserDevices), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) ClaimKeys(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, oneTimeKeys map[string]map[string]string,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (fclient.RespClaimKeys, error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
2022-10-27 15:34:26 +01:00
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.ClaimKeys(ctx, origin, s, oneTimeKeys)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespClaimKeys{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.RespClaimKeys), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) QueryKeys(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, keys map[string][]string,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (fclient.RespQueryKeys, error) {
|
2022-01-27 14:29:14 +00:00
|
|
|
ires, err := a.doRequestIfNotBackingOffOrBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.QueryKeys(ctx, origin, s, keys)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespQueryKeys{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.RespQueryKeys), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) Backfill(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, roomID string, limit int, eventIDs []string,
|
2022-01-27 14:29:14 +00:00
|
|
|
) (res gomatrixserverlib.Transaction, err error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.Backfill(ctx, origin, s, roomID, limit, eventIDs)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return gomatrixserverlib.Transaction{}, err
|
|
|
|
}
|
|
|
|
return ires.(gomatrixserverlib.Transaction), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) LookupState(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, roomID, eventID string, roomVersion gomatrixserverlib.RoomVersion,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (res gomatrixserverlib.StateResponse, err error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.LookupState(ctx, origin, s, roomID, eventID, roomVersion)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return &fclient.RespState{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
r := ires.(fclient.RespState)
|
|
|
|
return &r, nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) LookupStateIDs(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, roomID, eventID string,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (res gomatrixserverlib.StateIDResponse, err error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.LookupStateIDs(ctx, origin, s, roomID, eventID)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespStateIDs{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.RespStateIDs), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) LookupMissingEvents(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, roomID string,
|
2023-04-06 09:55:01 +01:00
|
|
|
missing fclient.MissingEvents, roomVersion gomatrixserverlib.RoomVersion,
|
|
|
|
) (res fclient.RespMissingEvents, err error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.LookupMissingEvents(ctx, origin, s, roomID, missing, roomVersion)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespMissingEvents{}, err
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.RespMissingEvents), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) GetEvent(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, eventID string,
|
2022-01-27 14:29:14 +00:00
|
|
|
) (res gomatrixserverlib.Transaction, err error) {
|
2023-04-27 01:43:46 +01:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
|
2022-01-27 14:29:14 +00:00
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.GetEvent(ctx, origin, s, eventID)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return gomatrixserverlib.Transaction{}, err
|
|
|
|
}
|
|
|
|
return ires.(gomatrixserverlib.Transaction), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) LookupServerKeys(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, s spec.ServerName, keyRequests map[gomatrixserverlib.PublicKeyLookupRequest]spec.Timestamp,
|
2022-01-27 14:29:14 +00:00
|
|
|
) ([]gomatrixserverlib.ServerKeys, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
|
|
|
return a.federation.LookupServerKeys(ctx, s, keyRequests)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return []gomatrixserverlib.ServerKeys{}, err
|
|
|
|
}
|
|
|
|
return ires.([]gomatrixserverlib.ServerKeys), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) MSC2836EventRelationships(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, r fclient.MSC2836EventRelationshipsRequest,
|
2022-01-27 14:29:14 +00:00
|
|
|
roomVersion gomatrixserverlib.RoomVersion,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (res fclient.MSC2836EventRelationshipsResponse, err error) {
|
2022-01-27 14:29:14 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.MSC2836EventRelationships(ctx, origin, s, r, roomVersion)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.MSC2836EventRelationshipsResponse), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *FederationInternalAPI) MSC2946Spaces(
|
2023-04-19 15:50:33 +01:00
|
|
|
ctx context.Context, origin, s spec.ServerName, roomID string, suggestedOnly bool,
|
2023-04-06 09:55:01 +01:00
|
|
|
) (res fclient.MSC2946SpacesResponse, err error) {
|
2022-01-27 14:29:14 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequestIfNotBlacklisted(s, func() (interface{}, error) {
|
2022-11-15 15:05:23 +00:00
|
|
|
return a.federation.MSC2946Spaces(ctx, origin, s, roomID, suggestedOnly)
|
2022-01-27 14:29:14 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return ires.(fclient.MSC2946SpacesResponse), nil
|
2022-01-27 14:29:14 +00:00
|
|
|
}
|