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

Commit 5680169

Browse files
authored
Clarify that a method returns only unthreaded receipts. (#13937)
By renaming it and updating the docstring. Additionally, refactors a method which is used only by tests.
1 parent 99a7e7e commit 5680169

File tree

4 files changed

+47
-76
lines changed

4 files changed

+47
-76
lines changed

changelog.d/13937.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Experimental support for thread-specific receipts ([MSC3771](https://github.com/matrix-org/matrix-spec-proposals/pull/3771)).

synapse/storage/databases/main/event_push_actions.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,11 @@ def _get_unread_counts_by_receipt_txn(
366366
user_id: str,
367367
) -> NotifCounts:
368368
# Get the stream ordering of the user's latest receipt in the room.
369-
result = self.get_last_receipt_for_user_txn(
369+
result = self.get_last_unthreaded_receipt_for_user_txn(
370370
txn,
371371
user_id,
372372
room_id,
373-
receipt_types=(
374-
ReceiptTypes.READ,
375-
ReceiptTypes.READ_PRIVATE,
376-
),
373+
receipt_types=(ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
377374
)
378375

379376
if result:
@@ -574,10 +571,7 @@ def _get_receipts_by_room_txn(
574571
receipt_types_clause, args = make_in_list_sql_clause(
575572
self.database_engine,
576573
"receipt_type",
577-
(
578-
ReceiptTypes.READ,
579-
ReceiptTypes.READ_PRIVATE,
580-
),
574+
(ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
581575
)
582576

583577
sql = f"""

synapse/storage/databases/main/receipts.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -135,48 +135,21 @@ def get_max_receipt_stream_id(self) -> int:
135135
"""Get the current max stream ID for receipts stream"""
136136
return self._receipts_id_gen.get_current_token()
137137

138-
async def get_last_receipt_event_id_for_user(
139-
self, user_id: str, room_id: str, receipt_types: Collection[str]
140-
) -> Optional[str]:
141-
"""
142-
Fetch the event ID for the latest receipt in a room with one of the given receipt types.
143-
144-
Args:
145-
user_id: The user to fetch receipts for.
146-
room_id: The room ID to fetch the receipt for.
147-
receipt_type: The receipt types to fetch.
148-
149-
Returns:
150-
The latest receipt, if one exists.
151-
"""
152-
result = await self.db_pool.runInteraction(
153-
"get_last_receipt_event_id_for_user",
154-
self.get_last_receipt_for_user_txn,
155-
user_id,
156-
room_id,
157-
receipt_types,
158-
)
159-
if not result:
160-
return None
161-
162-
event_id, _ = result
163-
return event_id
164-
165-
def get_last_receipt_for_user_txn(
138+
def get_last_unthreaded_receipt_for_user_txn(
166139
self,
167140
txn: LoggingTransaction,
168141
user_id: str,
169142
room_id: str,
170143
receipt_types: Collection[str],
171144
) -> Optional[Tuple[str, int]]:
172145
"""
173-
Fetch the event ID and stream_ordering for the latest receipt in a room
174-
with one of the given receipt types.
146+
Fetch the event ID and stream_ordering for the latest unthreaded receipt
147+
in a room with one of the given receipt types.
175148
176149
Args:
177150
user_id: The user to fetch receipts for.
178151
room_id: The room ID to fetch the receipt for.
179-
receipt_type: The receipt types to fetch.
152+
receipt_types: The receipt types to fetch.
180153
181154
Returns:
182155
The event ID and stream ordering of the latest receipt, if one exists.
@@ -193,6 +166,7 @@ def get_last_receipt_for_user_txn(
193166
WHERE {clause}
194167
AND user_id = ?
195168
AND room_id = ?
169+
AND thread_id IS NULL
196170
ORDER BY stream_ordering DESC
197171
LIMIT 1
198172
"""

tests/storage/test_receipts.py

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

15+
from typing import Collection, Optional
1516

1617
from synapse.api.constants import ReceiptTypes
1718
from synapse.types import UserID, create_requester
@@ -84,6 +85,33 @@ def prepare(self, reactor, clock, homeserver) -> None:
8485
)
8586
)
8687

88+
def get_last_unthreaded_receipt(
89+
self, receipt_types: Collection[str], room_id: Optional[str] = None
90+
) -> Optional[str]:
91+
"""
92+
Fetch the event ID for the latest unthreaded receipt in the test room for the test user.
93+
94+
Args:
95+
receipt_types: The receipt types to fetch.
96+
97+
Returns:
98+
The latest receipt, if one exists.
99+
"""
100+
result = self.get_success(
101+
self.store.db_pool.runInteraction(
102+
"get_last_receipt_event_id_for_user",
103+
self.store.get_last_unthreaded_receipt_for_user_txn,
104+
OUR_USER_ID,
105+
room_id or self.room_id1,
106+
receipt_types,
107+
)
108+
)
109+
if not result:
110+
return None
111+
112+
event_id, _ = result
113+
return event_id
114+
87115
def test_return_empty_with_no_data(self) -> None:
88116
res = self.get_success(
89117
self.store.get_receipts_for_user(
@@ -107,16 +135,10 @@ def test_return_empty_with_no_data(self) -> None:
107135
)
108136
self.assertEqual(res, {})
109137

110-
res = self.get_success(
111-
self.store.get_last_receipt_event_id_for_user(
112-
OUR_USER_ID,
113-
self.room_id1,
114-
[
115-
ReceiptTypes.READ,
116-
ReceiptTypes.READ_PRIVATE,
117-
],
118-
)
138+
res = self.get_last_unthreaded_receipt(
139+
[ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
119140
)
141+
120142
self.assertEqual(res, None)
121143

122144
def test_get_receipts_for_user(self) -> None:
@@ -228,29 +250,17 @@ def test_get_last_receipt_event_id_for_user(self) -> None:
228250
)
229251

230252
# Test we get the latest event when we want both private and public receipts
231-
res = self.get_success(
232-
self.store.get_last_receipt_event_id_for_user(
233-
OUR_USER_ID,
234-
self.room_id1,
235-
[ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE],
236-
)
253+
res = self.get_last_unthreaded_receipt(
254+
[ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
237255
)
238256
self.assertEqual(res, event1_2_id)
239257

240258
# Test we get the older event when we want only public receipt
241-
res = self.get_success(
242-
self.store.get_last_receipt_event_id_for_user(
243-
OUR_USER_ID, self.room_id1, [ReceiptTypes.READ]
244-
)
245-
)
259+
res = self.get_last_unthreaded_receipt([ReceiptTypes.READ])
246260
self.assertEqual(res, event1_1_id)
247261

248262
# Test we get the latest event when we want only the private receipt
249-
res = self.get_success(
250-
self.store.get_last_receipt_event_id_for_user(
251-
OUR_USER_ID, self.room_id1, [ReceiptTypes.READ_PRIVATE]
252-
)
253-
)
263+
res = self.get_last_unthreaded_receipt([ReceiptTypes.READ_PRIVATE])
254264
self.assertEqual(res, event1_2_id)
255265

256266
# Test receipt updating
@@ -259,11 +269,7 @@ def test_get_last_receipt_event_id_for_user(self) -> None:
259269
self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], None, {}
260270
)
261271
)
262-
res = self.get_success(
263-
self.store.get_last_receipt_event_id_for_user(
264-
OUR_USER_ID, self.room_id1, [ReceiptTypes.READ]
265-
)
266-
)
272+
res = self.get_last_unthreaded_receipt([ReceiptTypes.READ])
267273
self.assertEqual(res, event1_2_id)
268274

269275
# Send some events into the second room
@@ -282,11 +288,7 @@ def test_get_last_receipt_event_id_for_user(self) -> None:
282288
{},
283289
)
284290
)
285-
res = self.get_success(
286-
self.store.get_last_receipt_event_id_for_user(
287-
OUR_USER_ID,
288-
self.room_id2,
289-
[ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE],
290-
)
291+
res = self.get_last_unthreaded_receipt(
292+
[ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE], room_id=self.room_id2
291293
)
292294
self.assertEqual(res, event2_1_id)

0 commit comments

Comments
 (0)