Skip to content

Commit

Permalink
Replace message getters with message template properties
Browse files Browse the repository at this point in the history
  • Loading branch information
SnoopJ committed Mar 26, 2023
1 parent 3a7063b commit d1b5c58
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 58 deletions.
6 changes: 3 additions & 3 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,13 @@ def rate_limit_info(self, rule: plugin_rules.Rule, trigger: Trigger) -> plugin_r

# TODO: these functions call now() and probably shouldn't
if rule.is_user_rate_limited(trigger.nick):
template = rule.get_user_rate_message(trigger.nick)
template = rule.user_rate_template
rate_limit_sec = rule._user_rate_limit
elif rule.is_channel_rate_limited(trigger.sender):
template = rule.get_channel_rate_message(trigger.nick, trigger.sender)
template = rule.channel_rate_template
rate_limit_sec = rule._channel_rate_limit
elif rule.is_global_rate_limited():
template = rule.get_global_rate_message(trigger.nick)
template = rule.global_rate_template
rate_limit_sec = rule._global_rate_limit
else:
return plugin_rules.RateLimitCheckInfo()
Expand Down
50 changes: 18 additions & 32 deletions sopel/plugins/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,38 +754,33 @@ def is_global_rate_limited(self) -> bool:
:return: ``True`` when the rule reached the limit, ``False`` otherwise
"""

@property
@abc.abstractmethod
def get_user_rate_message(self, nick: Identifier) -> Optional[str]:
"""Give the message to send with a NOTICE to ``nick``.
def user_rate_template(self) -> Optional[str]:
"""Give the message template to send with a NOTICE to ``nick``.
:param nick: the nick that is rate limited
:return: A formatted string, or ``None`` if no message is set.
This method is called by the bot when a trigger hits the user rate
This property is accessed by the bot when a trigger hits the user rate
limit (i.e. for the specificed ``nick``).
"""

@property
@abc.abstractmethod
def get_channel_rate_message(
self,
nick: Identifier,
channel: Identifier,
) -> Optional[str]:
"""Give the message to send with a NOTICE to ``nick``.
def channel_rate_template(self) -> Optional[str]:
"""Give the message template to send with a NOTICE to ``nick``.
:param nick: the nick that reached the channel's rate limit
:param channel: the channel that is rate limited
:return: A formatted string, or ``None`` if no message is set.
This method is called by the bot when a trigger hits the channel rate
limit (i.e. for the specificed ``channel``).
"""

@property
@abc.abstractmethod
def get_global_rate_message(self, nick: Identifier) -> Optional[str]:
def global_rate_template(self) -> Optional[str]:
"""Give the message to send with a NOTICE to ``nick``.
:param nick: the nick that reached the global rate limit
:return: A formatted string, or ``None`` if no message is set.
This method is called by the bot when a trigger hits the global rate
Expand Down Expand Up @@ -1166,28 +1161,19 @@ def is_global_rate_limited(self):
rate_limit = datetime.timedelta(seconds=self._global_rate_limit)
return self._metrics_global.is_limited(now - rate_limit)

def get_user_rate_message(self, nick):
template = self._user_rate_message or self._default_rate_message

if not template:
return None

@property
def user_rate_template(self):
template = self._user_rate_message or self._default_rate_message or None
return template

def get_channel_rate_message(self, nick, channel):
template = self._channel_rate_message or self._default_rate_message

if not template:
return None

@property
def channel_rate_template(self):
template = self._channel_rate_message or self._default_rate_message or None
return template

def get_global_rate_message(self, nick):
template = self._global_rate_message or self._default_rate_message

if not template:
return None

@property
def global_rate_template(self):
template = self._global_rate_message or self._default_rate_message or None
return template

def execute(self, bot, trigger):
Expand Down
38 changes: 15 additions & 23 deletions test/plugins/test_plugins_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,13 +1649,9 @@ def handler(bot, trigger):
global_rate_message='Server message: {nick}',
default_rate_message='Default message: {nick}',
)
assert rule.get_user_rate_message(mocktrigger.nick) == 'User message: {nick}'
assert rule.get_channel_rate_message(
mocktrigger.nick, mocktrigger.sender
) == 'Channel message: {nick}/{channel}'
assert rule.get_global_rate_message(
mocktrigger.nick
) == 'Server message: {nick}'
assert rule.user_rate_template == 'User message: {nick}'
assert rule.channel_rate_template == 'Channel message: {nick}/{channel}'
assert rule.global_rate_template == 'Server message: {nick}'


def test_rule_rate_limit_messages_default(mockbot, triggerfactory):
Expand All @@ -1675,10 +1671,9 @@ def handler(bot, trigger):
channel_rate_limit=20,
default_rate_message='Default message',
)
assert rule.get_user_rate_message(mocktrigger.nick) == 'Default message'
assert rule.get_channel_rate_message(
mocktrigger.nick, mocktrigger.sender) == 'Default message'
assert rule.get_global_rate_message(mocktrigger.nick) == 'Default message'
assert rule.user_rate_template == 'Default message'
assert rule.channel_rate_template == 'Default message'
assert rule.global_rate_template == 'Default message'


def test_rule_rate_limit_messages_default_mixed(mockbot, triggerfactory):
Expand All @@ -1699,10 +1694,9 @@ def handler(bot, trigger):
user_rate_message='User message.',
default_rate_message='The default.',
)
assert rule.get_user_rate_message(mocktrigger.nick) == 'User message.'
assert rule.get_channel_rate_message(
mocktrigger.nick, mocktrigger.sender) == 'The default.'
assert rule.get_global_rate_message(mocktrigger.nick) == 'The default.'
assert rule.user_rate_template == 'User message.'
assert rule.channel_rate_template == 'The default.'
assert rule.global_rate_template == 'The default.'

rule = rules.Rule(
[regex],
Expand All @@ -1713,10 +1707,9 @@ def handler(bot, trigger):
channel_rate_message='Channel message.',
default_rate_message='The default.',
)
assert rule.get_user_rate_message(mocktrigger.nick) == 'The default.'
assert rule.get_channel_rate_message(
mocktrigger.nick, mocktrigger.sender) == 'Channel message.'
assert rule.get_global_rate_message(mocktrigger.nick) == 'The default.'
assert rule.user_rate_template == 'The default.'
assert rule.channel_rate_template == 'Channel message.'
assert rule.global_rate_template == 'The default.'

rule = rules.Rule(
[regex],
Expand All @@ -1727,10 +1720,9 @@ def handler(bot, trigger):
global_rate_message='Server message.',
default_rate_message='The default.',
)
assert rule.get_user_rate_message(mocktrigger.nick) == 'The default.'
assert rule.get_channel_rate_message(
mocktrigger.nick, mocktrigger.sender) == 'The default.'
assert rule.get_global_rate_message(mocktrigger.nick) == 'Server message.'
assert rule.user_rate_template == 'The default.'
assert rule.channel_rate_template == 'The default.'
assert rule.global_rate_template == 'Server message.'


# -----------------------------------------------------------------------------
Expand Down

0 comments on commit d1b5c58

Please sign in to comment.