2024-10-21 09:15:44 +00:00
|
|
|
import enum
|
2024-03-04 07:35:33 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from sqlalchemy import JSON, Column, ForeignKey, Integer, String
|
2024-04-19 15:22:07 +00:00
|
|
|
from sqlalchemy.orm import relationship
|
2024-03-04 07:35:33 +00:00
|
|
|
|
2025-05-16 06:23:48 +00:00
|
|
|
from auth.orm import Author
|
2025-06-01 23:56:11 +00:00
|
|
|
from services.db import BaseModel as Base
|
2024-03-04 07:35:33 +00:00
|
|
|
|
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
class NotificationEntity(enum.Enum):
|
2024-04-17 15:32:23 +00:00
|
|
|
REACTION = "reaction"
|
|
|
|
SHOUT = "shout"
|
|
|
|
FOLLOWER = "follower"
|
2024-10-21 09:15:44 +00:00
|
|
|
COMMUNITY = "community"
|
2024-03-04 07:35:33 +00:00
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
@classmethod
|
|
|
|
def from_string(cls, value):
|
|
|
|
return cls(value)
|
2024-03-04 07:35:33 +00:00
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
|
|
|
|
class NotificationAction(enum.Enum):
|
2024-04-17 15:32:23 +00:00
|
|
|
CREATE = "create"
|
|
|
|
UPDATE = "update"
|
|
|
|
DELETE = "delete"
|
|
|
|
SEEN = "seen"
|
|
|
|
FOLLOW = "follow"
|
|
|
|
UNFOLLOW = "unfollow"
|
2024-03-04 07:35:33 +00:00
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
@classmethod
|
|
|
|
def from_string(cls, value):
|
|
|
|
return cls(value)
|
|
|
|
|
2024-03-04 07:35:33 +00:00
|
|
|
|
|
|
|
class NotificationSeen(Base):
|
2024-04-17 15:32:23 +00:00
|
|
|
__tablename__ = "notification_seen"
|
2024-03-04 07:35:33 +00:00
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
viewer = Column(ForeignKey("author.id"), primary_key=True)
|
|
|
|
notification = Column(ForeignKey("notification.id"), primary_key=True)
|
2024-03-04 07:35:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Notification(Base):
|
2024-04-17 15:32:23 +00:00
|
|
|
__tablename__ = "notification"
|
2024-03-04 07:35:33 +00:00
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2024-03-04 07:35:33 +00:00
|
|
|
created_at = Column(Integer, server_default=str(int(time.time())))
|
|
|
|
entity = Column(String, nullable=False)
|
|
|
|
action = Column(String, nullable=False)
|
|
|
|
payload = Column(JSON, nullable=True)
|
|
|
|
|
2024-10-21 09:15:44 +00:00
|
|
|
seen = relationship(Author, secondary="notification_seen")
|
|
|
|
|
|
|
|
def set_entity(self, entity: NotificationEntity):
|
2025-06-01 23:56:11 +00:00
|
|
|
self.entity = entity.value # type: ignore[assignment]
|
2024-10-21 09:15:44 +00:00
|
|
|
|
|
|
|
def get_entity(self) -> NotificationEntity:
|
|
|
|
return NotificationEntity.from_string(self.entity)
|
|
|
|
|
|
|
|
def set_action(self, action: NotificationAction):
|
2025-06-01 23:56:11 +00:00
|
|
|
self.action = action.value # type: ignore[assignment]
|
2024-10-21 09:15:44 +00:00
|
|
|
|
|
|
|
def get_action(self) -> NotificationAction:
|
|
|
|
return NotificationAction.from_string(self.action)
|