2017-08-08 16:38:03 +01: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.
|
|
|
|
|
2020-05-01 10:48:17 +01:00
|
|
|
package internal
|
2017-08-08 16:38:03 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-13 16:30:19 +01:00
|
|
|
"context"
|
2020-05-11 18:21:25 +01:00
|
|
|
"errors"
|
2017-08-08 16:38:03 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-04-24 10:38:58 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2017-08-08 16:38:03 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
// updateMembership updates the current membership and the invites for each
|
|
|
|
// user affected by a change in the current state of the room.
|
|
|
|
// Returns a list of output events to write to the kafka log to inform the
|
|
|
|
// consumers about the invites added or retired by the change in current state.
|
|
|
|
func updateMemberships(
|
2017-09-13 16:30:19 +01:00
|
|
|
ctx context.Context,
|
2020-04-24 10:38:58 +01:00
|
|
|
db storage.Database,
|
2017-09-13 16:30:19 +01:00
|
|
|
updater types.RoomRecentEventsUpdater,
|
|
|
|
removed, added []types.StateEntry,
|
2017-08-08 16:38:03 +01:00
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
changes := membershipChanges(removed, added)
|
|
|
|
var eventNIDs []types.EventNID
|
|
|
|
for _, change := range changes {
|
|
|
|
if change.addedEventNID != 0 {
|
|
|
|
eventNIDs = append(eventNIDs, change.addedEventNID)
|
|
|
|
}
|
|
|
|
if change.removedEventNID != 0 {
|
|
|
|
eventNIDs = append(eventNIDs, change.removedEventNID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the event JSON so we can look up the "membership" key.
|
|
|
|
// TODO: Maybe add a membership key to the events table so we can load that
|
|
|
|
// key without having to load the entire event JSON?
|
2017-09-13 16:30:19 +01:00
|
|
|
events, err := db.Events(ctx, eventNIDs)
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var updates []api.OutputEvent
|
|
|
|
|
|
|
|
for _, change := range changes {
|
|
|
|
var ae *gomatrixserverlib.Event
|
|
|
|
var re *gomatrixserverlib.Event
|
|
|
|
targetUserNID := change.EventStateKeyNID
|
|
|
|
if change.removedEventNID != 0 {
|
|
|
|
ev, _ := eventMap(events).lookup(change.removedEventNID)
|
|
|
|
if ev != nil {
|
|
|
|
re = &ev.Event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if change.addedEventNID != 0 {
|
|
|
|
ev, _ := eventMap(events).lookup(change.addedEventNID)
|
|
|
|
if ev != nil {
|
|
|
|
ae = &ev.Event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if updates, err = updateMembership(updater, targetUserNID, re, ae, updates); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 13:34:04 +01:00
|
|
|
return updates, nil
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateMembership(
|
|
|
|
updater types.RoomRecentEventsUpdater, targetUserNID types.EventStateKeyNID,
|
|
|
|
remove, add *gomatrixserverlib.Event,
|
|
|
|
updates []api.OutputEvent,
|
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
var err error
|
2017-09-20 15:25:25 +01:00
|
|
|
// Default the membership to Leave if no event was added or removed.
|
2019-08-06 15:07:36 +01:00
|
|
|
oldMembership := gomatrixserverlib.Leave
|
|
|
|
newMembership := gomatrixserverlib.Leave
|
2017-08-08 16:38:03 +01:00
|
|
|
|
|
|
|
if remove != nil {
|
2018-04-20 15:50:44 +01:00
|
|
|
oldMembership, err = remove.Membership()
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if add != nil {
|
2018-04-20 15:50:44 +01:00
|
|
|
newMembership, err = add.Membership()
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 15:07:36 +01:00
|
|
|
if oldMembership == newMembership && newMembership != gomatrixserverlib.Join {
|
2017-08-08 16:38:03 +01:00
|
|
|
// If the membership is the same then nothing changed and we can return
|
2017-09-20 15:25:25 +01:00
|
|
|
// immediately, unless it's a Join update (e.g. profile update).
|
2017-08-08 16:38:03 +01:00
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:21:25 +01:00
|
|
|
if add == nil {
|
|
|
|
// This shouldn't happen. Returning an error here is better than panicking
|
|
|
|
// in the membership updater functions later on.
|
|
|
|
// TODO: Why does this happen to begin with?
|
|
|
|
return updates, errors.New("add should not be nil")
|
|
|
|
}
|
|
|
|
|
2017-08-08 16:38:03 +01:00
|
|
|
mu, err := updater.MembershipUpdater(targetUserNID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-20 15:50:44 +01:00
|
|
|
switch newMembership {
|
2019-08-06 15:07:36 +01:00
|
|
|
case gomatrixserverlib.Invite:
|
2020-04-03 14:29:06 +01:00
|
|
|
return updateToInviteMembership(mu, add, updates, updater.RoomVersion())
|
2019-08-06 15:07:36 +01:00
|
|
|
case gomatrixserverlib.Join:
|
2017-08-08 16:38:03 +01:00
|
|
|
return updateToJoinMembership(mu, add, updates)
|
2019-08-06 15:07:36 +01:00
|
|
|
case gomatrixserverlib.Leave, gomatrixserverlib.Ban:
|
2018-04-20 15:50:44 +01:00
|
|
|
return updateToLeaveMembership(mu, add, newMembership, updates)
|
2017-08-08 16:38:03 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf(
|
2018-04-20 15:50:44 +01:00
|
|
|
"input: membership %q is not one of the allowed values", newMembership,
|
2017-08-08 16:38:03 +01:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateToInviteMembership(
|
|
|
|
mu types.MembershipUpdater, add *gomatrixserverlib.Event, updates []api.OutputEvent,
|
2020-04-03 14:29:06 +01:00
|
|
|
roomVersion gomatrixserverlib.RoomVersion,
|
2017-08-08 16:38:03 +01:00
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
// We may have already sent the invite to the user, either because we are
|
|
|
|
// reprocessing this event, or because the we received this invite from a
|
|
|
|
// remote server via the federation invite API. In those cases we don't need
|
|
|
|
// to send the event.
|
|
|
|
needsSending, err := mu.SetToInvite(*add)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if needsSending {
|
|
|
|
// We notify the consumers using a special event even though we will
|
|
|
|
// notify them about the change in current state as part of the normal
|
|
|
|
// room event stream. This ensures that the consumers only have to
|
|
|
|
// consider a single stream of events when determining whether a user
|
|
|
|
// is invited, rather than having to combine multiple streams themselves.
|
|
|
|
onie := api.OutputNewInviteEvent{
|
2020-04-24 16:30:25 +01:00
|
|
|
Event: add.Headered(roomVersion),
|
2020-04-03 14:29:06 +01:00
|
|
|
RoomVersion: roomVersion,
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
updates = append(updates, api.OutputEvent{
|
|
|
|
Type: api.OutputTypeNewInviteEvent,
|
|
|
|
NewInviteEvent: &onie,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateToJoinMembership(
|
|
|
|
mu types.MembershipUpdater, add *gomatrixserverlib.Event, updates []api.OutputEvent,
|
|
|
|
) ([]api.OutputEvent, error) {
|
2017-08-21 16:34:26 +01:00
|
|
|
// If the user is already marked as being joined, we call SetToJoin to update
|
|
|
|
// the event ID then we can return immediately. Retired is ignored as there
|
|
|
|
// is no invite event to retire.
|
2017-08-08 16:38:03 +01:00
|
|
|
if mu.IsJoin() {
|
2017-08-21 16:34:26 +01:00
|
|
|
_, err := mu.SetToJoin(add.Sender(), add.EventID(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-08 16:38:03 +01:00
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
// When we mark a user as being joined we will invalidate any invites that
|
|
|
|
// are active for that user. We notify the consumers that the invites have
|
|
|
|
// been retired using a special event, even though they could infer this
|
|
|
|
// by studying the state changes in the room event stream.
|
2017-08-21 16:34:26 +01:00
|
|
|
retired, err := mu.SetToJoin(add.Sender(), add.EventID(), false)
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, eventID := range retired {
|
|
|
|
orie := api.OutputRetireInviteEvent{
|
2017-09-20 15:36:41 +01:00
|
|
|
EventID: eventID,
|
2019-08-06 15:07:36 +01:00
|
|
|
Membership: gomatrixserverlib.Join,
|
2017-09-20 15:36:41 +01:00
|
|
|
RetiredByEventID: add.EventID(),
|
|
|
|
TargetUserID: *add.StateKey(),
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
updates = append(updates, api.OutputEvent{
|
|
|
|
Type: api.OutputTypeRetireInviteEvent,
|
|
|
|
RetireInviteEvent: &orie,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateToLeaveMembership(
|
|
|
|
mu types.MembershipUpdater, add *gomatrixserverlib.Event,
|
|
|
|
newMembership string, updates []api.OutputEvent,
|
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
// If the user is already neither joined, nor invited to the room then we
|
|
|
|
// can return immediately.
|
|
|
|
if mu.IsLeave() {
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
// When we mark a user as having left we will invalidate any invites that
|
|
|
|
// are active for that user. We notify the consumers that the invites have
|
|
|
|
// been retired using a special event, even though they could infer this
|
|
|
|
// by studying the state changes in the room event stream.
|
2017-08-21 16:34:26 +01:00
|
|
|
retired, err := mu.SetToLeave(add.Sender(), add.EventID())
|
2017-08-08 16:38:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, eventID := range retired {
|
|
|
|
orie := api.OutputRetireInviteEvent{
|
2017-09-20 15:36:41 +01:00
|
|
|
EventID: eventID,
|
|
|
|
Membership: newMembership,
|
|
|
|
RetiredByEventID: add.EventID(),
|
|
|
|
TargetUserID: *add.StateKey(),
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
updates = append(updates, api.OutputEvent{
|
|
|
|
Type: api.OutputTypeRetireInviteEvent,
|
|
|
|
RetireInviteEvent: &orie,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:58:47 +01:00
|
|
|
// membershipChanges pairs up the membership state changes.
|
2017-08-08 16:38:03 +01:00
|
|
|
func membershipChanges(removed, added []types.StateEntry) []stateChange {
|
|
|
|
changes := pairUpChanges(removed, added)
|
|
|
|
var result []stateChange
|
|
|
|
for _, c := range changes {
|
|
|
|
if c.EventTypeNID == types.MRoomMemberNID {
|
|
|
|
result = append(result, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
type stateChange struct {
|
|
|
|
types.StateKeyTuple
|
|
|
|
removedEventNID types.EventNID
|
|
|
|
addedEventNID types.EventNID
|
|
|
|
}
|
|
|
|
|
|
|
|
// pairUpChanges pairs up the state events added and removed for each type,
|
2020-05-14 14:58:47 +01:00
|
|
|
// state key tuple.
|
2017-08-08 16:38:03 +01:00
|
|
|
func pairUpChanges(removed, added []types.StateEntry) []stateChange {
|
2020-05-14 14:58:47 +01:00
|
|
|
tuples := make(map[types.StateKeyTuple]stateChange)
|
|
|
|
changes := []stateChange{}
|
|
|
|
|
|
|
|
// First, go through the newly added state entries.
|
|
|
|
for _, add := range added {
|
|
|
|
if change, ok := tuples[add.StateKeyTuple]; ok {
|
|
|
|
// If we already have an entry, update it.
|
|
|
|
change.addedEventNID = add.EventNID
|
|
|
|
tuples[add.StateKeyTuple] = change
|
|
|
|
} else {
|
|
|
|
// Otherwise, create a new entry.
|
|
|
|
tuples[add.StateKeyTuple] = stateChange{add.StateKeyTuple, 0, add.EventNID}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through the removed state entries.
|
|
|
|
for _, remove := range removed {
|
|
|
|
if change, ok := tuples[remove.StateKeyTuple]; ok {
|
|
|
|
// If we already have an entry, update it.
|
|
|
|
change.removedEventNID = remove.EventNID
|
|
|
|
tuples[remove.StateKeyTuple] = change
|
|
|
|
} else {
|
|
|
|
// Otherwise, create a new entry.
|
|
|
|
tuples[remove.StateKeyTuple] = stateChange{remove.StateKeyTuple, remove.EventNID, 0}
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:58:47 +01:00
|
|
|
|
|
|
|
// Now return the changes as an array.
|
|
|
|
for _, change := range tuples {
|
|
|
|
changes = append(changes, change)
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes
|
2017-08-08 16:38:03 +01:00
|
|
|
}
|