From b13cb43785aa0a392d644ffc4901dc3111616a21 Mon Sep 17 00:00:00 2001 From: Till <2353100+S7evinK@users.noreply.github.com> Date: Fri, 4 Nov 2022 13:23:00 +0100 Subject: [PATCH] Send presence to joined hosts only (#2858) Send presence events only to rooms the user is participating, not all servers we know about. Should fix #2752 --- federationapi/consumers/presence.go | 18 +++++++++++++++++- federationapi/federationapi.go | 2 +- sytest-blacklist | 3 +++ sytest-whitelist | 1 - 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/federationapi/consumers/presence.go b/federationapi/consumers/presence.go index 3445d34a9..153fc40b5 100644 --- a/federationapi/consumers/presence.go +++ b/federationapi/consumers/presence.go @@ -22,6 +22,7 @@ import ( "github.com/matrix-org/dendrite/federationapi/queue" "github.com/matrix-org/dendrite/federationapi/storage" fedTypes "github.com/matrix-org/dendrite/federationapi/types" + roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/process" @@ -39,6 +40,7 @@ type OutputPresenceConsumer struct { db storage.Database queues *queue.OutgoingQueues isLocalServerName func(gomatrixserverlib.ServerName) bool + rsAPI roomserverAPI.FederationRoomserverAPI topic string outboundPresenceEnabled bool } @@ -50,6 +52,7 @@ func NewOutputPresenceConsumer( js nats.JetStreamContext, queues *queue.OutgoingQueues, store storage.Database, + rsAPI roomserverAPI.FederationRoomserverAPI, ) *OutputPresenceConsumer { return &OutputPresenceConsumer{ ctx: process.Context(), @@ -60,6 +63,7 @@ func NewOutputPresenceConsumer( durable: cfg.Matrix.JetStream.Durable("FederationAPIPresenceConsumer"), topic: cfg.Matrix.JetStream.Prefixed(jetstream.OutputPresenceEvent), outboundPresenceEnabled: cfg.Matrix.Presence.EnableOutbound, + rsAPI: rsAPI, } } @@ -89,6 +93,16 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msgs []*nats.Msg return true } + var queryRes roomserverAPI.QueryRoomsForUserResponse + err = t.rsAPI.QueryRoomsForUser(t.ctx, &roomserverAPI.QueryRoomsForUserRequest{ + UserID: userID, + WantMembership: "join", + }, &queryRes) + if err != nil { + log.WithError(err).Error("failed to calculate joined rooms for user") + return true + } + presence := msg.Header.Get("presence") ts, err := strconv.Atoi(msg.Header.Get("last_active_ts")) @@ -96,11 +110,13 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msgs []*nats.Msg return true } - joined, err := t.db.GetAllJoinedHosts(ctx) + // send this presence to all servers who share rooms with this user. + joined, err := t.db.GetJoinedHostsForRooms(t.ctx, queryRes.RoomIDs, true) if err != nil { log.WithError(err).Error("failed to get joined hosts") return true } + if len(joined) == 0 { return true } diff --git a/federationapi/federationapi.go b/federationapi/federationapi.go index a58cba1b1..202da6c51 100644 --- a/federationapi/federationapi.go +++ b/federationapi/federationapi.go @@ -164,7 +164,7 @@ func NewInternalAPI( } presenceConsumer := consumers.NewOutputPresenceConsumer( - base.ProcessContext, cfg, js, queues, federationDB, + base.ProcessContext, cfg, js, queues, federationDB, rsAPI, ) if err = presenceConsumer.Start(); err != nil { logrus.WithError(err).Panic("failed to start presence consumer") diff --git a/sytest-blacklist b/sytest-blacklist index e2859dcb6..c35b03bd7 100644 --- a/sytest-blacklist +++ b/sytest-blacklist @@ -46,3 +46,6 @@ If a device list update goes missing, the server resyncs on the next one # Might be a bug in the test because leaves do appear :-( Leaves are present in non-gapped incremental syncs + +# Below test was passing for the wrong reason, failing correctly since #2858 +New federated private chats get full presence information (SYN-115) \ No newline at end of file diff --git a/sytest-whitelist b/sytest-whitelist index 28235b772..f4311d339 100644 --- a/sytest-whitelist +++ b/sytest-whitelist @@ -682,7 +682,6 @@ Presence changes are reported to local room members Presence changes are also reported to remote room members Presence changes to UNAVAILABLE are reported to local room members Presence changes to UNAVAILABLE are reported to remote room members -New federated private chats get full presence information (SYN-115) /upgrade copies >100 power levels to the new room Room state after a rejected message event is the same as before Room state after a rejected state event is the same as before