Move calling of channel-msg hooks to channel side

This commit is contained in:
Griatch 2021-05-09 17:01:26 +02:00
parent 05beca9196
commit 858d00c853
2 changed files with 16 additions and 15 deletions

View file

@ -965,9 +965,9 @@ class DefaultAccount(AccountDB, metaclass=TypeclassBase):
def at_pre_channel_msg(self, message, channel, senders=None, **kwargs): def at_pre_channel_msg(self, message, channel, senders=None, **kwargs):
""" """
Called by `self.channel_msg` before sending a channel message to the Called by the Channel just before passing a message into `channel_msg`.
user. This allows for customizing messages per-user and also to abort This allows for tweak messages per-user and also to abort the
the receive on the receiver-level. receive on the receiver-level.
Args: Args:
message (str): The message sent to the channel. message (str): The message sent to the channel.
@ -1020,22 +1020,13 @@ class DefaultAccount(AccountDB, metaclass=TypeclassBase):
`Channel.msg`. `Channel.msg`.
Notes: Notes:
Before this, `Channel.at_before_msg` will fire, which offers a way Before this, `Channel.at_pre_channel_msg` will fire, which offers a way
to customize the message for the receiver on the channel-level. to customize the message for the receiver on the channel-level.
""" """
# channel pre-msg hook
message = self.at_pre_channel_msg(message, channel, senders=senders, **kwargs)
if message in (None, False):
return
# the actual sending
self.msg(text=(message, {"from_channel": channel.id}), self.msg(text=(message, {"from_channel": channel.id}),
from_obj=senders, options={"from_channel": channel.id}) from_obj=senders, options={"from_channel": channel.id})
# channel post-msg hook
self.at_post_channel_msg(message, channel, senders=senders, **kwargs)
def at_post_channel_msg(self, message, channel, senders=None, **kwargs): def at_post_channel_msg(self, message, channel, senders=None, **kwargs):
""" """
Called by `self.channel_msg` after message was received. Called by `self.channel_msg` after message was received.

View file

@ -444,11 +444,21 @@ class DefaultChannel(ChannelDB, metaclass=TypeclassBase):
for receiver in receivers: for receiver in receivers:
# send to each individual subscriber # send to each individual subscriber
try: try:
# this will in turn call receiver.at_pre/post_channel_msg message = receiver.at_pre_channel_msg(message, self, **send_kwargs)
if message in (None, False):
return
receiver.channel_msg(message, self, **send_kwargs) receiver.channel_msg(message, self, **send_kwargs)
receiver.at_post_channel_msg(message, self, **send_kwargs)
except Exception: except Exception:
logger.log_trace(f"Cannot send channel message to {receiver}.") logger.log_trace(f"Error sending channel message to {receiver}.")
# post-send hook # post-send hook
self.at_post_msg(message, **send_kwargs) self.at_post_msg(message, **send_kwargs)