feat(notification): support notification
This commit is contained in:
@@ -29,6 +29,7 @@ from .lazer_user import (
|
||||
UserResp,
|
||||
)
|
||||
from .multiplayer_event import MultiplayerEvent, MultiplayerEventResp
|
||||
from .notification import Notification, UserNotification
|
||||
from .playlist_attempts import (
|
||||
ItemAttemptsCount,
|
||||
ItemAttemptsResp,
|
||||
@@ -86,6 +87,7 @@ __all__ = [
|
||||
"MultiplayerEvent",
|
||||
"MultiplayerEventResp",
|
||||
"MultiplayerScores",
|
||||
"Notification",
|
||||
"OAuthClient",
|
||||
"OAuthToken",
|
||||
"PPBestScore",
|
||||
@@ -120,6 +122,7 @@ __all__ = [
|
||||
"UserAchievement",
|
||||
"UserAchievementResp",
|
||||
"UserLoginLog",
|
||||
"UserNotification",
|
||||
"UserResp",
|
||||
"UserStatistics",
|
||||
"UserStatisticsResp",
|
||||
|
||||
@@ -190,6 +190,7 @@ class ChatMessageBase(UTCBaseModel, SQLModel):
|
||||
class ChatMessage(ChatMessageBase, table=True):
|
||||
__tablename__ = "chat_messages" # pyright: ignore[reportAssignmentType]
|
||||
user: User = Relationship(sa_relationship_kwargs={"lazy": "joined"})
|
||||
channel: ChatChannel = Relationship()
|
||||
|
||||
|
||||
class ChatMessageResp(ChatMessageBase):
|
||||
|
||||
73
app/database/notification.py
Normal file
73
app/database/notification.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from app.models.notification import NotificationDetail, NotificationName
|
||||
|
||||
from sqlmodel import (
|
||||
JSON,
|
||||
BigInteger,
|
||||
Column,
|
||||
DateTime,
|
||||
Field,
|
||||
ForeignKey,
|
||||
Relationship,
|
||||
SQLModel,
|
||||
)
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
|
||||
class Notification(SQLModel, table=True):
|
||||
__tablename__ = "notifications" # pyright: ignore[reportAssignmentType]
|
||||
|
||||
id: int = Field(primary_key=True, index=True, default=None)
|
||||
name: NotificationName = Field(index=True)
|
||||
category: str = Field(max_length=255, index=True)
|
||||
created_at: datetime = Field(sa_column=Column(DateTime))
|
||||
object_type: str = Field(index=True)
|
||||
object_id: int = Field(sa_column=Column(BigInteger, index=True))
|
||||
source_user_id: int = Field(index=True)
|
||||
details: dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
|
||||
class UserNotification(SQLModel, table=True):
|
||||
__tablename__ = "user_notifications" # pyright: ignore[reportAssignmentType]
|
||||
id: int = Field(
|
||||
sa_column=Column(
|
||||
BigInteger,
|
||||
primary_key=True,
|
||||
index=True,
|
||||
),
|
||||
default=None,
|
||||
)
|
||||
notification_id: int = Field(index=True, foreign_key="notifications.id")
|
||||
user_id: int = Field(
|
||||
sa_column=Column(BigInteger, ForeignKey("lazer_users.id"), index=True)
|
||||
)
|
||||
is_read: bool = Field(index=True)
|
||||
|
||||
notification: Notification = Relationship(sa_relationship_kwargs={"lazy": "joined"})
|
||||
|
||||
|
||||
async def insert_notification(session: AsyncSession, detail: NotificationDetail):
|
||||
notification = Notification(
|
||||
name=detail.name,
|
||||
category=detail.name.category,
|
||||
object_type=detail.object_type,
|
||||
object_id=detail.object_id,
|
||||
source_user_id=detail.source_user_id,
|
||||
details=detail.model_dump(),
|
||||
created_at=datetime.now(UTC),
|
||||
)
|
||||
session.add(notification)
|
||||
await session.commit()
|
||||
await session.refresh(notification)
|
||||
id_ = notification.id
|
||||
for receiver in await detail.get_receivers(session):
|
||||
user_notification = UserNotification(
|
||||
notification_id=id_,
|
||||
user_id=receiver,
|
||||
is_read=False,
|
||||
)
|
||||
session.add(user_notification)
|
||||
await session.commit()
|
||||
return id_
|
||||
Reference in New Issue
Block a user