|
| 1 | +import logging |
| 2 | +from typing import TYPE_CHECKING, Optional |
| 3 | + |
| 4 | +from synapse.api.errors import AuthError, NotFoundError |
| 5 | +from synapse.storage.databases.main.thread_subscriptions import ThreadSubscription |
| 6 | +from synapse.types import UserID |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from synapse.server import HomeServer |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class ThreadSubscriptionsHandler: |
| 15 | + def __init__(self, hs: "HomeServer"): |
| 16 | + self.store = hs.get_datastores().main |
| 17 | + self.event_handler = hs.get_event_handler() |
| 18 | + self.auth = hs.get_auth() |
| 19 | + |
| 20 | + async def get_thread_subscription_settings( |
| 21 | + self, |
| 22 | + user_id: UserID, |
| 23 | + room_id: str, |
| 24 | + thread_root_event_id: str, |
| 25 | + ) -> Optional[ThreadSubscription]: |
| 26 | + """Get thread subscription settings for a specific thread and user. |
| 27 | + Checks that the thread root is both a real event and also that it is visible |
| 28 | + to the user. |
| 29 | +
|
| 30 | + Args: |
| 31 | + user_id: The ID of the user |
| 32 | + thread_root_event_id: The event ID of the thread root |
| 33 | +
|
| 34 | + Returns: |
| 35 | + A `ThreadSubscription` containing the active subscription settings or None if not set |
| 36 | + """ |
| 37 | + # First check that the user can access the thread root event |
| 38 | + # and that it exists |
| 39 | + try: |
| 40 | + event = await self.event_handler.get_event( |
| 41 | + user_id, room_id, thread_root_event_id |
| 42 | + ) |
| 43 | + if event is None: |
| 44 | + raise NotFoundError("No such thread root") |
| 45 | + except AuthError: |
| 46 | + raise NotFoundError("No such thread root") |
| 47 | + |
| 48 | + return await self.store.get_subscription_for_thread( |
| 49 | + user_id.to_string(), event.room_id, thread_root_event_id |
| 50 | + ) |
| 51 | + |
| 52 | + async def subscribe_user_to_thread( |
| 53 | + self, |
| 54 | + user_id: UserID, |
| 55 | + room_id: str, |
| 56 | + thread_root_event_id: str, |
| 57 | + *, |
| 58 | + automatic: bool, |
| 59 | + ) -> Optional[int]: |
| 60 | + """Sets or updates a user's subscription settings for a specific thread root. |
| 61 | +
|
| 62 | + Args: |
| 63 | + requester_user_id: The ID of the user whose settings are being updated. |
| 64 | + thread_root_event_id: The event ID of the thread root. |
| 65 | + automatic: whether the user was subscribed by an automatic decision by |
| 66 | + their client. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + The stream ID for this update, if the update isn't no-opped. |
| 70 | +
|
| 71 | + Raises: |
| 72 | + NotFoundError if the user cannot access the thread root event, or it isn't |
| 73 | + known to this homeserver. |
| 74 | + """ |
| 75 | + # First check that the user can access the thread root event |
| 76 | + # and that it exists |
| 77 | + try: |
| 78 | + event = await self.event_handler.get_event( |
| 79 | + user_id, room_id, thread_root_event_id |
| 80 | + ) |
| 81 | + if event is None: |
| 82 | + raise NotFoundError("No such thread root") |
| 83 | + except AuthError: |
| 84 | + logger.info("rejecting thread subscriptions change (thread not accessible)") |
| 85 | + raise NotFoundError("No such thread root") |
| 86 | + |
| 87 | + return await self.store.subscribe_user_to_thread( |
| 88 | + user_id.to_string(), |
| 89 | + event.room_id, |
| 90 | + thread_root_event_id, |
| 91 | + automatic=automatic, |
| 92 | + ) |
| 93 | + |
| 94 | + async def unsubscribe_user_from_thread( |
| 95 | + self, user_id: UserID, room_id: str, thread_root_event_id: str |
| 96 | + ) -> Optional[int]: |
| 97 | + """Clears a user's subscription settings for a specific thread root. |
| 98 | +
|
| 99 | + Args: |
| 100 | + requester_user_id: The ID of the user whose settings are being updated. |
| 101 | + thread_root_event_id: The event ID of the thread root. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + The stream ID for this update, if the update isn't no-opped. |
| 105 | +
|
| 106 | + Raises: |
| 107 | + NotFoundError if the user cannot access the thread root event, or it isn't |
| 108 | + known to this homeserver. |
| 109 | + """ |
| 110 | + # First check that the user can access the thread root event |
| 111 | + # and that it exists |
| 112 | + try: |
| 113 | + event = await self.event_handler.get_event( |
| 114 | + user_id, room_id, thread_root_event_id |
| 115 | + ) |
| 116 | + if event is None: |
| 117 | + raise NotFoundError("No such thread root") |
| 118 | + except AuthError: |
| 119 | + logger.info("rejecting thread subscriptions change (thread not accessible)") |
| 120 | + raise NotFoundError("No such thread root") |
| 121 | + |
| 122 | + return await self.store.unsubscribe_user_from_thread( |
| 123 | + user_id.to_string(), |
| 124 | + event.room_id, |
| 125 | + thread_root_event_id, |
| 126 | + ) |
0 commit comments