-
Notifications
You must be signed in to change notification settings - Fork 2
Support batched notifications #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,107 @@ | ||
| """Module for decoding received webhook notifications from Fishjam.""" | ||
|
|
||
| from typing import Union | ||
| import warnings | ||
| from typing import List, Union | ||
|
|
||
| import betterproto | ||
|
|
||
| from fishjam.events._protos.fishjam import ServerMessage | ||
| from fishjam.events._protos.fishjam import ( | ||
| ServerMessage, | ||
| ServerMessageNotificationBatch, | ||
| ) | ||
| from fishjam.events.allowed_notifications import ( | ||
| ALLOWED_NOTIFICATIONS, | ||
| AllowedNotification, | ||
| ) | ||
|
|
||
|
|
||
| def receive_binary(binary: bytes) -> Union[AllowedNotification, None]: | ||
| def _content_of(message: ServerMessage) -> Union[AllowedNotification, None]: | ||
| """Return the message's `content` oneof if it is a supported notification.""" | ||
| _which, content = betterproto.which_one_of(message, "content") | ||
| if isinstance(content, ALLOWED_NOTIFICATIONS): | ||
| return content | ||
| return None | ||
|
|
||
|
|
||
| def _unpack_batch( | ||
| batch: ServerMessageNotificationBatch, | ||
| ) -> List[AllowedNotification]: | ||
| """Flatten a notification batch into its supported notifications, in order. | ||
|
|
||
| Members whose content is not a supported notification are skipped. | ||
|
|
||
| Returns: | ||
| list[AllowedNotification]: The supported notifications from the batch. | ||
| """ | ||
| notifications = [] | ||
| for message in batch.notifications: | ||
| notification = _content_of(message) | ||
| if notification is not None: | ||
| notifications.append(notification) | ||
| return notifications | ||
|
|
||
|
|
||
| def decode_server_notifications(binary: bytes) -> List[AllowedNotification]: | ||
| """Decode a received protobuf payload into a list of notifications. | ||
|
|
||
| Handles both single notifications and batches transparently: a single | ||
| notification is returned as a one-element list, a batch is unpacked into | ||
| its members (in order), and anything unsupported yields an empty list. | ||
|
|
||
| The available notifications are listed in the `fishjam.events` module. | ||
|
|
||
| Args: | ||
| binary: The raw binary data received from the webhook. | ||
|
|
||
| Returns: | ||
| list[AllowedNotification]: The decoded notifications, in order. Empty | ||
| when the payload carries no supported notification. | ||
| """ | ||
| message = ServerMessage().parse(binary) | ||
| _which, content = betterproto.which_one_of(message, "content") | ||
|
|
||
| if isinstance(content, ServerMessageNotificationBatch): | ||
| return _unpack_batch(content) | ||
|
|
||
| if isinstance(content, ALLOWED_NOTIFICATIONS): | ||
| return [content] | ||
|
|
||
| return [] | ||
|
|
||
|
|
||
| def receive_binary( | ||
| binary: bytes, | ||
| ) -> Union[AllowedNotification, List[AllowedNotification], None]: | ||
| """Transforms a received protobuf notification into a notification instance. | ||
|
|
||
| .. deprecated:: | ||
| Use `decode_server_notifications` instead, which always returns a list | ||
| and handles batched payloads with a single, consistent return type. | ||
|
|
||
| The available notifications are listed in `fishjam.events` module. | ||
|
|
||
| Args: | ||
| binary: The raw binary data received from the webhook. | ||
|
|
||
| Returns: | ||
| AllowedNotification | None: The parsed notification object, or None if | ||
| the message type is not supported. | ||
| AllowedNotification: A single notification when the payload carries one. | ||
| list[AllowedNotification]: The unpacked notifications, in order, when the | ||
| payload is a batch (webhook batching enabled). | ||
| None: When the payload is not a supported notification. | ||
| """ | ||
| warnings.warn( | ||
| "receive_binary is deprecated; use decode_server_notifications instead.", | ||
| DeprecationWarning, | ||
| stacklevel=2, | ||
| ) | ||
|
|
||
| message = ServerMessage().parse(binary) | ||
| _which, message = betterproto.which_one_of(message, "content") | ||
| _which, content = betterproto.which_one_of(message, "content") | ||
|
|
||
| if isinstance(content, ServerMessageNotificationBatch): | ||
| return _unpack_batch(content) | ||
|
|
||
| if isinstance(message, ALLOWED_NOTIFICATIONS): | ||
| return message | ||
| if isinstance(content, ALLOWED_NOTIFICATIONS): | ||
| return content | ||
|
|
||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.