Skip to content

Commit

Permalink
feat: try banning-unbanning for kicking
Browse files Browse the repository at this point in the history
  • Loading branch information
vas3k committed Feb 4, 2025
1 parent a9af4d3 commit efca1f0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gdpr/forget.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def delete_user_data(user: User):
user.save()

# delete from chats
ban_user_in_all_chats(user=user)
ban_user_in_all_chats(user=user, is_permanent=False)

# delete intro
Post.objects.filter(author=user, type=Post.TYPE_INTRO).delete()
Expand Down
5 changes: 4 additions & 1 deletion rooms/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
log = logging.getLogger(__name__)


def ban_user_in_all_chats(user: User):
def ban_user_in_all_chats(user: User, is_permanent=True):
if not user.telegram_id:
log.warning(f"User {user.slug} has no telegram_id, can't ban")
return
Expand All @@ -21,6 +21,9 @@ def ban_user_in_all_chats(user: User):
is_ok = bot.kick_chat_member(room.chat_id, user.telegram_id)
if is_ok:
log.info(f"User {user.slug} banned in chat {room.slug}")
if not is_permanent:
# banning-unbanning the user works like kicking from the chat
bot.unban_chat_member(room.chat_id, user.telegram_id)
except telegram.TelegramError as ex:
log.warning(f"Failed to ban user {user.slug} in chat {room.slug}: {ex}")

Expand Down
2 changes: 1 addition & 1 deletion rooms/management/commands/bulk_chat_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def handle(self, *args, **options):
self.stderr.write(f"No such user: {email_or_slugs}")

if action == "ban":
ban_user_in_all_chats(user)
ban_user_in_all_chats(user, is_permanent=True)
elif action == "unban":
unban_user_in_all_chats(user)
else:
Expand Down
3 changes: 2 additions & 1 deletion users/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def permanently_ban_user(user: User, reason: BanReason):
cancel_all_stripe_subscriptions(user.stripe_id)
async_task(
ban_user_in_all_chats,
user=user
user=user,
is_permanent=True,
)

return custom_ban_user(
Expand Down
8 changes: 8 additions & 0 deletions users/views/delete_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from gdpr.models import DataRequests
from notifications.email.users import send_delete_account_request_email, send_delete_account_confirm_email
from payments.helpers import cancel_all_stripe_subscriptions
from rooms.helpers import ban_user_in_all_chats


@require_auth
Expand Down Expand Up @@ -74,6 +75,13 @@ def confirm_delete_account(request):
text=f"💀 Юзер удалился: {settings.APP_HOST}/user/{request.me.slug}/",
)

# kick from chats
async_task(
ban_user_in_all_chats,
user=request.me,
is_permanent=False,
)

# an actual deletion will be done in a cron task ("manage.py delete_users")

return render(request, "users/messages/delete_account_confirmed.html",)

0 comments on commit efca1f0

Please sign in to comment.