diff --git a/orm/notification.py b/orm/notification.py index df6cdbbf..a64a4710 100644 --- a/orm/notification.py +++ b/orm/notification.py @@ -99,6 +99,23 @@ class NotificationSeen(Base): ) +class NotificationUnsubscribe(Base): + """Модель для хранения отписок пользователей от уведомлений по определенным thread_id.""" + + __tablename__ = "notification_unsubscribe" + + author_id: Mapped[int] = mapped_column(ForeignKey("author.id"), nullable=False) + thread_id: Mapped[str] = mapped_column(String, nullable=False) + created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow) + + __table_args__ = ( + PrimaryKeyConstraint(author_id, thread_id), + Index("idx_notification_unsubscribe_author", "author_id"), + Index("idx_notification_unsubscribe_thread", "thread_id"), + {"extend_existing": True}, + ) + + class Notification(Base): __tablename__ = "notification" diff --git a/resolvers/notifier.py b/resolvers/notifier.py index 131454b2..093893cf 100644 --- a/resolvers/notifier.py +++ b/resolvers/notifier.py @@ -105,7 +105,6 @@ def get_notifications_grouped(author_id: int, after: int = 0, limit: int = 10, o authors: List[NotificationAuthor], # List of authors involved in the thread. } """ - # TODO: use all stats _total, _unread, notifications = query_notifications(author_id, after) groups_by_thread = {} groups_amount = 0 @@ -121,6 +120,7 @@ def get_notifications_grouped(author_id: int, after: int = 0, limit: int = 10, o shout_id = shout.get("id") author_id = shout.get("created_by") thread_id = f"shout-{shout_id}" + with local_session() as session: author = session.query(Author).where(Author.id == author_id).first() shout = session.query(Shout).where(Shout.id == shout_id).first() @@ -154,7 +154,8 @@ def get_notifications_grouped(author_id: int, after: int = 0, limit: int = 10, o reply_id = reaction.get("reply_to") thread_id = f"shout-{shout_id}" if reply_id and reaction.get("kind", "").lower() == "comment": - thread_id += f"{reply_id}" + thread_id = f"shout-{shout_id}::{reply_id}" + existing_group = groups_by_thread.get(thread_id) if existing_group: existing_group["seen"] = False @@ -214,6 +215,10 @@ async def load_notifications(_: None, info: GraphQLResolveInfo, after: int, limi if author_id: groups_list = get_notifications_grouped(author_id, after, limit) notifications = sorted(groups_list, key=lambda group: group.get("updated_at", 0), reverse=True) + + # Считаем реальное количество сгруппированных уведомлений + total = len(notifications) + unread = sum(1 for n in notifications if not n.get("seen", False)) except Exception as e: error = str(e) logger.error(e)