2020-05-01 11:48:17 +02:00
|
|
|
package internal
|
2020-04-28 12:46:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-04-29 19:41:45 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/auth"
|
2020-04-28 12:46:47 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2020-05-19 19:42:55 +02:00
|
|
|
"github.com/matrix-org/util"
|
2020-04-28 12:46:47 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// backfillRequester implements gomatrixserverlib.BackfillRequester
|
|
|
|
type backfillRequester struct {
|
|
|
|
db storage.Database
|
|
|
|
fedClient *gomatrixserverlib.FederationClient
|
|
|
|
thisServer gomatrixserverlib.ServerName
|
2020-05-20 17:04:31 +02:00
|
|
|
bwExtrems map[string][]string
|
2020-04-28 12:46:47 +02:00
|
|
|
|
|
|
|
// per-request state
|
|
|
|
servers []gomatrixserverlib.ServerName
|
|
|
|
eventIDToBeforeStateIDs map[string][]string
|
|
|
|
eventIDMap map[string]gomatrixserverlib.Event
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:04:31 +02:00
|
|
|
func newBackfillRequester(db storage.Database, fedClient *gomatrixserverlib.FederationClient, thisServer gomatrixserverlib.ServerName, bwExtrems map[string][]string) *backfillRequester {
|
2020-04-28 12:46:47 +02:00
|
|
|
return &backfillRequester{
|
|
|
|
db: db,
|
|
|
|
fedClient: fedClient,
|
|
|
|
thisServer: thisServer,
|
|
|
|
eventIDToBeforeStateIDs: make(map[string][]string),
|
|
|
|
eventIDMap: make(map[string]gomatrixserverlib.Event),
|
2020-05-20 17:04:31 +02:00
|
|
|
bwExtrems: bwExtrems,
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backfillRequester) StateIDsBeforeEvent(ctx context.Context, targetEvent gomatrixserverlib.HeaderedEvent) ([]string, error) {
|
|
|
|
b.eventIDMap[targetEvent.EventID()] = targetEvent.Unwrap()
|
|
|
|
if ids, ok := b.eventIDToBeforeStateIDs[targetEvent.EventID()]; ok {
|
|
|
|
return ids, nil
|
|
|
|
}
|
2020-05-19 19:42:55 +02:00
|
|
|
if len(targetEvent.PrevEventIDs()) == 0 && targetEvent.Type() == "m.room.create" && targetEvent.StateKeyEquals("") {
|
|
|
|
util.GetLogger(ctx).WithField("room_id", targetEvent.RoomID()).Info("Backfilled to the beginning of the room")
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = []string{}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-04-28 12:46:47 +02:00
|
|
|
// if we have exactly 1 prev event and we know the state of the room at that prev event, then just roll forward the prev event.
|
|
|
|
// Else, we have to hit /state_ids because either we don't know the state at all at this event (new backwards extremity) or
|
|
|
|
// we don't know the result of state res to merge forks (2 or more prev_events)
|
|
|
|
if len(targetEvent.PrevEventIDs()) == 1 {
|
|
|
|
prevEventID := targetEvent.PrevEventIDs()[0]
|
|
|
|
prevEvent, ok := b.eventIDMap[prevEventID]
|
|
|
|
if !ok {
|
|
|
|
goto FederationHit
|
|
|
|
}
|
|
|
|
prevEventStateIDs, ok := b.eventIDToBeforeStateIDs[prevEventID]
|
|
|
|
if !ok {
|
|
|
|
goto FederationHit
|
|
|
|
}
|
|
|
|
newStateIDs := b.calculateNewStateIDs(targetEvent.Unwrap(), prevEvent, prevEventStateIDs)
|
|
|
|
if newStateIDs != nil {
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = newStateIDs
|
|
|
|
return newStateIDs, nil
|
|
|
|
}
|
|
|
|
// else we failed to calculate the new state, so fallthrough
|
|
|
|
}
|
|
|
|
|
|
|
|
FederationHit:
|
|
|
|
var lastErr error
|
|
|
|
logrus.WithField("event_id", targetEvent.EventID()).Info("Requesting /state_ids at event")
|
|
|
|
for _, srv := range b.servers { // hit any valid server
|
|
|
|
c := gomatrixserverlib.FederatedStateProvider{
|
2020-04-29 19:41:45 +02:00
|
|
|
FedClient: b.fedClient,
|
|
|
|
RememberAuthEvents: false,
|
|
|
|
Server: srv,
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
res, err := c.StateIDsBeforeEvent(ctx, targetEvent)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = res
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
return nil, lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backfillRequester) calculateNewStateIDs(targetEvent, prevEvent gomatrixserverlib.Event, prevEventStateIDs []string) []string {
|
|
|
|
newStateIDs := prevEventStateIDs[:]
|
|
|
|
if prevEvent.StateKey() == nil {
|
|
|
|
// state is the same as the previous event
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = newStateIDs
|
|
|
|
return newStateIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
missingState := false // true if we are missing the info for a state event ID
|
|
|
|
foundEvent := false // true if we found a (type, state_key) match
|
|
|
|
// find which state ID to replace, if any
|
|
|
|
for i, id := range newStateIDs {
|
|
|
|
ev, ok := b.eventIDMap[id]
|
|
|
|
if !ok {
|
|
|
|
missingState = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// The state IDs BEFORE the target event are the state IDs BEFORE the prev_event PLUS the prev_event itself
|
|
|
|
if ev.Type() == prevEvent.Type() && ev.StateKey() != nil && *ev.StateKey() == *prevEvent.StateKey() {
|
|
|
|
newStateIDs[i] = prevEvent.EventID()
|
|
|
|
foundEvent = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundEvent && !missingState {
|
|
|
|
// we can be certain that this is new state
|
|
|
|
newStateIDs = append(newStateIDs, prevEvent.EventID())
|
|
|
|
foundEvent = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundEvent {
|
|
|
|
b.eventIDToBeforeStateIDs[targetEvent.EventID()] = newStateIDs
|
|
|
|
return newStateIDs
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:41:45 +02:00
|
|
|
func (b *backfillRequester) StateBeforeEvent(ctx context.Context, roomVer gomatrixserverlib.RoomVersion,
|
|
|
|
event gomatrixserverlib.HeaderedEvent, eventIDs []string) (map[string]*gomatrixserverlib.Event, error) {
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// try to fetch the events from the database first
|
|
|
|
events, err := b.ProvideEvents(roomVer, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
// non-fatal, fallthrough
|
|
|
|
logrus.WithError(err).Info("Failed to fetch events")
|
|
|
|
} else {
|
|
|
|
logrus.Infof("Fetched %d/%d events from the database", len(events), len(eventIDs))
|
|
|
|
if len(events) == len(eventIDs) {
|
|
|
|
result := make(map[string]*gomatrixserverlib.Event)
|
|
|
|
for i := range events {
|
|
|
|
result[events[i].EventID()] = &events[i]
|
|
|
|
b.eventIDMap[events[i].EventID()] = events[i]
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := gomatrixserverlib.FederatedStateProvider{
|
2020-04-29 19:41:45 +02:00
|
|
|
FedClient: b.fedClient,
|
|
|
|
RememberAuthEvents: false,
|
|
|
|
Server: b.servers[0],
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
result, err := c.StateBeforeEvent(ctx, roomVer, event, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for eventID, ev := range result {
|
|
|
|
b.eventIDMap[eventID] = *ev
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServersAtEvent is called when trying to determine which server to request from.
|
|
|
|
// It returns a list of servers which can be queried for backfill requests. These servers
|
|
|
|
// will be servers that are in the room already. The entries at the beginning are preferred servers
|
|
|
|
// and will be tried first. An empty list will fail the request.
|
2020-05-20 17:04:31 +02:00
|
|
|
func (b *backfillRequester) ServersAtEvent(ctx context.Context, roomID, eventID string) []gomatrixserverlib.ServerName {
|
|
|
|
// eventID will be a prev_event ID of a backwards extremity, meaning we will not have a database entry for it. Instead, use
|
|
|
|
// its successor, so look it up.
|
|
|
|
successor := ""
|
|
|
|
FindSuccessor:
|
|
|
|
for sucID, prevEventIDs := range b.bwExtrems {
|
|
|
|
for _, pe := range prevEventIDs {
|
|
|
|
if pe == eventID {
|
|
|
|
successor = sucID
|
|
|
|
break FindSuccessor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if successor == "" {
|
|
|
|
logrus.WithField("event_id", eventID).Error("ServersAtEvent: failed to find successor of this event to determine room state")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
eventID = successor
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// getMembershipsBeforeEventNID requires a NID, so retrieving the NID for
|
|
|
|
// the event is necessary.
|
|
|
|
NIDs, err := b.db.EventNIDs(ctx, []string{eventID})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("event_id", eventID).WithError(err).Error("ServersAtEvent: failed to get event NID for event")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 19:41:45 +02:00
|
|
|
stateEntries, err := stateBeforeEvent(ctx, b.db, NIDs[eventID])
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("event_id", eventID).WithError(err).Error("ServersAtEvent: failed to load state before event")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// possibly return all joined servers depending on history visiblity
|
|
|
|
memberEventsFromVis, err := joinEventsFromHistoryVisibility(ctx, b.db, roomID, stateEntries)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("ServersAtEvent: failed calculate servers from history visibility rules")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-29 19:41:45 +02:00
|
|
|
}
|
|
|
|
logrus.Infof("ServersAtEvent including %d current events from history visibility", len(memberEventsFromVis))
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
// 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.
|
2020-04-29 19:41:45 +02:00
|
|
|
memberEvents, err := getMembershipsAtState(ctx, b.db, stateEntries, true)
|
2020-04-28 12:46:47 +02:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("event_id", eventID).WithError(err).Error("ServersAtEvent: failed to get memberships before event")
|
2020-05-20 17:04:31 +02:00
|
|
|
return nil
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
2020-04-29 19:41:45 +02:00
|
|
|
memberEvents = append(memberEvents, memberEventsFromVis...)
|
2020-04-28 12:46:47 +02:00
|
|
|
|
|
|
|
// Store the server names in a temporary map to avoid duplicates.
|
|
|
|
serverSet := make(map[gomatrixserverlib.ServerName]bool)
|
2020-04-29 19:41:45 +02:00
|
|
|
for _, event := range memberEvents {
|
2020-04-28 12:46:47 +02:00
|
|
|
serverSet[event.Origin()] = true
|
|
|
|
}
|
2020-05-20 17:04:31 +02:00
|
|
|
var servers []gomatrixserverlib.ServerName
|
2020-04-28 12:46:47 +02:00
|
|
|
for server := range serverSet {
|
|
|
|
if server == b.thisServer {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
servers = append(servers, server)
|
|
|
|
}
|
|
|
|
b.servers = servers
|
2020-05-20 17:04:31 +02:00
|
|
|
return servers
|
2020-04-28 12:46:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Backfill performs a backfill request to the given server.
|
|
|
|
// https://matrix.org/docs/spec/server_server/latest#get-matrix-federation-v1-backfill-roomid
|
2020-04-29 19:41:45 +02:00
|
|
|
func (b *backfillRequester) Backfill(ctx context.Context, server gomatrixserverlib.ServerName, roomID string,
|
|
|
|
fromEventIDs []string, limit int) (*gomatrixserverlib.Transaction, error) {
|
|
|
|
|
2020-04-28 12:46:47 +02:00
|
|
|
tx, err := b.fedClient.Backfill(ctx, server, roomID, limit, fromEventIDs)
|
|
|
|
return &tx, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backfillRequester) ProvideEvents(roomVer gomatrixserverlib.RoomVersion, eventIDs []string) ([]gomatrixserverlib.Event, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
nidMap, err := b.db.EventNIDs(ctx, eventIDs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_ids", eventIDs).Error("Failed to find events")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eventNIDs := make([]types.EventNID, len(nidMap))
|
|
|
|
i := 0
|
|
|
|
for _, nid := range nidMap {
|
|
|
|
eventNIDs[i] = nid
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
eventsWithNids, err := b.db.Events(ctx, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("event_nids", eventNIDs).Error("Failed to load events")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events := make([]gomatrixserverlib.Event, len(eventsWithNids))
|
|
|
|
for i := range eventsWithNids {
|
|
|
|
events[i] = eventsWithNids[i].Event
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
2020-04-29 19:41:45 +02:00
|
|
|
|
|
|
|
// joinEventsFromHistoryVisibility returns all CURRENTLY joined members if the provided state indicated a 'shared' history visibility.
|
|
|
|
// TODO: Long term we probably want a history_visibility table which stores eventNID | visibility_enum so we can just
|
|
|
|
// pull all events and then filter by that table.
|
|
|
|
func joinEventsFromHistoryVisibility(
|
|
|
|
ctx context.Context, db storage.Database, roomID string, stateEntries []types.StateEntry) ([]types.Event, error) {
|
|
|
|
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, entry := range stateEntries {
|
|
|
|
// Filter the events to retrieve to only keep the membership events
|
|
|
|
if entry.EventTypeNID == types.MRoomHistoryVisibilityNID && entry.EventStateKeyNID == types.EmptyStateKeyNID {
|
|
|
|
eventNIDs = append(eventNIDs, entry.EventNID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all of the events in this state
|
|
|
|
stateEvents, err := db.Events(ctx, eventNIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events := make([]gomatrixserverlib.Event, len(stateEvents))
|
|
|
|
for i := range stateEvents {
|
|
|
|
events[i] = stateEvents[i].Event
|
|
|
|
}
|
|
|
|
visibility := auth.HistoryVisibilityForRoom(events)
|
|
|
|
if visibility != "shared" {
|
|
|
|
logrus.Infof("ServersAtEvent history visibility not shared: %s", visibility)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
// get joined members
|
|
|
|
roomNID, err := db.RoomNID(ctx, roomID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-20 19:03:06 +02:00
|
|
|
joinEventNIDs, err := db.GetMembershipEventNIDsForRoom(ctx, roomNID, true, false)
|
2020-04-29 19:41:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db.Events(ctx, joinEventNIDs)
|
|
|
|
}
|