Skip to content

Commit

Permalink
feat: 查询被自己q过的语录
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Nov 19, 2023
1 parent 94c4e7d commit ab6df55
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 17 deletions.
58 changes: 42 additions & 16 deletions kmua/callbacks/userdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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}) <qer quote manage>")
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,
Expand Down
65 changes: 65 additions & 0 deletions kmua/common/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions kmua/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 2 additions & 1 deletion kmua/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ab6df55

Please sign in to comment.