2021-01-08 10:59:06 -06:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-07 09:08:19 -05:00
|
|
|
"database/sql"
|
2022-04-20 10:06:46 -05:00
|
|
|
"fmt"
|
2021-01-29 10:32:54 -06:00
|
|
|
"time"
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2022-04-19 03:46:45 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2022-04-26 03:53:17 -05:00
|
|
|
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 06:54:20 -05:00
|
|
|
rstypes "github.com/matrix-org/dendrite/roomserver/types"
|
2022-08-11 11:23:35 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/internal"
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/storage"
|
2023-04-04 12:16:53 -05:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/synctypes"
|
2021-01-08 10:59:06 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-08-05 07:27:27 -05:00
|
|
|
|
2023-01-12 03:06:03 -06:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/notifier"
|
2021-01-08 10:59:06 -06:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2022-08-11 11:23:35 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-08 10:59:06 -06:00
|
|
|
)
|
|
|
|
|
2021-01-29 10:32:54 -06:00
|
|
|
// The max number of per-room goroutines to have running.
|
|
|
|
// Too high and this will consume lots of CPU, too low and complete
|
|
|
|
// sync responses will take longer to process.
|
|
|
|
const PDU_STREAM_WORKERS = 256
|
|
|
|
|
|
|
|
// The maximum number of tasks that can be queued in total before
|
|
|
|
// backpressure will build up and the rests will start to block.
|
|
|
|
const PDU_STREAM_QUEUESIZE = PDU_STREAM_WORKERS * 8
|
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
type PDUStreamProvider struct {
|
2022-09-30 06:48:10 -05:00
|
|
|
DefaultStreamProvider
|
2021-01-29 10:32:54 -06:00
|
|
|
|
2022-04-19 03:46:45 -05:00
|
|
|
// userID+deviceID -> lazy loading cache
|
2022-05-06 08:33:34 -05:00
|
|
|
lazyLoadCache caching.LazyLoadCache
|
2022-05-05 03:56:03 -05:00
|
|
|
rsAPI roomserverAPI.SyncRoomserverAPI
|
2022-08-05 07:27:27 -05:00
|
|
|
notifier *notifier.Notifier
|
2021-01-29 10:32:54 -06:00
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
func (p *PDUStreamProvider) Setup(
|
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction,
|
|
|
|
) {
|
|
|
|
p.DefaultStreamProvider.Setup(ctx, snapshot)
|
2021-01-08 10:59:06 -06:00
|
|
|
|
|
|
|
p.latestMutex.Lock()
|
|
|
|
defer p.latestMutex.Unlock()
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
id, err := snapshot.MaxStreamPositionForPDUs(ctx)
|
2021-01-08 10:59:06 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.latest = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PDUStreamProvider) CompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 10:59:06 -06:00
|
|
|
req *types.SyncRequest,
|
|
|
|
) types.StreamPosition {
|
|
|
|
from := types.StreamPosition(0)
|
|
|
|
to := p.LatestPosition(ctx)
|
|
|
|
|
|
|
|
// Get the current sync position which we will base the sync response on.
|
|
|
|
// For complete syncs, we want to start at the most recent events and work
|
|
|
|
// backwards, so that we show the most recent events in the room.
|
|
|
|
r := types.Range{
|
|
|
|
From: to,
|
|
|
|
To: 0,
|
|
|
|
Backwards: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract room state and recent events for all rooms the user is joined to.
|
2023-04-19 09:50:33 -05:00
|
|
|
joinedRoomIDs, err := snapshot.RoomIDsWithMembership(ctx, req.Device.UserID, spec.Join)
|
2021-01-08 10:59:06 -06:00
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.RoomIDsWithMembership failed")
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:00:42 -06:00
|
|
|
stateFilter := req.Filter.Room.State
|
|
|
|
eventFilter := req.Filter.Room.Timeline
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
if err = p.addIgnoredUsersToFilter(ctx, snapshot, req, &eventFilter); err != nil {
|
2022-04-07 09:08:19 -05:00
|
|
|
req.Log.WithError(err).Error("unable to update event filter with ignored users")
|
|
|
|
}
|
|
|
|
|
2023-08-31 10:33:38 -05:00
|
|
|
eventFormat := synctypes.FormatSync
|
|
|
|
if req.Filter.EventFormat == synctypes.EventFormatFederation {
|
|
|
|
eventFormat = synctypes.FormatSyncFederation
|
|
|
|
}
|
|
|
|
|
2023-02-07 07:31:23 -06:00
|
|
|
recentEvents, err := snapshot.RecentEvents(ctx, joinedRoomIDs, r, &eventFilter, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
// Build up a /sync response. Add joined rooms.
|
2022-08-05 07:27:27 -05:00
|
|
|
for _, roomID := range joinedRoomIDs {
|
2023-02-07 07:31:23 -06:00
|
|
|
events := recentEvents[roomID]
|
|
|
|
// Invalidate the lazyLoadCache, otherwise we end up with missing displaynames/avatars
|
|
|
|
// TODO: This might be inefficient, when joined to many and/or large rooms.
|
2022-08-05 07:27:27 -05:00
|
|
|
joinedUsers := p.notifier.JoinedUsers(roomID)
|
|
|
|
for _, sharedUser := range joinedUsers {
|
|
|
|
p.lazyLoadCache.InvalidateLazyLoadedUser(req.Device, roomID, sharedUser)
|
|
|
|
}
|
|
|
|
|
2023-02-07 07:31:23 -06:00
|
|
|
// get the join response for each room
|
2022-09-30 06:48:10 -05:00
|
|
|
jr, jerr := p.getJoinResponseForCompleteSync(
|
2023-02-07 07:31:23 -06:00
|
|
|
ctx, snapshot, roomID, &stateFilter, req.WantFullState, req.Device, false,
|
2023-08-31 10:33:38 -05:00
|
|
|
events.Events, events.Limited, eventFormat,
|
2022-09-30 06:48:10 -05:00
|
|
|
)
|
|
|
|
if jerr != nil {
|
|
|
|
req.Log.WithError(jerr).Error("p.getJoinResponseForCompleteSync failed")
|
2022-10-28 01:17:40 -05:00
|
|
|
if ctxErr := req.Context.Err(); ctxErr != nil || jerr == sql.ErrTxDone {
|
2022-09-30 11:07:37 -05:00
|
|
|
return from
|
|
|
|
}
|
2022-10-03 05:38:20 -05:00
|
|
|
continue
|
2022-09-30 06:48:10 -05:00
|
|
|
}
|
2022-10-05 07:47:13 -05:00
|
|
|
req.Response.Rooms.Join[roomID] = jr
|
2023-04-19 09:50:33 -05:00
|
|
|
req.Rooms[roomID] = spec.Join
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add peeked rooms.
|
2022-09-30 06:48:10 -05:00
|
|
|
peeks, err := snapshot.PeeksInRange(ctx, req.Device.UserID, req.Device.ID, r)
|
2021-01-08 10:59:06 -06:00
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.DB.PeeksInRange failed")
|
|
|
|
return from
|
|
|
|
}
|
2023-02-07 07:31:23 -06:00
|
|
|
if len(peeks) > 0 {
|
|
|
|
peekRooms := make([]string, 0, len(peeks))
|
|
|
|
for _, peek := range peeks {
|
|
|
|
if !peek.Deleted {
|
|
|
|
peekRooms = append(peekRooms, peek.RoomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
recentEvents, err = snapshot.RecentEvents(ctx, peekRooms, r, &eventFilter, true, true)
|
|
|
|
if err != nil {
|
|
|
|
return from
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, roomID := range peekRooms {
|
2021-01-08 10:59:06 -06:00
|
|
|
var jr *types.JoinResponse
|
2023-02-07 07:31:23 -06:00
|
|
|
events := recentEvents[roomID]
|
2021-01-08 10:59:06 -06:00
|
|
|
jr, err = p.getJoinResponseForCompleteSync(
|
2023-02-07 07:31:23 -06:00
|
|
|
ctx, snapshot, roomID, &stateFilter, req.WantFullState, req.Device, true,
|
2023-08-31 10:33:38 -05:00
|
|
|
events.Events, events.Limited, eventFormat,
|
2021-01-08 10:59:06 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("p.getJoinResponseForCompleteSync failed")
|
2022-10-03 05:38:20 -05:00
|
|
|
if err == context.DeadlineExceeded || err == context.Canceled || err == sql.ErrTxDone {
|
2022-09-30 11:07:37 -05:00
|
|
|
return from
|
|
|
|
}
|
2022-10-03 05:38:20 -05:00
|
|
|
continue
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
2023-02-07 07:31:23 -06:00
|
|
|
req.Response.Rooms.Peek[roomID] = jr
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PDUStreamProvider) IncrementalSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 10:59:06 -06:00
|
|
|
req *types.SyncRequest,
|
|
|
|
from, to types.StreamPosition,
|
|
|
|
) (newPos types.StreamPosition) {
|
|
|
|
r := types.Range{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Backwards: from > to,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var stateDeltas []types.StateDelta
|
2022-08-19 08:32:24 -05:00
|
|
|
var syncJoinedRooms []string
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2021-01-19 12:00:42 -06:00
|
|
|
stateFilter := req.Filter.Room.State
|
|
|
|
eventFilter := req.Filter.Room.Timeline
|
2021-01-08 10:59:06 -06:00
|
|
|
|
|
|
|
if req.WantFullState {
|
2023-06-07 12:14:35 -05:00
|
|
|
if stateDeltas, syncJoinedRooms, err = snapshot.GetStateDeltasForFullStateSync(ctx, req.Device, r, req.Device.UserID, &stateFilter, p.rsAPI); err != nil {
|
2021-01-08 10:59:06 -06:00
|
|
|
req.Log.WithError(err).Error("p.DB.GetStateDeltasForFullStateSync failed")
|
2022-10-03 05:38:20 -05:00
|
|
|
return from
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
} else {
|
2023-06-07 12:14:35 -05:00
|
|
|
if stateDeltas, syncJoinedRooms, err = snapshot.GetStateDeltas(ctx, req.Device, r, req.Device.UserID, &stateFilter, p.rsAPI); err != nil {
|
2021-01-08 10:59:06 -06:00
|
|
|
req.Log.WithError(err).Error("p.DB.GetStateDeltas failed")
|
2022-10-03 05:38:20 -05:00
|
|
|
return from
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 08:32:24 -05:00
|
|
|
for _, roomID := range syncJoinedRooms {
|
2023-04-19 09:50:33 -05:00
|
|
|
req.Rooms[roomID] = spec.Join
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-03-25 07:38:16 -05:00
|
|
|
if len(stateDeltas) == 0 {
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
if err = p.addIgnoredUsersToFilter(ctx, snapshot, req, &eventFilter); err != nil {
|
2022-04-07 09:08:19 -05:00
|
|
|
req.Log.WithError(err).Error("unable to update event filter with ignored users")
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:10:46 -06:00
|
|
|
dbEvents, err := p.getRecentEvents(ctx, stateDeltas, r, eventFilter, snapshot)
|
|
|
|
if err != nil {
|
|
|
|
req.Log.WithError(err).Error("unable to get recent events")
|
|
|
|
return r.From
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:38:16 -05:00
|
|
|
newPos = from
|
2021-01-08 10:59:06 -06:00
|
|
|
for _, delta := range stateDeltas {
|
2022-08-25 07:42:47 -05:00
|
|
|
newRange := r
|
|
|
|
// If this room was joined in this sync, try to fetch
|
|
|
|
// as much timeline events as allowed by the filter.
|
|
|
|
if delta.NewlyJoined {
|
|
|
|
// Reverse the range, so we get the most recent first.
|
|
|
|
// This will be limited by the eventFilter.
|
|
|
|
newRange = types.Range{
|
|
|
|
From: r.To,
|
|
|
|
To: 0,
|
|
|
|
Backwards: true,
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 07:38:16 -05:00
|
|
|
var pos types.StreamPosition
|
2024-01-25 13:10:46 -06:00
|
|
|
if pos, err = p.addRoomDeltaToResponse(ctx, snapshot, req.Device, newRange, delta, &eventFilter, &stateFilter, req, dbEvents); err != nil {
|
2021-01-08 10:59:06 -06:00
|
|
|
req.Log.WithError(err).Error("d.addRoomDeltaToResponse failed")
|
2022-10-03 05:38:20 -05:00
|
|
|
if err == context.DeadlineExceeded || err == context.Canceled || err == sql.ErrTxDone {
|
2022-09-30 10:34:06 -05:00
|
|
|
return newPos
|
|
|
|
}
|
2022-10-03 05:38:20 -05:00
|
|
|
continue
|
2022-03-25 07:38:16 -05:00
|
|
|
}
|
2022-08-25 07:42:47 -05:00
|
|
|
// Reset the position, as it is only for the special case of newly joined rooms
|
|
|
|
if delta.NewlyJoined {
|
|
|
|
pos = newRange.From
|
|
|
|
}
|
2022-03-25 07:38:16 -05:00
|
|
|
switch {
|
|
|
|
case r.Backwards && pos < newPos:
|
|
|
|
fallthrough
|
|
|
|
case !r.Backwards && pos > newPos:
|
|
|
|
newPos = pos
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:38:16 -05:00
|
|
|
return newPos
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2024-01-25 13:10:46 -06:00
|
|
|
func (p *PDUStreamProvider) getRecentEvents(ctx context.Context, stateDeltas []types.StateDelta, r types.Range, eventFilter synctypes.RoomEventFilter, snapshot storage.DatabaseTransaction) (map[string]types.RecentEvents, error) {
|
|
|
|
var roomIDs []string
|
|
|
|
var newlyJoinedRoomIDs []string
|
|
|
|
for _, delta := range stateDeltas {
|
|
|
|
if delta.NewlyJoined {
|
|
|
|
newlyJoinedRoomIDs = append(newlyJoinedRoomIDs, delta.RoomID)
|
|
|
|
} else {
|
|
|
|
roomIDs = append(roomIDs, delta.RoomID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbEvents := make(map[string]types.RecentEvents)
|
|
|
|
if len(roomIDs) > 0 {
|
|
|
|
events, err := snapshot.RecentEvents(
|
|
|
|
ctx, roomIDs, r,
|
|
|
|
&eventFilter, true, true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range events {
|
|
|
|
dbEvents[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(newlyJoinedRoomIDs) > 0 {
|
|
|
|
// For rooms that were joined in this sync, try to fetch
|
|
|
|
// as much timeline events as allowed by the filter.
|
|
|
|
|
|
|
|
filter := eventFilter
|
|
|
|
// If we're going backwards, grep at least X events, this is mostly to satisfy Sytest
|
|
|
|
if eventFilter.Limit < recentEventBackwardsLimit {
|
|
|
|
filter.Limit = recentEventBackwardsLimit // TODO: Figure out a better way
|
|
|
|
diff := r.From - r.To
|
|
|
|
if diff > 0 && diff < recentEventBackwardsLimit {
|
|
|
|
filter.Limit = int(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := snapshot.RecentEvents(
|
|
|
|
ctx, newlyJoinedRoomIDs, types.Range{
|
|
|
|
From: r.To,
|
|
|
|
To: 0,
|
|
|
|
Backwards: true,
|
|
|
|
},
|
|
|
|
&filter, true, true,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range events {
|
|
|
|
dbEvents[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbEvents, nil
|
|
|
|
}
|
|
|
|
|
2022-10-28 06:40:51 -05:00
|
|
|
// Limit the recent events to X when going backwards
|
|
|
|
const recentEventBackwardsLimit = 100
|
|
|
|
|
2022-04-20 10:06:46 -05:00
|
|
|
// nolint:gocyclo
|
2021-01-08 10:59:06 -06:00
|
|
|
func (p *PDUStreamProvider) addRoomDeltaToResponse(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 10:59:06 -06:00
|
|
|
device *userapi.Device,
|
|
|
|
r types.Range,
|
|
|
|
delta types.StateDelta,
|
2023-04-04 12:16:53 -05:00
|
|
|
eventFilter *synctypes.RoomEventFilter,
|
|
|
|
stateFilter *synctypes.StateFilter,
|
2022-10-19 07:05:39 -05:00
|
|
|
req *types.SyncRequest,
|
2024-01-25 13:10:46 -06:00
|
|
|
dbEvents map[string]types.RecentEvents,
|
2022-03-25 07:38:16 -05:00
|
|
|
) (types.StreamPosition, error) {
|
2023-02-07 07:31:23 -06:00
|
|
|
var err error
|
|
|
|
recentStreamEvents := dbEvents[delta.RoomID].Events
|
|
|
|
limited := dbEvents[delta.RoomID].Limited
|
|
|
|
|
2023-04-28 10:00:22 -05:00
|
|
|
recEvents := gomatrixserverlib.ReverseTopologicalOrdering(
|
2023-06-07 12:14:35 -05:00
|
|
|
gomatrixserverlib.ToPDUs(snapshot.StreamEventsToEvents(ctx, device, recentStreamEvents, p.rsAPI)),
|
2022-10-31 10:14:08 -05:00
|
|
|
gomatrixserverlib.TopologicalOrderByPrevEvents,
|
|
|
|
)
|
2023-04-27 06:54:20 -05:00
|
|
|
recentEvents := make([]*rstypes.HeaderedEvent, len(recEvents))
|
|
|
|
for i := range recEvents {
|
2023-04-28 10:00:22 -05:00
|
|
|
recentEvents[i] = recEvents[i].(*rstypes.HeaderedEvent)
|
2023-04-27 06:54:20 -05:00
|
|
|
}
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2022-03-25 07:38:16 -05:00
|
|
|
// If we didn't return any events at all then don't bother doing anything else.
|
2021-01-08 10:59:06 -06:00
|
|
|
if len(recentEvents) == 0 && len(delta.StateEvents) == 0 {
|
2022-03-25 07:38:16 -05:00
|
|
|
return r.To, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Work out what the highest stream position is for all of the events in this
|
|
|
|
// room that were returned.
|
|
|
|
latestPosition := r.To
|
2022-11-04 09:39:09 -05:00
|
|
|
if r.Backwards {
|
|
|
|
latestPosition = r.From
|
|
|
|
}
|
2022-03-25 07:38:16 -05:00
|
|
|
updateLatestPosition := func(mostRecentEventID string) {
|
2022-04-19 03:46:45 -05:00
|
|
|
var pos types.StreamPosition
|
2022-09-30 06:48:10 -05:00
|
|
|
if _, pos, err = snapshot.PositionInTopology(ctx, mostRecentEventID); err == nil {
|
2022-03-25 07:38:16 -05:00
|
|
|
switch {
|
2022-08-02 03:43:48 -05:00
|
|
|
case r.Backwards && pos < latestPosition:
|
2022-03-25 07:38:16 -05:00
|
|
|
fallthrough
|
2022-08-02 03:43:48 -05:00
|
|
|
case !r.Backwards && pos > latestPosition:
|
2022-03-25 07:38:16 -05:00
|
|
|
latestPosition = pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-08 10:59:06 -06:00
|
|
|
|
2022-04-19 03:46:45 -05:00
|
|
|
if stateFilter.LazyLoadMembers {
|
|
|
|
delta.StateEvents, err = p.lazyLoadMembers(
|
2022-09-30 06:48:10 -05:00
|
|
|
ctx, snapshot, delta.RoomID, true, limited, stateFilter,
|
2022-04-19 03:46:45 -05:00
|
|
|
device, recentEvents, delta.StateEvents,
|
|
|
|
)
|
2022-04-20 10:06:46 -05:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
return r.From, fmt.Errorf("p.lazyLoadMembers: %w", err)
|
2022-04-19 03:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 10:14:38 -05:00
|
|
|
hasMembershipChange := false
|
|
|
|
for _, recentEvent := range recentStreamEvents {
|
2023-04-19 09:50:33 -05:00
|
|
|
if recentEvent.Type() == spec.MRoomMember && recentEvent.StateKey() != nil {
|
|
|
|
if membership, _ := recentEvent.Membership(); membership == spec.Join {
|
2022-10-19 07:05:39 -05:00
|
|
|
req.MembershipChanges[*recentEvent.StateKey()] = struct{}{}
|
|
|
|
}
|
2022-04-01 10:14:38 -05:00
|
|
|
hasMembershipChange = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 11:23:35 -05:00
|
|
|
// Applies the history visibility rules
|
2022-11-02 04:34:19 -05:00
|
|
|
events, err := applyHistoryVisibilityFilter(ctx, snapshot, p.rsAPI, delta.RoomID, device.UserID, recentEvents)
|
2022-08-11 11:23:35 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("unable to apply history visibility filter")
|
|
|
|
}
|
|
|
|
|
2024-01-25 13:10:46 -06:00
|
|
|
if r.Backwards && len(events) > eventFilter.Limit {
|
2022-10-25 08:15:24 -05:00
|
|
|
// We're going backwards and the events are ordered chronologically, so take the last `limit` events
|
2024-01-25 13:10:46 -06:00
|
|
|
events = events[len(events)-eventFilter.Limit:]
|
2022-10-25 08:15:24 -05:00
|
|
|
limited = true
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:56:21 -05:00
|
|
|
prevBatch, err := snapshot.GetBackwardTopologyPos(ctx, events)
|
|
|
|
if err != nil {
|
|
|
|
return r.From, fmt.Errorf("p.DB.GetBackwardTopologyPos: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-31 10:33:38 -05:00
|
|
|
eventFormat := synctypes.FormatSync
|
|
|
|
if req.Filter.EventFormat == synctypes.EventFormatFederation {
|
|
|
|
eventFormat = synctypes.FormatSyncFederation
|
|
|
|
}
|
|
|
|
|
2022-10-31 10:14:08 -05:00
|
|
|
// Now that we've filtered the timeline, work out which state events are still
|
|
|
|
// left. Anything that appears in the filtered timeline will be removed from the
|
|
|
|
// "state" section and kept in "timeline".
|
2023-04-27 06:54:20 -05:00
|
|
|
sEvents := gomatrixserverlib.HeaderedReverseTopologicalOrdering(
|
2023-04-28 10:00:22 -05:00
|
|
|
gomatrixserverlib.ToPDUs(removeDuplicates(delta.StateEvents, events)),
|
2022-10-31 10:14:08 -05:00
|
|
|
gomatrixserverlib.TopologicalOrderByAuthEvents,
|
|
|
|
)
|
2023-04-27 06:54:20 -05:00
|
|
|
delta.StateEvents = make([]*rstypes.HeaderedEvent, len(sEvents))
|
2023-07-07 15:00:10 -05:00
|
|
|
var skipped int
|
2023-04-27 06:54:20 -05:00
|
|
|
for i := range sEvents {
|
2023-06-28 13:29:49 -05:00
|
|
|
ev := sEvents[i]
|
2023-07-07 15:00:10 -05:00
|
|
|
he, ok := ev.(*rstypes.HeaderedEvent)
|
|
|
|
if !ok {
|
|
|
|
skipped++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delta.StateEvents[i-skipped] = he
|
2023-04-27 06:54:20 -05:00
|
|
|
}
|
2023-07-07 15:00:10 -05:00
|
|
|
delta.StateEvents = delta.StateEvents[:len(sEvents)-skipped]
|
2022-10-31 10:14:08 -05:00
|
|
|
|
2022-08-11 11:23:35 -05:00
|
|
|
if len(delta.StateEvents) > 0 {
|
2022-11-01 09:45:15 -05:00
|
|
|
if last := delta.StateEvents[len(delta.StateEvents)-1]; last != nil {
|
|
|
|
updateLatestPosition(last.EventID())
|
|
|
|
}
|
2022-08-11 11:23:35 -05:00
|
|
|
}
|
2022-08-25 07:42:47 -05:00
|
|
|
if len(events) > 0 {
|
2022-11-01 09:45:15 -05:00
|
|
|
if last := events[len(events)-1]; last != nil {
|
|
|
|
updateLatestPosition(last.EventID())
|
|
|
|
}
|
2022-08-25 07:42:47 -05:00
|
|
|
}
|
2022-08-11 11:23:35 -05:00
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
switch delta.Membership {
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Join:
|
2021-01-08 10:59:06 -06:00
|
|
|
jr := types.NewJoinResponse()
|
2022-04-01 10:14:38 -05:00
|
|
|
if hasMembershipChange {
|
2023-01-12 03:06:03 -06:00
|
|
|
jr.Summary, err = snapshot.GetRoomSummary(ctx, delta.RoomID, device.UserID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Warn("failed to get room summary")
|
|
|
|
}
|
2022-04-01 10:14:38 -05:00
|
|
|
}
|
2021-01-08 10:59:06 -06:00
|
|
|
jr.Timeline.PrevBatch = &prevBatch
|
2023-08-31 10:33:38 -05:00
|
|
|
jr.Timeline.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(events), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2022-08-11 11:23:35 -05:00
|
|
|
// If we are limited by the filter AND the history visibility filter
|
|
|
|
// didn't "remove" events, return that the response is limited.
|
2022-10-19 07:05:39 -05:00
|
|
|
jr.Timeline.Limited = (limited && len(events) == len(recentEvents)) || delta.NewlyJoined
|
2023-08-31 10:33:38 -05:00
|
|
|
jr.State.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(delta.StateEvents), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2022-10-19 07:05:39 -05:00
|
|
|
req.Response.Rooms.Join[delta.RoomID] = jr
|
2021-01-13 08:32:49 -06:00
|
|
|
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Peek:
|
2021-01-08 10:59:06 -06:00
|
|
|
jr := types.NewJoinResponse()
|
|
|
|
jr.Timeline.PrevBatch = &prevBatch
|
2022-08-11 11:23:35 -05:00
|
|
|
// TODO: Apply history visibility on peeked rooms
|
2023-08-31 10:33:38 -05:00
|
|
|
jr.Timeline.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(recentEvents), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2021-01-08 10:59:06 -06:00
|
|
|
jr.Timeline.Limited = limited
|
2023-08-31 10:33:38 -05:00
|
|
|
jr.State.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(delta.StateEvents), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2022-10-19 07:05:39 -05:00
|
|
|
req.Response.Rooms.Peek[delta.RoomID] = jr
|
2021-01-13 08:32:49 -06:00
|
|
|
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Leave:
|
2021-01-08 10:59:06 -06:00
|
|
|
fallthrough // transitions to leave are the same as ban
|
2021-01-13 08:32:49 -06:00
|
|
|
|
2023-04-19 09:50:33 -05:00
|
|
|
case spec.Ban:
|
2021-01-08 10:59:06 -06:00
|
|
|
lr := types.NewLeaveResponse()
|
|
|
|
lr.Timeline.PrevBatch = &prevBatch
|
2023-08-31 10:33:38 -05:00
|
|
|
lr.Timeline.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(events), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2022-08-11 11:23:35 -05:00
|
|
|
// If we are limited by the filter AND the history visibility filter
|
|
|
|
// didn't "remove" events, return that the response is limited.
|
|
|
|
lr.Timeline.Limited = limited && len(events) == len(recentEvents)
|
2023-08-31 10:33:38 -05:00
|
|
|
lr.State.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(delta.StateEvents), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2022-10-19 07:05:39 -05:00
|
|
|
req.Response.Rooms.Leave[delta.RoomID] = lr
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-03-25 07:38:16 -05:00
|
|
|
return latestPosition, nil
|
2021-01-08 10:59:06 -06:00
|
|
|
}
|
|
|
|
|
2022-08-11 11:23:35 -05:00
|
|
|
// applyHistoryVisibilityFilter gets the current room state and supplies it to ApplyHistoryVisibilityFilter, to make
|
|
|
|
// sure we always return the required events in the timeline.
|
|
|
|
func applyHistoryVisibilityFilter(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2022-08-11 11:23:35 -05:00
|
|
|
rsAPI roomserverAPI.SyncRoomserverAPI,
|
|
|
|
roomID, userID string,
|
2023-04-27 06:54:20 -05:00
|
|
|
recentEvents []*rstypes.HeaderedEvent,
|
|
|
|
) ([]*rstypes.HeaderedEvent, error) {
|
2023-01-17 03:08:23 -06:00
|
|
|
// We need to make sure we always include the latest state events, if they are in the timeline.
|
|
|
|
alwaysIncludeIDs := make(map[string]struct{})
|
|
|
|
var stateTypes []string
|
|
|
|
var senders []string
|
|
|
|
for _, ev := range recentEvents {
|
|
|
|
if ev.StateKey() != nil {
|
|
|
|
stateTypes = append(stateTypes, ev.Type())
|
2023-06-07 12:14:35 -05:00
|
|
|
senders = append(senders, string(ev.SenderID()))
|
2023-01-17 03:08:23 -06:00
|
|
|
}
|
2022-08-11 11:23:35 -05:00
|
|
|
}
|
2023-01-17 03:08:23 -06:00
|
|
|
|
|
|
|
// Only get the state again if there are state events in the timeline
|
|
|
|
if len(stateTypes) > 0 {
|
2023-04-04 12:16:53 -05:00
|
|
|
filter := synctypes.DefaultStateFilter()
|
2023-01-17 03:08:23 -06:00
|
|
|
filter.Types = &stateTypes
|
|
|
|
filter.Senders = &senders
|
|
|
|
stateEvents, err := snapshot.CurrentState(ctx, roomID, &filter, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get current room state for history visibility calculation: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ev := range stateEvents {
|
|
|
|
alwaysIncludeIDs[ev.EventID()] = struct{}{}
|
|
|
|
}
|
2022-08-11 11:23:35 -05:00
|
|
|
}
|
2023-01-17 03:08:23 -06:00
|
|
|
|
2023-08-15 06:37:04 -05:00
|
|
|
parsedUserID, err := spec.NewUserID(userID, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-11 11:23:35 -05:00
|
|
|
startTime := time.Now()
|
2023-08-15 06:37:04 -05:00
|
|
|
events, err := internal.ApplyHistoryVisibilityFilter(ctx, snapshot, rsAPI, recentEvents, alwaysIncludeIDs, *parsedUserID, "sync")
|
2022-08-11 11:23:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"duration": time.Since(startTime),
|
|
|
|
"room_id": roomID,
|
2022-08-25 07:42:47 -05:00
|
|
|
"before": len(recentEvents),
|
|
|
|
"after": len(events),
|
2023-02-07 07:31:23 -06:00
|
|
|
}).Debugf("Applied history visibility (sync)")
|
2022-08-11 11:23:35 -05:00
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
2023-06-28 13:29:49 -05:00
|
|
|
// nolint: gocyclo
|
2021-01-08 10:59:06 -06:00
|
|
|
func (p *PDUStreamProvider) getJoinResponseForCompleteSync(
|
|
|
|
ctx context.Context,
|
2022-09-30 06:48:10 -05:00
|
|
|
snapshot storage.DatabaseTransaction,
|
2021-01-08 10:59:06 -06:00
|
|
|
roomID string,
|
2023-04-04 12:16:53 -05:00
|
|
|
stateFilter *synctypes.StateFilter,
|
2021-02-04 06:20:37 -06:00
|
|
|
wantFullState bool,
|
2021-01-19 12:00:42 -06:00
|
|
|
device *userapi.Device,
|
2022-08-11 11:23:35 -05:00
|
|
|
isPeek bool,
|
2023-02-07 07:31:23 -06:00
|
|
|
recentStreamEvents []types.StreamEvent,
|
|
|
|
limited bool,
|
2023-08-31 10:33:38 -05:00
|
|
|
eventFormat synctypes.ClientEventFormat,
|
2021-01-08 10:59:06 -06:00
|
|
|
) (jr *types.JoinResponse, err error) {
|
2022-04-20 10:51:37 -05:00
|
|
|
jr = types.NewJoinResponse()
|
2021-01-08 10:59:06 -06:00
|
|
|
// TODO: When filters are added, we may need to call this multiple times to get enough events.
|
|
|
|
// See: https://github.com/matrix-org/synapse/blob/v0.19.3/synapse/handlers/sync.py#L316
|
|
|
|
|
2022-03-16 05:18:08 -05:00
|
|
|
// Work our way through the timeline events and pick out the event IDs
|
|
|
|
// of any state events that appear in the timeline. We'll specifically
|
|
|
|
// exclude them at the next step, so that we don't get duplicate state
|
|
|
|
// events in both `recentStreamEvents` and `stateEvents`.
|
|
|
|
var excludingEventIDs []string
|
|
|
|
if !wantFullState {
|
|
|
|
excludingEventIDs = make([]string, 0, len(recentStreamEvents))
|
|
|
|
for _, event := range recentStreamEvents {
|
|
|
|
if event.StateKey() != nil {
|
|
|
|
excludingEventIDs = append(excludingEventIDs, event.EventID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 06:48:10 -05:00
|
|
|
stateEvents, err := snapshot.CurrentState(ctx, roomID, stateFilter, excludingEventIDs)
|
2022-03-16 05:18:08 -05:00
|
|
|
if err != nil {
|
2023-06-02 08:48:04 -05:00
|
|
|
return jr, err
|
2022-03-16 05:18:08 -05:00
|
|
|
}
|
|
|
|
|
2023-01-12 03:06:03 -06:00
|
|
|
jr.Summary, err = snapshot.GetRoomSummary(ctx, roomID, device.UserID)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Warn("failed to get room summary")
|
|
|
|
}
|
2022-04-01 10:14:38 -05:00
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
// We don't include a device here as we don't need to send down
|
|
|
|
// transaction IDs for complete syncs, but we do it anyway because Sytest demands it for:
|
|
|
|
// "Can sync a room with a message with a transaction id" - which does a complete sync to check.
|
2023-06-07 12:14:35 -05:00
|
|
|
recentEvents := snapshot.StreamEventsToEvents(ctx, device, recentStreamEvents, p.rsAPI)
|
2022-04-19 03:46:45 -05:00
|
|
|
|
2022-08-11 11:23:35 -05:00
|
|
|
events := recentEvents
|
|
|
|
// Only apply history visibility checks if the response is for joined rooms
|
|
|
|
if !isPeek {
|
2022-11-02 04:34:19 -05:00
|
|
|
events, err = applyHistoryVisibilityFilter(ctx, snapshot, p.rsAPI, roomID, device.UserID, recentEvents)
|
2022-08-11 11:23:35 -05:00
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("unable to apply history visibility filter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are limited by the filter AND the history visibility filter
|
|
|
|
// didn't "remove" events, return that the response is limited.
|
|
|
|
limited = limited && len(events) == len(recentEvents)
|
2022-11-03 05:19:37 -05:00
|
|
|
stateEvents = removeDuplicates(stateEvents, events)
|
2022-04-19 03:46:45 -05:00
|
|
|
if stateFilter.LazyLoadMembers {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-30 06:48:10 -05:00
|
|
|
stateEvents, err = p.lazyLoadMembers(
|
|
|
|
ctx, snapshot, roomID,
|
2022-08-30 07:18:47 -05:00
|
|
|
false, limited, stateFilter,
|
2022-04-19 03:46:45 -05:00
|
|
|
device, recentEvents, stateEvents,
|
|
|
|
)
|
2022-04-20 10:51:37 -05:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2022-04-19 03:46:45 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:56:21 -05:00
|
|
|
// Retrieve the backward topology position, i.e. the position of the
|
|
|
|
// oldest event in the room's topology.
|
|
|
|
var prevBatch *types.TopologyToken
|
|
|
|
if len(events) > 0 {
|
|
|
|
var backwardTopologyPos, backwardStreamPos types.StreamPosition
|
|
|
|
event := events[0]
|
|
|
|
// If this is the beginning of the room, we can't go back further. We're going to return
|
|
|
|
// the TopologyToken from the last event instead. (Synapse returns the /sync next_Batch)
|
2023-04-19 09:50:33 -05:00
|
|
|
if event.Type() == spec.MRoomCreate && event.StateKeyEquals("") {
|
2022-11-03 11:56:21 -05:00
|
|
|
event = events[len(events)-1]
|
|
|
|
}
|
|
|
|
backwardTopologyPos, backwardStreamPos, err = snapshot.PositionInTopology(ctx, event.EventID())
|
|
|
|
if err != nil {
|
2023-06-02 08:48:04 -05:00
|
|
|
return jr, err
|
2022-11-03 11:56:21 -05:00
|
|
|
}
|
|
|
|
prevBatch = &types.TopologyToken{
|
|
|
|
Depth: backwardTopologyPos,
|
|
|
|
PDUPosition: backwardStreamPos,
|
|
|
|
}
|
|
|
|
prevBatch.Decrement()
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:59:06 -06:00
|
|
|
jr.Timeline.PrevBatch = prevBatch
|
2023-08-31 10:33:38 -05:00
|
|
|
jr.Timeline.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(events), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2022-08-11 11:23:35 -05:00
|
|
|
// If we are limited by the filter AND the history visibility filter
|
|
|
|
// didn't "remove" events, return that the response is limited.
|
|
|
|
jr.Timeline.Limited = limited && len(events) == len(recentEvents)
|
2023-08-31 10:33:38 -05:00
|
|
|
jr.State.Events = synctypes.ToClientEvents(gomatrixserverlib.ToPDUs(stateEvents), eventFormat, func(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-06 15:55:18 -05:00
|
|
|
return p.rsAPI.QueryUserIDForSender(ctx, roomID, senderID)
|
|
|
|
})
|
2021-01-08 10:59:06 -06:00
|
|
|
return jr, nil
|
|
|
|
}
|
|
|
|
|
2022-04-19 03:46:45 -05:00
|
|
|
func (p *PDUStreamProvider) lazyLoadMembers(
|
2022-09-30 06:48:10 -05:00
|
|
|
ctx context.Context, snapshot storage.DatabaseTransaction, roomID string,
|
2023-04-04 12:16:53 -05:00
|
|
|
incremental, limited bool, stateFilter *synctypes.StateFilter,
|
2022-04-19 03:46:45 -05:00
|
|
|
device *userapi.Device,
|
2023-04-27 06:54:20 -05:00
|
|
|
timelineEvents, stateEvents []*rstypes.HeaderedEvent,
|
|
|
|
) ([]*rstypes.HeaderedEvent, error) {
|
2022-04-19 03:46:45 -05:00
|
|
|
if len(timelineEvents) == 0 {
|
|
|
|
return stateEvents, nil
|
|
|
|
}
|
2023-06-14 09:23:46 -05:00
|
|
|
validRoomID, err := spec.NewRoomID(roomID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-19 03:46:45 -05:00
|
|
|
// Work out which memberships to include
|
|
|
|
timelineUsers := make(map[string]struct{})
|
|
|
|
if !incremental {
|
|
|
|
timelineUsers[device.UserID] = struct{}{}
|
|
|
|
}
|
|
|
|
// Add all users the client doesn't know about yet to a list
|
|
|
|
for _, event := range timelineEvents {
|
|
|
|
// Membership is not yet cached, add it to the list
|
2023-06-07 12:14:35 -05:00
|
|
|
if _, ok := p.lazyLoadCache.IsLazyLoadedUserCached(device, roomID, string(event.SenderID())); !ok {
|
|
|
|
timelineUsers[string(event.SenderID())] = struct{}{}
|
2022-04-19 03:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Preallocate with the same amount, even if it will end up with fewer values
|
2023-04-27 06:54:20 -05:00
|
|
|
newStateEvents := make([]*rstypes.HeaderedEvent, 0, len(stateEvents))
|
2022-04-19 03:46:45 -05:00
|
|
|
// Remove existing membership events we don't care about, e.g. users not in the timeline.events
|
|
|
|
for _, event := range stateEvents {
|
2023-04-19 09:50:33 -05:00
|
|
|
if event.Type() == spec.MRoomMember && event.StateKey() != nil {
|
2022-04-19 03:46:45 -05:00
|
|
|
// If this is a gapped incremental sync, we still want this membership
|
|
|
|
isGappedIncremental := limited && incremental
|
|
|
|
// We want this users membership event, keep it in the list
|
2023-06-12 06:19:25 -05:00
|
|
|
userID := ""
|
2023-06-14 09:23:46 -05:00
|
|
|
stateKeyUserID, queryErr := p.rsAPI.QueryUserIDForSender(ctx, *validRoomID, spec.SenderID(*event.StateKey()))
|
|
|
|
if queryErr == nil && stateKeyUserID != nil {
|
2023-06-12 06:19:25 -05:00
|
|
|
userID = stateKeyUserID.String()
|
|
|
|
}
|
|
|
|
if _, ok := timelineUsers[userID]; ok || isGappedIncremental || userID == device.UserID {
|
2022-04-19 03:46:45 -05:00
|
|
|
newStateEvents = append(newStateEvents, event)
|
2022-08-30 07:18:47 -05:00
|
|
|
if !stateFilter.IncludeRedundantMembers {
|
2023-06-12 06:19:25 -05:00
|
|
|
p.lazyLoadCache.StoreLazyLoadedUser(device, roomID, userID, event.EventID())
|
2022-04-19 03:46:45 -05:00
|
|
|
}
|
2023-06-12 06:19:25 -05:00
|
|
|
delete(timelineUsers, userID)
|
2022-04-19 03:46:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newStateEvents = append(newStateEvents, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wantUsers := make([]string, 0, len(timelineUsers))
|
|
|
|
for userID := range timelineUsers {
|
|
|
|
wantUsers = append(wantUsers, userID)
|
|
|
|
}
|
|
|
|
// Query missing membership events
|
2023-04-04 12:16:53 -05:00
|
|
|
filter := synctypes.DefaultStateFilter()
|
2022-08-16 08:42:00 -05:00
|
|
|
filter.Senders = &wantUsers
|
2023-04-19 09:50:33 -05:00
|
|
|
filter.Types = &[]string{spec.MRoomMember}
|
2022-09-30 06:48:10 -05:00
|
|
|
memberships, err := snapshot.GetStateEventsForRoom(ctx, roomID, &filter)
|
2022-04-19 03:46:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return stateEvents, err
|
|
|
|
}
|
|
|
|
// cache the membership events
|
|
|
|
for _, membership := range memberships {
|
2022-08-16 08:42:00 -05:00
|
|
|
p.lazyLoadCache.StoreLazyLoadedUser(device, roomID, *membership.StateKey(), membership.EventID())
|
2022-04-19 03:46:45 -05:00
|
|
|
}
|
|
|
|
stateEvents = append(newStateEvents, memberships...)
|
|
|
|
return stateEvents, nil
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:08:19 -05:00
|
|
|
// addIgnoredUsersToFilter adds ignored users to the eventfilter and
|
|
|
|
// the syncreq itself for further use in streams.
|
2023-04-04 12:16:53 -05:00
|
|
|
func (p *PDUStreamProvider) addIgnoredUsersToFilter(ctx context.Context, snapshot storage.DatabaseTransaction, req *types.SyncRequest, eventFilter *synctypes.RoomEventFilter) error {
|
2022-09-30 06:48:10 -05:00
|
|
|
ignores, err := snapshot.IgnoresForUser(ctx, req.Device.UserID)
|
2022-04-07 09:08:19 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.IgnoredUsers = *ignores
|
2022-04-11 02:05:23 -05:00
|
|
|
userList := make([]string, 0, len(ignores.List))
|
2022-04-07 09:08:19 -05:00
|
|
|
for userID := range ignores.List {
|
2022-04-11 02:05:23 -05:00
|
|
|
userList = append(userList, userID)
|
|
|
|
}
|
|
|
|
if len(userList) > 0 {
|
|
|
|
eventFilter.NotSenders = &userList
|
2022-04-07 09:08:19 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:00:22 -05:00
|
|
|
func removeDuplicates[T gomatrixserverlib.PDU](stateEvents, recentEvents []T) []T {
|
2021-01-08 10:59:06 -06:00
|
|
|
for _, recentEv := range recentEvents {
|
|
|
|
if recentEv.StateKey() == nil {
|
|
|
|
continue // not a state event
|
|
|
|
}
|
|
|
|
// TODO: This is a linear scan over all the current state events in this room. This will
|
|
|
|
// be slow for big rooms. We should instead sort the state events by event ID (ORDER BY)
|
|
|
|
// then do a binary search to find matching events, similar to what roomserver does.
|
|
|
|
for j := 0; j < len(stateEvents); j++ {
|
|
|
|
if stateEvents[j].EventID() == recentEv.EventID() {
|
|
|
|
// overwrite the element to remove with the last element then pop the last element.
|
|
|
|
// This is orders of magnitude faster than re-slicing, but doesn't preserve ordering
|
|
|
|
// (we don't care about the order of stateEvents)
|
|
|
|
stateEvents[j] = stateEvents[len(stateEvents)-1]
|
|
|
|
stateEvents = stateEvents[:len(stateEvents)-1]
|
|
|
|
break // there shouldn't be multiple events with the same event ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stateEvents
|
|
|
|
}
|