Skip to content

Commit

Permalink
chore: improve ban system (#151)
Browse files Browse the repository at this point in the history
* chore: improve ban system

* refactor: use 'post' as keyword

* refactor: better handling sban error

* chore: fix lint
  • Loading branch information
LightDestory authored Dec 13, 2023
1 parent 3fa6c34 commit 4d5bf02
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
29 changes: 21 additions & 8 deletions src/spotted/handlers/ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from telegram import Update
from telegram.ext import CallbackContext

from spotted.data import PendingPost, User
from spotted.data import PendingPost, Report, User
from spotted.utils import EventInfo


Expand All @@ -16,14 +16,27 @@ async def ban_cmd(update: Update, context: CallbackContext):
"""
info = EventInfo.from_message(update, context)
g_message_id = update.message.reply_to_message.message_id
pending_post = PendingPost.from_group(admin_group_id=info.chat_id, g_message_id=g_message_id)

if pending_post is None:
await info.bot.send_message(chat_id=info.chat_id, text="Per bannare qualcuno, rispondi al suo post con /ban")
user_id = -1
if (pending_post := PendingPost.from_group(admin_group_id=info.chat_id, g_message_id=g_message_id)) is not None:
user_id = pending_post.user_id
pending_post.delete_post()
await info.edit_inline_keyboard(message_id=g_message_id)
elif (report := Report.from_group(admin_group_id=info.chat_id, g_message_id=g_message_id)) is not None:
user_id = report.user_id
else: # the reply does not refer to a pending post or a report
await info.bot.send_message(
chat_id=info.chat_id, text="Per bannare qualcuno, rispondi con /ban al suo post o report"
)
return

user = User(pending_post.user_id)
user = User(user_id)
if user.is_banned:
await info.bot.send_message(chat_id=info.chat_id, text="L'utente è già bannato")
return
user.ban()
pending_post.delete_post()
await info.edit_inline_keyboard(message_id=g_message_id)
await info.bot.send_message(chat_id=info.chat_id, text="L'utente è stato bannato")
await info.bot.send_message(
chat_id=user.user_id,
text="Grazie per il tuo contributo alla community, a causa "
"di un tuo comportamento inadeguato sei stato bannato da Spotted DMI. Alla prossima!",
)
8 changes: 6 additions & 2 deletions src/spotted/handlers/report_spot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
filters,
)

from spotted.data import Config, Report
from spotted.data import Config, Report, User
from spotted.utils import EventInfo, conv_cancel

from .constants import INVALID_MESSAGE_TYPE_ERROR, ConversationState
Expand Down Expand Up @@ -52,8 +52,12 @@ async def report_spot_callback(update: Update, context: CallbackContext) -> int:
abusive_message_id = info.message.reply_to_message.message_id if Config.post_get("comments") else info.message_id

report = Report.get_post_report(user_id=info.user_id, channel_id=info.chat_id, c_message_id=abusive_message_id)
user = User(info.user_id)
if user.is_banned:
await info.answer_callback_query(text="Sei stato bannato, non puoi segnalare post")
return ConversationState.END.value
if report is not None: # this user has already reported this post
await info.answer_callback_query(text="Hai già segnalato questo spot")
await info.answer_callback_query(text="Hai già segnalato questo post")
return ConversationState.END.value
try:
await info.bot.forward_message(chat_id=info.user_id, from_chat_id=info.chat_id, message_id=abusive_message_id)
Expand Down
25 changes: 16 additions & 9 deletions src/spotted/handlers/sban.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""/sban command"""
from telegram import Update
from telegram.error import Forbidden
from telegram.ext import CallbackContext

from spotted.data import User
Expand All @@ -15,19 +16,25 @@ async def sban_cmd(update: Update, context: CallbackContext):
context: context passed by the handler
"""
info = EventInfo.from_message(update, context)
sban_fail = []
if context.args is None or len(context.args) == 0: # if no args have been passed
banned_users = "\n".join(f"{user.user_id} ({user.ban_date:%d/%m/%Y %H:%M})" for user in User.banned_users())
banned_users = "Nessuno" if len(banned_users) == 0 else f"{banned_users}"
text = f"[uso]: /sban <user_id1> [...user_id2]\nGli utenti attualmente bannati sono:\n{banned_users}"
await info.bot.send_message(chat_id=info.chat_id, text=text)
return

for user_id in context.args:
# the sban was unsuccessful (maybe the user id was not found)
if not User(int(user_id)).sban():
break
else:
await info.bot.send_message(chat_id=info.chat_id, text="Sban effettuato")
return

await info.bot.send_message(chat_id=info.chat_id, text="Uno o più sban sono falliti")
if User(int(user_id)).sban():
try:
await info.bot.send_message(
chat_id=user_id, text="Sei stato sbannato da Spotted DMI, puoi tornare a postare!"
)
except Forbidden:
pass
else:
# the sban was unsuccessful (maybe the user id was not found)
sban_fail.append(user_id)
reply_text = (
"Sban effettuato" if not sban_fail else "Impossibile sbannare i seguenti utenti:\n" + ",".join(sban_fail)
)
await info.bot.send_message(chat_id=info.chat_id, text=reply_text)
5 changes: 4 additions & 1 deletion tests/integration/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ async def test_ban_cmd(self, telegram: TelegramSimulator, admin_group: Chat, pen
The bot bans the user associated with the pending post
"""
await telegram.send_message("/ban", chat=admin_group, reply_to_message=pending_post)
assert telegram.last_message.text == "L'utente è stato bannato"
assert (
telegram.last_message.text
== "Grazie per il tuo contributo alla community, a causa di un tuo comportamento inadeguato sei stato bannato da Spotted DMI. Alla prossima!"
)
assert PendingPost.from_group(pending_post.message_id, pending_post.chat_id) is None
assert User(1).is_banned

Expand Down

0 comments on commit 4d5bf02

Please sign in to comment.