2017-04-21 00:40:52 +02:00
|
|
|
// Copyright 2017 Vector Creations Ltd
|
|
|
|
//
|
|
|
|
// 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-02-21 15:50:30 +01:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-13 17:30:19 +02:00
|
|
|
"context"
|
2017-07-12 11:46:29 +02:00
|
|
|
|
2017-08-21 17:37:11 +02:00
|
|
|
"github.com/matrix-org/dendrite/common"
|
2017-02-27 12:25:35 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2017-03-08 16:10:26 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/state"
|
2017-02-21 15:50:30 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2017-06-27 14:20:04 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-02-21 15:50:30 +01:00
|
|
|
)
|
|
|
|
|
2017-02-27 12:25:35 +01: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 15:50:30 +01: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 16:22:06 +01:00
|
|
|
// Can only be called once at a time
|
2017-02-21 15:50:30 +01:00
|
|
|
func updateLatestEvents(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context,
|
2017-06-27 16:28:44 +02:00
|
|
|
db RoomEventDatabase,
|
|
|
|
ow OutputRoomEventWriter,
|
|
|
|
roomNID types.RoomNID,
|
|
|
|
stateAtEvent types.StateAtEvent,
|
|
|
|
event gomatrixserverlib.Event,
|
|
|
|
sendAsServer string,
|
2017-12-04 19:07:52 +01:00
|
|
|
transactionID *api.TransactionID,
|
2017-02-21 15:50:30 +01:00
|
|
|
) (err error) {
|
2017-09-13 17:30:19 +02:00
|
|
|
updater, err := db.GetLatestEventsForUpdate(ctx, roomNID)
|
2017-02-21 15:50:30 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-08-21 17:37:11 +02:00
|
|
|
succeeded := false
|
|
|
|
defer common.EndTransaction(updater, &succeeded)
|
2017-02-21 15:50:30 +01:00
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
u := latestEventsUpdater{
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx: ctx, db: db, updater: updater, ow: ow, roomNID: roomNID,
|
2017-08-08 17:38:03 +02:00
|
|
|
stateAtEvent: stateAtEvent, event: event, sendAsServer: sendAsServer,
|
2017-12-04 19:07:52 +01:00
|
|
|
transactionID: transactionID,
|
2017-08-08 17:38:03 +02:00
|
|
|
}
|
2017-08-21 17:37:11 +02:00
|
|
|
if err = u.doUpdateLatestEvents(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
succeeded = true
|
|
|
|
return
|
2017-02-21 15:50:30 +01:00
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02: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 19:07:52 +01:00
|
|
|
ctx context.Context
|
|
|
|
db RoomEventDatabase
|
|
|
|
updater types.RoomRecentEventsUpdater
|
|
|
|
ow OutputRoomEventWriter
|
|
|
|
roomNID types.RoomNID
|
|
|
|
stateAtEvent types.StateAtEvent
|
|
|
|
event gomatrixserverlib.Event
|
|
|
|
transactionID *api.TransactionID
|
2017-08-08 17:38:03 +02: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.
|
|
|
|
latest []types.StateAtEventAndReference
|
|
|
|
// 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 {
|
2017-09-20 15:15:38 +02:00
|
|
|
prevEvents := u.event.PrevEvents()
|
2017-08-08 17:38:03 +02:00
|
|
|
oldLatest := u.updater.LatestEvents()
|
|
|
|
u.lastEventIDSent = u.updater.LastEventIDSent()
|
|
|
|
u.oldStateNID = u.updater.CurrentStateSnapshotNID()
|
2017-02-21 15:50:30 +01:00
|
|
|
|
2017-09-05 18:40:46 +02:00
|
|
|
hasBeenSent, err := u.updater.HasEventBeenSent(u.stateAtEvent.EventNID)
|
|
|
|
if err != nil {
|
2017-02-27 12:25:35 +01:00
|
|
|
return err
|
|
|
|
} else if hasBeenSent {
|
|
|
|
// Already sent this event so we can stop processing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
if err = u.updater.StorePreviousEvents(u.stateAtEvent.EventNID, prevEvents); err != nil {
|
2017-02-21 15:50:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
eventReference := u.event.EventReference()
|
2017-02-27 12:25:35 +01:00
|
|
|
// Check if this event is already referenced by another event in the room.
|
2017-09-05 18:40:46 +02:00
|
|
|
alreadyReferenced, err := u.updater.IsReferenced(eventReference)
|
|
|
|
if err != nil {
|
2017-02-27 12:25:35 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
u.latest = calculateLatest(oldLatest, alreadyReferenced, prevEvents, types.StateAtEventAndReference{
|
2017-02-27 12:25:35 +01:00
|
|
|
EventReference: eventReference,
|
2017-08-08 17:38:03 +02:00
|
|
|
StateAtEvent: u.stateAtEvent,
|
2017-02-27 12:25:35 +01:00
|
|
|
})
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
if err = u.latestState(); err != nil {
|
2017-03-07 11:25:01 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
updates, err := updateMemberships(u.ctx, u.db, u.updater, u.removed, u.added)
|
2017-03-07 11:25:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
update, err := u.makeOutputNewRoomEvent()
|
2017-06-27 14:20:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
updates = append(updates, *update)
|
2017-06-27 14:20:04 +02:00
|
|
|
|
2017-02-27 12:25:35 +01: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.
|
2017-08-08 17:38:03 +02:00
|
|
|
if err = u.ow.WriteOutputEvents(u.event.RoomID(), updates); err != nil {
|
2017-02-27 12:25:35 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
if err = u.updater.SetLatestEvents(u.roomNID, u.latest, u.stateAtEvent.EventNID, u.newStateNID); err != nil {
|
2017-02-27 12:25:35 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-15 12:13:09 +01:00
|
|
|
return u.updater.MarkEventAsSent(u.stateAtEvent.EventNID)
|
2017-02-27 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
func (u *latestEventsUpdater) latestState() error {
|
|
|
|
var err error
|
2020-02-05 17:25:58 +01:00
|
|
|
// TODO: get the correct room version
|
|
|
|
state, err := state.GetStateResolutionAlgorithm(state.StateResolutionAlgorithmV1, u.db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
|
|
|
|
latestStateAtEvents := make([]types.StateAtEvent, len(u.latest))
|
|
|
|
for i := range u.latest {
|
|
|
|
latestStateAtEvents[i] = u.latest[i].StateAtEvent
|
|
|
|
}
|
2017-09-13 17:30:19 +02:00
|
|
|
u.newStateNID, err = state.CalculateAndStoreStateAfterEvents(
|
2020-02-05 17:25:58 +01:00
|
|
|
u.ctx, u.roomNID, latestStateAtEvents,
|
2017-09-13 17:30:19 +02:00
|
|
|
)
|
2017-08-08 17:38:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
u.removed, u.added, err = state.DifferenceBetweeenStateSnapshots(
|
2020-02-05 17:25:58 +01:00
|
|
|
u.ctx, u.oldStateNID, u.newStateNID,
|
2017-09-13 17:30:19 +02:00
|
|
|
)
|
2017-08-08 17:38:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.stateBeforeEventRemoves, u.stateBeforeEventAdds, err = state.DifferenceBetweeenStateSnapshots(
|
2020-02-05 17:25:58 +01:00
|
|
|
u.ctx, u.newStateNID, u.stateAtEvent.BeforeStateSnapshotNID,
|
2017-08-08 17:38:03 +02:00
|
|
|
)
|
2017-09-20 15:15:38 +02:00
|
|
|
return err
|
2017-08-08 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
func calculateLatest(
|
|
|
|
oldLatest []types.StateAtEventAndReference,
|
|
|
|
alreadyReferenced bool,
|
|
|
|
prevEvents []gomatrixserverlib.EventReference,
|
|
|
|
newEvent types.StateAtEventAndReference,
|
|
|
|
) []types.StateAtEventAndReference {
|
2017-02-22 17:51:10 +01:00
|
|
|
var alreadyInLatest bool
|
2017-02-21 15:50:30 +01:00
|
|
|
var newLatest []types.StateAtEventAndReference
|
|
|
|
for _, l := range oldLatest {
|
2017-02-22 17:51:10 +01:00
|
|
|
keep := true
|
2017-02-21 15:50:30 +01:00
|
|
|
for _, prevEvent := range prevEvents {
|
2017-09-20 15:15:38 +02:00
|
|
|
if l.EventID == prevEvent.EventID && bytes.Equal(l.EventSHA256, prevEvent.EventSHA256) {
|
2017-02-21 15:50:30 +01:00
|
|
|
// This event can be removed from the latest events cause we've found an event that references it.
|
|
|
|
// (If an event is referenced by another event then it can't be one of the latest events in the room
|
|
|
|
// because we have an event that comes after it)
|
2017-02-22 17:51:10 +01:00
|
|
|
keep = false
|
|
|
|
break
|
2017-02-21 15:50:30 +01:00
|
|
|
}
|
2017-02-22 17:51:10 +01:00
|
|
|
}
|
2017-02-27 12:25:35 +01:00
|
|
|
if l.EventNID == newEvent.EventNID {
|
2017-02-22 17:51:10 +01:00
|
|
|
alreadyInLatest = true
|
|
|
|
}
|
|
|
|
if keep {
|
2017-02-21 15:50:30 +01:00
|
|
|
// Keep the event in the latest events.
|
|
|
|
newLatest = append(newLatest, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 17:51:10 +01:00
|
|
|
if !alreadyReferenced && !alreadyInLatest {
|
|
|
|
// This event is not referenced by any of the events in the room
|
|
|
|
// and the event is not already in the latest events.
|
2017-02-21 15:50:30 +01:00
|
|
|
// Add it to the latest events
|
2017-02-27 12:25:35 +01:00
|
|
|
newLatest = append(newLatest, newEvent)
|
2017-02-21 15:50:30 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 12:25:35 +01:00
|
|
|
return newLatest
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
func (u *latestEventsUpdater) makeOutputNewRoomEvent() (*api.OutputEvent, error) {
|
2017-02-27 12:25:35 +01:00
|
|
|
|
2017-08-08 17:38:03 +02:00
|
|
|
latestEventIDs := make([]string, len(u.latest))
|
|
|
|
for i := range u.latest {
|
|
|
|
latestEventIDs[i] = u.latest[i].EventID
|
2017-02-21 15:50:30 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 11:46:29 +02:00
|
|
|
ore := api.OutputNewRoomEvent{
|
2017-08-08 17:38:03 +02:00
|
|
|
Event: u.event,
|
|
|
|
LastSentEventID: u.lastEventIDSent,
|
2017-02-27 12:25:35 +01:00
|
|
|
LatestEventIDs: latestEventIDs,
|
2017-12-04 19:07:52 +01:00
|
|
|
TransactionID: u.transactionID,
|
2017-03-07 11:25:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var stateEventNIDs []types.EventNID
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.added {
|
2017-03-07 11:25:01 +01:00
|
|
|
stateEventNIDs = append(stateEventNIDs, entry.EventNID)
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.removed {
|
2017-03-07 11:25:01 +01:00
|
|
|
stateEventNIDs = append(stateEventNIDs, entry.EventNID)
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.stateBeforeEventRemoves {
|
2017-06-27 14:20:04 +02:00
|
|
|
stateEventNIDs = append(stateEventNIDs, entry.EventNID)
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.stateBeforeEventAdds {
|
2017-06-27 14:20:04 +02:00
|
|
|
stateEventNIDs = append(stateEventNIDs, entry.EventNID)
|
|
|
|
}
|
|
|
|
stateEventNIDs = stateEventNIDs[:util.SortAndUnique(eventNIDSorter(stateEventNIDs))]
|
2017-09-13 17:30:19 +02:00
|
|
|
eventIDMap, err := u.db.EventIDs(u.ctx, stateEventNIDs)
|
2017-03-07 11:25:01 +01:00
|
|
|
if err != nil {
|
2017-08-08 17:38:03 +02:00
|
|
|
return nil, err
|
2017-03-07 11:25:01 +01:00
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.added {
|
2017-03-07 11:25:01 +01:00
|
|
|
ore.AddsStateEventIDs = append(ore.AddsStateEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.removed {
|
2017-03-07 11:25:01 +01:00
|
|
|
ore.RemovesStateEventIDs = append(ore.RemovesStateEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.stateBeforeEventRemoves {
|
2017-06-27 14:20:04 +02:00
|
|
|
ore.StateBeforeRemovesEventIDs = append(ore.StateBeforeRemovesEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
for _, entry := range u.stateBeforeEventAdds {
|
2017-06-27 14:20:04 +02:00
|
|
|
ore.StateBeforeAddsEventIDs = append(ore.StateBeforeAddsEventIDs, eventIDMap[entry.EventNID])
|
|
|
|
}
|
2017-08-08 17:38:03 +02:00
|
|
|
ore.SendAsServer = u.sendAsServer
|
|
|
|
|
|
|
|
return &api.OutputEvent{
|
|
|
|
Type: api.OutputTypeNewRoomEvent,
|
|
|
|
NewRoomEvent: &ore,
|
|
|
|
}, nil
|
2017-02-21 15:50:30 +01:00
|
|
|
}
|
2017-06-27 14:20:04 +02: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] }
|