2017-08-08 10:38:03 -05: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-09-02 11:13:15 -05:00
|
|
|
package input
|
2017-08-08 10:38:03 -05:00
|
|
|
|
|
|
|
import (
|
2017-09-13 10:30:19 -05:00
|
|
|
"context"
|
2017-08-08 10:38:03 -05:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-13 10:45:14 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-03-13 10:45:14 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2017-08-08 10:38:03 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-09-02 07:47:31 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/internal/helpers"
|
2020-08-19 07:24:54 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
2022-07-22 08:44:04 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-08-08 10:38:03 -05:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2020-09-02 11:13:15 -05:00
|
|
|
func (r *Inputer) updateMemberships(
|
2017-09-13 10:30:19 -05:00
|
|
|
ctx context.Context,
|
2022-02-04 04:39:34 -06:00
|
|
|
updater *shared.RoomUpdater,
|
2017-09-13 10:30:19 -05:00
|
|
|
removed, added []types.StateEntry,
|
2017-08-08 10:38:03 -05:00
|
|
|
) ([]api.OutputEvent, error) {
|
2023-03-13 10:45:14 -05:00
|
|
|
trace, ctx := internal.StartRegion(ctx, "updateMemberships")
|
|
|
|
defer trace.EndRegion()
|
2022-06-07 08:23:26 -05:00
|
|
|
|
2017-08-08 10:38:03 -05:00
|
|
|
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?
|
2023-05-08 14:25:44 -05:00
|
|
|
events, err := updater.Events(ctx, "", eventNIDs)
|
2017-08-08 10:38:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var updates []api.OutputEvent
|
|
|
|
|
|
|
|
for _, change := range changes {
|
2022-07-22 08:44:04 -05:00
|
|
|
var ae *types.Event
|
|
|
|
var re *types.Event
|
2017-08-08 10:38:03 -05:00
|
|
|
targetUserNID := change.EventStateKeyNID
|
|
|
|
if change.removedEventNID != 0 {
|
2022-07-22 08:44:04 -05:00
|
|
|
re, _ = helpers.EventMap(events).Lookup(change.removedEventNID)
|
2017-08-08 10:38:03 -05:00
|
|
|
}
|
|
|
|
if change.addedEventNID != 0 {
|
2022-07-22 08:44:04 -05:00
|
|
|
ae, _ = helpers.EventMap(events).Lookup(change.addedEventNID)
|
2017-08-08 10:38:03 -05:00
|
|
|
}
|
2020-05-20 12:03:06 -05:00
|
|
|
if updates, err = r.updateMembership(updater, targetUserNID, re, ae, updates); err != nil {
|
2017-08-08 10:38:03 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-05-21 07:34:04 -05:00
|
|
|
return updates, nil
|
2017-08-08 10:38:03 -05:00
|
|
|
}
|
|
|
|
|
2020-09-02 11:13:15 -05:00
|
|
|
func (r *Inputer) updateMembership(
|
2022-02-04 04:39:34 -06:00
|
|
|
updater *shared.RoomUpdater,
|
2020-05-20 12:03:06 -05:00
|
|
|
targetUserNID types.EventStateKeyNID,
|
2022-07-22 08:44:04 -05:00
|
|
|
remove, add *types.Event,
|
2017-08-08 10:38:03 -05:00
|
|
|
updates []api.OutputEvent,
|
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
var err error
|
2017-09-20 09:25:25 -05:00
|
|
|
// Default the membership to Leave if no event was added or removed.
|
2023-04-19 09:50:33 -05:00
|
|
|
newMembership := spec.Leave
|
2017-08-08 10:38:03 -05:00
|
|
|
if add != nil {
|
2018-04-20 09:50:44 -05:00
|
|
|
newMembership, err = add.Membership()
|
2017-08-08 10:38:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 08:44:04 -05:00
|
|
|
|
|
|
|
var targetLocal bool
|
|
|
|
if add != nil {
|
|
|
|
targetLocal = r.isLocalTarget(add)
|
|
|
|
}
|
|
|
|
|
|
|
|
mu, err := updater.MembershipUpdater(targetUserNID, targetLocal)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-08-08 10:38:03 -05:00
|
|
|
}
|
|
|
|
|
2021-02-06 05:49:18 -06:00
|
|
|
// In an ideal world, we shouldn't ever have "add" be nil and "remove" be
|
|
|
|
// set, as this implies that we're deleting a state event without replacing
|
|
|
|
// it (a thing that ordinarily shouldn't happen in Matrix). However, state
|
|
|
|
// resets are sadly a thing occasionally and we have to account for that.
|
|
|
|
// Beforehand there used to be a check here which stopped dead if we hit
|
|
|
|
// this scenario, but that meant that the membership table got out of sync
|
|
|
|
// after a state reset, often thinking that the user was still joined to
|
|
|
|
// the room even though the room state said otherwise, and this would prevent
|
|
|
|
// the user from being able to attempt to rejoin the room without modifying
|
2022-07-22 08:44:04 -05:00
|
|
|
// the database. So instead we're going to remove the membership from the
|
|
|
|
// database altogether, so that it doesn't create future problems.
|
|
|
|
if add == nil && remove != nil {
|
|
|
|
return nil, mu.Delete()
|
2017-08-08 10:38:03 -05:00
|
|
|
}
|
|
|
|
|
2018-04-20 09:50:44 -05:00
|
|
|
switch newMembership {
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Invite:
|
2020-09-02 07:47:31 -05:00
|
|
|
return helpers.UpdateToInviteMembership(mu, add, updates, updater.RoomVersion())
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Join:
|
2017-08-08 10:38:03 -05:00
|
|
|
return updateToJoinMembership(mu, add, updates)
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Leave, spec.Ban:
|
2018-04-20 09:50:44 -05:00
|
|
|
return updateToLeaveMembership(mu, add, newMembership, updates)
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Knock:
|
2021-07-22 06:26:58 -05:00
|
|
|
return updateToKnockMembership(mu, add, updates)
|
2017-08-08 10:38:03 -05:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf(
|
2018-04-20 09:50:44 -05:00
|
|
|
"input: membership %q is not one of the allowed values", newMembership,
|
2017-08-08 10:38:03 -05:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 08:44:04 -05:00
|
|
|
func (r *Inputer) isLocalTarget(event *types.Event) bool {
|
2020-05-20 12:03:06 -05:00
|
|
|
isTargetLocalUser := false
|
|
|
|
if statekey := event.StateKey(); statekey != nil {
|
|
|
|
_, domain, _ := gomatrixserverlib.SplitID('@', *statekey)
|
2020-09-02 11:13:15 -05:00
|
|
|
isTargetLocalUser = domain == r.ServerName
|
2020-05-20 12:03:06 -05:00
|
|
|
}
|
|
|
|
return isTargetLocalUser
|
|
|
|
}
|
|
|
|
|
2017-08-08 10:38:03 -05:00
|
|
|
func updateToJoinMembership(
|
2022-07-22 08:44:04 -05:00
|
|
|
mu *shared.MembershipUpdater, add *types.Event, updates []api.OutputEvent,
|
2017-08-08 10:38:03 -05:00
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
// 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.
|
2022-07-22 08:44:04 -05:00
|
|
|
_, retired, err := mu.Update(tables.MembershipStateJoin, add)
|
2017-08-08 10:38:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, eventID := range retired {
|
|
|
|
updates = append(updates, api.OutputEvent{
|
2022-07-22 08:44:04 -05:00
|
|
|
Type: api.OutputTypeRetireInviteEvent,
|
|
|
|
RetireInviteEvent: &api.OutputRetireInviteEvent{
|
|
|
|
EventID: eventID,
|
2023-04-19 09:50:33 -05:00
|
|
|
Membership: spec.Join,
|
2022-07-22 08:44:04 -05:00
|
|
|
RetiredByEventID: add.EventID(),
|
|
|
|
TargetUserID: *add.StateKey(),
|
|
|
|
},
|
2017-08-08 10:38:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateToLeaveMembership(
|
2022-07-22 08:44:04 -05:00
|
|
|
mu *shared.MembershipUpdater, add *types.Event,
|
2017-08-08 10:38:03 -05:00
|
|
|
newMembership string, updates []api.OutputEvent,
|
|
|
|
) ([]api.OutputEvent, error) {
|
|
|
|
// 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.
|
2022-07-22 08:44:04 -05:00
|
|
|
_, retired, err := mu.Update(tables.MembershipStateLeaveOrBan, add)
|
2017-08-08 10:38:03 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, eventID := range retired {
|
|
|
|
updates = append(updates, api.OutputEvent{
|
2022-07-22 08:44:04 -05:00
|
|
|
Type: api.OutputTypeRetireInviteEvent,
|
|
|
|
RetireInviteEvent: &api.OutputRetireInviteEvent{
|
|
|
|
EventID: eventID,
|
|
|
|
Membership: newMembership,
|
|
|
|
RetiredByEventID: add.EventID(),
|
|
|
|
TargetUserID: *add.StateKey(),
|
|
|
|
},
|
2017-08-08 10:38:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 06:26:58 -05:00
|
|
|
func updateToKnockMembership(
|
2022-07-22 08:44:04 -05:00
|
|
|
mu *shared.MembershipUpdater, add *types.Event, updates []api.OutputEvent,
|
2021-07-22 06:26:58 -05:00
|
|
|
) ([]api.OutputEvent, error) {
|
2022-07-22 08:44:04 -05:00
|
|
|
if _, _, err := mu.Update(tables.MembershipStateKnock, add); err != nil {
|
|
|
|
return nil, err
|
2021-07-22 06:26:58 -05:00
|
|
|
}
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 08:58:47 -05:00
|
|
|
// membershipChanges pairs up the membership state changes.
|
2017-08-08 10:38:03 -05: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 08:58:47 -05:00
|
|
|
// state key tuple.
|
2017-08-08 10:38:03 -05:00
|
|
|
func pairUpChanges(removed, added []types.StateEntry) []stateChange {
|
2020-05-14 08:58:47 -05: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 10:38:03 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 08:58:47 -05:00
|
|
|
|
|
|
|
// Now return the changes as an array.
|
|
|
|
for _, change := range tuples {
|
|
|
|
changes = append(changes, change)
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes
|
2017-08-08 10:38:03 -05:00
|
|
|
}
|