-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfavorite.py
More file actions
126 lines (108 loc) · 5.67 KB
/
favorite.py
File metadata and controls
126 lines (108 loc) · 5.67 KB
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
122
123
124
125
126
from typing import Optional
from persian_tools.digits import convert_to_fa
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery, CopyTextButton
from telegram.ext import ContextTypes
import const
from config import settings
from db import DataBase
from util import Util
class Favorite:
@staticmethod
def get_add_to_favorites_button(poem_id: int) -> InlineKeyboardButton:
return InlineKeyboardButton(const.FAVORITE_ADD, callback_data=f'favadd:{poem_id}')
@staticmethod
def get_remove_favorites_button(poem_id: int) -> InlineKeyboardButton:
return InlineKeyboardButton(const.FAVORITE_REMOVE, callback_data=f'favremove:{poem_id}')
@staticmethod
def create_favorites_messages(favorites: list[dict], offset: int) -> list[str]:
"""Creates formatted message strings for a list of favorite poems."""
messages = list()
for ind, favorite in enumerate(favorites):
number = convert_to_fa(ind + offset + 1)
message = f"{number}. {Util.trim_text(favorite['title'])} - {favorite['name']}\n{Util.trim_text(favorite['text'])}"
messages.append(message)
return messages
@staticmethod
async def get_offset(query: Optional[CallbackQuery]) -> int:
if query:
await query.answer()
return int(query.data.split(':')[1])
return 0
@staticmethod
async def add_to_favorites(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Adds the poem to the user's favorite poems."""
query = update.callback_query
poem_id = int(query.data.split(':')[1])
user = update.callback_query.from_user
Util.ensure_user_exists(user)
user_id = user.id
if DataBase().check_is_favorite(poem_id, user_id):
await query.answer(const.FAVORITE_ALREADY_IN_FAVORITES)
elif DataBase().get_favorite_count(user_id) >= settings.MAX_FAVORITE:
await query.answer(const.FAVORITE_NO_MORE_ADD_ALLOWED.format(count=settings.MAX_FAVORITE))
return
else:
DataBase().add_to_favorites(poem_id, user_id)
await query.answer(const.FAVORITE_ADDED)
keyboard = InlineKeyboardMarkup(
[[Favorite.get_remove_favorites_button(poem_id)],
[InlineKeyboardButton('پیوند اشتراک اثر',
copy_text=CopyTextButton(f"https://t.me/{context.bot.username}?start={poem_id}"))]])
await update.effective_message.edit_reply_markup(keyboard)
@staticmethod
async def remove_from_favorites(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Removes the poem from the user's favorite poems."""
query = update.callback_query
poem_id = int(query.data.split(':')[1])
user = update.callback_query.from_user
Util.ensure_user_exists(user)
user_id = user.id
if DataBase().check_is_favorite(poem_id, user_id):
DataBase().remove_from_favorites(poem_id, user_id)
await query.answer(const.FAVORITE_REMOVED)
else:
await query.answer(const.FAVORITE_NOT_IN_FAVORITES)
keyboard = InlineKeyboardMarkup(
[[Favorite.get_add_to_favorites_button(poem_id)],
[InlineKeyboardButton('پیوند اشتراک اثر',
copy_text=CopyTextButton(f"https://t.me/{context.bot.username}?start={poem_id}"))]])
await update.effective_message.edit_reply_markup(keyboard)
@staticmethod
async def list_of_favorite_poems(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Displays the list of favorite poems of the user.
This method can be called in two ways:
1) The user sends the /favorites command.
2) In the list of favorite poems, the user taps on previous/next buttons.
"""
offset = await Favorite.get_offset(update.callback_query)
user = update.effective_user
Util.ensure_user_exists(user)
user_id = user.id
favorites = DataBase().get_favorite_poems(user_id, offset)
if not favorites:
await context.bot.send_message(update.effective_chat.id, const.FAVORITE_NO_RESULT)
else:
all_favorites_count = DataBase().get_favorite_count(user_id)
messages = Favorite.create_favorites_messages(favorites, offset)
message_text = '\n\n'.join(messages)
# Create numbered buttons for each favorite poem.
button_texts = [convert_to_fa(i + offset + 1) for i in range(len(favorites))]
callback_data = [f"poem:{favorite['poem_id']}" for favorite in favorites]
buttons = Util.create_inline_buttons(4, len(favorites), button_texts, callback_data)
# Add pagination buttons if needed.
last_row = []
if offset > 0:
last_row.append(
InlineKeyboardButton("قبلی", callback_data=f'favorites:{offset - settings.MAX_FAVORITE_PER_PAGE}'))
if len(favorites) + offset < all_favorites_count:
last_row.append(
InlineKeyboardButton("بعدی", callback_data=f'favorites:{offset + settings.MAX_FAVORITE_PER_PAGE}'))
if last_row:
buttons.append(last_row)
keyboard = InlineKeyboardMarkup(buttons)
# Send new message if called from command, otherwise edit existing message.
if update.message:
await context.bot.send_message(update.effective_chat.id, message_text, reply_markup=keyboard)
else:
await update.effective_message.edit_text(message_text)
await update.effective_message.edit_reply_markup(keyboard)