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-09-02 17:13:15 +01:00
|
|
|
package input
|
2017-02-21 14:50:30 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-13 16:30:19 +01:00
|
|
|
"context"
|
2020-06-11 19:50:40 +01:00
|
|
|
"fmt"
|
2017-07-12 10:46:29 +01:00
|
|
|
|
2017-02-27 11:25:35 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-03-08 15:10:26 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2020-08-19 13:24:54 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
2017-02-21 14:50:30 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-06-27 13:20:04 +01:00
|
|
|
"github.com/matrix-org/util"
|
2017-02-21 14:50:30 +00:00
|
|
|
)
|
|
|
|
|
2017-02-27 11:25:35 +00:00
|
|
|
// updateLatestEvents updates the list of latest events for this room in the database and writes the
|
|
|
|
// event to the output log.
|
2017-02-21 14:50:30 +00:00
|
|
|
// The latest events are the events that aren't referenced by another event in the database:
|
|
|
|
//
|
|
|
|
// Time goes down the page. 1 is the m.room.create event (root).
|
|
|
|
//
|
|
|
|
// 1 After storing 1 the latest events are {1}
|
|
|
|
// | After storing 2 the latest events are {2}
|
|
|
|
// 2 After storing 3 the latest events are {3}
|
|
|
|
// / \ After storing 4 the latest events are {3,4}
|
|
|
|
// 3 4 After storing 5 the latest events are {5,4}
|
|
|
|
// | | After storing 6 the latest events are {5,6}
|
|
|
|
// 5 6 <--- latest After storing 7 the latest events are {6,7}
|
|
|
|
// |
|
|
|
|
// 7 <----- latest
|
|
|
|
//
|
2017-12-15 15:22:06 +00:00
|
|
|
// Can only be called once at a time
|
2020-09-02 17:13:15 +01:00
|
|
|
func (r *Inputer) updateLatestEvents(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context,
|
2022-02-04 10:39:34 +00:00
|
|
|
updater *shared.RoomUpdater,
|
2020-09-02 10:02:48 +01:00
|
|
|
roomInfo *types.RoomInfo,
|
2017-06-27 15:28:44 +01:00
|
|
|
stateAtEvent types.StateAtEvent,
|
2020-11-16 15:44:53 +00:00
|
|
|
event *gomatrixserverlib.Event,
|
2017-06-27 15:28:44 +01:00
|
|
|
sendAsServer string,
|
2017-12-04 18:07:52 +00:00
|
|
|
transactionID *api.TransactionID,
|
2020-09-15 11:17:46 +01:00
|
|
|
rewritesState bool,
|
2017-02-21 14:50:30 +00:00
|
|
|
) (err error) {
|
2017-08-08 16:38:03 +01:00
|
|
|
u := latestEventsUpdater{
|
2020-05-18 17:49:24 +01:00
|
|
|
ctx: ctx,
|
2020-05-20 18:03:06 +01:00
|
|
|
api: r,
|
2020-05-18 17:49:24 +01:00
|
|
|
updater: updater,
|
2020-09-02 10:02:48 +01:00
|
|
|
roomInfo: roomInfo,
|
2020-05-18 17:49:24 +01:00
|
|
|
stateAtEvent: stateAtEvent,
|
|
|
|
event: event,
|
|
|
|
sendAsServer: sendAsServer,
|
2017-12-04 18:07:52 +00:00
|
|
|
transactionID: transactionID,
|
2020-09-15 11:17:46 +01:00
|
|
|
rewritesState: rewritesState,
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
2020-05-18 17:49:24 +01:00
|
|
|
|
2017-08-21 16:37:11 +01:00
|
|
|
if err = u.doUpdateLatestEvents(); err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("u.doUpdateLatestEvents: %w", err)
|
2017-08-21 16:37:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 16:38:03 +01:00
|
|
|
// latestEventsUpdater tracks the state used to update the latest events in the
|
|
|
|
// room. It mostly just ferries state between the various function calls.
|
|
|
|
// The state could be passed using function arguments, but it becomes impractical
|
|
|
|
// when there are so many variables to pass around.
|
|
|
|
type latestEventsUpdater struct {
|
2017-12-04 18:07:52 +00:00
|
|
|
ctx context.Context
|
2020-09-02 17:13:15 +01:00
|
|
|
api *Inputer
|
2022-02-04 10:39:34 +00:00
|
|
|
updater *shared.RoomUpdater
|
2020-09-02 10:02:48 +01:00
|
|
|
roomInfo *types.RoomInfo
|
2017-12-04 18:07:52 +00:00
|
|
|
stateAtEvent types.StateAtEvent
|
2020-11-16 15:44:53 +00:00
|
|
|
event *gomatrixserverlib.Event
|
2017-12-04 18:07:52 +00:00
|
|
|
transactionID *api.TransactionID
|
2020-09-15 11:17:46 +01:00
|
|
|
rewritesState bool
|
2017-08-08 16:38:03 +01:00
|
|
|
// Which server to send this event as.
|
|
|
|
sendAsServer string
|
|
|
|
// The eventID of the event that was processed before this one.
|
|
|
|
lastEventIDSent string
|
|
|
|
// The latest events in the room after processing this event.
|
2021-01-18 13:21:33 +00:00
|
|
|
oldLatest []types.StateAtEventAndReference
|
|
|
|
latest []types.StateAtEventAndReference
|
2017-08-08 16:38:03 +01:00
|
|
|
// The state entries removed from and added to the current state of the
|
|
|
|
// room as a result of processing this event. They are sorted lists.
|
|
|
|
removed []types.StateEntry
|
|
|
|
added []types.StateEntry
|
|
|
|
// The state entries that are removed and added to recover the state before
|
|
|
|
// the event being processed. They are sorted lists.
|
|
|
|
stateBeforeEventRemoves []types.StateEntry
|
|
|
|
stateBeforeEventAdds []types.StateEntry
|
|
|
|
// The snapshots of current state before and after processing this event
|
|
|
|
oldStateNID types.StateSnapshotNID
|
|
|
|
newStateNID types.StateSnapshotNID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *latestEventsUpdater) doUpdateLatestEvents() error {
|
|
|
|
u.lastEventIDSent = u.updater.LastEventIDSent()
|
2017-02-21 14:50:30 +00:00
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// If we are doing a regular event update then we will get the
|
|
|
|
// previous latest events to use as a part of the calculation. If
|
|
|
|
// we are overwriting the latest events because we have a complete
|
|
|
|
// state snapshot from somewhere else, e.g. a federated room join,
|
|
|
|
// then start with an empty set - none of the forward extremities
|
|
|
|
// that we knew about before matter anymore.
|
2021-01-18 13:21:33 +00:00
|
|
|
u.oldLatest = []types.StateAtEventAndReference{}
|
2020-10-22 10:39:16 +01:00
|
|
|
if !u.rewritesState {
|
|
|
|
u.oldStateNID = u.updater.CurrentStateSnapshotNID()
|
2021-01-18 13:21:33 +00:00
|
|
|
u.oldLatest = u.updater.LatestEvents()
|
2020-05-18 17:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the event has already been written to the output log then we
|
|
|
|
// don't need to do anything, as we've handled it already.
|
2020-10-14 12:39:37 +01:00
|
|
|
if hasBeenSent, err := u.updater.HasEventBeenSent(u.stateAtEvent.EventNID); err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("u.updater.HasEventBeenSent: %w", err)
|
2017-02-27 11:25:35 +00:00
|
|
|
} else if hasBeenSent {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:05:33 +01:00
|
|
|
// Work out what the latest events are. This will include the new
|
|
|
|
// event if it is not already referenced.
|
2020-10-21 15:37:07 +01:00
|
|
|
extremitiesChanged, err := u.calculateLatest(
|
2021-01-18 13:21:33 +00:00
|
|
|
u.oldLatest, u.event,
|
2020-05-18 17:49:24 +01:00
|
|
|
types.StateAtEventAndReference{
|
2020-10-07 14:05:33 +01:00
|
|
|
EventReference: u.event.EventReference(),
|
2020-05-18 17:49:24 +01:00
|
|
|
StateAtEvent: u.stateAtEvent,
|
|
|
|
},
|
2020-10-21 15:37:07 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
2020-10-14 12:39:37 +01:00
|
|
|
return fmt.Errorf("u.calculateLatest: %w", err)
|
|
|
|
}
|
2017-02-27 11:25:35 +00:00
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// Now that we know what the latest events are, it's time to get the
|
|
|
|
// latest state.
|
2020-10-21 15:37:07 +01:00
|
|
|
var updates []api.OutputEvent
|
2020-10-22 10:39:16 +01:00
|
|
|
if extremitiesChanged || u.rewritesState {
|
2020-10-21 15:37:07 +01:00
|
|
|
if err = u.latestState(); err != nil {
|
|
|
|
return fmt.Errorf("u.latestState: %w", err)
|
|
|
|
}
|
2017-03-07 10:25:01 +00:00
|
|
|
|
2020-10-21 15:37:07 +01:00
|
|
|
// If we need to generate any output events then here's where we do it.
|
|
|
|
// TODO: Move this!
|
|
|
|
if updates, err = u.api.updateMemberships(u.ctx, u.updater, u.removed, u.added); err != nil {
|
|
|
|
return fmt.Errorf("u.api.updateMemberships: %w", err)
|
|
|
|
}
|
2020-10-21 16:21:36 +01:00
|
|
|
} else {
|
|
|
|
u.newStateNID = u.oldStateNID
|
2017-03-07 10:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:59:13 +01:00
|
|
|
update, err := u.makeOutputNewRoomEvent()
|
2017-06-27 13:20:04 +01:00
|
|
|
if err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("u.makeOutputNewRoomEvent: %w", err)
|
2017-06-27 13:20:04 +01:00
|
|
|
}
|
2017-08-08 16:38:03 +01:00
|
|
|
updates = append(updates, *update)
|
2017-06-27 13:20:04 +01:00
|
|
|
|
2017-02-27 11:25:35 +00:00
|
|
|
// Send the event to the output logs.
|
|
|
|
// We do this inside the database transaction to ensure that we only mark an event as sent if we sent it.
|
|
|
|
// (n.b. this means that it's possible that the same event will be sent twice if the transaction fails but
|
|
|
|
// the write to the output log succeeds)
|
|
|
|
// TODO: This assumes that writing the event to the output log is synchronous. It should be possible to
|
|
|
|
// send the event asynchronously but we would need to ensure that 1) the events are written to the log in
|
|
|
|
// the correct order, 2) that pending writes are resent across restarts. In order to avoid writing all the
|
|
|
|
// necessary bookkeeping we'll keep the event sending synchronous for now.
|
2020-05-20 18:03:06 +01:00
|
|
|
if err = u.api.WriteOutputEvents(u.event.RoomID(), updates); err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("u.api.WriteOutputEvents: %w", err)
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 10:02:48 +01:00
|
|
|
if err = u.updater.SetLatestEvents(u.roomInfo.RoomNID, u.latest, u.stateAtEvent.EventNID, u.newStateNID); err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("u.updater.SetLatestEvents: %w", err)
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 15:38:27 +01:00
|
|
|
if err = u.updater.MarkEventAsSent(u.stateAtEvent.EventNID); err != nil {
|
|
|
|
return fmt.Errorf("u.updater.MarkEventAsSent: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 16:38:03 +01:00
|
|
|
func (u *latestEventsUpdater) latestState() error {
|
|
|
|
var err error
|
2022-02-04 10:39:34 +00:00
|
|
|
roomState := state.NewStateResolution(u.updater, u.roomInfo)
|
2017-08-08 16:38:03 +01:00
|
|
|
|
2021-01-18 13:21:33 +00:00
|
|
|
// Work out if the state at the extremities has actually changed
|
|
|
|
// or not. If they haven't then we won't bother doing all of the
|
|
|
|
// hard work.
|
|
|
|
if u.event.StateKey() == nil {
|
|
|
|
stateChanged := false
|
|
|
|
oldStateNIDs := make([]types.StateSnapshotNID, 0, len(u.oldLatest))
|
|
|
|
newStateNIDs := make([]types.StateSnapshotNID, 0, len(u.latest))
|
|
|
|
for _, old := range u.oldLatest {
|
|
|
|
oldStateNIDs = append(oldStateNIDs, old.BeforeStateSnapshotNID)
|
|
|
|
}
|
|
|
|
for _, new := range u.latest {
|
|
|
|
newStateNIDs = append(newStateNIDs, new.BeforeStateSnapshotNID)
|
|
|
|
}
|
|
|
|
oldStateNIDs = state.UniqueStateSnapshotNIDs(oldStateNIDs)
|
|
|
|
newStateNIDs = state.UniqueStateSnapshotNIDs(newStateNIDs)
|
|
|
|
if len(oldStateNIDs) != len(newStateNIDs) {
|
|
|
|
stateChanged = true
|
|
|
|
} else {
|
|
|
|
for i := range oldStateNIDs {
|
|
|
|
if oldStateNIDs[i] != newStateNIDs[i] {
|
|
|
|
stateChanged = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !stateChanged {
|
|
|
|
u.newStateNID = u.oldStateNID
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 14:05:33 +01:00
|
|
|
// Get a list of the current latest events. This may or may not
|
|
|
|
// include the new event from the input path, depending on whether
|
|
|
|
// it is a forward extremity or not.
|
|
|
|
latestStateAtEvents := make([]types.StateAtEvent, len(u.latest))
|
2017-08-08 16:38:03 +01:00
|
|
|
for i := range u.latest {
|
2020-10-07 14:05:33 +01:00
|
|
|
latestStateAtEvents[i] = u.latest[i].StateAtEvent
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
2020-05-18 17:49:24 +01:00
|
|
|
|
|
|
|
// Takes the NIDs of the latest events and creates a state snapshot
|
|
|
|
// of the state after the events. The snapshot state will be resolved
|
|
|
|
// using the correct state resolution algorithm for the room.
|
2020-02-05 18:06:39 +00:00
|
|
|
u.newStateNID, err = roomState.CalculateAndStoreStateAfterEvents(
|
2020-09-02 10:02:48 +01:00
|
|
|
u.ctx, latestStateAtEvents,
|
2017-09-13 16:30:19 +01:00
|
|
|
)
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("roomState.CalculateAndStoreStateAfterEvents: %w", err)
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// If we are overwriting the state then we should make sure that we
|
|
|
|
// don't send anything out over federation again, it will very likely
|
|
|
|
// be a repeat.
|
|
|
|
if u.stateAtEvent.Overwrite {
|
|
|
|
u.sendAsServer = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have a new state snapshot based on the latest events,
|
|
|
|
// we can compare that new snapshot to the previous one and see what
|
|
|
|
// has changed. This gives us one list of removed state events and
|
|
|
|
// another list of added ones. Replacing a value for a state-key tuple
|
|
|
|
// will result one removed (the old event) and one added (the new event).
|
2020-02-05 18:06:39 +00:00
|
|
|
u.removed, u.added, err = roomState.DifferenceBetweeenStateSnapshots(
|
2020-02-05 16:25:58 +00:00
|
|
|
u.ctx, u.oldStateNID, u.newStateNID,
|
2017-09-13 16:30:19 +01:00
|
|
|
)
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
2020-08-19 15:38:27 +01:00
|
|
|
return fmt.Errorf("roomState.DifferenceBetweenStateSnapshots: %w", err)
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 17:49:24 +01:00
|
|
|
// Also work out the state before the event removes and the event
|
|
|
|
// adds.
|
2020-02-05 18:06:39 +00:00
|
|
|
u.stateBeforeEventRemoves, u.stateBeforeEventAdds, err = roomState.DifferenceBetweeenStateSnapshots(
|
2020-02-05 16:25:58 +00:00
|
|
|
u.ctx, u.newStateNID, u.stateAtEvent.BeforeStateSnapshotNID,
|
2017-08-08 16:38:03 +01:00
|
|
|
)
|
2020-08-19 15:38:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("roomState.DifferenceBetweeenStateSnapshots: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:59:13 +01:00
|
|
|
// calculateLatest works out the new set of forward extremities. Returns
|
|
|
|
// true if the new event is included in those extremites, false otherwise.
|
2020-10-07 14:05:33 +01:00
|
|
|
func (u *latestEventsUpdater) calculateLatest(
|
2017-09-13 16:30:19 +01:00
|
|
|
oldLatest []types.StateAtEventAndReference,
|
2020-10-21 15:37:07 +01:00
|
|
|
newEvent *gomatrixserverlib.Event,
|
|
|
|
newStateAndRef types.StateAtEventAndReference,
|
|
|
|
) (bool, error) {
|
|
|
|
// First of all, get a list of all of the events in our current
|
|
|
|
// set of forward extremities.
|
|
|
|
existingRefs := make(map[string]*types.StateAtEventAndReference)
|
|
|
|
for i, old := range oldLatest {
|
|
|
|
existingRefs[old.EventID] = &oldLatest[i]
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:47:25 +00:00
|
|
|
// If the "new" event is already a forward extremity then stop, as
|
|
|
|
// nothing changes.
|
|
|
|
if _, ok := existingRefs[newEvent.EventID()]; ok {
|
|
|
|
u.latest = oldLatest
|
|
|
|
return false, nil
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 12:47:25 +00:00
|
|
|
// If the "new" event is already referenced by an existing event
|
2020-10-21 15:37:07 +01:00
|
|
|
// then do nothing - it's not a candidate to be a new extremity if
|
|
|
|
// it has been referenced.
|
2021-01-11 12:47:25 +00:00
|
|
|
if referenced, err := u.updater.IsReferenced(newEvent.EventReference()); err != nil {
|
|
|
|
return false, fmt.Errorf("u.updater.IsReferenced(new): %w", err)
|
|
|
|
} else if referenced {
|
2020-12-09 13:34:37 +00:00
|
|
|
u.latest = oldLatest
|
2020-10-21 15:37:07 +01:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 12:47:25 +00:00
|
|
|
// Then let's see if any of the existing forward extremities now
|
|
|
|
// have entries in the previous events table. If they do then we
|
|
|
|
// will no longer include them as forward extremities.
|
|
|
|
existingPrevs := make(map[string]struct{})
|
|
|
|
for _, l := range existingRefs {
|
|
|
|
referenced, err := u.updater.IsReferenced(l.EventReference)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("u.updater.IsReferenced: %w", err)
|
|
|
|
} else if referenced {
|
|
|
|
existingPrevs[l.EventID] = struct{}{}
|
|
|
|
}
|
2020-10-07 14:05:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:37:07 +01:00
|
|
|
// Include our new event in the extremities.
|
|
|
|
newLatest := []types.StateAtEventAndReference{newStateAndRef}
|
|
|
|
|
|
|
|
// Then run through and see if the other extremities are still valid.
|
|
|
|
// If our new event references them then they are no longer good
|
|
|
|
// candidates.
|
|
|
|
for _, prevEventID := range newEvent.PrevEventIDs() {
|
|
|
|
delete(existingRefs, prevEventID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that we don't add any candidate forward extremities from
|
|
|
|
// the old set that are, themselves, referenced by the old set of
|
|
|
|
// forward extremities. This shouldn't happen but guards against
|
|
|
|
// the possibility anyway.
|
|
|
|
for prevEventID := range existingPrevs {
|
|
|
|
delete(existingRefs, prevEventID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then re-add any old extremities that are still valid after all.
|
|
|
|
for _, old := range existingRefs {
|
|
|
|
newLatest = append(newLatest, *old)
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 14:05:33 +01:00
|
|
|
u.latest = newLatest
|
2020-10-21 15:37:07 +01:00
|
|
|
return true, nil
|
2017-02-27 11:25:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 16:38:03 +01:00
|
|
|
func (u *latestEventsUpdater) makeOutputNewRoomEvent() (*api.OutputEvent, error) {
|
|
|
|
latestEventIDs := make([]string, len(u.latest))
|
|
|
|
for i := range u.latest {
|
|
|
|
latestEventIDs[i] = u.latest[i].EventID
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 10:46:29 +01:00
|
|
|
ore := api.OutputNewRoomEvent{
|
2020-09-02 10:02:48 +01:00
|
|
|
Event: u.event.Headered(u.roomInfo.RoomVersion),
|
2020-09-15 11:17:46 +01:00
|
|
|
RewritesState: u.rewritesState,
|
2017-08-08 16:38:03 +01:00
|
|
|
LastSentEventID: u.lastEventIDSent,
|
2017-02-27 11:25:35 +00:00
|
|
|
LatestEventIDs: latestEventIDs,
|
2017-12-04 18:07:52 +00:00
|
|
|
TransactionID: u.transactionID,
|
2017-03-07 10:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 19:50:40 +01:00
|
|
|
eventIDMap, err := u.stateEventMap()
|
2017-03-07 10:25:01 +00:00
|
|
|
if err != nil {
|
2017-08-08 16:38:03 +01:00
|
|
|
return nil, err
|
2017-03-07 10:25:01 +00:00
|
|
|
}
|
2017-08-08 16:38:03 +01:00
|
|
|
for _, entry := range u.added {
|
2017-03-07 10:25:01 +00:00
|
|
|
ore.AddsStateEventIDs = append(ore.AddsStateEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 16:38:03 +01:00
|
|
|
for _, entry := range u.removed {
|
2017-03-07 10:25:01 +00:00
|
|
|
ore.RemovesStateEventIDs = append(ore.RemovesStateEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 16:38:03 +01:00
|
|
|
for _, entry := range u.stateBeforeEventRemoves {
|
2017-06-27 13:20:04 +01:00
|
|
|
ore.StateBeforeRemovesEventIDs = append(ore.StateBeforeRemovesEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 16:38:03 +01:00
|
|
|
for _, entry := range u.stateBeforeEventAdds {
|
2017-06-27 13:20:04 +01:00
|
|
|
ore.StateBeforeAddsEventIDs = append(ore.StateBeforeAddsEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2020-10-19 14:59:13 +01:00
|
|
|
|
2017-08-08 16:38:03 +01:00
|
|
|
ore.SendAsServer = u.sendAsServer
|
|
|
|
|
2020-06-11 19:50:40 +01:00
|
|
|
// include extra state events if they were added as nearly every downstream component will care about it
|
|
|
|
// and we'd rather not have them all hit QueryEventsByID at the same time!
|
|
|
|
if len(ore.AddsStateEventIDs) > 0 {
|
2020-10-19 14:59:13 +01:00
|
|
|
var err error
|
|
|
|
if ore.AddStateEvents, err = u.extraEventsForIDs(u.roomInfo.RoomVersion, ore.AddsStateEventIDs); err != nil {
|
2020-06-11 19:50:40 +01:00
|
|
|
return nil, fmt.Errorf("failed to load add_state_events from db: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 16:38:03 +01:00
|
|
|
return &api.OutputEvent{
|
|
|
|
Type: api.OutputTypeNewRoomEvent,
|
|
|
|
NewRoomEvent: &ore,
|
|
|
|
}, nil
|
2017-02-21 14:50:30 +00:00
|
|
|
}
|
2017-06-27 13:20:04 +01:00
|
|
|
|
2020-06-11 19:50:40 +01:00
|
|
|
// extraEventsForIDs returns the full events for the event IDs given, but does not include the current event being
|
|
|
|
// updated.
|
2020-11-16 15:44:53 +00:00
|
|
|
func (u *latestEventsUpdater) extraEventsForIDs(roomVersion gomatrixserverlib.RoomVersion, eventIDs []string) ([]*gomatrixserverlib.HeaderedEvent, error) {
|
2020-06-11 19:50:40 +01:00
|
|
|
var extraEventIDs []string
|
|
|
|
for _, e := range eventIDs {
|
|
|
|
if e == u.event.EventID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
extraEventIDs = append(extraEventIDs, e)
|
|
|
|
}
|
|
|
|
if len(extraEventIDs) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-02-04 10:39:34 +00:00
|
|
|
extraEvents, err := u.updater.EventsFromIDs(u.ctx, extraEventIDs)
|
2020-06-11 19:50:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-16 15:44:53 +00:00
|
|
|
var h []*gomatrixserverlib.HeaderedEvent
|
2020-06-11 19:50:40 +01:00
|
|
|
for _, e := range extraEvents {
|
|
|
|
h = append(h, e.Headered(roomVersion))
|
|
|
|
}
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve an event nid -> event ID map for all events that need updating
|
|
|
|
func (u *latestEventsUpdater) stateEventMap() (map[types.EventNID]string, error) {
|
|
|
|
var stateEventNIDs []types.EventNID
|
|
|
|
var allStateEntries []types.StateEntry
|
|
|
|
allStateEntries = append(allStateEntries, u.added...)
|
|
|
|
allStateEntries = append(allStateEntries, u.removed...)
|
|
|
|
allStateEntries = append(allStateEntries, u.stateBeforeEventRemoves...)
|
|
|
|
allStateEntries = append(allStateEntries, u.stateBeforeEventAdds...)
|
|
|
|
for _, entry := range allStateEntries {
|
|
|
|
stateEventNIDs = append(stateEventNIDs, entry.EventNID)
|
|
|
|
}
|
|
|
|
stateEventNIDs = stateEventNIDs[:util.SortAndUnique(eventNIDSorter(stateEventNIDs))]
|
2022-02-04 10:39:34 +00:00
|
|
|
return u.updater.EventIDs(u.ctx, stateEventNIDs)
|
2020-06-11 19:50:40 +01:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:20:04 +01:00
|
|
|
type eventNIDSorter []types.EventNID
|
|
|
|
|
|
|
|
func (s eventNIDSorter) Len() int { return len(s) }
|
|
|
|
func (s eventNIDSorter) Less(i, j int) bool { return s[i] < s[j] }
|
|
|
|
func (s eventNIDSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|