From a9fb6f252553ae49a2ba372434073824babe31e4 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Thu, 23 Feb 2023 21:46:27 -0800 Subject: bot: support changing default toot visibility --- command.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'command.py') diff --git a/command.py b/command.py index b12483a..f124e2a 100644 --- a/command.py +++ b/command.py @@ -2,7 +2,9 @@ from telegram import Update 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 +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 * @@ -50,8 +52,32 @@ async def logout_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> 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) -> None: - await update.message.reply_text(PROMPT_TOGGLE_VIS, parse_mode=ParseMode.HTML, reply_markup=MAIN_MENU) +async def toggle_visibility_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> 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: + query = update.callback_query + await query.answer() + + if query.data not in [TOOT_VISIBILITY_PRIVATE, TOOT_VISIBILITY_UNLISTED, TOOT_VISIBILITY_PUBLIC]: + await query.edit_message_text(text="Invalid visibility", + reply_markup=MAIN_MENU) + return ConversationHandler.END + + update_user_visibility(str(update.effective_user.id), query.data) + await query.edit_message_text(text=f"Default visibility changed to {query.data}") + return ConversationHandler.END async def cancel_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: -- cgit v1.2.3