Skip to content

Commit

Permalink
Fix exception in blacklist feature (#164)
Browse files Browse the repository at this point in the history
* fix: ensure the spam_comment handler is only called on valid targets

* test: move test configuration in conftest

* chore: add empty   blacklist_messages in default config

* chore: update CHANGELOG

* fix: put the blacklst handler on a different handler level
  • Loading branch information
TendTo authored Mar 25, 2024
1 parent f017755 commit 8aec13e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add blacklist messages feature as anti-spam

### Fixed

- Fix **spam_comment_msg** handler to avoid raising an exception when the message does not contain text

...

## [3.1.0] - 2024-02-18
Expand Down
1 change: 1 addition & 0 deletions src/spotted/config/yaml/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ post:
delete_anonymous_comments: true
reject_after_autoreply: true
autoreplies_per_page: 6
blacklist_messages: []

token: ""
bot_tag: "@bot_tag"
11 changes: 6 additions & 5 deletions src/spotted/handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async def add_commands(app: Application):
BotCommand("sban", "banna un utente"),
BotCommand("reply", "rispondi ad uno spot o un report"),
BotCommand("autoreply", "rispondi ad uno spot o un report con un messaggio automatico"),
BotCommand("reload", "ricarica la configurazione del bot"),
BotCommand("reload", "ricarica la configurazione del bot. Modificare gli handler richiede un riavvio"),
BotCommand("db_backup", "esegui il backup del database"),
BotCommand("clean_pending", "elimina tutti gli spot in sospeso"),
]
Expand Down Expand Up @@ -136,16 +136,17 @@ def add_handlers(app: Application):
)
)

app.add_handler(MessageHandler(community_filter & filters.REPLY, follow_spot_comment))

if Config.post_get("blacklist_messages") and len(Config.post_get("blacklist_messages")) > 0:
app.add_handler(
MessageHandler(
community_filter,
community_filter & filters.Text(),
spam_comment_msg,
)
),
2,
)

app.add_handler(MessageHandler(community_filter & filters.REPLY, follow_spot_comment))


def add_jobs(app: Application):
"""Adds all the jobs to be scheduled to the application
Expand Down
2 changes: 2 additions & 0 deletions src/spotted/handlers/spam_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ async def spam_comment_msg(update: Update, context: CallbackContext) -> None:
context: context passed by the handler
"""
info = EventInfo.from_message(update, context)
# The following code performs a safety check.
# The filter already ensures that the message's text contains the spam word
for message in Config.post_get("blacklist_messages"):
if message in info.message.text:
await info.message.delete()
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def setup():
{
"token": "1234567890:qY9gv7pRJgFj4EVmN3Z1gfJOgQpCbh0vmp5",
"bot_tag": "@test_bot",
"post": {"blacklist_messages": ["myspamword1", "myspamword2"]},
"debug": {
"db_file": "test_db.sqlite3",
},
Expand Down
10 changes: 2 additions & 8 deletions tests/integration/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,12 +908,6 @@ async def test_spam_comment_msg(
"""Tests the replacement of an anonymous comment.
Copies the message and deletes the original
"""
Config.override_settings(
{
"post": {"blacklist_messages": ["myspamword1", "myspamword2"]},
}
)

for word in Config.post_get("blacklist_messages"):
spam_comment = await telegram.send_message(
f"a message with the {word} will be deleted",
Expand Down Expand Up @@ -982,8 +976,8 @@ async def test_receive_follow_message(
message_thread_id=message_thread_id,
)
assert telegram.last_message.text == "Test follow"
# assert telegram.last_message.from_user.is_bot is True
# assert telegram.last_message.chat_id == user.id
assert telegram.last_message.from_user.is_bot is True
assert telegram.last_message.chat_id == user.id

async def test_skip_follow_message_same_user(
self, telegram: TelegramSimulator, published_post: Message, channel_group: Chat, user: TGUser
Expand Down

0 comments on commit 8aec13e

Please sign in to comment.