Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions ably/pubsub/http/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async def _send_update(
version=version,
extras=message.extras,
annotations=message.annotations,
patches=message.patches,
)

# Encrypt if needed
Expand Down Expand Up @@ -265,6 +266,21 @@ async def append_message(self, message: Message, operation: MessageOperation = N
"""
return await self._send_update(message, MessageAction.MESSAGE_APPEND, operation, params)

async def patch_message(self, message: Message, operation: MessageOperation = None, params: dict = None):
"""Patches an existing message on this channel (PSRFC-44): changes parts of its data at JSON
Pointer paths, and its headers, without sending the whole message.

Parameters:
- message: Message object with a serial field, and the changes in its patches field, a
MessagePatches. Its other fields are sent as they are.
- operation: Optional MessageOperation containing description and metadata for the patch.
- params: Optional dict of query parameters.

Returns:
- UpdateDeleteResult containing the version serial of the patched message.
"""
return await self._send_update(message, MessageAction.MESSAGE_PATCH, operation, params)

async def get_message(self, serial_or_message):
"""Retrieves a single message by its serial.

Expand Down
30 changes: 30 additions & 0 deletions ably/pubsub/realtime/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ async def _send_update(
version=version,
extras=message.extras,
annotations=message.annotations,
patches=message.patches,
)

# Encrypt if needed
Expand Down Expand Up @@ -681,6 +682,35 @@ async def append_message(
"""
return await self._send_update(message, MessageAction.MESSAGE_APPEND, operation, params)

async def patch_message(
self,
message: Message,
operation: MessageOperation | None = None,
params: dict | None = None,
) -> UpdateDeleteResult:
"""Patches an existing message on this channel (PSRFC-44): changes parts of its data at JSON
Pointer paths, and its headers, without sending the whole message.

Parameters
----------
message : Message
Message object with a serial field, and the changes in its patches field, a MessagePatches.
Its other fields are sent as they are.
operation : MessageOperation, optional
Optional MessageOperation containing description and metadata for the patch.

Returns
-------
UpdateDeleteResult
Result containing the version serial of the patched message.

Raises
------
AblyException
If message serial is missing or connection/channel state prevents the patch
"""
return await self._send_update(message, MessageAction.MESSAGE_PATCH, operation, params)

async def get_message(self, serial_or_message):
"""Retrieves a single message by its serial using the REST API.

Expand Down
14 changes: 14 additions & 0 deletions ably/pubsub/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
from ably.pubsub.types.connectionstate import ConnectionEvent, ConnectionState, ConnectionStateChange
from ably.pubsub.types.device import DeviceDetails, DevicePushDetails
from ably.pubsub.types.message import Message, MessageAction, MessageVersion
from ably.pubsub.types.messagepatch import (
MessagePatchDataOperation,
MessagePatches,
MessagePatchExtras,
MessagePatchHeaderOperation,
MessagePatchPrivilegedOperation,
MessagePatchUserClaimOperation,
)
from ably.pubsub.types.operations import MessageOperation, PublishResult, UpdateDeleteResult
from ably.pubsub.types.options import Options, VCDiffDecoder
from ably.pubsub.types.presence import Presence, PresenceAction, PresenceMessage
Expand Down Expand Up @@ -326,6 +334,12 @@ def create_realtime_client(**kwargs) -> PubSubRealtimeClient:
'Message',
'MessageAction',
'MessageOperation',
'MessagePatchDataOperation',
'MessagePatchExtras',
'MessagePatchHeaderOperation',
'MessagePatchPrivilegedOperation',
'MessagePatchUserClaimOperation',
'MessagePatches',
'MessageVersion',
'Options',
'PaginatedResult',
Expand Down
14 changes: 14 additions & 0 deletions ably/pubsub/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
from ably.pubsub.sync.types.channelsubscription import PushChannelSubscription
from ably.pubsub.sync.types.device import DeviceDetails, DevicePushDetails
from ably.pubsub.sync.types.message import Message, MessageAction, MessageVersion
from ably.pubsub.sync.types.messagepatch import (
MessagePatchDataOperation,
MessagePatches,
MessagePatchExtras,
MessagePatchHeaderOperation,
MessagePatchPrivilegedOperation,
MessagePatchUserClaimOperation,
)
from ably.pubsub.sync.types.operations import MessageOperation, PublishResult, UpdateDeleteResult
from ably.pubsub.sync.types.options import Options, VCDiffDecoder
from ably.pubsub.sync.types.presence import PresenceAction, PresenceMessage, PresenceSync
Expand Down Expand Up @@ -118,6 +126,12 @@ def create_http_client(**kwargs) -> PubSubHttpClient:
'Message',
'MessageAction',
'MessageOperation',
'MessagePatchDataOperation',
'MessagePatchExtras',
'MessagePatchHeaderOperation',
'MessagePatchPrivilegedOperation',
'MessagePatchUserClaimOperation',
'MessagePatches',
'MessageVersion',
'Options',
'PaginatedResultSync',
Expand Down
40 changes: 40 additions & 0 deletions ably/pubsub/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from enum import IntEnum

from ably.pubsub.types.messagepatch import MessagePatches, apply_to_message_fields, check_patch
from ably.pubsub.types.mixins import DeltaExtras, EncodeDataMixin
from ably.pubsub.types.typedbuffer import TypedBuffer
from ably.pubsub.util.crypto import CipherData
Expand Down Expand Up @@ -133,6 +134,7 @@ class MessageAction(IntEnum):
META = 3
MESSAGE_SUMMARY = 4
MESSAGE_APPEND = 5
MESSAGE_PATCH = 6 # PSRFC-44


class Message(EncodeDataMixin):
Expand All @@ -151,6 +153,7 @@ def __init__(self,
action=None, # TM2j
version=None, # TM2s
annotations=None, # TM2t
patches=None, # TM2v: the MessagePatches of a MESSAGE_PATCH
):

super().__init__(encoding)
Expand All @@ -167,6 +170,7 @@ def __init__(self,
self.__action = action
self.__version = version
self.__annotations = annotations
self.__patches = patches

def __eq__(self, other):
if isinstance(other, Message):
Expand Down Expand Up @@ -235,6 +239,40 @@ def action(self):
def annotations(self):
return self.__annotations

@property
def patches(self):
"""The changes a MESSAGE_PATCH makes (TM2v), as MessagePatches: set by the user to send with
patch_message, and decoded on receipt per MPD4. None for other actions."""
return self.__patches

def apply_patch(self, patch: 'Message') -> 'Message':
"""Applies ``patch``, a received MESSAGE_PATCH of this message, to a copy of it (PSRFC-44), with the
same result as Ably: the data operations to its data, then the header operations and those set by Ably
to its extras. The copy has the patch's version and the action MESSAGE_UPDATE; every other field is this
message's. JSON data that arrived as text, in a string or binary, keeps every character an operation
does not change; decoded JSON is copied only along each path, so this message is unchanged.

:raises AblyException: with code 40000 if ``patch`` is not a patch of this message, or any operation
cannot be applied, in which case none is. Fetch the latest version with ``get_message(serial)``.
"""
check_patch(self.serial, patch)
data, encoding, extras = apply_to_message_fields(self.data, self.encoding, self.extras, patch.patches)
return Message(
name=self.name,
data=data,
client_id=self.client_id,
id=self.id,
connection_id=self.connection_id,
connection_key=self.connection_key,
encoding=encoding,
timestamp=self.timestamp,
extras=extras,
serial=self.serial,
action=MessageAction.MESSAGE_UPDATE,
version=patch.version,
annotations=self.annotations,
)

def encrypt(self, channel_cipher):
if isinstance(self.data, CipherData):
return
Expand Down Expand Up @@ -280,6 +318,7 @@ def as_dict(self, binary=False):
'serial': self.serial,
'action': int(self.action) if self.action is not None else None,
'annotations': self.annotations.as_dict() if self.annotations else None,
'patches': self.patches.as_dict(binary) if isinstance(self.patches, MessagePatches) else None,
**encode_data(self.data, self._encoding_array, binary),
}

Expand Down Expand Up @@ -365,6 +404,7 @@ def from_encoded(obj, cipher=None, context=None):
action=action,
version=version,
annotations=annotations,
patches=MessagePatches.from_dict(obj.get('patches')),
**decoded_data
)

Expand Down
Loading
Loading