refactor: update group duty pin functionality and documentation
All checks were successful
CI / lint-and-test (push) Successful in 24s

- Changed the behavior of the group duty pin feature to send a new message, unpin the old one, and pin the new one instead of editing the existing message. This ensures the pinned message is always fresh.
- Updated the `DUTY_PIN_NOTIFY` configuration description in the documentation to reflect the new message handling approach.
- Revised the architecture documentation to clarify the updated group duty pin process.
- Enhanced tests to verify the new behavior of the group duty pin functionality, ensuring proper message handling and scheduling.
This commit is contained in:
2026-02-24 11:49:07 +03:00
parent 8091c608e8
commit 28b769b9d6
6 changed files with 72 additions and 57 deletions

View File

@@ -94,12 +94,12 @@ async def _schedule_next_update(
async def _refresh_pin_for_chat(
context: ContextTypes.DEFAULT_TYPE, chat_id: int
) -> Literal["updated", "no_message", "failed"]:
"""Refresh pinned duty message for a chat: edit text, optionally re-pin, schedule next.
"""Refresh pinned duty message: send new message, unpin old, pin new, save new message_id.
Returns:
"updated" if the message was updated successfully;
"updated" if the message was sent, pinned and saved successfully;
"no_message" if there is no pin record for this chat;
"failed" if edit or permissions failed.
"failed" if send_message or permissions failed.
"""
loop = asyncio.get_running_loop()
message_id = await loop.run_in_executor(None, _sync_get_message_id, chat_id)
@@ -110,28 +110,27 @@ async def _refresh_pin_for_chat(
None, lambda: _get_duty_message_text_sync(config.DEFAULT_LANGUAGE)
)
try:
await context.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=text,
)
msg = await context.bot.send_message(chat_id=chat_id, text=text)
except (BadRequest, Forbidden) as e:
logger.warning("Failed to edit pinned message chat_id=%s: %s", chat_id, e)
logger.warning(
"Failed to send duty message for pin refresh chat_id=%s: %s", chat_id, e
)
next_end = await loop.run_in_executor(None, _get_next_shift_end_sync)
await _schedule_next_update(context.application, chat_id, next_end)
return "failed"
if config.DUTY_PIN_NOTIFY:
try:
await context.bot.unpin_chat_message(chat_id=chat_id)
await context.bot.pin_chat_message(
chat_id=chat_id,
message_id=message_id,
disable_notification=False,
)
except (BadRequest, Forbidden) as e:
logger.warning(
"Re-pin after update failed chat_id=%s: %s", chat_id, e
)
try:
await context.bot.unpin_chat_message(chat_id=chat_id)
await context.bot.pin_chat_message(
chat_id=chat_id,
message_id=msg.message_id,
disable_notification=not config.DUTY_PIN_NOTIFY,
)
except (BadRequest, Forbidden) as e:
logger.warning("Unpin or pin after refresh failed chat_id=%s: %s", chat_id, e)
next_end = await loop.run_in_executor(None, _get_next_shift_end_sync)
await _schedule_next_update(context.application, chat_id, next_end)
return "failed"
await loop.run_in_executor(None, _sync_save_pin, chat_id, msg.message_id)
next_end = await loop.run_in_executor(None, _get_next_shift_end_sync)
await _schedule_next_update(context.application, chat_id, next_end)
return "updated"
@@ -253,9 +252,7 @@ async def pin_duty_cmd(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
await update.message.reply_text(t(lang, "pin_duty.failed"))
async def refresh_pin_cmd(
update: Update, context: ContextTypes.DEFAULT_TYPE
) -> None:
async def refresh_pin_cmd(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle /refresh_pin: immediately refresh pinned duty message in the group."""
if not update.message or not update.effective_chat or not update.effective_user:
return