aboutsummaryrefslogtreecommitdiff
blob: db855383db2238945c899b461031f0fa8d574f95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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) -> 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) -> 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)