From d7c1835ed41d50d8e0fd42fd8c33fa760ce24bb5 Mon Sep 17 00:00:00 2001 From: clarkzjw Date: Thu, 23 Feb 2023 21:11:05 -0800 Subject: bot: implement /list command --- command.py | 13 +++++++++++-- dbstore/peewee_store.py | 13 +++++++++++++ prompt/string.py | 3 ++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/command.py b/command.py index 22f7066..6feeec7 100644 --- a/command.py +++ b/command.py @@ -2,7 +2,7 @@ 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 +from dbstore.peewee_store import get_user_access_key, get_user_home_instance from config import * @@ -33,7 +33,16 @@ async def tos_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non async def list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await update.message.reply_text(PROMPT_LIST, parse_mode=ParseMode.HTML, reply_markup=MAIN_MENU) + 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) + else: + await update.message.reply_text(f"You are linked with the following Fediverse accounts:\n\n" + f"Instance: {result['home_instance']}\n" + f"Instance type: {result['home_instance_type']}\n" + f"Default visibility: {result['default_visibility']}\n", + parse_mode=ParseMode.HTML, + reply_markup=MAIN_MENU) async def logout_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: diff --git a/dbstore/peewee_store.py b/dbstore/peewee_store.py index ed52257..2f637bf 100644 --- a/dbstore/peewee_store.py +++ b/dbstore/peewee_store.py @@ -51,6 +51,19 @@ def get_user_access_key(telegram_user_id: str) -> str: return "" +def get_user_home_instance(telegram_user_id: str) -> dict: + with db.connection_context(): + try: + user = User.get(User.telegram_user_id == telegram_user_id) + return { + "home_instance": user.home_instance, + "home_instance_type": user.home_instance_type, + "default_visibility": user.toot_visibility, + } + except DoesNotExist: + return {} + + class Location(BaseModel): fsq_id = CharField(unique=True, primary_key=True) name = CharField(max_length=128) diff --git a/prompt/string.py b/prompt/string.py index eda0df8..207713f 100644 --- a/prompt/string.py +++ b/prompt/string.py @@ -39,7 +39,8 @@ PROMPT_HELP = "Available commands:" \ "\n`/tos` - show ToS message" \ "\n`/cancel` - cancel current operation during checkins" -PROMPT_LIST = "List all linked Fediverse accounts" +PROMPT_LIST = "You are linked with the following Fediverse accounts:" +PROMPT_LIST_NO_RESULT = "You are not linked with any Fediverse account yet. \n\n Input /login to login." PROMPT_LOGOUT = "Choose Fediverse account to logout" PROMPT_TOGGLE_VIS = "Choose visibility of your checkins on Fediverse" -- cgit v1.2.3