notification-model-fix

This commit is contained in:
Untone 2024-01-23 12:05:28 +03:00
parent 140e46e9f2
commit ac424cf803
3 changed files with 17 additions and 17 deletions

View File

@ -1,7 +1,7 @@
from enum import Enum as Enumeration from enum import Enum as Enumeration
from sqlalchemy import JSON as JSONType, func, cast from sqlalchemy import JSON as JSONType, func, cast
from sqlalchemy import Column, Enum, ForeignKey, Integer from sqlalchemy import Column, Enum, ForeignKey, Integer, String
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from sqlalchemy.orm.session import engine from sqlalchemy.orm.session import engine
@ -10,18 +10,18 @@ from services.db import Base
import time import time
class NotificationEntity(Enumeration): class NotificationEntity(Enumeration):
REACTION = 1 REACTION = "reaction"
SHOUT = 2 SHOUT = "shout"
FOLLOWER = 3 FOLLOWER = "follower"
class NotificationAction(Enumeration): class NotificationAction(Enumeration):
CREATE = 1 CREATE = "create"
UPDATE = 2 UPDATE = "update"
DELETE = 3 DELETE = "delete"
SEEN = 4 SEEN = "seen"
FOLLOW = 5 FOLLOW = "follow"
UNFOLLOW = 6 UNFOLLOW = "unfollow"
class NotificationSeen(Base): class NotificationSeen(Base):
@ -35,8 +35,8 @@ class Notification(Base):
__tablename__ = "notification" __tablename__ = "notification"
created_at = Column(Integer, server_default=str(int(time.time()))) created_at = Column(Integer, server_default=str(int(time.time())))
entity = Column(Enum(NotificationEntity), nullable=False) entity = Column(String, nullable=False)
action = Column(Enum(NotificationAction), nullable=False) action = Column(String, nullable=False)
payload = Column(JSONType, nullable=True) payload = Column(JSONType, nullable=True)
seen = relationship(lambda: Author, secondary="notification_seen") seen = relationship(lambda: Author, secondary="notification_seen")

View File

@ -1,4 +1,4 @@
from orm.notification import Notification from orm.notification import Notification, NotificationAction, NotificationEntity
from resolvers.model import NotificationReaction, NotificationAuthor, NotificationShout from resolvers.model import NotificationReaction, NotificationAuthor, NotificationShout
from services.db import local_session from services.db import local_session
from services.rediscache import redis from services.rediscache import redis

View File

@ -6,7 +6,7 @@ from resolvers.model import (
NotificationAuthor, NotificationAuthor,
NotificationsResult, NotificationsResult,
) )
from orm.notification import NotificationSeen, Notification from orm.notification import NotificationAction, NotificationEntity, NotificationSeen, Notification
from typing import Dict, List from typing import Dict, List
import time, json import time, json
import strawberry import strawberry
@ -59,8 +59,8 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
notifications_by_thread: Dict[str, List[Notification]] = {} notifications_by_thread: Dict[str, List[Notification]] = {}
groups_by_thread: Dict[str, NotificationGroup] = {} groups_by_thread: Dict[str, NotificationGroup] = {}
with local_session() as session: with local_session() as session:
total = session.query(Notification).filter(and_(Notification.action == "create", Notification.created_at > after)).count() total = session.query(Notification).filter(and_(Notification.action == NotificationAction.CREATE.value, Notification.created_at > after)).count()
unread = session.query(Notification).filter(and_(Notification.action == "create", Notification.created_at > after, Notification.seen.is_not(True))).count() unread = session.query(Notification).filter(and_(Notification.action == NotificationAction.CREATE.value, Notification.created_at > after, Notification.seen.is_not(True))).count()
notifications_result = session.execute(query) notifications_result = session.execute(query)
for n, seen in notifications_result: for n, seen in notifications_result:
thread_id = "" thread_id = ""
@ -87,7 +87,7 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
notifications.append(n) notifications.append(n)
notifications_by_thread[thread_id] = notifications notifications_by_thread[thread_id] = notifications
groups_amount += 1 groups_amount += 1
elif n.entity == "reaction" and n.action == "create": elif n.entity == NotificationEntity.REACTION.value and n.action == NotificationAction.CREATE.value:
reaction: NotificationReaction = payload reaction: NotificationReaction = payload
shout: NotificationShout = reaction.shout shout: NotificationShout = reaction.shout
thread_id += f"{reaction.shout}" thread_id += f"{reaction.shout}"