From a1c7c5a223d22017508998599388c5adf9a90713 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Mon, 27 Feb 2023 15:48:09 -0800 Subject: bot: add check_user decorator to check user login status --- command.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'command.py') diff --git a/command.py b/command.py index f124e2a..db85538 100644 --- a/command.py +++ b/command.py @@ -1,11 +1,11 @@ -from telegram import Update +from telegram import Update, User from telegram.constants import ParseMode from telegram.error import BadRequest from telegram.ext import ContextTypes, ConversationHandler from dbstore.peewee_store import get_user_access_key, get_user_home_instance, delete_user_by_id, update_user_visibility -from dbstore.peewee_store import get_user_by_id from dbstore.peewee_store import TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_UNLISTED, TOOT_VISIBILITY_PUBLIC from config import * +from util import check_user async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: @@ -34,10 +34,11 @@ async def tos_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non await update.message.reply_text(PROMPT_TOS, parse_mode=ParseMode.HTML, reply_markup=MAIN_MENU) +@check_user async def list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: result = get_user_home_instance(str(update.effective_user.id)) if len(result) == 0: - await update.message.reply_text(PROMPT_LIST_NO_RESULT, parse_mode=ParseMode.HTML) + pass else: await update.message.reply_text(f"You are linked with the following Fediverse accounts:\n\n" f"Instance: {result['home_instance']}\n" @@ -47,26 +48,28 @@ async def list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No reply_markup=MAIN_MENU) +@check_user async def logout_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if delete_user_by_id(str(update.effective_user.id)): await update.message.reply_text(PROMPT_LOGOUT_SUCCESS, parse_mode=ParseMode.HTML, reply_markup=LOGIN_MENU) -async def toggle_visibility_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: +@check_user +async def toggle_visibility_command(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: visibility_menu = InlineKeyboardMarkup([ [InlineKeyboardButton("Private", callback_data=TOOT_VISIBILITY_PRIVATE)], [InlineKeyboardButton("Unlisted", callback_data=TOOT_VISIBILITY_UNLISTED)], [InlineKeyboardButton("Public", callback_data=TOOT_VISIBILITY_PUBLIC)] ]) - user = get_user_by_id(str(update.effective_user.id)) await update.message.reply_text(PROMPT_TOGGLE_VIS.format(user["toot_visibility"]), parse_mode=ParseMode.HTML, reply_markup=visibility_menu) return WAIT_VISIBILITY -async def callback_toggle_visibility(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: +@check_user +async def callback_toggle_visibility(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> int: query = update.callback_query await query.answer() -- cgit v1.2.3