123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- # Copyright 2016 OpenMarket Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from typing import Collection, Optional
- from twisted.test.proto_helpers import MemoryReactor
- from synapse.api.constants import ReceiptTypes
- from synapse.server import HomeServer
- from synapse.types import UserID, create_requester
- from synapse.util import Clock
- from tests.test_utils.event_injection import create_event
- from tests.unittest import HomeserverTestCase
- OTHER_USER_ID = "@other:test"
- OUR_USER_ID = "@our:test"
- class ReceiptTestCase(HomeserverTestCase):
- def prepare(
- self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
- ) -> None:
- super().prepare(reactor, clock, homeserver)
- self.store = homeserver.get_datastores().main
- self.room_creator = homeserver.get_room_creation_handler()
- persist_event_storage_controller = self.hs.get_storage_controllers().persistence
- assert persist_event_storage_controller is not None
- self.persist_event_storage_controller = persist_event_storage_controller
- # Create a test user
- self.ourUser = UserID.from_string(OUR_USER_ID)
- self.ourRequester = create_requester(self.ourUser)
- # Create a second test user
- self.otherUser = UserID.from_string(OTHER_USER_ID)
- self.otherRequester = create_requester(self.otherUser)
- # Create a test room
- self.room_id1, _, _ = self.get_success(
- self.room_creator.create_room(self.ourRequester, {})
- )
- # Create a second test room
- self.room_id2, _, _ = self.get_success(
- self.room_creator.create_room(self.ourRequester, {})
- )
- # Join the second user to the first room
- memberEvent, memberEventContext = self.get_success(
- create_event(
- self.hs,
- room_id=self.room_id1,
- type="m.room.member",
- sender=self.otherRequester.user.to_string(),
- state_key=self.otherRequester.user.to_string(),
- content={"membership": "join"},
- )
- )
- self.get_success(
- self.persist_event_storage_controller.persist_event(
- memberEvent, memberEventContext
- )
- )
- # Join the second user to the second room
- memberEvent, memberEventContext = self.get_success(
- create_event(
- self.hs,
- room_id=self.room_id2,
- type="m.room.member",
- sender=self.otherRequester.user.to_string(),
- state_key=self.otherRequester.user.to_string(),
- content={"membership": "join"},
- )
- )
- self.get_success(
- self.persist_event_storage_controller.persist_event(
- memberEvent, memberEventContext
- )
- )
- def get_last_unthreaded_receipt(
- self, receipt_types: Collection[str], room_id: Optional[str] = None
- ) -> Optional[str]:
- """
- Fetch the event ID for the latest unthreaded receipt in the test room for the test user.
- Args:
- receipt_types: The receipt types to fetch.
- Returns:
- The latest receipt, if one exists.
- """
- result = self.get_success(
- self.store.db_pool.runInteraction(
- "get_last_receipt_event_id_for_user",
- self.store.get_last_unthreaded_receipt_for_user_txn,
- OUR_USER_ID,
- room_id or self.room_id1,
- receipt_types,
- )
- )
- if not result:
- return None
- event_id, _ = result
- return event_id
- def test_return_empty_with_no_data(self) -> None:
- res = self.get_success(
- self.store.get_receipts_for_user(
- OUR_USER_ID,
- [
- ReceiptTypes.READ,
- ReceiptTypes.READ_PRIVATE,
- ],
- )
- )
- self.assertEqual(res, {})
- res = self.get_success(
- self.store.get_receipts_for_user_with_orderings(
- OUR_USER_ID,
- [
- ReceiptTypes.READ,
- ReceiptTypes.READ_PRIVATE,
- ],
- )
- )
- self.assertEqual(res, {})
- res2 = self.get_last_unthreaded_receipt(
- [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
- )
- self.assertIsNone(res2)
- def test_get_receipts_for_user(self) -> None:
- # Send some events into the first room
- event1_1_id = self.create_and_send_event(
- self.room_id1, UserID.from_string(OTHER_USER_ID)
- )
- event1_2_id = self.create_and_send_event(
- self.room_id1, UserID.from_string(OTHER_USER_ID)
- )
- # Send public read receipt for the first event
- self.get_success(
- self.store.insert_receipt(
- self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], None, {}
- )
- )
- # Send private read receipt for the second event
- self.get_success(
- self.store.insert_receipt(
- self.room_id1,
- ReceiptTypes.READ_PRIVATE,
- OUR_USER_ID,
- [event1_2_id],
- None,
- {},
- )
- )
- # Test we get the latest event when we want both private and public receipts
- res = self.get_success(
- self.store.get_receipts_for_user(
- OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
- )
- )
- self.assertEqual(res, {self.room_id1: event1_2_id})
- # Test we get the older event when we want only public receipt
- res = self.get_success(
- self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
- )
- self.assertEqual(res, {self.room_id1: event1_1_id})
- # Test we get the latest event when we want only the public receipt
- res = self.get_success(
- self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ_PRIVATE])
- )
- self.assertEqual(res, {self.room_id1: event1_2_id})
- # Test receipt updating
- self.get_success(
- self.store.insert_receipt(
- self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], None, {}
- )
- )
- res = self.get_success(
- self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
- )
- self.assertEqual(res, {self.room_id1: event1_2_id})
- # Send some events into the second room
- event2_1_id = self.create_and_send_event(
- self.room_id2, UserID.from_string(OTHER_USER_ID)
- )
- # Test new room is reflected in what the method returns
- self.get_success(
- self.store.insert_receipt(
- self.room_id2,
- ReceiptTypes.READ_PRIVATE,
- OUR_USER_ID,
- [event2_1_id],
- None,
- {},
- )
- )
- res = self.get_success(
- self.store.get_receipts_for_user(
- OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
- )
- )
- self.assertEqual(res, {self.room_id1: event1_2_id, self.room_id2: event2_1_id})
- def test_get_last_receipt_event_id_for_user(self) -> None:
- # Send some events into the first room
- event1_1_id = self.create_and_send_event(
- self.room_id1, UserID.from_string(OTHER_USER_ID)
- )
- event1_2_id = self.create_and_send_event(
- self.room_id1, UserID.from_string(OTHER_USER_ID)
- )
- # Send public read receipt for the first event
- self.get_success(
- self.store.insert_receipt(
- self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], None, {}
- )
- )
- # Send private read receipt for the second event
- self.get_success(
- self.store.insert_receipt(
- self.room_id1,
- ReceiptTypes.READ_PRIVATE,
- OUR_USER_ID,
- [event1_2_id],
- None,
- {},
- )
- )
- # Test we get the latest event when we want both private and public receipts
- res = self.get_last_unthreaded_receipt(
- [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
- )
- self.assertEqual(res, event1_2_id)
- # Test we get the older event when we want only public receipt
- res = self.get_last_unthreaded_receipt([ReceiptTypes.READ])
- self.assertEqual(res, event1_1_id)
- # Test we get the latest event when we want only the private receipt
- res = self.get_last_unthreaded_receipt([ReceiptTypes.READ_PRIVATE])
- self.assertEqual(res, event1_2_id)
- # Test receipt updating
- self.get_success(
- self.store.insert_receipt(
- self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], None, {}
- )
- )
- res = self.get_last_unthreaded_receipt([ReceiptTypes.READ])
- self.assertEqual(res, event1_2_id)
- # Send some events into the second room
- event2_1_id = self.create_and_send_event(
- self.room_id2, UserID.from_string(OTHER_USER_ID)
- )
- # Test new room is reflected in what the method returns
- self.get_success(
- self.store.insert_receipt(
- self.room_id2,
- ReceiptTypes.READ_PRIVATE,
- OUR_USER_ID,
- [event2_1_id],
- None,
- {},
- )
- )
- res = self.get_last_unthreaded_receipt(
- [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE], room_id=self.room_id2
- )
- self.assertEqual(res, event2_1_id)
|