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

Commit 73cf3c3

Browse files
committed
remove support for msc2654
1 parent e53a8a5 commit 73cf3c3

File tree

5 files changed

+37
-68
lines changed

5 files changed

+37
-68
lines changed

synapse/push/bulk_push_rule_evaluator.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
Union,
2828
)
2929

30-
import attr
3130
from prometheus_client import Counter
3231

3332
from synapse.api.constants import (
@@ -108,17 +107,6 @@ def _should_count_as_unread(event: EventBase, context: EventContext) -> bool:
108107
return False
109108

110109

111-
@attr.s(slots=True, auto_attribs=True)
112-
class ActionsForUser:
113-
"""
114-
A class to hold the actions for a given event and whether the event should
115-
increment the unread count.
116-
"""
117-
118-
actions: Collection[Union[Mapping, str]]
119-
count_as_unread: bool
120-
121-
122110
class BulkPushRuleEvaluator:
123111
"""Calculates the outcome of push rules for an event for all users in the
124112
room at once.
@@ -348,8 +336,15 @@ async def _action_for_event_by_user(
348336
# (historical messages persisted in reverse-chronological order).
349337
return
350338

339+
# Disable counting as unread unless the experimental configuration is
340+
# enabled, as it can cause additional (unwanted) rows to be added to the
341+
# event_push_actions table.
342+
count_as_unread = False
343+
if self.hs.config.experimental.msc2654_enabled:
344+
count_as_unread = _should_count_as_unread(event, context)
345+
351346
rules_by_user = await self._get_rules_for_event(event)
352-
actions_by_user: Dict[str, ActionsForUser] = {}
347+
actions_by_user: Dict[str, Collection[Union[Mapping, str]]] = {}
353348

354349
room_member_count = await self.store.get_number_joined_users_in_room(
355350
event.room_id
@@ -434,22 +429,17 @@ async def _action_for_event_by_user(
434429
if not isinstance(display_name, str):
435430
display_name = None
436431

437-
actions = evaluator.run(rules, uid, display_name)
438-
439-
# check whether unread counts are enabled for this user
440-
unread_enabled = await self.store.get_feature_enabled(uid, "msc2654")
441-
if not unread_enabled:
442-
unread_enabled = self.hs.config.experimental.msc2654_enabled
432+
if count_as_unread:
433+
# Add an element for the current user if the event needs to be marked as
434+
# unread, so that add_push_actions_to_staging iterates over it.
435+
# If the event shouldn't be marked as unread but should notify the
436+
# current user, it'll be added to the dict later.
437+
actions_by_user[uid] = []
443438

444-
if unread_enabled:
445-
count_as_unread = _should_count_as_unread(event, context)
446-
else:
447-
count_as_unread = False
448-
449-
if "notify" in actions or count_as_unread:
450-
# Push rules say we should notify the user of this event or the event should
451-
# increment the unread count
452-
actions_by_user[uid] = ActionsForUser(actions, count_as_unread)
439+
actions = evaluator.run(rules, uid, display_name)
440+
if "notify" in actions:
441+
# Push rules say we should notify the user of this event
442+
actions_by_user[uid] = actions
453443

454444
# If there aren't any actions then we can skip the rest of the
455445
# processing.
@@ -477,6 +467,7 @@ async def _action_for_event_by_user(
477467
await self.store.add_push_actions_to_staging(
478468
event.event_id,
479469
actions_by_user,
470+
count_as_unread,
480471
thread_id,
481472
)
482473

synapse/rest/client/sync.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def __init__(self, hs: "HomeServer"):
100100
self.presence_handler = hs.get_presence_handler()
101101
self._server_notices_sender = hs.get_server_notices_sender()
102102
self._event_serializer = hs.get_event_client_serializer()
103+
self._msc_2654_enabled = hs.config.experimental.msc2654_enabled
103104
self._msc3773_enabled = hs.config.experimental.msc3773_enabled
104105

105106
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
@@ -260,7 +261,7 @@ async def encode_response(
260261
)
261262

262263
joined = await self.encode_joined(
263-
sync_result.joined, time_now, serialize_options, requester
264+
sync_result.joined, time_now, serialize_options
264265
)
265266

266267
invited = await self.encode_invited(
@@ -272,7 +273,7 @@ async def encode_response(
272273
)
273274

274275
archived = await self.encode_archived(
275-
sync_result.archived, time_now, serialize_options, requester
276+
sync_result.archived, time_now, serialize_options
276277
)
277278

278279
logger.debug("building sync response dict")
@@ -343,7 +344,6 @@ async def encode_joined(
343344
rooms: List[JoinedSyncResult],
344345
time_now: int,
345346
serialize_options: SerializeEventConfig,
346-
requester: Requester,
347347
) -> JsonDict:
348348
"""
349349
Encode the joined rooms in a sync result
@@ -352,7 +352,6 @@ async def encode_joined(
352352
rooms: list of sync results for rooms this user is joined to
353353
time_now: current time - used as a baseline for age calculations
354354
serialize_options: Event serializer options
355-
requester: The requester of the sync
356355
Returns:
357356
The joined rooms list, in our response format
358357
"""
@@ -363,7 +362,6 @@ async def encode_joined(
363362
time_now,
364363
joined=True,
365364
serialize_options=serialize_options,
366-
requester=requester,
367365
)
368366

369367
return joined
@@ -454,7 +452,6 @@ async def encode_archived(
454452
rooms: List[ArchivedSyncResult],
455453
time_now: int,
456454
serialize_options: SerializeEventConfig,
457-
requester: Requester,
458455
) -> JsonDict:
459456
"""
460457
Encode the archived rooms in a sync result
@@ -463,7 +460,6 @@ async def encode_archived(
463460
rooms: list of sync results for rooms this user is joined to
464461
time_now: current time - used as a baseline for age calculations
465462
serialize_options: Event serializer options
466-
requester: the requester of the sync
467463
Returns:
468464
The archived rooms list, in our response format
469465
"""
@@ -474,7 +470,6 @@ async def encode_archived(
474470
time_now,
475471
joined=False,
476472
serialize_options=serialize_options,
477-
requester=requester,
478473
)
479474

480475
return joined
@@ -485,7 +480,6 @@ async def encode_room(
485480
time_now: int,
486481
joined: bool,
487482
serialize_options: SerializeEventConfig,
488-
requester: Requester,
489483
) -> JsonDict:
490484
"""
491485
Args:
@@ -553,13 +547,7 @@ async def encode_room(
553547
] = room.unread_thread_notifications
554548
result["summary"] = room.summary
555549

556-
msc2654_enabled = await self.hs.get_datastores().main.get_feature_enabled(
557-
requester.user.to_string(), "msc2654"
558-
)
559-
if not msc2654_enabled:
560-
msc2654_enabled = self.hs.config.experimental.msc2654_enabled
561-
562-
if msc2654_enabled:
550+
if self._msc_2654_enabled:
563551
result["org.matrix.msc2654.unread_count"] = room.unread_count
564552

565553
return result

synapse/storage/databases/main/event_push_actions.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
from synapse.util.caches.descriptors import cached
106106

107107
if TYPE_CHECKING:
108-
from synapse.push.bulk_push_rule_evaluator import ActionsForUser
109108
from synapse.server import HomeServer
110109

111110
logger = logging.getLogger(__name__)
@@ -1216,7 +1215,8 @@ def _get_if_maybe_push_in_range_for_user_txn(txn: LoggingTransaction) -> bool:
12161215
async def add_push_actions_to_staging(
12171216
self,
12181217
event_id: str,
1219-
user_id_actions: Dict[str, "ActionsForUser"],
1218+
user_id_actions: Dict[str, Collection[Union[Mapping, str]]],
1219+
count_as_unread: bool,
12201220
thread_id: str,
12211221
) -> None:
12221222
"""Add the push actions for the event to the push action staging area.
@@ -1234,19 +1234,17 @@ async def add_push_actions_to_staging(
12341234
# This is a helper function for generating the necessary tuple that
12351235
# can be used to insert into the `event_push_actions_staging` table.
12361236
def _gen_entry(
1237-
user_id: str, actions_by_user: "ActionsForUser"
1237+
user_id: str, actions: Collection[Union[Mapping, str]]
12381238
) -> Tuple[str, str, str, int, int, int, str, int]:
1239-
is_highlight = 1 if _action_has_highlight(actions_by_user.actions) else 0
1240-
notif = 1 if "notify" in actions_by_user.actions else 0
1239+
is_highlight = 1 if _action_has_highlight(actions) else 0
1240+
notif = 1 if "notify" in actions else 0
12411241
return (
12421242
event_id, # event_id column
12431243
user_id, # user_id column
1244-
_serialize_action(
1245-
actions_by_user.actions, bool(is_highlight)
1246-
), # actions column
1244+
_serialize_action(actions, bool(is_highlight)), # actions column
12471245
notif, # notif column
12481246
is_highlight, # highlight column
1249-
int(actions_by_user.count_as_unread), # unread column
1247+
int(count_as_unread), # unread column
12501248
thread_id, # thread_id column
12511249
self._clock.time_msec(), # inserted_ts column
12521250
)
@@ -1264,8 +1262,8 @@ def _gen_entry(
12641262
"inserted_ts",
12651263
),
12661264
values=[
1267-
_gen_entry(user_id, actions_by_user)
1268-
for user_id, actions_by_user in user_id_actions.items()
1265+
_gen_entry(user_id, actions)
1266+
for user_id, actions in user_id_actions.items()
12691267
],
12701268
desc="add_push_actions_to_staging",
12711269
)

tests/replication/slave/storage/test_events.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from synapse.events import EventBase, _EventInternalMetadata, make_event_from_dict
2525
from synapse.events.snapshot import EventContext
2626
from synapse.handlers.room import RoomEventSource
27-
from synapse.push.bulk_push_rule_evaluator import ActionsForUser
2827
from synapse.server import HomeServer
2928
from synapse.storage.databases.main.event_push_actions import (
3029
NotifCounts,
@@ -413,10 +412,8 @@ def build_event(
413412
self.get_success(
414413
self.master_store.add_push_actions_to_staging(
415414
event.event_id,
416-
{
417-
user_id: ActionsForUser(actions, False)
418-
for user_id, actions in push_actions
419-
},
415+
dict(push_actions),
416+
False,
420417
"main",
421418
)
422419
)

tests/rest/client/test_sync.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
ReceiptTypes,
2929
RelationTypes,
3030
)
31-
from synapse.rest.admin.experimental_features import ExperimentalFeature
3231
from synapse.rest.client import devices, knock, login, read_marker, receipts, room, sync
3332
from synapse.server import HomeServer
3433
from synapse.types import JsonDict
@@ -539,12 +538,14 @@ class UnreadMessagesTestCase(unittest.HomeserverTestCase):
539538

540539
def default_config(self) -> JsonDict:
541540
config = super().default_config()
541+
config["experimental_features"] = {
542+
"msc2654_enabled": True,
543+
}
542544
return config
543545

544546
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
545547
self.url = "/sync?since=%s"
546548
self.next_batch = "s0"
547-
self.hs = hs
548549

549550
# Register the first user (used to check the unread counts).
550551
self.user_id = self.register_user("kermit", "monkey")
@@ -587,12 +588,6 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
587588

588589
def test_unread_counts(self) -> None:
589590
"""Tests that /sync returns the right value for the unread count (MSC2654)."""
590-
# add per-user flag to the DB
591-
self.get_success(
592-
self.hs.get_datastores().main.set_features_for_user(
593-
self.user_id, {ExperimentalFeature.MSC2654: True}
594-
)
595-
)
596591

597592
# Check that our own messages don't increase the unread count.
598593
self.helper.send(self.room_id, "hello", tok=self.tok)

0 commit comments

Comments
 (0)