2020-05-01 07:01:50 -05:00
|
|
|
package internal
|
2019-08-22 06:47:52 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-15 11:45:37 -05:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2019-08-22 06:47:52 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/federationsender/api"
|
2021-07-15 11:45:37 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
2019-08-22 06:47:52 -05:00
|
|
|
)
|
|
|
|
|
2020-04-29 05:34:31 -05:00
|
|
|
// QueryJoinedHostServerNamesInRoom implements api.FederationSenderInternalAPI
|
|
|
|
func (f *FederationSenderInternalAPI) QueryJoinedHostServerNamesInRoom(
|
2019-08-22 06:47:52 -05:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryJoinedHostServerNamesInRoomRequest,
|
|
|
|
response *api.QueryJoinedHostServerNamesInRoomResponse,
|
|
|
|
) (err error) {
|
2020-11-19 05:34:59 -06:00
|
|
|
joinedHosts, err := f.db.GetJoinedHostsForRooms(ctx, []string{request.RoomID})
|
2019-08-22 06:47:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-11-19 05:34:59 -06:00
|
|
|
response.ServerNames = joinedHosts
|
2019-08-22 06:47:52 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-07-15 11:45:37 -05: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
|
|
|
|
}
|