Implement group duty pinning and user phone management
- Added functionality to pin duty messages in group chats, including scheduling updates and handling bot add/remove events. - Introduced a new `GroupDutyPin` model to store pinned message details and a `phone` field in the `User` model for user contact information. - Implemented commands for users to set or clear their phone numbers in private chats. - Enhanced the repository with functions to manage group duty pins and user phone data. - Updated handlers to register new commands and manage duty pin updates effectively.
This commit is contained in:
10
db/models.py
10
db/models.py
@@ -21,6 +21,7 @@ class User(Base):
|
||||
username: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
first_name: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
last_name: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
phone: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
|
||||
duties: Mapped[list["Duty"]] = relationship("Duty", back_populates="user")
|
||||
|
||||
@@ -39,3 +40,12 @@ class Duty(Base):
|
||||
event_type: Mapped[str] = mapped_column(Text, nullable=False, server_default="duty")
|
||||
|
||||
user: Mapped["User"] = relationship("User", back_populates="duties")
|
||||
|
||||
|
||||
class GroupDutyPin(Base):
|
||||
"""Stores which message to update in each group for the pinned duty notice."""
|
||||
|
||||
__tablename__ = "group_duty_pins"
|
||||
|
||||
chat_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
||||
message_id: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
|
||||
102
db/repository.py
102
db/repository.py
@@ -1,10 +1,10 @@
|
||||
"""Repository: get_or_create_user, get_duties, insert_duty."""
|
||||
"""Repository: get_or_create_user, get_duties, insert_duty, get_current_duty, group_duty_pins."""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from db.models import User, Duty
|
||||
from db.models import User, Duty, GroupDutyPin
|
||||
|
||||
|
||||
def get_or_create_user(
|
||||
@@ -116,3 +116,101 @@ def insert_duty(
|
||||
session.commit()
|
||||
session.refresh(duty)
|
||||
return duty
|
||||
|
||||
|
||||
def get_current_duty(
|
||||
session: Session, at_utc: datetime
|
||||
) -> tuple[Duty, User] | None:
|
||||
"""Return the duty (and user) for which start_at <= at_utc < end_at, event_type='duty'.
|
||||
at_utc is in UTC (naive or aware); comparison uses ISO strings."""
|
||||
from datetime import timezone
|
||||
if at_utc.tzinfo is not None:
|
||||
at_utc = at_utc.astimezone(timezone.utc)
|
||||
now_iso = at_utc.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
|
||||
row = (
|
||||
session.query(Duty, User)
|
||||
.join(User, Duty.user_id == User.id)
|
||||
.filter(
|
||||
Duty.event_type == "duty",
|
||||
Duty.start_at <= now_iso,
|
||||
Duty.end_at > now_iso,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if row is None:
|
||||
return None
|
||||
return (row[0], row[1])
|
||||
|
||||
|
||||
def get_next_shift_end(session: Session, after_utc: datetime) -> datetime | None:
|
||||
"""Return the end_at of the current duty (if after_utc is inside one) or of the next duty.
|
||||
For scheduling the next pin update. Returns naive UTC datetime."""
|
||||
from datetime import timezone
|
||||
if after_utc.tzinfo is not None:
|
||||
after_utc = after_utc.astimezone(timezone.utc)
|
||||
after_iso = after_utc.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
|
||||
# Current duty: start_at <= after_iso < end_at → use this end_at
|
||||
current = (
|
||||
session.query(Duty)
|
||||
.filter(
|
||||
Duty.event_type == "duty",
|
||||
Duty.start_at <= after_iso,
|
||||
Duty.end_at > after_iso,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if current:
|
||||
return datetime.fromisoformat(current.end_at.replace("Z", "+00:00")).replace(tzinfo=None)
|
||||
# Next future duty: start_at > after_iso, order by start_at
|
||||
next_duty = (
|
||||
session.query(Duty)
|
||||
.filter(Duty.event_type == "duty", Duty.start_at > after_iso)
|
||||
.order_by(Duty.start_at)
|
||||
.first()
|
||||
)
|
||||
if next_duty:
|
||||
return datetime.fromisoformat(next_duty.end_at.replace("Z", "+00:00")).replace(tzinfo=None)
|
||||
return None
|
||||
|
||||
|
||||
def get_group_duty_pin(session: Session, chat_id: int) -> GroupDutyPin | None:
|
||||
"""Get the pinned message record for a chat, if any."""
|
||||
return session.query(GroupDutyPin).filter(GroupDutyPin.chat_id == chat_id).first()
|
||||
|
||||
|
||||
def save_group_duty_pin(
|
||||
session: Session, chat_id: int, message_id: int
|
||||
) -> GroupDutyPin:
|
||||
"""Save or update the pinned message for a chat."""
|
||||
pin = session.query(GroupDutyPin).filter(GroupDutyPin.chat_id == chat_id).first()
|
||||
if pin:
|
||||
pin.message_id = message_id
|
||||
else:
|
||||
pin = GroupDutyPin(chat_id=chat_id, message_id=message_id)
|
||||
session.add(pin)
|
||||
session.commit()
|
||||
session.refresh(pin)
|
||||
return pin
|
||||
|
||||
|
||||
def delete_group_duty_pin(session: Session, chat_id: int) -> None:
|
||||
"""Remove the pinned message record when the bot leaves the group."""
|
||||
session.query(GroupDutyPin).filter(GroupDutyPin.chat_id == chat_id).delete()
|
||||
session.commit()
|
||||
|
||||
|
||||
def get_all_group_duty_pin_chat_ids(session: Session) -> list[int]:
|
||||
"""Return all chat_ids that have a pinned duty message (for restoring jobs on startup)."""
|
||||
rows = session.query(GroupDutyPin.chat_id).all()
|
||||
return [r[0] for r in rows]
|
||||
|
||||
|
||||
def set_user_phone(session: Session, telegram_user_id: int, phone: str | None) -> User | None:
|
||||
"""Set phone for user by telegram_user_id. Returns User or None if not found."""
|
||||
user = session.query(User).filter(User.telegram_user_id == telegram_user_id).first()
|
||||
if not user:
|
||||
return None
|
||||
user.phone = phone
|
||||
session.commit()
|
||||
session.refresh(user)
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user