|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from ably.rest.annotations import RestAnnotations, construct_validate_annotation |
| 7 | +from ably.transport.websockettransport import ProtocolMessageAction |
| 8 | +from ably.types.annotation import Annotation, AnnotationAction |
| 9 | +from ably.types.channelstate import ChannelState |
| 10 | +from ably.types.flags import Flag |
| 11 | +from ably.util.eventemitter import EventEmitter |
| 12 | +from ably.util.exceptions import AblyException |
| 13 | +from ably.util.helper import is_callable_or_coroutine |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from ably.realtime.channel import RealtimeChannel |
| 17 | + from ably.realtime.connectionmanager import ConnectionManager |
| 18 | + |
| 19 | +log = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class RealtimeAnnotations: |
| 23 | + """ |
| 24 | + Provides realtime methods for managing annotations on messages, |
| 25 | + including publishing annotations and subscribing to annotation events. |
| 26 | + """ |
| 27 | + |
| 28 | + __connection_manager: ConnectionManager |
| 29 | + __channel: RealtimeChannel |
| 30 | + |
| 31 | + def __init__(self, channel: RealtimeChannel, connection_manager: ConnectionManager): |
| 32 | + """ |
| 33 | + Initialize RealtimeAnnotations. |
| 34 | +
|
| 35 | + Args: |
| 36 | + channel: The Realtime Channel this annotations instance belongs to |
| 37 | + """ |
| 38 | + self.__channel = channel |
| 39 | + self.__connection_manager = connection_manager |
| 40 | + self.__subscriptions = EventEmitter() |
| 41 | + self.__rest_annotations = RestAnnotations(channel) |
| 42 | + |
| 43 | + async def publish(self, msg_or_serial, annotation: dict | Annotation, params: dict=None): |
| 44 | + """ |
| 45 | + Publish an annotation on a message via the realtime connection. |
| 46 | +
|
| 47 | + Args: |
| 48 | + msg_or_serial: Either a message serial (string) or a Message object |
| 49 | + annotation: Dict containing annotation properties (type, name, data, etc.) or Annotation object |
| 50 | + params: Optional dict of query parameters |
| 51 | +
|
| 52 | + Returns: |
| 53 | + None |
| 54 | +
|
| 55 | + Raises: |
| 56 | + AblyException: If the request fails, inputs are invalid, or channel is in unpublishable state |
| 57 | + """ |
| 58 | + annotation = construct_validate_annotation(msg_or_serial, annotation) |
| 59 | + |
| 60 | + # Check if channel and connection are in publishable state |
| 61 | + self.__channel._throw_if_unpublishable_state() |
| 62 | + |
| 63 | + log.info( |
| 64 | + f'RealtimeAnnotations.publish(), channelName = {self.__channel.name}, ' |
| 65 | + f'sending annotation with messageSerial = {annotation.message_serial}, ' |
| 66 | + f'type = {annotation.type}' |
| 67 | + ) |
| 68 | + |
| 69 | + # Convert to wire format (array of annotations) |
| 70 | + wire_annotation = annotation.as_dict(binary=self.__channel.ably.options.use_binary_protocol) |
| 71 | + |
| 72 | + # Build protocol message |
| 73 | + protocol_message = { |
| 74 | + "action": ProtocolMessageAction.ANNOTATION, |
| 75 | + "channel": self.__channel.name, |
| 76 | + "annotations": [wire_annotation], |
| 77 | + } |
| 78 | + |
| 79 | + if params: |
| 80 | + # Stringify boolean params |
| 81 | + stringified_params = {k: str(v).lower() if isinstance(v, bool) else v for k, v in params.items()} |
| 82 | + protocol_message["params"] = stringified_params |
| 83 | + |
| 84 | + # Send via WebSocket |
| 85 | + await self.__connection_manager.send_protocol_message(protocol_message) |
| 86 | + |
| 87 | + async def delete(self, msg_or_serial, annotation: dict | Annotation, params=None, timeout=None): |
| 88 | + """ |
| 89 | + Delete an annotation on a message. |
| 90 | +
|
| 91 | + This is a convenience method that sets the action to 'annotation.delete' |
| 92 | + and calls publish(). |
| 93 | +
|
| 94 | + Args: |
| 95 | + msg_or_serial: Either a message serial (string) or a Message object |
| 96 | + annotation: Dict containing annotation properties or Annotation object |
| 97 | + params: Optional dict of query parameters |
| 98 | + timeout: Optional timeout (not used for realtime, kept for compatibility) |
| 99 | +
|
| 100 | + Returns: |
| 101 | + None |
| 102 | +
|
| 103 | + Raises: |
| 104 | + AblyException: If the request fails or inputs are invalid |
| 105 | + """ |
| 106 | + if isinstance(annotation, Annotation): |
| 107 | + annotation_values = annotation.as_dict() |
| 108 | + else: |
| 109 | + annotation_values = annotation.copy() |
| 110 | + annotation_values['action'] = AnnotationAction.ANNOTATION_DELETE |
| 111 | + return await self.publish(msg_or_serial, annotation_values, params) |
| 112 | + |
| 113 | + async def subscribe(self, *args): |
| 114 | + """ |
| 115 | + Subscribe to annotation events on this channel. |
| 116 | +
|
| 117 | + Parameters |
| 118 | + ---------- |
| 119 | + *args: type, listener |
| 120 | + Subscribe type and listener |
| 121 | +
|
| 122 | + arg1(type): str, optional |
| 123 | + Subscribe to annotations of the given type |
| 124 | +
|
| 125 | + arg2(listener): callable |
| 126 | + Subscribe to all annotations on the channel |
| 127 | +
|
| 128 | + When no type is provided, arg1 is used as the listener. |
| 129 | +
|
| 130 | + Raises |
| 131 | + ------ |
| 132 | + AblyException |
| 133 | + If unable to subscribe due to invalid channel state or missing ANNOTATION_SUBSCRIBE mode |
| 134 | + ValueError |
| 135 | + If no valid subscribe arguments are passed |
| 136 | + """ |
| 137 | + # Parse arguments similar to channel.subscribe |
| 138 | + if len(args) == 0: |
| 139 | + raise ValueError("annotations.subscribe called without arguments") |
| 140 | + |
| 141 | + if len(args) >= 2 and isinstance(args[0], str): |
| 142 | + annotation_type = args[0] |
| 143 | + if not args[1]: |
| 144 | + raise ValueError("annotations.subscribe called without listener") |
| 145 | + if not is_callable_or_coroutine(args[1]): |
| 146 | + raise ValueError("subscribe listener must be function or coroutine function") |
| 147 | + listener = args[1] |
| 148 | + elif is_callable_or_coroutine(args[0]): |
| 149 | + listener = args[0] |
| 150 | + annotation_type = None |
| 151 | + else: |
| 152 | + raise ValueError('invalid subscribe arguments') |
| 153 | + |
| 154 | + # Register subscription |
| 155 | + if annotation_type is not None: |
| 156 | + self.__subscriptions.on(annotation_type, listener) |
| 157 | + else: |
| 158 | + self.__subscriptions.on(listener) |
| 159 | + |
| 160 | + await self.__channel.attach() |
| 161 | + |
| 162 | + # Check if ANNOTATION_SUBSCRIBE mode is enabled |
| 163 | + if self.__channel.state == ChannelState.ATTACHED: |
| 164 | + if not Flag.ANNOTATION_SUBSCRIBE in self.__channel.modes: |
| 165 | + raise AblyException( |
| 166 | + "You are trying to add an annotation listener, but you haven't requested the " |
| 167 | + "annotation_subscribe channel mode in ChannelOptions, so this won't do anything " |
| 168 | + "(we only deliver annotations to clients who have explicitly requested them)", |
| 169 | + 93001, |
| 170 | + 400 |
| 171 | + ) |
| 172 | + |
| 173 | + def unsubscribe(self, *args): |
| 174 | + """ |
| 175 | + Unsubscribe from annotation events on this channel. |
| 176 | +
|
| 177 | + Parameters |
| 178 | + ---------- |
| 179 | + *args: type, listener |
| 180 | + Unsubscribe type and listener |
| 181 | +
|
| 182 | + arg1(type): str, optional |
| 183 | + Unsubscribe from annotations of the given type |
| 184 | +
|
| 185 | + arg2(listener): callable |
| 186 | + Unsubscribe from all annotations on the channel |
| 187 | +
|
| 188 | + When no type is provided, arg1 is used as the listener. |
| 189 | +
|
| 190 | + Raises |
| 191 | + ------ |
| 192 | + ValueError |
| 193 | + If no valid unsubscribe arguments are passed |
| 194 | + """ |
| 195 | + if len(args) == 0: |
| 196 | + raise ValueError("annotations.unsubscribe called without arguments") |
| 197 | + |
| 198 | + if len(args) >= 2 and isinstance(args[0], str): |
| 199 | + annotation_type = args[0] |
| 200 | + listener = args[1] |
| 201 | + self.__subscriptions.off(annotation_type, listener) |
| 202 | + elif is_callable_or_coroutine(args[0]): |
| 203 | + listener = args[0] |
| 204 | + self.__subscriptions.off(listener) |
| 205 | + else: |
| 206 | + raise ValueError('invalid unsubscribe arguments') |
| 207 | + |
| 208 | + def _process_incoming(self, incoming_annotations): |
| 209 | + """ |
| 210 | + Process incoming annotations from the server. |
| 211 | +
|
| 212 | + This is called internally when ANNOTATION protocol messages are received. |
| 213 | +
|
| 214 | + Args: |
| 215 | + incoming_annotations: List of Annotation objects received from the server |
| 216 | + """ |
| 217 | + for annotation in incoming_annotations: |
| 218 | + # Emit to type-specific listeners and catch-all listeners |
| 219 | + annotation_type = annotation.type or '' |
| 220 | + self.__subscriptions._emit(annotation_type, annotation) |
| 221 | + |
| 222 | + async def get(self, msg_or_serial, params=None): |
| 223 | + """ |
| 224 | + Retrieve annotations for a message with pagination support. |
| 225 | +
|
| 226 | + This delegates to the REST implementation. |
| 227 | +
|
| 228 | + Args: |
| 229 | + msg_or_serial: Either a message serial (string) or a Message object |
| 230 | + params: Optional dict of query parameters (limit, start, end, direction) |
| 231 | +
|
| 232 | + Returns: |
| 233 | + PaginatedResult: A paginated result containing Annotation objects |
| 234 | +
|
| 235 | + Raises: |
| 236 | + AblyException: If the request fails or serial is invalid |
| 237 | + """ |
| 238 | + # Delegate to REST implementation |
| 239 | + return await self.__rest_annotations.get(msg_or_serial, params) |
0 commit comments