aboutsummaryrefslogtreecommitdiff
blob: 5ae99f6af606f7675e9137287093982387a30fc6 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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, update_delayed_checkin
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)


async def delayed_checkin_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    await update.message.reply_text(PROMPT_DELAYED_CHECKIN, parse_mode=ParseMode.HTML)
    return DELAYED_CHECKIN


async def callback_delayed_checkin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    try:
        delayed_minutes = int(update.effective_message.text)
    except ValueError:
        await update.message.edit_text(text="Integer expected, try again")
        return DELAYED_CHECKIN

    if delayed_minutes < 5:
        delayed_minutes = 0

    update_delayed_checkin(str(update.effective_user.id), delayed_minutes)
    u = get_user_by_id(str(update.effective_user.id))
    await update.message.reply_text(text=f"Delayed check-in set to {u['delayed_checkin']} minutes")
    return ConversationHandler.END


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)