From 4d2d79b4daceadfbd2aab3e2dc4f78454daed968 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 6 Apr 2022 14:33:14 +0100 Subject: [PATCH] Refactor `SharedUsers` --- syncapi/notifier/notifier.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/syncapi/notifier/notifier.go b/syncapi/notifier/notifier.go index 7c1e3c2db..099dbc0af 100644 --- a/syncapi/notifier/notifier.go +++ b/syncapi/notifier/notifier.go @@ -22,7 +22,6 @@ import ( "github.com/matrix-org/dendrite/syncapi/storage" "github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/gomatrixserverlib" - "github.com/matrix-org/util" log "github.com/sirupsen/logrus" ) @@ -253,15 +252,25 @@ func (n *Notifier) OnNewPresence( n.wakeupUsers(sharedUsers, nil, n.currPos) } -func (n *Notifier) SharedUsers(userID string) (sharedUsers []string) { +func (n *Notifier) SharedUsers(userID string) []string { n.mapLock.RLock() defer n.mapLock.RUnlock() + sharedMap := map[string]struct{}{ + userID: {}, + } for roomID, users := range n.roomIDToJoinedUsers { - if _, ok := users[userID]; ok { - sharedUsers = append(sharedUsers, n.JoinedUsers(roomID)...) + if _, ok := users[userID]; !ok { + continue + } + for _, userID := range n.JoinedUsers(roomID) { + sharedMap[userID] = struct{}{} } } - return util.UniqueStrings(sharedUsers) + sharedUsers := make([]string, 0, len(sharedMap)) + for userID := range sharedMap { + sharedUsers = append(sharedUsers, userID) + } + return sharedUsers } func (n *Notifier) IsSharedUser(userA, userB string) bool {