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

Commit 2ffd678

Browse files
authored
Revert #7736 (#8039)
1 parent fe6cfc8 commit 2ffd678

File tree

12 files changed

+19
-339
lines changed

12 files changed

+19
-339
lines changed

changelog.d/7736.feature

Lines changed: 0 additions & 1 deletion
This file was deleted.

changelog.d/8039.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert MSC2654 implementation because of perf issues. Please delete this line when processing the 1.19 changelog.

scripts/synapse_port_db

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ logger = logging.getLogger("synapse_port_db")
6767

6868

6969
BOOLEAN_COLUMNS = {
70-
"events": ["processed", "outlier", "contains_url", "count_as_unread"],
70+
"events": ["processed", "outlier", "contains_url"],
7171
"rooms": ["is_public"],
7272
"event_edges": ["is_state"],
7373
"presence_list": ["accepted"],

synapse/handlers/sync.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class JoinedSyncResult:
103103
account_data = attr.ib(type=List[JsonDict])
104104
unread_notifications = attr.ib(type=JsonDict)
105105
summary = attr.ib(type=Optional[JsonDict])
106-
unread_count = attr.ib(type=int)
107106

108107
def __nonzero__(self) -> bool:
109108
"""Make the result appear empty if there are no updates. This is used
@@ -1887,10 +1886,6 @@ async def _generate_room_entry(
18871886

18881887
if room_builder.rtype == "joined":
18891888
unread_notifications = {} # type: Dict[str, str]
1890-
1891-
unread_count = await self.store.get_unread_message_count_for_user(
1892-
room_id, sync_config.user.to_string(),
1893-
)
18941889
room_sync = JoinedSyncResult(
18951890
room_id=room_id,
18961891
timeline=batch,
@@ -1899,7 +1894,6 @@ async def _generate_room_entry(
18991894
account_data=account_data_events,
19001895
unread_notifications=unread_notifications,
19011896
summary=summary,
1902-
unread_count=unread_count,
19031897
)
19041898

19051899
if room_sync or always_include:

synapse/push/push_tools.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@ async def get_badge_count(store, user_id):
2121
invites = await store.get_invited_rooms_for_local_user(user_id)
2222
joins = await store.get_rooms_for_user(user_id)
2323

24+
my_receipts_by_room = await store.get_receipts_for_user(user_id, "m.read")
25+
2426
badge = len(invites)
2527

2628
for room_id in joins:
27-
unread_count = await store.get_unread_message_count_for_user(room_id, user_id)
28-
# return one badge count per conversation, as count per
29-
# message is so noisy as to be almost useless
30-
badge += 1 if unread_count else 0
29+
if room_id in my_receipts_by_room:
30+
last_unread_event_id = my_receipts_by_room[room_id]
31+
32+
notifs = await (
33+
store.get_unread_event_push_actions_by_room_for_user(
34+
room_id, user_id, last_unread_event_id
35+
)
36+
)
37+
# return one badge count per conversation, as count per
38+
# message is so noisy as to be almost useless
39+
badge += 1 if notifs["notify_count"] else 0
3140
return badge
3241

3342

synapse/rest/client/v2_alpha/sync.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ def serialize(events):
426426
result["ephemeral"] = {"events": ephemeral_events}
427427
result["unread_notifications"] = room.unread_notifications
428428
result["summary"] = room.summary
429-
result["org.matrix.msc2654.unread_count"] = room.unread_count
430429

431430
return result
432431

synapse/storage/databases/main/cache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def _invalidate_caches_for_event(
172172

173173
self.get_latest_event_ids_in_room.invalidate((room_id,))
174174

175-
self.get_unread_message_count_for_user.invalidate_many((room_id,))
176175
self.get_unread_event_push_actions_by_room_for_user.invalidate_many((room_id,))
177176

178177
if not backfilled:

synapse/storage/databases/main/events.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,47 +53,6 @@
5353
["type", "origin_type", "origin_entity"],
5454
)
5555

56-
STATE_EVENT_TYPES_TO_MARK_UNREAD = {
57-
EventTypes.Topic,
58-
EventTypes.Name,
59-
EventTypes.RoomAvatar,
60-
EventTypes.Tombstone,
61-
}
62-
63-
64-
def should_count_as_unread(event: EventBase, context: EventContext) -> bool:
65-
# Exclude rejected and soft-failed events.
66-
if context.rejected or event.internal_metadata.is_soft_failed():
67-
return False
68-
69-
# Exclude notices.
70-
if (
71-
not event.is_state()
72-
and event.type == EventTypes.Message
73-
and event.content.get("msgtype") == "m.notice"
74-
):
75-
return False
76-
77-
# Exclude edits.
78-
relates_to = event.content.get("m.relates_to", {})
79-
if relates_to.get("rel_type") == RelationTypes.REPLACE:
80-
return False
81-
82-
# Mark events that have a non-empty string body as unread.
83-
body = event.content.get("body")
84-
if isinstance(body, str) and body:
85-
return True
86-
87-
# Mark some state events as unread.
88-
if event.is_state() and event.type in STATE_EVENT_TYPES_TO_MARK_UNREAD:
89-
return True
90-
91-
# Mark encrypted events as unread.
92-
if not event.is_state() and event.type == EventTypes.Encrypted:
93-
return True
94-
95-
return False
96-
9756

9857
def encode_json(json_object):
9958
"""
@@ -239,10 +198,6 @@ def _persist_events_and_state_updates(
239198

240199
event_counter.labels(event.type, origin_type, origin_entity).inc()
241200

242-
self.store.get_unread_message_count_for_user.invalidate_many(
243-
(event.room_id,),
244-
)
245-
246201
for room_id, new_state in current_state_for_room.items():
247202
self.store.get_current_state_ids.prefill((room_id,), new_state)
248203

@@ -864,9 +819,8 @@ def event_dict(event):
864819
"contains_url": (
865820
"url" in event.content and isinstance(event.content["url"], str)
866821
),
867-
"count_as_unread": should_count_as_unread(event, context),
868822
}
869-
for event, context in events_and_contexts
823+
for event, _ in events_and_contexts
870824
],
871825
)
872826

