-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hive.messaging: Factor Notifier out of hive-tell-user
- Loading branch information
Showing
7 changed files
with
60 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,10 +1,11 @@ | ||
from .channel import Channel | ||
from .channel import Channel, Notifier | ||
from .connection import Connection | ||
from .message_bus import MessageBus, Producer | ||
|
||
DEFAULT_MESSAGE_BUS = MessageBus() | ||
|
||
blocking_connection = DEFAULT_MESSAGE_BUS.blocking_connection | ||
send_to_queue = DEFAULT_MESSAGE_BUS.send_to_queue | ||
tell_user = DEFAULT_MESSAGE_BUS.tell_user | ||
|
||
producer_connection = DEFAULT_MESSAGE_BUS.producer_connection |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from .notifier import Notifier |
21 changes: 21 additions & 0 deletions
21
libs/messaging/hive/messaging/channel_services/channel_service.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from __future__ import annotations | ||
|
||
import weakref | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from ..channel import Channel | ||
|
||
|
||
class ChannelService: | ||
def __init__(self, channel: Channel): | ||
self.channel = weakref.proxy(channel) | ||
|
||
def _declared_queue(self, queue, **kwargs) -> str: | ||
"""Declare a queue, returning its name. | ||
Intended to be wrapped in a cached_property. | ||
""" | ||
self.channel.queue_declare(queue, **kwargs) | ||
return queue |
18 changes: 18 additions & 0 deletions
18
libs/messaging/hive/messaging/channel_services/notifier.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from functools import cached_property | ||
|
||
from .channel_service import ChannelService | ||
|
||
|
||
class Notifier(ChannelService): | ||
@cached_property | ||
def outbound_queue(self) -> str: | ||
return self._declared_queue( | ||
"matrix.messages.outgoing", | ||
durable=True, | ||
) | ||
|
||
def tell_user(self, message: str, format: str = "text"): | ||
self.channel.send_to_queue(self.outbound_queue, { | ||
"format": format, | ||
"messages": [message], | ||
}) |
This file contains 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 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