Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/removing-messages-once-pushed #124

Merged
merged 10 commits into from
Jul 17, 2023
2 changes: 2 additions & 0 deletions src/bot/handlers/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from src.bot.keyboards import get_categories_keyboard, get_open_tasks_and_menu_keyboard
from src.bot.services.category import CategoryService
from src.bot.services.user import UserService
from src.bot.utils import delete_previous
from src.core.logging.utils import logger_decor


@logger_decor
@delete_previous
async def categories_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_service = UserService()
category_service = CategoryService()
Expand Down
4 changes: 4 additions & 0 deletions src/bot/handlers/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
from src.bot.constants import callback_data, commands, enum, patterns
from src.bot.keyboards import get_back_menu, get_menu_keyboard, get_no_mailing_keyboard
from src.bot.services.user import UserService
from src.bot.utils import delete_previous
from src.core.logging.utils import logger_decor
from src.settings import settings

log = structlog.get_logger()


@logger_decor
@delete_previous
async def menu_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Возвращает в меню."""
await context.bot.send_message(
Expand All @@ -36,6 +38,7 @@ async def menu_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):


@logger_decor
@delete_previous
async def set_mailing(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Включение/выключение подписки пользователя на почтовую рассылку."""
telegram_id = update.effective_user.id
Expand Down Expand Up @@ -118,6 +121,7 @@ async def web_app_data(update: Update):


@logger_decor
@delete_previous
async def about_project(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id,
Expand Down
2 changes: 2 additions & 0 deletions src/bot/handlers/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from src.bot.constants import callback_data, commands
from src.bot.keyboards import get_confirm_keyboard, get_start_keyboard
from src.bot.services.user import UserService
from src.bot.utils import delete_previous
from src.core.logging.utils import logger_decor


Expand Down Expand Up @@ -34,6 +35,7 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):


@logger_decor
@delete_previous
async def confirm_chosen_categories(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = get_confirm_keyboard()

Expand Down
3 changes: 3 additions & 0 deletions src/bot/handlers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from src.bot.services.category import CategoryService
from src.bot.services.task import TaskService
from src.bot.utils import delete_previous
from src.core.logging.utils import logger_decor
from src.core.utils import display_tasks

Expand Down Expand Up @@ -70,6 +71,7 @@ async def back_subcategory_callback(update: Update, context: ContextTypes.DEFAUL


@logger_decor
@delete_previous
async def view_task_callback(update: Update, context: CallbackContext, limit: int = 3):
task_service = TaskService()
tasks_to_show, offset, page_number = await task_service.get_user_tasks_by_page(
Expand All @@ -84,6 +86,7 @@ async def view_task_callback(update: Update, context: CallbackContext, limit: in
await show_next_tasks(update, context, limit, offset, page_number)


@delete_previous
async def show_next_tasks(update: Update, context: CallbackContext, limit: int, offset: int, page_number: int):
task_service = TaskService()
remaining_tasks = await task_service.get_remaining_user_tasks_count(limit, offset)
Expand Down
22 changes: 22 additions & 0 deletions src/bot/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from telegram import Update


def delete_previous(coroutine):
NiKuma0 marked this conversation as resolved.
Show resolved Hide resolved
"""Для функций, отправляющих сообщения с inline-кнопками.
Удаляет сообщение с кнопками, приведшее к вызову функции."""

async def wrapper(*args, **kwargs):
result = await coroutine(*args, **kwargs)
thesupercalifragilisticexpialidocious marked this conversation as resolved.
Show resolved Hide resolved
if "update" in kwargs and isinstance(kwargs["update"], Update):
update = kwargs["update"]
else:
for arg in args:
if isinstance(arg, Update):
update = arg
break
else:
return result # если нет update, просто возвращаем результат работы
await update.callback_query.message.delete()
return result

return wrapper
Loading