synapse/storage/databases/main/events_worker.py

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,9 @@
4141
from synapse.replication.tcp.streams.events import EventsStream
4242
from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause
4343
from synapse.storage.database import DatabasePool
44-
from synapse.storage.types import Cursor
4544
from synapse.storage.util.id_generators import StreamIdGenerator
4645
from synapse.types import get_domain_from_id
47-
from synapse.util.caches.descriptors import (
48-
Cache,
49-
_CacheContext,
50-
cached,
51-
cachedInlineCallbacks,
52-
)
46+
from synapse.util.caches.descriptors import Cache, cached, cachedInlineCallbacks
5347
from synapse.util.iterutils import batch_iter
5448
from synapse.util.metrics import Measure
5549

@@ -1364,84 +1358,6 @@ def get_next_event_to_expire_txn(txn):
13641358
desc="get_next_event_to_expire", func=get_next_event_to_expire_txn
13651359
)
13661360

1367-
@cached(tree=True, cache_context=True)
1368-
async def get_unread_message_count_for_user(
1369-
self, room_id: str, user_id: str, cache_context: _CacheContext,
1370-
) -> int:
1371-
"""Retrieve the count of unread messages for the given room and user.
1372-
1373-
Args:
1374-
room_id: The ID of the room to count unread messages in.
1375-
user_id: The ID of the user to count unread messages for.
1376-
1377-
Returns:
1378-
The number of unread messages for the given user in the given room.
1379-
"""
1380-
with Measure(self._clock, "get_unread_message_count_for_user"):
1381-
last_read_event_id = await self.get_last_receipt_event_id_for_user(
1382-
user_id=user_id,
1383-
room_id=room_id,
1384-
receipt_type="m.read",
1385-
on_invalidate=cache_context.invalidate,
1386-
)
1387-
1388-
return await self.db_pool.runInteraction(
1389-
"get_unread_message_count_for_user",
1390-
self._get_unread_message_count_for_user_txn,
1391-
user_id,
1392-
room_id,
1393-
last_read_event_id,
1394-
)
1395-
1396-
def _get_unread_message_count_for_user_txn(
1397-
self,
1398-
txn: Cursor,
1399-
user_id: str,
1400-
room_id: str,
1401-
last_read_event_id: Optional[str],
1402-
) -> int:
1403-
if last_read_event_id:
1404-
# Get the stream ordering for the last read event.
1405-
stream_ordering = self.db_pool.simple_select_one_onecol_txn(
1406-
txn=txn,
1407-
table="events",
1408-
keyvalues={"room_id": room_id, "event_id": last_read_event_id},
1409-
retcol="stream_ordering",
1410-
)
1411-
else:
1412-
# If there's no read receipt for that room, it probably means the user hasn't
1413-
# opened it yet, in which case use the stream ID of their join event.
1414-
# We can't just set it to 0 otherwise messages from other local users from
1415-
# before this user joined will be counted as well.
1416-
txn.execute(
1417-
"""
1418-
SELECT stream_ordering FROM local_current_membership
1419-
LEFT JOIN events USING (event_id, room_id)
1420-
WHERE membership = 'join'
1421-
AND user_id = ?
1422-
AND room_id = ?
1423-
""",
1424-
(user_id, room_id),
1425-
)
1426-
row = txn.fetchone()
1427-
1428-
if row is None:
1429-
return 0
1430-
1431-
stream_ordering = row[0]
1432-
1433-
# Count the messages that qualify as unread after the stream ordering we've just
1434-
# retrieved.
1435-
sql = """
1436-
SELECT COUNT(*) FROM events
1437-
WHERE sender != ? AND room_id = ? AND stream_ordering > ? AND count_as_unread
1438-
"""
1439-
1440-
txn.execute(sql, (user_id, room_id, stream_ordering))
1441-
row = txn.fetchone()
1442-
1443-
return row[0] if row else 0
1444-
14451361

14461362
AllNewEventsResult = namedtuple(
14471363
"AllNewEventsResult",

synapse/storage/databases/main/schema/delta/58/12unread_messages.sql

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/rest/client/v1/utils.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,6 @@ def send_event(
165165

166166
return channel.json_body
167167

168-
def redact(self, room_id, event_id, txn_id=None, tok=None, expect_code=200):
169-
if txn_id is None:
170-
txn_id = "m%s" % (str(time.time()))
171-
172-
path = "/_matrix/client/r0/rooms/%s/redact/%s/%s" % (room_id, event_id, txn_id)
173-
if tok:
174-
path = path + "?access_token=%s" % tok
175-
176-
request, channel = make_request(
177-
self.hs.get_reactor(), "PUT", path, json.dumps({}).encode("utf8")
178-
)
179-
render(request, self.resource, self.hs.get_reactor())
180-
181-
assert int(channel.result["code"]) == expect_code, (
182-
"Expected: %d, got: %d, resp: %r"
183-
% (expect_code, int(channel.result["code"]), channel.result["body"])
184-
)
185-
186-
return channel.json_body
187-
188168
def _read_write_state(
189169
self,
190170
room_id: str,

0 commit comments

Comments
 (0)