Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Replace some helpers.IsServerCurrentlyInRoom calls with query API #1912

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions roomserver/internal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (r *RoomserverInternalAPI) SetFederationSenderAPI(fsAPI fsAPI.FederationSen
FSAPI: r.fsAPI,
RSAPI: r,
Inputer: r.Inputer,
Queryer: r.Queryer,
}
r.Peeker = &perform.Peeker{
ServerName: r.Cfg.Matrix.ServerName,
Expand Down
8 changes: 8 additions & 0 deletions roomserver/internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func UpdateToInviteMembership(
return updates, nil
}

// IsServerCurrentlyInRoom checks if a server is in a given room, based on the room
// memberships. If the servername is not supplied then the local server will be
// checked instead using a faster code path.
kegsay marked this conversation as resolved.
Show resolved Hide resolved
// TODO: This should probably be replaced by an API call.
func IsServerCurrentlyInRoom(ctx context.Context, db storage.Database, serverName gomatrixserverlib.ServerName, roomID string) (bool, error) {
info, err := db.RoomInfo(ctx, roomID)
if err != nil {
Expand All @@ -59,6 +63,10 @@ func IsServerCurrentlyInRoom(ctx context.Context, db storage.Database, serverNam
return false, fmt.Errorf("unknown room %s", roomID)
}

if serverName == "" {
return db.GetLocalServerInRoom(ctx, info.RoomNID)
}

eventNIDs, err := db.GetMembershipEventNIDsForRoom(ctx, info.RoomNID, true, false)
if err != nil {
return false, err
Expand Down
11 changes: 10 additions & 1 deletion roomserver/internal/perform/perform_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
rsAPI "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
"github.com/matrix-org/dendrite/roomserver/internal/input"
"github.com/matrix-org/dendrite/roomserver/internal/query"
"github.com/matrix-org/dendrite/roomserver/storage"
"github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/gomatrixserverlib"
Expand All @@ -42,6 +43,7 @@ type Joiner struct {
DB storage.Database

Inputer *input.Inputer
Queryer *query.Queryer
}

// PerformJoin handles joining matrix rooms, including over federation by talking to the federationsender.
Expand Down Expand Up @@ -205,7 +207,14 @@ func (r *Joiner) performJoinRoomByID(

// Force a federated join if we aren't in the room and we've been
// given some server names to try joining by.
serverInRoom, _ := helpers.IsServerCurrentlyInRoom(ctx, r.DB, r.ServerName, req.RoomIDOrAlias)
inRoomReq := &api.QueryServerJoinedToRoomRequest{
RoomID: req.RoomIDOrAlias,
}
kegsay marked this conversation as resolved.
Show resolved Hide resolved
inRoomRes := &api.QueryServerJoinedToRoomResponse{}
if err = r.Queryer.QueryServerJoinedToRoom(ctx, inRoomReq, inRoomRes); err != nil {
return "", "", fmt.Errorf("r.Queryer.QueryServerJoinedToRoom: %w", err)
}
serverInRoom := inRoomRes.IsInRoom
forceFederatedJoin := len(req.ServerNames) > 0 && !serverInRoom

// Force a federated join if we're dealing with a pending invite
Expand Down
14 changes: 10 additions & 4 deletions roomserver/internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,16 @@ func (r *Queryer) QueryServerAllowedToSeeEvent(
return
}
roomID := events[0].RoomID()
isServerInRoom, err := helpers.IsServerCurrentlyInRoom(ctx, r.DB, request.ServerName, roomID)
if err != nil {
return

inRoomReq := &api.QueryServerJoinedToRoomRequest{
RoomID: roomID,
ServerName: request.ServerName,
}
inRoomRes := &api.QueryServerJoinedToRoomResponse{}
if err = r.QueryServerJoinedToRoom(ctx, inRoomReq, inRoomRes); err != nil {
return fmt.Errorf("r.Queryer.QueryServerJoinedToRoom: %w", err)
}

info, err := r.DB.RoomInfo(ctx, roomID)
if err != nil {
return err
Expand All @@ -400,7 +406,7 @@ func (r *Queryer) QueryServerAllowedToSeeEvent(
return fmt.Errorf("QueryServerAllowedToSeeEvent: no room info for room %s", roomID)
}
response.AllowedToSeeEvent, err = helpers.CheckServerAllowedToSeeEvent(
ctx, r.DB, *info, request.EventID, request.ServerName, isServerInRoom,
ctx, r.DB, *info, request.EventID, request.ServerName, inRoomRes.IsInRoom,
)
return
}
Expand Down