Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7e8d455

Browse files
authored
Fix a bug in the send_local_online_presence_to module API (#14880)
Destination was being used incorrectly (a single destination instead of a list of destinations was being passed). This also updates some of the types in the area to not use Collection[str], which is a footgun.
1 parent 3c3ba31 commit 7e8d455

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

changelog.d/14880.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug when using the `send_local_online_presence_to` module API.

synapse/handlers/presence.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@
6464
from synapse.replication.tcp.streams import PresenceFederationStream, PresenceStream
6565
from synapse.storage.databases.main import DataStore
6666
from synapse.streams import EventSource
67-
from synapse.types import JsonDict, StreamKeyType, UserID, get_domain_from_id
67+
from synapse.types import (
68+
JsonDict,
69+
StrCollection,
70+
StreamKeyType,
71+
UserID,
72+
get_domain_from_id,
73+
)
6874
from synapse.util.async_helpers import Linearizer
6975
from synapse.util.metrics import Measure
7076
from synapse.util.wheel_timer import WheelTimer
@@ -320,7 +326,7 @@ async def maybe_send_presence_to_interested_destinations(
320326
for destination, host_states in hosts_to_states.items():
321327
self._federation.send_presence_to_destinations(host_states, [destination])
322328

323-
async def send_full_presence_to_users(self, user_ids: Collection[str]) -> None:
329+
async def send_full_presence_to_users(self, user_ids: StrCollection) -> None:
324330
"""
325331
Adds to the list of users who should receive a full snapshot of presence
326332
upon their next sync. Note that this only works for local users.
@@ -1601,7 +1607,7 @@ async def get_new_events(
16011607
# Having a default limit doesn't match the EventSource API, but some
16021608
# callers do not provide it. It is unused in this class.
16031609
limit: int = 0,
1604-
room_ids: Optional[Collection[str]] = None,
1610+
room_ids: Optional[StrCollection] = None,
16051611
is_guest: bool = False,
16061612
explicit_room_id: Optional[str] = None,
16071613
include_offline: bool = True,
@@ -1688,7 +1694,7 @@ async def get_new_events(
16881694

16891695
# The set of users that we're interested in and that have had a presence update.
16901696
# We'll actually pull the presence updates for these users at the end.
1691-
interested_and_updated_users: Collection[str]
1697+
interested_and_updated_users: StrCollection
16921698

16931699
if from_key is not None:
16941700
# First get all users that have had a presence update
@@ -2120,7 +2126,7 @@ def __init__(self, hs: "HomeServer", presence_handler: BasePresenceHandler):
21202126
# stream_id, destinations, user_ids)`. We don't store the full states
21212127
# for efficiency, and remote workers will already have the full states
21222128
# cached.
2123-
self._queue: List[Tuple[int, int, Collection[str], Set[str]]] = []
2129+
self._queue: List[Tuple[int, int, StrCollection, Set[str]]] = []
21242130

21252131
self._next_id = 1
21262132

@@ -2142,7 +2148,7 @@ def _clear_queue(self) -> None:
21422148
self._queue = self._queue[index:]
21432149

21442150
def send_presence_to_destinations(
2145-
self, states: Collection[UserPresenceState], destinations: Collection[str]
2151+
self, states: Collection[UserPresenceState], destinations: StrCollection
21462152
) -> None:
21472153
"""Send the presence states to the given destinations.
21482154

synapse/module_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ async def send_local_online_presence_to(self, users: Iterable[str]) -> None:
11581158
# Send to remote destinations.
11591159
destination = UserID.from_string(user).domain
11601160
presence_handler.get_federation_queue().send_presence_to_destinations(
1161-
presence_events, destination
1161+
presence_events, [destination]
11621162
)
11631163

11641164
def looping_background_call(

synapse/notifier.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
JsonDict,
4747
PersistedEventPosition,
4848
RoomStreamToken,
49+
StrCollection,
4950
StreamKeyType,
5051
StreamToken,
5152
UserID,
@@ -716,7 +717,7 @@ async def check_for_updates(
716717

717718
async def _get_room_ids(
718719
self, user: UserID, explicit_room_id: Optional[str]
719-
) -> Tuple[Collection[str], bool]:
720+
) -> Tuple[StrCollection, bool]:
720721
joined_room_ids = await self.store.get_rooms_for_user(user.to_string())
721722
if explicit_room_id:
722723
if explicit_room_id in joined_room_ids:

synapse/streams/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Collection, Generic, List, Optional, Tuple, TypeVar
15+
from typing import Generic, List, Optional, Tuple, TypeVar
1616

17-
from synapse.types import UserID
17+
from synapse.types import StrCollection, UserID
1818

1919
# The key, this is either a stream token or int.
2020
K = TypeVar("K")
@@ -28,7 +28,7 @@ async def get_new_events(
2828
user: UserID,
2929
from_key: K,
3030
limit: int,
31-
room_ids: Collection[str],
31+
room_ids: StrCollection,
3232
is_guest: bool,
3333
explicit_room_id: Optional[str] = None,
3434
) -> Tuple[List[R], K]:

0 commit comments

Comments
 (0)