2017-04-20 23:40:52 +01:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
2020-02-05 18:06:39 +00:00
|
|
|
// Copyright 2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-20 23:40:52 +01: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.
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
package internal
|
2017-03-06 14:29:39 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 13:37:50 +01:00
|
|
|
"context"
|
2020-04-28 11:46:47 +01:00
|
|
|
"fmt"
|
2017-07-13 11:41:30 +01:00
|
|
|
|
2017-03-06 14:29:39 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2018-06-26 11:25:49 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/auth"
|
2017-03-09 15:07:18 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-04-24 10:38:58 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2017-03-06 14:29:39 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2020-02-05 18:06:39 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/version"
|
2017-03-06 14:29:39 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/util"
|
2020-04-28 11:46:47 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-03-06 14:29:39 +00:00
|
|
|
)
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryLatestEventsAndState implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryLatestEventsAndState(
|
2017-09-13 13:37:50 +01:00
|
|
|
ctx context.Context,
|
2017-03-06 14:29:39 +00:00
|
|
|
request *api.QueryLatestEventsAndStateRequest,
|
|
|
|
response *api.QueryLatestEventsAndStateResponse,
|
2017-06-02 14:32:36 +01:00
|
|
|
) error {
|
2020-03-19 18:33:04 +00:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
2020-02-05 16:25:58 +00:00
|
|
|
if err != nil {
|
2020-03-19 18:33:04 +00:00
|
|
|
response.RoomExists = false
|
|
|
|
return nil
|
2020-02-05 16:25:58 +00:00
|
|
|
}
|
2020-03-19 18:33:04 +00:00
|
|
|
|
|
|
|
roomState := state.NewStateResolution(r.DB)
|
|
|
|
|
2020-04-24 16:30:25 +01:00
|
|
|
roomNID, err := r.DB.RoomNIDExcludingStubs(ctx, request.RoomID)
|
2017-03-06 14:29:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
2020-03-27 16:28:22 +00:00
|
|
|
response.RoomVersion = roomVersion
|
2020-03-16 17:29:52 +00:00
|
|
|
|
2017-03-09 15:07:18 +00:00
|
|
|
var currentStateSnapshotNID types.StateSnapshotNID
|
2017-09-13 16:30:19 +01:00
|
|
|
response.LatestEvents, currentStateSnapshotNID, response.Depth, err =
|
|
|
|
r.DB.LatestEventIDs(ctx, roomNID)
|
2017-03-09 15:07:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-14 18:36:08 +01:00
|
|
|
var stateEntries []types.StateEntry
|
|
|
|
if len(request.StateToFetch) == 0 {
|
|
|
|
// Look up all room state.
|
|
|
|
stateEntries, err = roomState.LoadStateAtSnapshot(
|
|
|
|
ctx, currentStateSnapshotNID,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Look up the current state for the requested tuples.
|
|
|
|
stateEntries, err = roomState.LoadStateAtSnapshotForStringTuples(
|
|
|
|
ctx, currentStateSnapshotNID, request.StateToFetch,
|
|
|
|
)
|
|
|
|
}
|
2017-03-09 15:07:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:30:19 +01:00
|
|
|
stateEvents, err := r.loadStateEvents(ctx, stateEntries)
|
2017-05-30 17:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:29:52 +00:00
|
|
|
for _, event := range stateEvents {
|
2020-03-17 11:01:25 +00:00
|
|
|
response.StateEvents = append(response.StateEvents, event.Headered(roomVersion))
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:44:31 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryStateAfterEvents implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryStateAfterEvents(
|
2017-09-13 13:37:50 +01:00
|
|
|
ctx context.Context,
|
2017-05-30 17:44:31 +01:00
|
|
|
request *api.QueryStateAfterEventsRequest,
|
|
|
|
response *api.QueryStateAfterEventsResponse,
|
2017-06-02 14:32:36 +01:00
|
|
|
) error {
|
2020-03-19 18:33:04 +00:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
2020-02-05 16:25:58 +00:00
|
|
|
if err != nil {
|
2020-03-19 18:33:04 +00:00
|
|
|
response.RoomExists = false
|
|
|
|
return nil
|
2020-02-05 16:25:58 +00:00
|
|
|
}
|
2020-03-19 18:33:04 +00:00
|
|
|
|
|
|
|
roomState := state.NewStateResolution(r.DB)
|
|
|
|
|
2020-04-24 16:30:25 +01:00
|
|
|
roomNID, err := r.DB.RoomNIDExcludingStubs(ctx, request.RoomID)
|
2017-05-30 17:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
2020-03-27 16:28:22 +00:00
|
|
|
response.RoomVersion = roomVersion
|
2017-05-30 17:44:31 +01:00
|
|
|
|
2017-09-13 16:30:19 +01:00
|
|
|
prevStates, err := r.DB.StateAtEventIDs(ctx, request.PrevEventIDs)
|
2017-05-30 17:44:31 +01:00
|
|
|
if err != nil {
|
2017-06-07 14:32:53 +01:00
|
|
|
switch err.(type) {
|
|
|
|
case types.MissingEventError:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2017-05-30 17:44:31 +01:00
|
|
|
}
|
|
|
|
response.PrevEventsExist = true
|
|
|
|
|
2017-08-23 15:08:48 +01:00
|
|
|
// Look up the currrent state for the requested tuples.
|
2020-02-05 18:06:39 +00:00
|
|
|
stateEntries, err := roomState.LoadStateAfterEventsForStringTuples(
|
2020-03-19 18:33:04 +00:00
|
|
|
ctx, roomNID, prevStates, request.StateToFetch,
|
2017-09-13 16:30:19 +01:00
|
|
|
)
|
2017-05-30 17:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:30:19 +01:00
|
|
|
stateEvents, err := r.loadStateEvents(ctx, stateEntries)
|
2017-05-30 17:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:29:52 +00:00
|
|
|
for _, event := range stateEvents {
|
2020-03-17 11:01:25 +00:00
|
|
|
response.StateEvents = append(response.StateEvents, event.Headered(roomVersion))
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:44:31 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryEventsByID implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryEventsByID(
|
2017-09-13 13:37:50 +01:00
|
|
|
ctx context.Context,
|
2017-06-02 14:32:36 +01:00
|
|
|
request *api.QueryEventsByIDRequest,
|
|
|
|
response *api.QueryEventsByIDResponse,
|
|
|
|
) error {
|
2017-09-13 16:30:19 +01:00
|
|
|
eventNIDMap, err := r.DB.EventNIDs(ctx, request.EventIDs)
|
2017-06-02 14:32:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, nid := range eventNIDMap {
|
|
|
|
eventNIDs = append(eventNIDs, nid)
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:30:19 +01:00
|
|
|
events, err := r.loadEvents(ctx, eventNIDs)
|
2017-06-02 14:32:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:29:52 +00:00
|
|
|
for _, event := range events {
|
2020-03-17 18:00:10 +00:00
|
|
|
roomVersion, verr := r.DB.GetRoomVersionForRoom(ctx, event.RoomID())
|
|
|
|
if verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
2020-03-16 17:29:52 +00:00
|
|
|
|
2020-03-17 11:01:25 +00:00
|
|
|
response.Events = append(response.Events, event.Headered(roomVersion))
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 14:32:36 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) loadStateEvents(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context, stateEntries []types.StateEntry,
|
|
|
|
) ([]gomatrixserverlib.Event, error) {
|
2017-03-09 15:07:18 +00:00
|
|
|
eventNIDs := make([]types.EventNID, len(stateEntries))
|
|
|
|
for i := range stateEntries {
|
|
|
|
eventNIDs[i] = stateEntries[i].EventNID
|
|
|
|
}
|
2017-09-13 16:30:19 +01:00
|
|
|
return r.loadEvents(ctx, eventNIDs)
|
2017-06-02 14:32:36 +01:00
|
|
|
}
|
2017-03-09 15:07:18 +00:00
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) loadEvents(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context, eventNIDs []types.EventNID,
|
|
|
|
) ([]gomatrixserverlib.Event, error) {
|
|
|
|
stateEvents, err := r.DB.Events(ctx, eventNIDs)
|
2017-03-09 15:07:18 +00:00
|
|
|
if err != nil {
|
2017-05-30 17:44:31 +01:00
|
|
|
return nil, err
|
2017-03-09 15:07:18 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:44:31 +01:00
|
|
|
result := make([]gomatrixserverlib.Event, len(stateEvents))
|
2017-03-09 15:07:18 +00:00
|
|
|
for i := range stateEvents {
|
2017-05-30 17:44:31 +01:00
|
|
|
result[i] = stateEvents[i].Event
|
2017-03-09 15:07:18 +00:00
|
|
|
}
|
2017-05-30 17:44:31 +01:00
|
|
|
return result, nil
|
2017-03-06 14:29:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryMembershipForUser implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryMembershipForUser(
|
2018-07-11 11:10:37 +01:00
|
|
|
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
|
2020-06-24 18:19:54 +01:00
|
|
|
|
|
|
|
evs, err := r.DB.Events(ctx, []types.EventNID{membershipEventNID})
|
2018-07-11 11:10:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-24 18:19:54 +01:00
|
|
|
if len(evs) != 1 {
|
|
|
|
return fmt.Errorf("failed to load membership event for event NID %d", membershipEventNID)
|
|
|
|
}
|
2018-07-11 11:10:37 +01:00
|
|
|
|
2020-06-24 18:19:54 +01:00
|
|
|
response.EventID = evs[0].EventID()
|
|
|
|
response.Membership, err = evs[0].Membership()
|
|
|
|
return err
|
2018-07-11 11:10:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryMembershipsForRoom implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryMembershipsForRoom(
|
2017-09-13 13:37:50 +01:00
|
|
|
ctx context.Context,
|
2017-08-21 16:34:26 +01:00
|
|
|
request *api.QueryMembershipsForRoomRequest,
|
|
|
|
response *api.QueryMembershipsForRoomResponse,
|
|
|
|
) error {
|
2017-09-13 16:30:19 +01:00
|
|
|
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
2017-08-21 16:34:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:30:19 +01:00
|
|
|
membershipEventNID, stillInRoom, err := r.DB.GetMembership(ctx, roomNID, request.Sender)
|
2017-08-21 16:34:26 +01:00
|
|
|
if err != nil {
|
2018-07-11 13:16:38 +01:00
|
|
|
return err
|
2017-08-21 16:34:26 +01:00
|
|
|
}
|
|
|
|
|
2017-08-24 16:00:14 +01:00
|
|
|
if membershipEventNID == 0 {
|
2017-08-21 16:34:26 +01:00
|
|
|
response.HasBeenInRoom = false
|
|
|
|
response.JoinEvents = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
response.HasBeenInRoom = true
|
|
|
|
response.JoinEvents = []gomatrixserverlib.ClientEvent{}
|
2017-08-24 16:00:14 +01:00
|
|
|
|
|
|
|
var events []types.Event
|
2020-04-29 18:41:45 +01:00
|
|
|
var stateEntries []types.StateEntry
|
2017-08-24 16:00:14 +01:00
|
|
|
if stillInRoom {
|
|
|
|
var eventNIDs []types.EventNID
|
2020-05-20 18:03:06 +01:00
|
|
|
eventNIDs, err = r.DB.GetMembershipEventNIDsForRoom(ctx, roomNID, request.JoinedOnly, false)
|
2017-08-24 16:00:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:30:19 +01:00
|
|
|
events, err = r.DB.Events(ctx, eventNIDs)
|
2017-08-24 16:00:14 +01:00
|
|
|
} else {
|
2020-04-29 18:41:45 +01:00
|
|
|
stateEntries, err = stateBeforeEvent(ctx, r.DB, membershipEventNID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("membership_event_nid", membershipEventNID).WithError(err).Error("failed to load state before event")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
events, err = getMembershipsAtState(ctx, r.DB, stateEntries, request.JoinedOnly)
|
2017-08-24 16:00:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-21 16:34:26 +01:00
|
|
|
for _, event := range events {
|
|
|
|
clientEvent := gomatrixserverlib.ToClientEvent(event.Event, gomatrixserverlib.FormatAll)
|
|
|
|
response.JoinEvents = append(response.JoinEvents, clientEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:41:45 +01:00
|
|
|
func stateBeforeEvent(ctx context.Context, db storage.Database, eventNID types.EventNID) ([]types.StateEntry, error) {
|
2020-04-28 11:46:47 +01:00
|
|
|
roomState := state.NewStateResolution(db)
|
2017-08-24 16:00:14 +01:00
|
|
|
// Lookup the event NID
|
2020-04-28 11:46:47 +01:00
|
|
|
eIDs, err := db.EventIDs(ctx, []types.EventNID{eventNID})
|
2017-08-24 16:00:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eventIDs := []string{eIDs[eventNID]}
|
|
|
|
|
2020-04-28 11:46:47 +01:00
|
|
|
prevState, err := db.StateAtEventIDs(ctx, eventIDs)
|
2017-08-24 16:00:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the state as it was when this event was fired
|
2020-04-29 18:41:45 +01:00
|
|
|
return roomState.LoadCombinedStateAfterEvents(ctx, prevState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMembershipsAtState 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.
|
|
|
|
func getMembershipsAtState(
|
|
|
|
ctx context.Context, db storage.Database, stateEntries []types.StateEntry, joinedOnly bool,
|
|
|
|
) ([]types.Event, error) {
|
2017-08-24 16:00:14 +01:00
|
|
|
|
|
|
|
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
|
2020-04-28 11:46:47 +01:00
|
|
|
stateEvents, err := db.Events(ctx, eventNIDs)
|
2017-08-24 16:00:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !joinedOnly {
|
|
|
|
return stateEvents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the events to only keep the "join" membership events
|
2020-04-29 18:41:45 +01:00
|
|
|
var events []types.Event
|
2017-08-24 16:00:14 +01:00
|
|
|
for _, event := range stateEvents {
|
|
|
|
membership, err := event.Membership()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 15:07:36 +01:00
|
|
|
if membership == gomatrixserverlib.Join {
|
2017-08-24 16:00:14 +01:00
|
|
|
events = append(events, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryServerAllowedToSeeEvent implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryServerAllowedToSeeEvent(
|
2017-09-13 13:37:50 +01:00
|
|
|
ctx context.Context,
|
2017-09-06 12:38:22 +01:00
|
|
|
request *api.QueryServerAllowedToSeeEventRequest,
|
|
|
|
response *api.QueryServerAllowedToSeeEventResponse,
|
2018-06-26 11:25:49 +01:00
|
|
|
) (err error) {
|
2020-03-24 12:20:10 +00:00
|
|
|
events, err := r.DB.EventsFromIDs(ctx, []string{request.EventID})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
response.AllowedToSeeEvent = false // event doesn't exist so not allowed to see
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isServerInRoom, err := r.isServerCurrentlyInRoom(ctx, request.ServerName, events[0].RoomID())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-06-26 11:25:49 +01:00
|
|
|
response.AllowedToSeeEvent, err = r.checkServerAllowedToSeeEvent(
|
2020-03-24 12:20:10 +00:00
|
|
|
ctx, request.EventID, request.ServerName, isServerInRoom,
|
2018-06-26 11:25:49 +01:00
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) checkServerAllowedToSeeEvent(
|
2020-03-24 12:20:10 +00:00
|
|
|
ctx context.Context, eventID string, serverName gomatrixserverlib.ServerName, isServerInRoom bool,
|
2018-06-26 11:25:49 +01:00
|
|
|
) (bool, error) {
|
2020-03-19 18:33:04 +00:00
|
|
|
roomState := state.NewStateResolution(r.DB)
|
2020-02-05 18:06:39 +00:00
|
|
|
stateEntries, err := roomState.LoadStateAtEvent(ctx, eventID)
|
2017-09-06 12:38:22 +01:00
|
|
|
if err != nil {
|
2018-06-26 11:25:49 +01:00
|
|
|
return false, err
|
2017-09-06 12:38:22 +01: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 16:30:19 +01:00
|
|
|
stateAtEvent, err := r.loadStateEvents(ctx, stateEntries)
|
2017-09-06 12:38:22 +01:00
|
|
|
if err != nil {
|
2018-06-26 11:25:49 +01:00
|
|
|
return false, err
|
2017-09-06 12:38:22 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 12:20:10 +00:00
|
|
|
return auth.IsServerAllowed(serverName, isServerInRoom, stateAtEvent), nil
|
2018-06-26 11:25:49 +01:00
|
|
|
}
|
2017-09-06 12:38:22 +01:00
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryMissingEvents implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryMissingEvents(
|
2018-06-26 11:25:49 +01:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryMissingEventsRequest,
|
|
|
|
response *api.QueryMissingEventsResponse,
|
|
|
|
) error {
|
|
|
|
var front []string
|
2019-01-14 10:20:19 +00:00
|
|
|
eventsToFilter := make(map[string]bool, len(request.LatestEvents))
|
2018-06-26 11:25:49 +01: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 12:38:22 +01:00
|
|
|
|
2018-06-26 11:25:49 +01:00
|
|
|
for _, id := range request.LatestEvents {
|
|
|
|
if !visited[id] {
|
|
|
|
front = append(front, id)
|
2019-01-14 10:20:19 +00:00
|
|
|
eventsToFilter[id] = true
|
2017-09-06 12:38:22 +01:00
|
|
|
}
|
2018-06-26 11:25:49 +01:00
|
|
|
}
|
2017-09-06 12:38:22 +01:00
|
|
|
|
2018-11-07 11:38:01 +00:00
|
|
|
resultNIDs, err := r.scanEventTree(ctx, front, visited, request.Limit, request.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:20:19 +00:00
|
|
|
loadedEvents, err := r.loadEvents(ctx, resultNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:29:52 +00:00
|
|
|
response.Events = make([]gomatrixserverlib.HeaderedEvent, 0, len(loadedEvents)-len(eventsToFilter))
|
2019-01-14 10:20:19 +00:00
|
|
|
for _, event := range loadedEvents {
|
|
|
|
if !eventsToFilter[event.EventID()] {
|
2020-03-17 18:00:10 +00:00
|
|
|
roomVersion, verr := r.DB.GetRoomVersionForRoom(ctx, event.RoomID())
|
|
|
|
if verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
2020-03-16 17:29:52 +00:00
|
|
|
|
2020-03-17 11:01:25 +00:00
|
|
|
response.Events = append(response.Events, event.Headered(roomVersion))
|
2019-01-14 10:20:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:38:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:50:40 +01:00
|
|
|
// PerformBackfill implements api.RoomServerQueryAPI
|
|
|
|
func (r *RoomserverInternalAPI) PerformBackfill(
|
2018-11-07 11:38:01 +00:00
|
|
|
ctx context.Context,
|
2020-06-11 19:50:40 +01:00
|
|
|
request *api.PerformBackfillRequest,
|
|
|
|
response *api.PerformBackfillResponse,
|
2018-11-07 11:38:01 +00:00
|
|
|
) error {
|
2020-04-28 11:46:47 +01:00
|
|
|
// if we are requesting the backfill then we need to do a federation hit
|
|
|
|
// TODO: we could be more sensible and fetch as many events we already have then request the rest
|
|
|
|
// which is what the syncapi does already.
|
|
|
|
if request.ServerName == r.ServerName {
|
|
|
|
return r.backfillViaFederation(ctx, request, response)
|
|
|
|
}
|
|
|
|
// someone else is requesting the backfill, try to service their request.
|
2018-11-07 11:38:01 +00:00
|
|
|
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)
|
|
|
|
|
2020-05-20 16:04:31 +01:00
|
|
|
// this will include these events which is what we want
|
|
|
|
front = request.PrevEventIDs()
|
2018-11-07 11:38:01 +00:00
|
|
|
|
|
|
|
// 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 17:29:52 +00:00
|
|
|
var loadedEvents []gomatrixserverlib.Event
|
|
|
|
loadedEvents, err = r.loadEvents(ctx, resultNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range loadedEvents {
|
2020-03-17 18:00:10 +00:00
|
|
|
roomVersion, verr := r.DB.GetRoomVersionForRoom(ctx, event.RoomID())
|
|
|
|
if verr != nil {
|
|
|
|
return verr
|
|
|
|
}
|
2020-03-16 17:29:52 +00:00
|
|
|
|
2020-03-17 11:01:25 +00:00
|
|
|
response.Events = append(response.Events, event.Headered(roomVersion))
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:38:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:50:40 +01:00
|
|
|
func (r *RoomserverInternalAPI) backfillViaFederation(ctx context.Context, req *api.PerformBackfillRequest, res *api.PerformBackfillResponse) error {
|
2020-04-28 11:46:47 +01:00
|
|
|
roomVer, err := r.DB.GetRoomVersionForRoom(ctx, req.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("backfillViaFederation: unknown room version for room %s : %w", req.RoomID, err)
|
|
|
|
}
|
2020-05-20 16:04:31 +01:00
|
|
|
requester := newBackfillRequester(r.DB, r.FedClient, r.ServerName, req.BackwardsExtremities)
|
2020-05-19 18:42:55 +01:00
|
|
|
// Request 100 items regardless of what the query asks for.
|
|
|
|
// We don't want to go much higher than this.
|
|
|
|
// We can't honour exactly the limit as some sytests rely on requesting more for tests to pass
|
|
|
|
// (so we don't need to hit /state_ids which the test has no listener for)
|
|
|
|
// Specifically the test "Outbound federation can backfill events"
|
2020-04-28 11:46:47 +01:00
|
|
|
events, err := gomatrixserverlib.RequestBackfill(
|
|
|
|
ctx, requester,
|
2020-05-20 16:04:31 +01:00
|
|
|
r.KeyRing, req.RoomID, roomVer, req.PrevEventIDs(), 100)
|
2020-04-28 11:46:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.WithField("room_id", req.RoomID).Infof("backfilled %d events", len(events))
|
|
|
|
|
|
|
|
// persist these new events - auth checks have already been done
|
|
|
|
roomNID, backfilledEventMap := persistEvents(ctx, r.DB, events)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range backfilledEventMap {
|
|
|
|
// now add state for these events
|
|
|
|
stateIDs, ok := requester.eventIDToBeforeStateIDs[ev.EventID()]
|
|
|
|
if !ok {
|
|
|
|
// this should be impossible as all events returned must have pass Step 5 of the PDU checks
|
|
|
|
// which requires a list of state IDs.
|
2020-04-29 18:41:45 +01:00
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to find state IDs for event which passed auth checks")
|
2020-04-28 11:46:47 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
var entries []types.StateEntry
|
|
|
|
if entries, err = r.DB.StateEntriesForEventIDs(ctx, stateIDs); err != nil {
|
2020-04-29 18:41:45 +01:00
|
|
|
// attempt to fetch the missing events
|
|
|
|
r.fetchAndStoreMissingEvents(ctx, roomVer, requester, stateIDs)
|
|
|
|
// try again
|
|
|
|
entries, err = r.DB.StateEntriesForEventIDs(ctx, stateIDs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to get state entries for event")
|
|
|
|
return err
|
|
|
|
}
|
2020-04-28 11:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var beforeStateSnapshotNID types.StateSnapshotNID
|
|
|
|
if beforeStateSnapshotNID, err = r.DB.AddState(ctx, roomNID, nil, entries); err != nil {
|
2020-04-29 18:41:45 +01:00
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to persist state entries to get snapshot nid")
|
2020-04-28 11:46:47 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = r.DB.SetState(ctx, ev.EventNID, beforeStateSnapshotNID); err != nil {
|
2020-04-29 18:41:45 +01:00
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("backfillViaFederation: failed to persist snapshot nid")
|
2020-04-28 11:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: update backwards extremities, as that should be moved from syncapi to roomserver at some point.
|
|
|
|
|
|
|
|
res.Events = events
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) isServerCurrentlyInRoom(ctx context.Context, serverName gomatrixserverlib.ServerName, roomID string) (bool, error) {
|
2020-03-24 12:20:10 +00:00
|
|
|
roomNID, err := r.DB.RoomNID(ctx, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 18:03:06 +01:00
|
|
|
eventNIDs, err := r.DB.GetMembershipEventNIDsForRoom(ctx, roomNID, true, false)
|
2020-03-24 12:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := r.DB.Events(ctx, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
gmslEvents := make([]gomatrixserverlib.Event, len(events))
|
|
|
|
for i := range events {
|
|
|
|
gmslEvents[i] = events[i].Event
|
|
|
|
}
|
|
|
|
return auth.IsAnyUserOnServerWithMembership(serverName, gmslEvents, gomatrixserverlib.Join), nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:41:45 +01:00
|
|
|
// fetchAndStoreMissingEvents does a best-effort fetch and store of missing events specified in stateIDs. Returns no error as it is just
|
|
|
|
// best effort.
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) fetchAndStoreMissingEvents(ctx context.Context, roomVer gomatrixserverlib.RoomVersion,
|
2020-04-29 18:41:45 +01:00
|
|
|
backfillRequester *backfillRequester, stateIDs []string) {
|
|
|
|
|
|
|
|
servers := backfillRequester.servers
|
|
|
|
|
|
|
|
// work out which are missing
|
|
|
|
nidMap, err := r.DB.EventNIDs(ctx, stateIDs)
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Warn("cannot query missing events")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
missingMap := make(map[string]*gomatrixserverlib.HeaderedEvent) // id -> event
|
|
|
|
for _, id := range stateIDs {
|
|
|
|
if _, ok := nidMap[id]; !ok {
|
|
|
|
missingMap[id] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
util.GetLogger(ctx).Infof("Fetching %d missing state events (from %d possible servers)", len(missingMap), len(servers))
|
|
|
|
|
|
|
|
// fetch the events from federation. Loop the servers first so if we find one that works we stick with them
|
|
|
|
for _, srv := range servers {
|
|
|
|
for id, ev := range missingMap {
|
|
|
|
if ev != nil {
|
|
|
|
continue // already found
|
|
|
|
}
|
|
|
|
logger := util.GetLogger(ctx).WithField("server", srv).WithField("event_id", id)
|
|
|
|
res, err := r.FedClient.GetEvent(ctx, srv, id)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Warn("failed to get event from server")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
loader := gomatrixserverlib.NewEventsLoader(roomVer, r.KeyRing, backfillRequester, backfillRequester.ProvideEvents, false)
|
|
|
|
result, err := loader.LoadAndVerify(ctx, res.PDUs, gomatrixserverlib.TopologicalOrderByPrevEvents)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Warn("failed to load and verify event")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.Infof("returned %d PDUs which made events %+v", len(res.PDUs), result)
|
|
|
|
for _, res := range result {
|
|
|
|
if res.Error != nil {
|
|
|
|
logger.WithError(err).Warn("event failed PDU checks")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
missingMap[id] = res.Event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newEvents []gomatrixserverlib.HeaderedEvent
|
|
|
|
for _, ev := range missingMap {
|
|
|
|
if ev != nil {
|
|
|
|
newEvents = append(newEvents, *ev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
util.GetLogger(ctx).Infof("Persisting %d new events", len(newEvents))
|
|
|
|
persistEvents(ctx, r.DB, newEvents)
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:20:10 +00:00
|
|
|
// TODO: Remove this when we have tests to assert correctness of this function
|
|
|
|
// nolint:gocyclo
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) scanEventTree(
|
2018-11-07 11:38:01 +00:00
|
|
|
ctx context.Context, front []string, visited map[string]bool, limit int,
|
|
|
|
serverName gomatrixserverlib.ServerName,
|
2020-03-24 12:20:10 +00:00
|
|
|
) ([]types.EventNID, error) {
|
|
|
|
var resultNIDs []types.EventNID
|
|
|
|
var err error
|
2018-11-07 11:38:01 +00:00
|
|
|
var allowed bool
|
|
|
|
var events []types.Event
|
|
|
|
var next []string
|
|
|
|
var pre string
|
|
|
|
|
2020-03-24 12:20:10 +00:00
|
|
|
// TODO: add tests for this function to ensure it meets the contract that callers expect (and doc what that is supposed to be)
|
2020-06-11 19:50:40 +01:00
|
|
|
// Currently, callers like PerformBackfill will call scanEventTree with a pre-populated `visited` map, assuming that by doing
|
2020-03-24 12:20:10 +00:00
|
|
|
// so means that the events in that map will NOT be returned from this function. That is not currently true, resulting in
|
|
|
|
// duplicate events being sent in response to /backfill requests.
|
|
|
|
initialIgnoreList := make(map[string]bool, len(visited))
|
|
|
|
for k, v := range visited {
|
|
|
|
initialIgnoreList[k] = v
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:38:01 +00:00
|
|
|
resultNIDs = make([]types.EventNID, 0, limit)
|
|
|
|
|
2020-03-24 12:20:10 +00:00
|
|
|
var checkedServerInRoom bool
|
|
|
|
var isServerInRoom bool
|
|
|
|
|
2018-11-07 11:38:01 +00:00
|
|
|
// 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 11:25:49 +01:00
|
|
|
BFSLoop:
|
|
|
|
for len(front) > 0 {
|
2018-11-07 11:38:01 +00: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 12:38:22 +01:00
|
|
|
if err != nil {
|
2020-03-24 12:20:10 +00:00
|
|
|
return resultNIDs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkedServerInRoom && len(events) > 0 {
|
|
|
|
// It's nasty that we have to extract the room ID from an event, but many federation requests
|
|
|
|
// only talk in event IDs, no room IDs at all (!!!)
|
|
|
|
ev := events[0]
|
|
|
|
isServerInRoom, err = r.isServerCurrentlyInRoom(ctx, serverName, ev.RoomID())
|
|
|
|
if err != nil {
|
|
|
|
util.GetLogger(ctx).WithError(err).Error("Failed to check if server is currently in room, assuming not.")
|
|
|
|
}
|
|
|
|
checkedServerInRoom = true
|
2017-09-06 12:38:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-26 11:25:49 +01:00
|
|
|
for _, ev := range events {
|
2018-11-07 11:38:01 +00:00
|
|
|
// Break out of the loop if the provided limit is reached.
|
|
|
|
if len(resultNIDs) == limit {
|
2018-06-26 11:25:49 +01:00
|
|
|
break BFSLoop
|
|
|
|
}
|
2020-03-24 12:20:10 +00:00
|
|
|
|
|
|
|
if !initialIgnoreList[ev.EventID()] {
|
|
|
|
// Update the list of events to retrieve.
|
|
|
|
resultNIDs = append(resultNIDs, ev.EventNID)
|
|
|
|
}
|
2018-11-07 11:38:01 +00: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 11:25:49 +01:00
|
|
|
if !visited[pre] {
|
|
|
|
visited[pre] = true
|
2020-03-24 12:20:10 +00:00
|
|
|
allowed, err = r.checkServerAllowedToSeeEvent(ctx, pre, serverName, isServerInRoom)
|
2018-06-26 11:25:49 +01:00
|
|
|
if err != nil {
|
2020-03-24 12:20:10 +00:00
|
|
|
util.GetLogger(ctx).WithField("server", serverName).WithField("event_id", pre).WithError(err).Error(
|
|
|
|
"Error checking if allowed to see event",
|
|
|
|
)
|
|
|
|
return resultNIDs, err
|
2018-06-26 11:25:49 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:38:01 +00: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 11:25:49 +01:00
|
|
|
if allowed {
|
|
|
|
next = append(next, pre)
|
2020-03-24 12:20:10 +00:00
|
|
|
} else {
|
|
|
|
util.GetLogger(ctx).WithField("server", serverName).WithField("event_id", pre).Info("Not allowed to see event")
|
2018-06-26 11:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-06 12:38:22 +01:00
|
|
|
}
|
2018-11-07 11:38:01 +00:00
|
|
|
// Repeat the same process with the parent events we just processed.
|
2018-06-26 11:25:49 +01:00
|
|
|
front = next
|
2017-09-06 12:38:22 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 12:20:10 +00:00
|
|
|
return resultNIDs, err
|
2017-09-06 12:38:22 +01:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryStateAndAuthChain implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryStateAndAuthChain(
|
2017-11-27 10:20:00 +00:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryStateAndAuthChainRequest,
|
|
|
|
response *api.QueryStateAndAuthChainResponse,
|
|
|
|
) error {
|
2020-04-24 16:30:25 +01:00
|
|
|
roomNID, err := r.DB.RoomNIDExcludingStubs(ctx, request.RoomID)
|
2017-11-27 10:20:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if roomNID == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
response.RoomExists = true
|
|
|
|
|
2020-03-17 18:00:10 +00:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-27 16:28:22 +00:00
|
|
|
response.RoomVersion = roomVersion
|
2020-03-17 18:00:10 +00:00
|
|
|
|
2020-03-17 17:18:48 +00:00
|
|
|
stateEvents, err := r.loadStateAtEventIDs(ctx, request.PrevEventIDs)
|
2017-11-27 10:20:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-17 17:18:48 +00:00
|
|
|
response.PrevEventsExist = true
|
2017-11-27 10:20:00 +00:00
|
|
|
|
2020-03-17 17:18:48 +00: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 10:20:00 +00:00
|
|
|
}
|
2020-03-17 17:18:48 +00:00
|
|
|
authEventIDs = util.UniqueStrings(authEventIDs) // de-dupe
|
2017-11-27 10:20:00 +00:00
|
|
|
|
2020-04-24 10:38:58 +01:00
|
|
|
authEvents, err := getAuthChain(ctx, r.DB.EventsFromIDs, authEventIDs)
|
2020-03-16 17:29:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
Federation for v3/v4 rooms (#954)
* Update gomatrixserverlib
* Default to room version 4
* Update gomatrixserverlib
* Limit prev_events and auth_events
* Fix auth_events, prev_events
* Fix linter issues
* Update gomatrixserverlib
* Fix getState
* Update sytest-whitelist
* Squashed commit of the following:
commit 067b87506357c996fd6ddb11271db9469ad4ce80
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date: Fri Apr 3 14:29:06 2020 +0100
Invites v2 endpoint (#952)
* Start converting v1 invite endpoint to v2
* Update gomatrixserverlib
* Early federationsender code for sending invites
* Sending invites sorta happens now
* Populate invite request with stripped state
* Remodel a bit, don't reflect received invites
* Handle invite_room_state
* Handle room versions a bit better
* Update gomatrixserverlib
* Tweak order in destinationQueue.next
* Revert check in processMessage
* Tweak federation sender destination queue code a bit
* Add comments
commit 955244c09298d0e6c870377dad3af2ffa1f5e578
Author: Ben B <benne@klimlive.de>
Date: Fri Apr 3 12:40:50 2020 +0200
use custom http client instead of the http DefaultClient (#823)
This commit replaces the default client from the http lib with a custom one.
The previously used default client doesn't come with a timeout. This could cause
unwanted locks.
That solution chosen here creates a http client in the base component dendrite
with a constant timeout of 30 seconds. If it should be necessary to overwrite
this, we could include the timeout in the dendrite configuration.
Here it would be a good idea to extend the type "Address" by a timeout and
create an http client for each service.
Closes #820
Signed-off-by: Benedikt Bongartz <benne@klimlive.de>
Co-authored-by: Kegsay <kegan@matrix.org>
* Update sytest-whitelist, sytest-blacklist
* Update go.mod/go.sum
* Add some error wrapping for debug
* Add a NOTSPEC to common/events.go
* Perform state resolution at send_join
* Set default room version to v2 again
* Tweak GetCapabilities
* Add comments to ResolveConflictsAdhoc
* Update sytest-blacklist
* go mod tidy
* Update sytest-whitelist, sytest-blacklist
* Update versions
* Updates from review comments
* Update sytest-blacklist, sytest-whitelist
* Check room versions compatible at make_join, add some comments, update gomatrixserverlib, other tweaks
* Set default room version back to v2
* Update gomatrixserverlib, sytest-whitelist
2020-04-09 15:46:06 +01:00
|
|
|
if request.ResolveState {
|
|
|
|
if stateEvents, err = state.ResolveConflictsAdhoc(
|
|
|
|
roomVersion, stateEvents, authEvents,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 17:29:52 +00:00
|
|
|
for _, event := range stateEvents {
|
2020-03-17 11:01:25 +00:00
|
|
|
response.StateEvents = append(response.StateEvents, event.Headered(roomVersion))
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range authEvents {
|
2020-03-17 11:01:25 +00:00
|
|
|
response.AuthChainEvents = append(response.AuthChainEvents, event.Headered(roomVersion))
|
2020-03-16 17:29:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:20:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
func (r *RoomserverInternalAPI) loadStateAtEventIDs(ctx context.Context, eventIDs []string) ([]gomatrixserverlib.Event, error) {
|
2020-03-19 18:33:04 +00:00
|
|
|
roomState := state.NewStateResolution(r.DB)
|
2020-03-17 17:18:48 +00: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-04-24 10:38:58 +01:00
|
|
|
type eventsFromIDs func(context.Context, []string) ([]types.Event, error)
|
|
|
|
|
2020-01-10 12:11:44 +00: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 10:20:00 +00:00
|
|
|
func getAuthChain(
|
2020-04-24 10:38:58 +01:00
|
|
|
ctx context.Context, fn eventsFromIDs, authEventIDs []string,
|
2017-11-27 10:20:00 +00:00
|
|
|
) ([]gomatrixserverlib.Event, error) {
|
2020-01-10 12:11:44 +00: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 10:20:00 +00:00
|
|
|
eventsToFetch := authEventIDs
|
2020-01-10 12:11:44 +00:00
|
|
|
authEventsMap := make(map[string]gomatrixserverlib.Event)
|
2017-11-27 10:20:00 +00:00
|
|
|
|
|
|
|
for len(eventsToFetch) > 0 {
|
2020-01-10 12:11:44 +00:00
|
|
|
// Try to retrieve the events from the database.
|
2020-04-24 10:38:58 +01:00
|
|
|
events, err := fn(ctx, eventsToFetch)
|
2017-11-27 10:20:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:11:44 +00: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 10:20:00 +00:00
|
|
|
eventsToFetch = eventsToFetch[:0]
|
2020-01-10 12:11:44 +00:00
|
|
|
|
2017-11-27 10:20:00 +00:00
|
|
|
for _, event := range events {
|
2020-01-10 12:11:44 +00: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 10:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:11:44 +00: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 10:20:00 +00:00
|
|
|
return authEvents, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:46:47 +01:00
|
|
|
func persistEvents(ctx context.Context, db storage.Database, events []gomatrixserverlib.HeaderedEvent) (types.RoomNID, map[string]types.Event) {
|
|
|
|
var roomNID types.RoomNID
|
|
|
|
backfilledEventMap := make(map[string]types.Event)
|
|
|
|
for _, ev := range events {
|
|
|
|
nidMap, err := db.EventNIDs(ctx, ev.AuthEventIDs())
|
|
|
|
if err != nil { // this shouldn't happen as RequestBackfill already found them
|
|
|
|
logrus.WithError(err).WithField("auth_events", ev.AuthEventIDs()).Error("Failed to find one or more auth events")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
authNids := make([]types.EventNID, len(nidMap))
|
|
|
|
i := 0
|
|
|
|
for _, nid := range nidMap {
|
|
|
|
authNids[i] = nid
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
var stateAtEvent types.StateAtEvent
|
|
|
|
roomNID, stateAtEvent, err = db.StoreEvent(ctx, ev.Unwrap(), nil, authNids)
|
|
|
|
if err != nil {
|
2020-04-29 18:41:45 +01:00
|
|
|
logrus.WithError(err).WithField("event_id", ev.EventID()).Error("Failed to persist event")
|
2020-04-28 11:46:47 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
backfilledEventMap[ev.EventID()] = types.Event{
|
|
|
|
EventNID: stateAtEvent.StateEntry.EventNID,
|
|
|
|
Event: ev.Unwrap(),
|
|
|
|
}
|
2020-01-23 17:51:10 +00:00
|
|
|
}
|
2020-04-28 11:46:47 +01:00
|
|
|
return roomNID, backfilledEventMap
|
2020-01-23 17:51:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryRoomVersionCapabilities implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryRoomVersionCapabilities(
|
2020-02-05 18:06:39 +00:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryRoomVersionCapabilitiesRequest,
|
|
|
|
response *api.QueryRoomVersionCapabilitiesResponse,
|
|
|
|
) error {
|
2020-03-19 12:07:01 +00:00
|
|
|
response.DefaultRoomVersion = version.DefaultRoomVersion()
|
|
|
|
response.AvailableRoomVersions = make(map[gomatrixserverlib.RoomVersion]string)
|
2020-03-16 16:05:29 +00:00
|
|
|
for v, desc := range version.SupportedRoomVersions() {
|
2020-02-05 18:06:39 +00:00
|
|
|
if desc.Stable {
|
2020-03-19 12:07:01 +00:00
|
|
|
response.AvailableRoomVersions[v] = "stable"
|
2020-02-05 18:06:39 +00:00
|
|
|
} else {
|
2020-03-19 12:07:01 +00:00
|
|
|
response.AvailableRoomVersions[v] = "unstable"
|
2020-02-05 18:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
// QueryRoomVersionCapabilities implements api.RoomserverInternalAPI
|
|
|
|
func (r *RoomserverInternalAPI) QueryRoomVersionForRoom(
|
2020-03-27 16:28:22 +00:00
|
|
|
ctx context.Context,
|
|
|
|
request *api.QueryRoomVersionForRoomRequest,
|
|
|
|
response *api.QueryRoomVersionForRoomResponse,
|
|
|
|
) error {
|
2020-06-05 16:42:01 +01:00
|
|
|
if roomVersion, ok := r.Cache.GetRoomVersion(request.RoomID); ok {
|
2020-04-22 13:00:05 +01:00
|
|
|
response.RoomVersion = roomVersion
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 16:28:22 +00:00
|
|
|
roomVersion, err := r.DB.GetRoomVersionForRoom(ctx, request.RoomID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
response.RoomVersion = roomVersion
|
2020-06-05 16:42:01 +01:00
|
|
|
r.Cache.StoreRoomVersion(request.RoomID, response.RoomVersion)
|
2020-03-27 16:28:22 +00:00
|
|
|
return nil
|
|
|
|
}
|