2020-05-01 13:01:50 +01:00
|
|
|
package internal
|
2019-08-22 12:47:52 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-15 17:45:37 +01:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2019-08-22 12:47:52 +01:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/api"
|
2021-07-15 17:45:37 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
2019-08-22 12:47:52 +01:00
|
|
|
)
|
|
|
|
|
2020-04-29 11:34:31 +01:00
|
|
|
// QueryJoinedHostServerNamesInRoom implements api.FederationSenderInternalAPI
|
|
|
|
func (f *FederationSenderInternalAPI) QueryJoinedHostServerNamesInRoom(
|
2019-08-22 12:47:52 +01:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryJoinedHostServerNamesInRoomRequest,
|
|
|
|
response *api.QueryJoinedHostServerNamesInRoomResponse,
|
|
|
|
) (err error) {
|
2020-11-19 11:34:59 +00:00
|
|
|
joinedHosts, err := f.db.GetJoinedHostsForRooms(ctx, []string{request.RoomID})
|
2019-08-22 12:47:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-19 11:34:59 +00:00
|
|
|
response.ServerNames = joinedHosts
|
2019-08-22 12:47:52 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-07-15 17:45:37 +01:00
|
|
|
|
|
|
|
func (a *FederationSenderInternalAPI) QueryServerKeys(
|
|
|
|
ctx context.Context, req *api.QueryServerKeysRequest, res *api.QueryServerKeysResponse,
|
|
|
|
) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
ires, err := a.doRequest(req.ServerName, func() (interface{}, error) {
|
|
|
|
return a.federation.GetServerKeys(ctx, req.ServerName)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// try to load from the cache
|
|
|
|
serverKeysResponses, dbErr := a.db.GetNotaryKeys(ctx, req.ServerName, req.OptionalKeyIDs)
|
|
|
|
if dbErr != nil {
|
|
|
|
return fmt.Errorf("server returned %s, and db returned %s", err, dbErr)
|
|
|
|
}
|
|
|
|
res.ServerKeys = serverKeysResponses
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
serverKeys := ires.(gomatrixserverlib.ServerKeys)
|
|
|
|
// cache it!
|
|
|
|
if err = a.db.UpdateNotaryKeys(context.Background(), req.ServerName, serverKeys); err != nil {
|
|
|
|
// non-fatal, still return the response
|
|
|
|
util.GetLogger(ctx).WithError(err).Warn("failed to UpdateNotaryKeys")
|
|
|
|
}
|
|
|
|
res.ServerKeys = []gomatrixserverlib.ServerKeys{serverKeys}
|
|
|
|
return nil
|
|
|
|
}
|