aboutsummaryrefslogblamecommitdiff
blob: e92dc1c94c0033b0996f436d9fb38d590c4cd3bb (plain) (tree)
1
2
3
4
5
6
7
8
                                 
                                        
                                     
                                                          
                                                                                                                       
                                                                                                          
                    
                           


                                                                                   
                                                                                








                                                                                                           

 
                                                                                        



                                                                                                       
                                                                                   






                                                                                                       
           
                                                                                               

                                                                  
            






                                                                                                       

 
           
                                                                                                 

                                                                                                                  

 

                                                                                                           





                                                                                   





                                                                                      

                                                                                                            










                                                                                                     


                                                                                    
                                                                                                                     









                                                                                      
                                                                                 
                             
                                  
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 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:
    await update.message.reply_text(PROMPT_START, parse_mode=ParseMode.MARKDOWN)
    user_access_key = get_user_access_key(str(update.effective_user.id))
    # TODO
    # verify user access key still valid
    if len(user_access_key) == 0:
        await update.message.reply_text(PROMPT_FEDI_LOGIN_WHERE_IS_INSTANCE, parse_mode=ParseMode.MARKDOWN)
        return FEDI_LOGIN
    else:
        await update.message.reply_text(PROMPT_CHOOSE_ACTION, reply_markup=MAIN_MENU)
        return WAIT_LOCATION


async def fedi_login_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    await update.message.reply_text(PROMPT_FEDI_LOGIN_WHERE_IS_INSTANCE, parse_mode=ParseMode.MARKDOWN)
    return FEDI_LOGIN


async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_text(PROMPT_HELP, parse_mode=ParseMode.MARKDOWN, reply_markup=MAIN_MENU)


async def tos_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    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, user: User) -> None:
    result = get_user_home_instance(str(update.effective_user.id))
    if len(result) == 0:
        pass
    else:
        await update.message.reply_text(f"You are linked with the following Fediverse accounts:\n\n"
                                        f"<b>Instance</b>: {result['home_instance']}\n"
                                        f"<b>Instance type</b>: {result['home_instance_type']}\n"
                                        f"<b>Default visibility</b>: {result['default_visibility']}\n",
                                        parse_mode=ParseMode.HTML,
                                        reply_markup=MAIN_MENU)


@check_user
async def logout_command(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> 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)


@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)]
    ])

    await update.message.reply_text(PROMPT_TOGGLE_VIS.format(user["toot_visibility"]),
                                    parse_mode=ParseMode.HTML,
                                    reply_markup=visibility_menu)
    return WAIT_VISIBILITY


@check_user
async def callback_toggle_visibility(update: Update, context: ContextTypes.DEFAULT_TYPE, user: User) -> 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:
    for prompt in [PROMPT_LOCATION_KEYWORD, PROMPT_WAIT_LOCATION_CONFIRMATION, PROMPT_ADD_COMMENT, PROMPT_ADD_MEDIA]:
        try:
            if context.user_data.get(prompt):
                await context.bot.delete_message(chat_id=update.message.chat_id,
                                                 message_id=context.user_data[prompt])
        except BadRequest as e:
            if "not found" in str(e.message):
                pass
        except Exception as e:
            print(e)

    await update.message.reply_text(text=PROMPT_CANCELED, reply_markup=MAIN_MENU)
    context.user_data.clear()
    return ConversationHandler.END
Powered by cgit v1.2.3 (git 2.41.0)