diff --git a/kmua/callbacks/userdata.py b/kmua/callbacks/userdata.py index dc2b2a0..6b07e4a 100644 --- a/kmua/callbacks/userdata.py +++ b/kmua/callbacks/userdata.py @@ -194,6 +194,9 @@ async def delete_user_quote(update: Update, context: ContextTypes.DEFAULT_TYPE): if "user_quote_manage" in query.data: await _user_quote_manage(update, context) return + if "qer_quote_manage" in query.data: + await _qer_quote_manage(update, context) + return quote_link = query.data.split(" ")[1] dao.delete_quote_by_link(quote_link) await query.answer("已删除", show_alert=False, cache_time=5) @@ -214,7 +217,7 @@ async def _user_quote_manage(update: Update, context: ContextTypes.DEFAULT_TYPE) ) # noqa: E501 await query.edit_message_caption( caption=caption, - reply_markup=common.back_home_markup, + reply_markup=common.no_quote_markup, ) return if page > max_page or page < 1: @@ -239,21 +242,44 @@ async def _user_quote_manage(update: Update, context: ContextTypes.DEFAULT_TYPE) ) ) keyboard.append(line) - navigation_buttons = [ - InlineKeyboardButton( - "上一页", - callback_data=f"user_quote_manage {page - 1}", - ), - InlineKeyboardButton( - "返回", - callback_data="back_home", - ), - InlineKeyboardButton( - "下一页", - callback_data=f"user_quote_manage {page + 1}", - ), - ] - keyboard.append(navigation_buttons) + keyboard.append(common.qer_quote_manage_button) + keyboard.append(common.get_user_quote_navigation_buttons(page)) + reply_markup = InlineKeyboardMarkup(keyboard) + await query.edit_message_caption( + caption=text, + parse_mode="MarkdownV2", + reply_markup=reply_markup, + ) + + +async def _qer_quote_manage(update: Update, context: ContextTypes.DEFAULT_TYPE): + user = update.effective_user + query = update.callback_query + logger.info(f"({user.name}) ") + page = int(query.data.split(" ")[-1]) if len(query.data.split(" ")) > 1 else 1 + page_size = 5 + quotes_count = dao.get_qer_quotes_count(user) + max_page = ceil(quotes_count / page_size) + if quotes_count == 0: + await query.edit_message_caption( + caption="这里空空如也", + reply_markup=common.back_home_markup, + ) + return + if page > max_page or page < 1: + await update.callback_query.answer("已经没有啦", show_alert=False, cache_time=5) + return + text = f"被你q过的语录: 共{quotes_count}条; 第{page}/{max_page}页\n" + quotes = dao.get_qer_quotes_page(user, page, page_size) + for index, quote in enumerate(quotes): + quote_content = ( + escape_markdown(quote.text[:100], 2) + if quote.text + else "A non\-text message" + ) + text += f"{index + 1}\. [{quote_content}]({escape_markdown(quote.link,2)})\n\n" + keyboard = [] + keyboard.append(common.get_qer_quote_navigation_buttons(page)) reply_markup = InlineKeyboardMarkup(keyboard) await query.edit_message_caption( caption=text, diff --git a/kmua/common/quote.py b/kmua/common/quote.py index 7f6badb..14a20fc 100644 --- a/kmua/common/quote.py +++ b/kmua/common/quote.py @@ -4,6 +4,71 @@ from PIL import Image, ImageFont from pilmoji import Pilmoji +from telegram import InlineKeyboardButton, InlineKeyboardMarkup + + +qer_quote_manage_button = [ + InlineKeyboardButton( + "看看别人的", + callback_data="qer_quote_manage", + ) +] + +user_quote_manage_button = [ + InlineKeyboardButton( + "看看我的", + callback_data="user_quote_manage", + ) +] + + +no_quote_markup = InlineKeyboardMarkup( + [ + qer_quote_manage_button, + [ + InlineKeyboardButton( + "返回", + callback_data="back_home", + ) + ], + ], +) + + +def get_user_quote_navigation_buttons(page: int) -> list[InlineKeyboardButton]: + navigation_buttons = [ + InlineKeyboardButton( + "上一页", + callback_data=f"user_quote_manage {page - 1}", + ), + InlineKeyboardButton( + "返回", + callback_data="back_home", + ), + InlineKeyboardButton( + "下一页", + callback_data=f"user_quote_manage {page + 1}", + ), + ] + return navigation_buttons + + +def get_qer_quote_navigation_buttons(page: int) -> list[InlineKeyboardButton]: + navigation_buttons = [ + InlineKeyboardButton( + "上一页", + callback_data=f"qer_quote_manage {page - 1}", + ), + InlineKeyboardButton( + "返回", + callback_data="back_home", + ), + InlineKeyboardButton( + "下一页", + callback_data=f"qer_quote_manage {page + 1}", + ), + ] + return navigation_buttons async def generate_quote_img(avatar: bytes, text: str, name: str) -> bytes: diff --git a/kmua/dao/user.py b/kmua/dao/user.py index 70deabd..ff9e39b 100644 --- a/kmua/dao/user.py +++ b/kmua/dao/user.py @@ -79,6 +79,23 @@ def get_user_quotes_page( ) +def get_qer_quotes_count(user: User | UserData) -> int: + return _db.query(Quote).filter(Quote.qer_id == user.id).count() + + +def get_qer_quotes_page( + user: User | UserData, page: int, page_size: int +) -> list[Quote]: + offset = (page - 1) * page_size + return ( + _db.query(Quote) + .filter(Quote.qer_id == user.id) + .offset(offset) + .limit(page_size) + .all() + ) + + def get_all_users_count() -> int: return _db.query(UserData).count() diff --git a/kmua/handlers.py b/kmua/handlers.py index 0a352dc..0bb871d 100644 --- a/kmua/handlers.py +++ b/kmua/handlers.py @@ -119,7 +119,8 @@ waifu.marry_waifu, pattern=r".*marry_waifu.*" ) user_quote_manage_handler = CallbackQueryHandler( - userdata.delete_user_quote, pattern="user_quote_manage|delete_user_quote" + userdata.delete_user_quote, + pattern="user_quote_manage|delete_user_quote|qer_quote_manage", ) bot_data_refresh_handler = CallbackQueryHandler( manage.bot_data_refresh, pattern="bot_data_refresh"