2017-04-21 00:40:52 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
2020-02-05 19:06:39 +01:00
|
|
|
// Copyright 2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-21 00:40:52 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-03-06 15:29:39 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2017-09-13 14:37:50 +02:00
|
|
|
"context"
|
2017-03-06 15:29:39 +01:00
|
|
|
"encoding/json"
|
2017-07-13 12:41:30 +02:00
|
|
|
"net/http"
|
|
|
|
|
2017-05-30 18:44:31 +02:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-03-06 15:29:39 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2018-06-26 12:25:49 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/auth"
|
2017-03-09 16:07:18 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-02-05 17:25:58 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state/database"
|
2017-03-06 15:29:39 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-02-05 19:06:39 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/version"
|
2017-03-06 15:29:39 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
)
|
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
// RoomserverQueryAPIEventDB has a convenience API to fetch events directly by
|
|
|
|
// EventIDs.
|
|
|
|
type RoomserverQueryAPIEventDB interface {
|
|
|
|
// Look up the Events for a list of event IDs. Does not error if event was
|
|
|
|
// not found.
|
|
|
|
// Returns an error if the retrieval went wrong.
|
|
|
|
EventsFromIDs(ctx context.Context, eventIDs []string) ([]types.Event, error)
|
|
|
|
}
|
|
|
|
|
2017-03-06 15:29:39 +01:00
|
|
|
// RoomserverQueryAPIDatabase has the storage APIs needed to implement the query API.
|
|
|
|
type RoomserverQueryAPIDatabase interface {
|
2020-02-05 17:25:58 +01:00
|
|
|
database.RoomStateDatabase
|
2017-11-27 11:20:00 +01:00
|
|
|
RoomserverQueryAPIEventDB
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the numeric ID for the room.
|
2017-03-06 15:29:39 +01:00
|
|
|
// Returns 0 if the room doesn't exists.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
RoomNID(ctx context.Context, roomID string) (types.RoomNID, error)
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up event references for the latest events in the room and the current state snapshot.
|
2017-06-27 16:28:44 +02:00
|
|
|
// Returns the latest events, the current state and the maximum depth of the latest events plus 1.
|
2017-03-06 15:29:39 +01:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
LatestEventIDs(
|
|
|
|
ctx context.Context, roomNID types.RoomNID,
|
|
|
|
) ([]gomatrixserverlib.EventReference, types.StateSnapshotNID, int64, error)
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the numeric IDs for a list of events.
|
2017-06-02 15:32:36 +02:00
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
EventNIDs(ctx context.Context, eventIDs []string) (map[string]types.EventNID, error)
|
2017-08-24 17:00:14 +02:00
|
|
|
// Lookup the event IDs for a batch of event numeric IDs.
|
|
|
|
// Returns an error if the retrieval went wrong.
|
2017-09-13 17:30:19 +02:00
|
|
|
EventIDs(ctx context.Context, eventNIDs []types.EventNID) (map[types.EventNID]string, error)
|
2017-08-24 17:00:14 +02:00
|
|
|
// Lookup the membership of a given user in a given room.
|
|
|
|
// Returns the numeric ID of the latest membership event sent from this user
|
|
|
|
// in this room, along a boolean set to true if the user is still in this room,
|
|
|
|
// false if not.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
GetMembership(
|
|
|
|
ctx context.Context, roomNID types.RoomNID, requestSenderUserID string,
|
|
|
|
) (membershipEventNID types.EventNID, stillInRoom bool, err error)
|
2017-08-24 17:00:14 +02:00
|
|
|
// Lookup the membership event numeric IDs for all user that are or have
|
|
|
|
// been members of a given room. Only lookup events of "join" membership if
|
|
|
|
// joinOnly is set to true.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
GetMembershipEventNIDsForRoom(
|
|
|
|
ctx context.Context, roomNID types.RoomNID, joinOnly bool,
|
|
|
|
) ([]types.EventNID, error)
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the active invites targeting a user in a room and return the
|
|
|
|
// numeric state key IDs for the user IDs who sent them.
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
GetInvitesForUser(
|
|
|
|
ctx context.Context,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
targetUserNID types.EventStateKeyNID,
|
|
|
|
) (senderUserNIDs []types.EventStateKeyNID, err error)
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the string event state keys for a list of numeric event state keys
|
|
|
|
// Returns an error if there was a problem talking to the database.
|
2017-09-13 17:30:19 +02:00
|
|
|
EventStateKeys(
|
|
|
|
context.Context, []types.EventStateKeyNID,
|
|
|
|
) (map[types.EventStateKeyNID]string, error)
|
2020-03-16 18:29:52 +01:00
|
|
|
// Look up the room version for a given room.
|
|
|
|
GetRoomVersionForRoom(
|
2020-03-17 19:00:10 +01:00
|
|
|
ctx context.Context, roomID string,
|
2020-03-16 18:29:52 +01:00
|
|
|
) (gomatrixserverlib.RoomVersion, error)
|
2017-03-06 15:29:39 +01:00
|
|
|
}
|
|
|
|
|
2017-07-28 12:31:43 +02:00
|
|
|
// RoomserverQueryAPI is an implementation of api.RoomserverQueryAPI
|
2017-03-06 15:29:39 +01:00
|
|
|
type RoomserverQueryAPI struct {
|
|
|
|
DB RoomserverQueryAPIDatabase
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryLatestEventsAndState implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryLatestEventsAndState(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-03-06 15:29:39 +01:00
|
|
|
request *api.QueryLatestEventsAndStateRequest,
|
|
|
|
response *api.QueryLatestEventsAndStateResponse,
|
2017-06-02 15:32:36 +02:00
|
|
|
) error {
|
2020-03-19 19:33:04 +01:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
2020-02-05 17:25:58 +01:00
|
|
|
if err != nil {
|
2020-03-19 19:33:04 +01:00
|
|
|
response.RoomExists = false
|
|
|
|
return nil
|
2020-02-05 17:25:58 +01:00
|
|
|
}
|
2020-03-19 19:33:04 +01:00
|
|
|
|
|
|
|
roomState := state.NewStateResolution(r.DB)
|
|
|
|
|
2017-03-06 15:29:39 +01:00
|
|
|
response.QueryLatestEventsAndStateRequest = *request
|
2017-09-13 17:30:19 +02:00
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
2017-03-06 15:29:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
2020-03-16 18:29:52 +01:00
|
|
|
|
2017-03-09 16:07:18 +01:00
|
|
|
var currentStateSnapshotNID types.StateSnapshotNID
|
2017-09-13 17:30:19 +02:00
|
|
|
response.LatestEvents, currentStateSnapshotNID, response.Depth, err =
|
|
|
|
r.DB.LatestEventIDs(ctx, roomNID)
|
2017-03-09 16:07:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the currrent state for the requested tuples.
|
2020-02-05 19:06:39 +01:00
|
|
|
stateEntries, err := roomState.LoadStateAtSnapshotForStringTuples(
|
2020-02-05 17:25:58 +01:00
|
|
|
ctx, currentStateSnapshotNID, request.StateToFetch,
|
2017-09-13 17:30:19 +02:00
|
|
|
)
|
2017-03-09 16:07:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
stateEvents, err := r.loadStateEvents(ctx, stateEntries)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
for _, event := range stateEvents {
|
2020-03-17 12:01:25 +01:00
|
|
|
response.StateEvents = append(response.StateEvents, event.Headered(roomVersion))
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 18:44:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryStateAfterEvents implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryStateAfterEvents(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-05-30 18:44:31 +02:00
|
|
|
request *api.QueryStateAfterEventsRequest,
|
|
|
|
response *api.QueryStateAfterEventsResponse,
|
2017-06-02 15:32:36 +02:00
|
|
|
) error {
|
2020-03-19 19:33:04 +01:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
2020-02-05 17:25:58 +01:00
|
|
|
if err != nil {
|
2020-03-19 19:33:04 +01:00
|
|
|
response.RoomExists = false
|
|
|
|
return nil
|
2020-02-05 17:25:58 +01:00
|
|
|
}
|
2020-03-19 19:33:04 +01:00
|
|
|
|
|
|
|
roomState := state.NewStateResolution(r.DB)
|
|
|
|
|
2017-05-30 18:44:31 +02:00
|
|
|
response.QueryStateAfterEventsRequest = *request
|
2017-09-13 17:30:19 +02:00
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
prevStates, err := r.DB.StateAtEventIDs(ctx, request.PrevEventIDs)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
2017-06-07 15:32:53 +02:00
|
|
|
switch err.(type) {
|
|
|
|
case types.MissingEventError:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 18:44:31 +02:00
|
|
|
}
|
|
|
|
response.PrevEventsExist = true
|
|
|
|
|
2017-08-23 16:08:48 +02:00
|
|
|
// Look up the currrent state for the requested tuples.
|
2020-02-05 19:06:39 +01:00
|
|
|
stateEntries, err := roomState.LoadStateAfterEventsForStringTuples(
|
2020-03-19 19:33:04 +01:00
|
|
|
ctx, roomNID, prevStates, request.StateToFetch,
|
2017-09-13 17:30:19 +02:00
|
|
|
)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
stateEvents, err := r.loadStateEvents(ctx, stateEntries)
|
2017-05-30 18:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
for _, event := range stateEvents {
|
2020-03-17 12:01:25 +01:00
|
|
|
response.StateEvents = append(response.StateEvents, event.Headered(roomVersion))
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 18:44:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 15:32:36 +02:00
|
|
|
// QueryEventsByID implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryEventsByID(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-06-02 15:32:36 +02:00
|
|
|
request *api.QueryEventsByIDRequest,
|
|
|
|
response *api.QueryEventsByIDResponse,
|
|
|
|
) error {
|
|
|
|
response.QueryEventsByIDRequest = *request
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
eventNIDMap, err := r.DB.EventNIDs(ctx, request.EventIDs)
|
2017-06-02 15:32:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, nid := range eventNIDMap {
|
|
|
|
eventNIDs = append(eventNIDs, nid)
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
events, err := r.loadEvents(ctx, eventNIDs)
|
2017-06-02 15:32:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
for _, event := range events {
|
2020-03-17 19:00:10 +01:00
|
|
|
roomVersion, verr := r.DB.GetRoomVersionForRoom(ctx, event.RoomID())
|
|
|
|
if verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
2020-03-16 18:29:52 +01:00
|
|
|
|
2020-03-17 12:01:25 +01:00
|
|
|
response.Events = append(response.Events, event.Headered(roomVersion))
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-06-02 15:32:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
func (r *RoomserverQueryAPI) loadStateEvents(
|
|
|
|
ctx context.Context, stateEntries []types.StateEntry,
|
|
|
|
) ([]gomatrixserverlib.Event, error) {
|
2017-03-09 16:07:18 +01:00
|
|
|
eventNIDs := make([]types.EventNID, len(stateEntries))
|
|
|
|
for i := range stateEntries {
|
|
|
|
eventNIDs[i] = stateEntries[i].EventNID
|
|
|
|
}
|
2017-09-13 17:30:19 +02:00
|
|
|
return r.loadEvents(ctx, eventNIDs)
|
2017-06-02 15:32:36 +02:00
|
|
|
}
|
2017-03-09 16:07:18 +01:00
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
func (r *RoomserverQueryAPI) loadEvents(
|
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
) ([]gomatrixserverlib.Event, error) {
|
|
|
|
stateEvents, err := r.DB.Events(ctx, eventNIDs)
|
2017-03-09 16:07:18 +01:00
|
|
|
if err != nil {
|
2017-05-30 18:44:31 +02:00
|
|
|
return nil, err
|
2017-03-09 16:07:18 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 18:44:31 +02:00
|
|
|
result := make([]gomatrixserverlib.Event, len(stateEvents))
|
2017-03-09 16:07:18 +01:00
|
|
|
for i := range stateEvents {
|
2017-05-30 18:44:31 +02:00
|
|
|
result[i] = stateEvents[i].Event
|
2017-03-09 16:07:18 +01:00
|
|
|
}
|
2017-05-30 18:44:31 +02:00
|
|
|
return result, nil
|
2017-03-06 15:29:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-11 12:10:37 +02:00
|
|
|
// QueryMembershipForUser implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryMembershipForUser(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryMembershipForUserRequest,
|
|
|
|
response *api.QueryMembershipForUserResponse,
|
|
|
|
) error {
|
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
membershipEventNID, stillInRoom, err := r.DB.GetMembership(ctx, roomNID, request.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if membershipEventNID == 0 {
|
|
|
|
response.HasBeenInRoom = false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response.IsInRoom = stillInRoom
|
|
|
|
eventIDMap, err := r.DB.EventIDs(ctx, []types.EventNID{membershipEventNID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.EventID = eventIDMap[membershipEventNID]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:34:26 +02:00
|
|
|
// QueryMembershipsForRoom implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryMembershipsForRoom(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-08-21 17:34:26 +02:00
|
|
|
request *api.QueryMembershipsForRoomRequest,
|
|
|
|
response *api.QueryMembershipsForRoomResponse,
|
|
|
|
) error {
|
2017-09-13 17:30:19 +02:00
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
2017-08-21 17:34:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
membershipEventNID, stillInRoom, err := r.DB.GetMembership(ctx, roomNID, request.Sender)
|
2017-08-21 17:34:26 +02:00
|
|
|
if err != nil {
|
2018-07-11 14:16:38 +02:00
|
|
|
return err
|
2017-08-21 17:34:26 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 17:00:14 +02:00
|
|
|
if membershipEventNID == 0 {
|
2017-08-21 17:34:26 +02:00
|
|
|
response.HasBeenInRoom = false
|
|
|
|
response.JoinEvents = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response.HasBeenInRoom = true
|
|
|
|
response.JoinEvents = []gomatrixserverlib.ClientEvent{}
|
2017-08-24 17:00:14 +02:00
|
|
|
|
|
|
|
var events []types.Event
|
|
|
|
if stillInRoom {
|
|
|
|
var eventNIDs []types.EventNID
|
2017-09-13 17:30:19 +02:00
|
|
|
eventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, roomNID, request.JoinedOnly)
|
2017-08-24 17:00:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
events, err = r.DB.Events(ctx, eventNIDs)
|
2017-08-24 17:00:14 +02:00
|
|
|
} else {
|
2017-09-13 17:30:19 +02:00
|
|
|
events, err = r.getMembershipsBeforeEventNID(ctx, membershipEventNID, request.JoinedOnly)
|
2017-08-24 17:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:34:26 +02:00
|
|
|
for _, event := range events {
|
|
|
|
clientEvent := gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll)
|
|
|
|
response.JoinEvents = append(response.JoinEvents, clientEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:00:14 +02:00
|
|
|
// getMembershipsBeforeEventNID takes the numeric ID of an event and fetches the state
|
|
|
|
// of the event's room as it was when this event was fired, then filters the state events to
|
|
|
|
// only keep the "m.room.member" events with a "join" membership. These events are returned.
|
|
|
|
// Returns an error if there was an issue fetching the events.
|
2017-09-13 17:30:19 +02:00
|
|
|
func (r *RoomserverQueryAPI) getMembershipsBeforeEventNID(
|
|
|
|
ctx context.Context, eventNID types.EventNID, joinedOnly bool,
|
|
|
|
) ([]types.Event, error) {
|
2020-03-19 19:33:04 +01:00
|
|
|
roomState := state.NewStateResolution(r.DB)
|
2017-08-24 17:00:14 +02:00
|
|
|
events := []types.Event{}
|
|
|
|
// Lookup the event NID
|
2017-09-13 17:30:19 +02:00
|
|
|
eIDs, err := r.DB.EventIDs(ctx, []types.EventNID{eventNID})
|
2017-08-24 17:00:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eventIDs := []string{eIDs[eventNID]}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
prevState, err := r.DB.StateAtEventIDs(ctx, eventIDs)
|
2017-08-24 17:00:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the state as it was when this event was fired
|
2020-02-05 19:06:39 +01:00
|
|
|
stateEntries, err := roomState.LoadCombinedStateAfterEvents(ctx, prevState)
|
2017-08-24 17:00:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, entry := range stateEntries {
|
|
|
|
// Filter the events to retrieve to only keep the membership events
|
|
|
|
if entry.EventTypeNID == types.MRoomMemberNID {
|
|
|
|
eventNIDs = append(eventNIDs, entry.EventNID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all of the events in this state
|
2017-09-13 17:30:19 +02:00
|
|
|
stateEvents, err := r.DB.Events(ctx, eventNIDs)
|
2017-08-24 17:00:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !joinedOnly {
|
|
|
|
return stateEvents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the events to only keep the "join" membership events
|
|
|
|
for _, event := range stateEvents {
|
|
|
|
membership, err := event.Membership()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:07:36 +02:00
|
|
|
if membership == gomatrixserverlib.Join {
|
2017-08-24 17:00:14 +02:00
|
|
|
events = append(events, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:08:48 +02:00
|
|
|
// QueryInvitesForUser implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryInvitesForUser(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context,
|
2017-08-23 16:08:48 +02:00
|
|
|
request *api.QueryInvitesForUserRequest,
|
|
|
|
response *api.QueryInvitesForUserResponse,
|
|
|
|
) error {
|
2017-09-13 17:30:19 +02:00
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
2017-08-23 16:08:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
targetUserNIDs, err := r.DB.EventStateKeyNIDs(ctx, []string{request.TargetUserID})
|
2017-08-23 16:08:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
targetUserNID := targetUserNIDs[request.TargetUserID]
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
senderUserNIDs, err := r.DB.GetInvitesForUser(ctx, roomNID, targetUserNID)
|
2017-08-23 16:08:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
senderUserIDs, err := r.DB.EventStateKeys(ctx, senderUserNIDs)
|
2017-08-23 16:08:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, senderUserID := range senderUserIDs {
|
|
|
|
response.InviteSenderUserIDs = append(response.InviteSenderUserIDs, senderUserID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 13:38:22 +02:00
|
|
|
// QueryServerAllowedToSeeEvent implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryServerAllowedToSeeEvent(
|
2017-09-13 14:37:50 +02:00
|
|
|
ctx context.Context,
|
2017-09-06 13:38:22 +02:00
|
|
|
request *api.QueryServerAllowedToSeeEventRequest,
|
|
|
|
response *api.QueryServerAllowedToSeeEventResponse,
|
2018-06-26 12:25:49 +02:00
|
|
|
) (err error) {
|
|
|
|
response.AllowedToSeeEvent, err = r.checkServerAllowedToSeeEvent(
|
|
|
|
ctx, request.EventID, request.ServerName,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoomserverQueryAPI) checkServerAllowedToSeeEvent(
|
|
|
|
ctx context.Context, eventID string, serverName gomatrixserverlib.ServerName,
|
|
|
|
) (bool, error) {
|
2020-03-19 19:33:04 +01:00
|
|
|
roomState := state.NewStateResolution(r.DB)
|
2020-02-05 19:06:39 +01:00
|
|
|
stateEntries, err := roomState.LoadStateAtEvent(ctx, eventID)
|
2017-09-06 13:38:22 +02:00
|
|
|
if err != nil {
|
2018-06-26 12:25:49 +02:00
|
|
|
return false, err
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We probably want to make it so that we don't have to pull
|
|
|
|
// out all the state if possible.
|
2017-09-13 17:30:19 +02:00
|
|
|
stateAtEvent, err := r.loadStateEvents(ctx, stateEntries)
|
2017-09-06 13:38:22 +02:00
|
|
|
if err != nil {
|
2018-06-26 12:25:49 +02:00
|
|
|
return false, err
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 12:25:49 +02:00
|
|
|
return auth.IsServerAllowed(serverName, stateAtEvent), nil
|
|
|
|
}
|
2017-09-06 13:38:22 +02:00
|
|
|
|
2018-06-26 12:25:49 +02:00
|
|
|
// QueryMissingEvents implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryMissingEvents(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryMissingEventsRequest,
|
|
|
|
response *api.QueryMissingEventsResponse,
|
|
|
|
) error {
|
|
|
|
var front []string
|
2019-01-14 11:20:19 +01:00
|
|
|
eventsToFilter := make(map[string]bool, len(request.LatestEvents))
|
2018-06-26 12:25:49 +02:00
|
|
|
visited := make(map[string]bool, request.Limit) // request.Limit acts as a hint to size.
|
|
|
|
for _, id := range request.EarliestEvents {
|
|
|
|
visited[id] = true
|
|
|
|
}
|
2017-09-06 13:38:22 +02:00
|
|
|
|
2018-06-26 12:25:49 +02:00
|
|
|
for _, id := range request.LatestEvents {
|
|
|
|
if !visited[id] {
|
|
|
|
front = append(front, id)
|
2019-01-14 11:20:19 +01:00
|
|
|
eventsToFilter[id] = true
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
2018-06-26 12:25:49 +02:00
|
|
|
}
|
2017-09-06 13:38:22 +02:00
|
|
|
|
2018-11-07 12:38:01 +01:00
|
|
|
resultNIDs, err := r.scanEventTree(ctx, front, visited, request.Limit, request.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-14 11:20:19 +01:00
|
|
|
loadedEvents, err := r.loadEvents(ctx, resultNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 18:29:52 +01:00
|
|
|
response.Events = make([]gomatrixserverlib.HeaderedEvent, 0, len(loadedEvents)-len(eventsToFilter))
|
2019-01-14 11:20:19 +01:00
|
|
|
for _, event := range loadedEvents {
|
|
|
|
if !eventsToFilter[event.EventID()] {
|
2020-03-17 19:00:10 +01:00
|
|
|
roomVersion, verr := r.DB.GetRoomVersionForRoom(ctx, event.RoomID())
|
|
|
|
if verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
2020-03-16 18:29:52 +01:00
|
|
|
|
2020-03-17 12:01:25 +01:00
|
|
|
response.Events = append(response.Events, event.Headered(roomVersion))
|
2019-01-14 11:20:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:38:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryBackfill implements api.RoomServerQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryBackfill(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryBackfillRequest,
|
|
|
|
response *api.QueryBackfillResponse,
|
|
|
|
) error {
|
|
|
|
var err error
|
|
|
|
var front []string
|
|
|
|
|
|
|
|
// The limit defines the maximum number of events to retrieve, so it also
|
|
|
|
// defines the highest number of elements in the map below.
|
|
|
|
visited := make(map[string]bool, request.Limit)
|
|
|
|
|
|
|
|
// The provided event IDs have already been seen by the request's emitter,
|
|
|
|
// and will be retrieved anyway, so there's no need to care about them if
|
|
|
|
// they appear in our exploration of the event tree.
|
|
|
|
for _, id := range request.EarliestEventsIDs {
|
|
|
|
visited[id] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
front = request.EarliestEventsIDs
|
|
|
|
|
|
|
|
// Scan the event tree for events to send back.
|
|
|
|
resultNIDs, err := r.scanEventTree(ctx, front, visited, request.Limit, request.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve events from the list that was filled previously.
|
2020-03-16 18:29:52 +01:00
|
|
|
var loadedEvents []gomatrixserverlib.Event
|
|
|
|
loadedEvents, err = r.loadEvents(ctx, resultNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range loadedEvents {
|
2020-03-17 19:00:10 +01:00
|
|
|
roomVersion, verr := r.DB.GetRoomVersionForRoom(ctx, event.RoomID())
|
|
|
|
if verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
2020-03-16 18:29:52 +01:00
|
|
|
|
2020-03-17 12:01:25 +01:00
|
|
|
response.Events = append(response.Events, event.Headered(roomVersion))
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:38:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RoomserverQueryAPI) scanEventTree(
|
|
|
|
ctx context.Context, front []string, visited map[string]bool, limit int,
|
|
|
|
serverName gomatrixserverlib.ServerName,
|
|
|
|
) (resultNIDs []types.EventNID, err error) {
|
|
|
|
var allowed bool
|
|
|
|
var events []types.Event
|
|
|
|
var next []string
|
|
|
|
var pre string
|
|
|
|
|
|
|
|
resultNIDs = make([]types.EventNID, 0, limit)
|
|
|
|
|
|
|
|
// Loop through the event IDs to retrieve the requested events and go
|
|
|
|
// through the whole tree (up to the provided limit) using the events'
|
|
|
|
// "prev_event" key.
|
2018-06-26 12:25:49 +02:00
|
|
|
BFSLoop:
|
|
|
|
for len(front) > 0 {
|
2018-11-07 12:38:01 +01:00
|
|
|
// Prevent unnecessary allocations: reset the slice only when not empty.
|
|
|
|
if len(next) > 0 {
|
|
|
|
next = make([]string, 0)
|
|
|
|
}
|
|
|
|
// Retrieve the events to process from the database.
|
|
|
|
events, err = r.DB.EventsFromIDs(ctx, front)
|
2017-09-06 13:38:22 +02:00
|
|
|
if err != nil {
|
2018-11-07 12:38:01 +01:00
|
|
|
return
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2018-06-26 12:25:49 +02:00
|
|
|
for _, ev := range events {
|
2018-11-07 12:38:01 +01:00
|
|
|
// Break out of the loop if the provided limit is reached.
|
|
|
|
if len(resultNIDs) == limit {
|
2018-06-26 12:25:49 +02:00
|
|
|
break BFSLoop
|
|
|
|
}
|
2018-11-07 12:38:01 +01:00
|
|
|
// Update the list of events to retrieve.
|
2018-06-26 12:25:49 +02:00
|
|
|
resultNIDs = append(resultNIDs, ev.EventNID)
|
2018-11-07 12:38:01 +01:00
|
|
|
// Loop through the event's parents.
|
|
|
|
for _, pre = range ev.PrevEventIDs() {
|
|
|
|
// Only add an event to the list of next events to process if it
|
|
|
|
// hasn't been seen before.
|
2018-06-26 12:25:49 +02:00
|
|
|
if !visited[pre] {
|
|
|
|
visited[pre] = true
|
2018-11-07 12:38:01 +01:00
|
|
|
allowed, err = r.checkServerAllowedToSeeEvent(ctx, pre, serverName)
|
2018-06-26 12:25:49 +02:00
|
|
|
if err != nil {
|
2018-11-07 12:38:01 +01:00
|
|
|
return
|
2018-06-26 12:25:49 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:38:01 +01:00
|
|
|
// If the event hasn't been seen before and the HS
|
|
|
|
// requesting to retrieve it is allowed to do so, add it to
|
|
|
|
// the list of events to retrieve.
|
2018-06-26 12:25:49 +02:00
|
|
|
if allowed {
|
|
|
|
next = append(next, pre)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
2018-11-07 12:38:01 +01:00
|
|
|
// Repeat the same process with the parent events we just processed.
|
2018-06-26 12:25:49 +02:00
|
|
|
front = next
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 12:38:01 +01:00
|
|
|
return
|
2017-09-06 13:38:22 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
// QueryStateAndAuthChain implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryStateAndAuthChain(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryStateAndAuthChainRequest,
|
|
|
|
response *api.QueryStateAndAuthChainResponse,
|
|
|
|
) error {
|
|
|
|
response.QueryStateAndAuthChainRequest = *request
|
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
|
|
|
|
2020-03-17 19:00:10 +01:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:18:48 +01:00
|
|
|
stateEvents, err := r.loadStateAtEventIDs(ctx, request.PrevEventIDs)
|
2017-11-27 11:20:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-17 18:18:48 +01:00
|
|
|
response.PrevEventsExist = true
|
2017-11-27 11:20:00 +01:00
|
|
|
|
2020-03-17 18:18:48 +01:00
|
|
|
// add the auth event IDs for the current state events too
|
|
|
|
var authEventIDs []string
|
|
|
|
authEventIDs = append(authEventIDs, request.AuthEventIDs...)
|
|
|
|
for _, se := range stateEvents {
|
|
|
|
authEventIDs = append(authEventIDs, se.AuthEventIDs()...)
|
2017-11-27 11:20:00 +01:00
|
|
|
}
|
2020-03-17 18:18:48 +01:00
|
|
|
authEventIDs = util.UniqueStrings(authEventIDs) // de-dupe
|
2017-11-27 11:20:00 +01:00
|
|
|
|
2020-03-17 18:18:48 +01:00
|
|
|
authEvents, err := getAuthChain(ctx, r.DB, authEventIDs)
|
2020-03-16 18:29:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range stateEvents {
|
2020-03-17 12:01:25 +01:00
|
|
|
response.StateEvents = append(response.StateEvents, event.Headered(roomVersion))
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range authEvents {
|
2020-03-17 12:01:25 +01:00
|
|
|
response.AuthChainEvents = append(response.AuthChainEvents, event.Headered(roomVersion))
|
2020-03-16 18:29:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:18:48 +01:00
|
|
|
func (r *RoomserverQueryAPI) loadStateAtEventIDs(ctx context.Context, eventIDs []string) ([]gomatrixserverlib.Event, error) {
|
2020-03-19 19:33:04 +01:00
|
|
|
roomState := state.NewStateResolution(r.DB)
|
2020-03-17 18:18:48 +01:00
|
|
|
prevStates, err := r.DB.StateAtEventIDs(ctx, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case types.MissingEventError:
|
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the currrent state for the requested tuples.
|
|
|
|
stateEntries, err := roomState.LoadCombinedStateAfterEvents(
|
|
|
|
ctx, prevStates,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.loadStateEvents(ctx, stateEntries)
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:11:44 +01:00
|
|
|
// getAuthChain fetches the auth chain for the given auth events. An auth chain
|
|
|
|
// is the list of all events that are referenced in the auth_events section, and
|
|
|
|
// all their auth_events, recursively. The returned set of events contain the
|
|
|
|
// given events. Will *not* error if we don't have all auth events.
|
2017-11-27 11:20:00 +01:00
|
|
|
func getAuthChain(
|
|
|
|
ctx context.Context, dB RoomserverQueryAPIEventDB, authEventIDs []string,
|
|
|
|
) ([]gomatrixserverlib.Event, error) {
|
2020-01-10 13:11:44 +01:00
|
|
|
// List of event IDs to fetch. On each pass, these events will be requested
|
|
|
|
// from the database and the `eventsToFetch` will be updated with any new
|
|
|
|
// events that we have learned about and need to find. When `eventsToFetch`
|
|
|
|
// is eventually empty, we should have reached the end of the chain.
|
2017-11-27 11:20:00 +01:00
|
|
|
eventsToFetch := authEventIDs
|
2020-01-10 13:11:44 +01:00
|
|
|
authEventsMap := make(map[string]gomatrixserverlib.Event)
|
2017-11-27 11:20:00 +01:00
|
|
|
|
|
|
|
for len(eventsToFetch) > 0 {
|
2020-01-10 13:11:44 +01:00
|
|
|
// Try to retrieve the events from the database.
|
2017-11-27 11:20:00 +01:00
|
|
|
events, err := dB.EventsFromIDs(ctx, eventsToFetch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:11:44 +01:00
|
|
|
// We've now fetched these events so clear out `eventsToFetch`. Soon we may
|
|
|
|
// add newly discovered events to this for the next pass.
|
2017-11-27 11:20:00 +01:00
|
|
|
eventsToFetch = eventsToFetch[:0]
|
2020-01-10 13:11:44 +01:00
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
for _, event := range events {
|
2020-01-10 13:11:44 +01:00
|
|
|
// Store the event in the event map - this prevents us from requesting it
|
|
|
|
// from the database again.
|
|
|
|
authEventsMap[event.EventID()] = event.Event
|
|
|
|
|
|
|
|
// Extract all of the auth events from the newly obtained event. If we
|
|
|
|
// don't already have a record of the event, record it in the list of
|
|
|
|
// events we want to request for the next pass.
|
|
|
|
for _, authEvent := range event.AuthEvents() {
|
|
|
|
if _, ok := authEventsMap[authEvent.EventID]; !ok {
|
|
|
|
eventsToFetch = append(eventsToFetch, authEvent.EventID)
|
2017-11-27 11:20:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:11:44 +01:00
|
|
|
// We've now retrieved all of the events we can. Flatten them down into an
|
|
|
|
// array and return them.
|
|
|
|
var authEvents []gomatrixserverlib.Event
|
|
|
|
for _, event := range authEventsMap {
|
|
|
|
authEvents = append(authEvents, event)
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:20:00 +01:00
|
|
|
return authEvents, nil
|
|
|
|
}
|
|
|
|
|
2020-01-23 18:51:10 +01:00
|
|
|
// QueryServersInRoomAtEvent implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryServersInRoomAtEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryServersInRoomAtEventRequest,
|
|
|
|
response *api.QueryServersInRoomAtEventResponse,
|
|
|
|
) error {
|
|
|
|
// getMembershipsBeforeEventNID requires a NID, so retrieving the NID for
|
|
|
|
// the event is necessary.
|
|
|
|
NIDs, err := r.DB.EventNIDs(ctx, []string{request.EventID})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve all "m.room.member" state events of "join" membership, which
|
|
|
|
// contains the list of users in the room before the event, therefore all
|
|
|
|
// the servers in it at that moment.
|
|
|
|
events, err := r.getMembershipsBeforeEventNID(ctx, NIDs[request.EventID], true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the server names in a temporary map to avoid duplicates.
|
|
|
|
servers := make(map[gomatrixserverlib.ServerName]bool)
|
|
|
|
for _, event := range events {
|
|
|
|
servers[event.Origin()] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the response.
|
|
|
|
for server := range servers {
|
|
|
|
response.Servers = append(response.Servers, server)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:06:39 +01:00
|
|
|
// QueryRoomVersionCapabilities implements api.RoomserverQueryAPI
|
|
|
|
func (r *RoomserverQueryAPI) QueryRoomVersionCapabilities(
|
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryRoomVersionCapabilitiesRequest,
|
|
|
|
response *api.QueryRoomVersionCapabilitiesResponse,
|
|
|
|
) error {
|
2020-03-19 13:07:01 +01:00
|
|
|
response.DefaultRoomVersion = version.DefaultRoomVersion()
|
|
|
|
response.AvailableRoomVersions = make(map[gomatrixserverlib.RoomVersion]string)
|
2020-03-16 17:05:29 +01:00
|
|
|
for v, desc := range version.SupportedRoomVersions() {
|
2020-02-05 19:06:39 +01:00
|
|
|
if desc.Stable {
|
2020-03-19 13:07:01 +01:00
|
|
|
response.AvailableRoomVersions[v] = "stable"
|
2020-02-05 19:06:39 +01:00
|
|
|
} else {
|
2020-03-19 13:07:01 +01:00
|
|
|
response.AvailableRoomVersions[v] = "unstable"
|
2020-02-05 19:06:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 15:29:39 +01:00
|
|
|
// SetupHTTP adds the RoomserverQueryAPI handlers to the http.ServeMux.
|
2017-09-06 13:38:22 +02:00
|
|
|
// nolint: gocyclo
|
2017-03-06 15:29:39 +01:00
|
|
|
func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryLatestEventsAndStatePath,
|
2017-09-28 15:50:40 +02:00
|
|
|
common.MakeInternalAPI("queryLatestEventsAndState", func(req *http.Request) util.JSONResponse {
|
2017-03-06 15:29:39 +01:00
|
|
|
var request api.QueryLatestEventsAndStateRequest
|
|
|
|
var response api.QueryLatestEventsAndStateResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := r.QueryLatestEventsAndState(req.Context(), &request, &response); err != nil {
|
2017-03-06 15:29:39 +01:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-03-06 15:29:39 +01:00
|
|
|
}),
|
|
|
|
)
|
2017-05-30 18:44:31 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryStateAfterEventsPath,
|
2017-09-28 15:50:40 +02:00
|
|
|
common.MakeInternalAPI("queryStateAfterEvents", func(req *http.Request) util.JSONResponse {
|
2017-05-30 18:44:31 +02:00
|
|
|
var request api.QueryStateAfterEventsRequest
|
|
|
|
var response api.QueryStateAfterEventsResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := r.QueryStateAfterEvents(req.Context(), &request, &response); err != nil {
|
2017-05-30 18:44:31 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-05-30 18:44:31 +02:00
|
|
|
}),
|
|
|
|
)
|
2017-06-02 15:32:36 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryEventsByIDPath,
|
2017-09-28 15:50:40 +02:00
|
|
|
common.MakeInternalAPI("queryEventsByID", func(req *http.Request) util.JSONResponse {
|
2017-06-02 15:32:36 +02:00
|
|
|
var request api.QueryEventsByIDRequest
|
|
|
|
var response api.QueryEventsByIDResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := r.QueryEventsByID(req.Context(), &request, &response); err != nil {
|
2017-06-02 15:32:36 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-06-02 15:32:36 +02:00
|
|
|
}),
|
|
|
|
)
|
2018-07-11 12:10:37 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryMembershipForUserPath,
|
|
|
|
common.MakeInternalAPI("QueryMembershipForUser", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryMembershipForUserRequest
|
|
|
|
var response api.QueryMembershipForUserResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryMembershipForUser(req.Context(), &request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-08-21 17:34:26 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryMembershipsForRoomPath,
|
2017-09-28 15:50:40 +02:00
|
|
|
common.MakeInternalAPI("queryMembershipsForRoom", func(req *http.Request) util.JSONResponse {
|
2017-08-21 17:34:26 +02:00
|
|
|
var request api.QueryMembershipsForRoomRequest
|
|
|
|
var response api.QueryMembershipsForRoomResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := r.QueryMembershipsForRoom(req.Context(), &request, &response); err != nil {
|
2017-08-21 17:34:26 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-08-21 17:34:26 +02:00
|
|
|
}),
|
|
|
|
)
|
2017-08-23 16:08:48 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryInvitesForUserPath,
|
2017-09-28 15:50:40 +02:00
|
|
|
common.MakeInternalAPI("queryInvitesForUser", func(req *http.Request) util.JSONResponse {
|
2017-08-23 16:08:48 +02:00
|
|
|
var request api.QueryInvitesForUserRequest
|
|
|
|
var response api.QueryInvitesForUserResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := r.QueryInvitesForUser(req.Context(), &request, &response); err != nil {
|
2017-08-23 16:08:48 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-08-23 16:08:48 +02:00
|
|
|
}),
|
|
|
|
)
|
2017-09-06 13:38:22 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryServerAllowedToSeeEventPath,
|
2017-09-28 15:50:40 +02:00
|
|
|
common.MakeInternalAPI("queryServerAllowedToSeeEvent", func(req *http.Request) util.JSONResponse {
|
2017-09-06 13:38:22 +02:00
|
|
|
var request api.QueryServerAllowedToSeeEventRequest
|
|
|
|
var response api.QueryServerAllowedToSeeEventResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2017-09-13 14:37:50 +02:00
|
|
|
if err := r.QueryServerAllowedToSeeEvent(req.Context(), &request, &response); err != nil {
|
2017-09-06 13:38:22 +02:00
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-09-06 13:38:22 +02:00
|
|
|
}),
|
|
|
|
)
|
2018-06-26 12:25:49 +02:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryMissingEventsPath,
|
|
|
|
common.MakeInternalAPI("queryMissingEvents", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryMissingEventsRequest
|
|
|
|
var response api.QueryMissingEventsResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryMissingEvents(req.Context(), &request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-11-27 11:20:00 +01:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryStateAndAuthChainPath,
|
|
|
|
common.MakeInternalAPI("queryStateAndAuthChain", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryStateAndAuthChainRequest
|
|
|
|
var response api.QueryStateAndAuthChainResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryStateAndAuthChain(req.Context(), &request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
2018-03-13 16:55:45 +01:00
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
2017-11-27 11:20:00 +01:00
|
|
|
}),
|
|
|
|
)
|
2018-11-07 12:38:01 +01:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryBackfillPath,
|
|
|
|
common.MakeInternalAPI("QueryBackfill", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryBackfillRequest
|
|
|
|
var response api.QueryBackfillResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryBackfill(req.Context(), &request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2020-01-23 18:51:10 +01:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryServersInRoomAtEventPath,
|
|
|
|
common.MakeInternalAPI("QueryServersInRoomAtEvent", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryServersInRoomAtEventRequest
|
|
|
|
var response api.QueryServersInRoomAtEventResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryServersInRoomAtEvent(req.Context(), &request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2020-02-05 19:06:39 +01:00
|
|
|
servMux.Handle(
|
|
|
|
api.RoomserverQueryRoomVersionCapabilitiesPath,
|
|
|
|
common.MakeInternalAPI("QueryRoomVersionCapabilities", func(req *http.Request) util.JSONResponse {
|
|
|
|
var request api.QueryRoomVersionCapabilitiesRequest
|
|
|
|
var response api.QueryRoomVersionCapabilitiesResponse
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
if err := r.QueryRoomVersionCapabilities(req.Context(), &request, &response); err != nil {
|
|
|
|
return util.ErrorResponse(err)
|
|
|
|
}
|
|
|
|
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
|
|
|
}),
|
|
|
|
)
|
2017-03-06 15:29:39 +01:00
|
|
|
}
|