Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create notification FastAPI #468

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions ara/domain/notification/notification_domain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from apps.core.models import Notification, NotificationReadLog
from apps.core.models import Comment
from ara.domain.notification.type import NotificationInfo
from ara.infra.notification.notification_infra import NotificationInfra

Expand All @@ -11,20 +11,13 @@ def get_all_notifications(self, user_id: int) -> list[NotificationInfo]:
return self.notification_infra.get_all_notifications(user_id)

def get_unread_notifications(self, user_id: int) -> list[NotificationInfo]:
notifications = self.notification_infra.get_all_notifications(user_id)
return [
notification
for notification in notifications
if NotificationReadLog.objects.filter(
notification=notification, read_by=user_id, is_read=False
).exists()
]
return self.notification_infra.get_unread_notifications(user_id)

def read_all_notifications(self, user_id: int) -> None:
return self.notification_infra.read_all_notifications(user_id)

def read_notification(self, user_id: int, notification_id: int) -> None:
return self.notification_infra.read_notification(user_id, notification_id)

def create_notification(self, notification_info: NotificationInfo):
return self.notification_infra.create_notification(notification_info)
def create_notification(self, comment: Comment):
return self.notification_infra.create_notification(comment)
7 changes: 2 additions & 5 deletions ara/domain/notification/type.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from typing import Optional

from django.contrib.auth import get_user_model
from pydantic import BaseModel

from apps.core.models import Article, Comment, Notification

User = get_user_model()


class NotificationReadLogInfo(BaseModel): # 사용 안하는데 ?? 그래도 적어두는게 낫겠죠 ??
class NotificationReadLogInfo(BaseModel):
is_read: bool
read_by: int # ??? 모르겠다 int ?? user ?? Foreign Key 인데 ??
read_by: int
notification: Notification

class Config:
Expand Down
103 changes: 82 additions & 21 deletions ara/infra/notification/notification_infra.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from apps.core.models import Article, Comment, Notification, NotificationReadLog
from apps.core.models.board import NameType
from ara.domain.notification.type import NotificationInfo
from ara.firebase import fcm_notify_comment
from ara.infra.django_infra import AraDjangoInfra


class NotificationInfra(AraDjangoInfra[Notification]):
def __init__(self, user_id: int) -> None:
def __init__(self) -> None:
super().__init__(Notification)
self.user_id = user_id

def get_all_notifications(self, user_id: int) -> list[NotificationInfo]:
queryset = Notification.objects.select_related(
Expand All @@ -18,9 +19,19 @@ def get_all_notifications(self, user_id: int) -> list[NotificationInfo]:
)
return [self._to_notification_info(notification) for notification in queryset]

def get_unread_notifications(self, user_id: int) -> list[NotificationInfo]:
notifications = self.notification_infra.get_all_notifications(user_id)
return [
notification
for notification in notifications
if NotificationReadLog.objects.filter(
notification=notification, read_by=user_id, is_read=False
).exists()
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 N+1 쿼리일 가능성이 높아 보이는데 시간 나면 리팩토링 하거나 TODO 체크해주세요 👍


def _to_notification_info(self, notification: Notification) -> NotificationInfo:
return NotificationInfo(
id=notification.id, # 이렇게 써도 되나요?
id=notification.id,
type=notification.type,
title=notification.title,
content=notification.content,
Expand All @@ -43,26 +54,76 @@ def read_notification(self, user_id: int, notification_id: int) -> None:
notification_read_log.is_read = True
notification_read_log.save()

"""
##수정해야함##

def create_notification(self, article: Article, comment: Comment) -> None:
if comment.parent_comment:
parent_comment = comment.parent_comment
related_comment = parent_comment
def get_display_name(self, article: Article, profile: int):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거는 외부로 노출(domain 이나 다른 infra layer에서 사용할거 같아 보이는게 아닌)되지 않으면 함수명에 _ 붙여 주세요

if article.name_type == NameType.REALNAME:
return "실명"
elif article.name_type == NameType.REGULAR:
return "nickname"
else:
related_comment = None
return "익명"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"실명", "nickname", "익명" 전부다 constant로 지정해서 사용해주세요!!!!


def create_notification(self, comment: Comment) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 transaction이 붙어야 할거 같아요 음... 근데 이거는 어려울 수도 있으니까 같이 한번 봐요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵~!

def notify_article_commented(_parent_article: Article, _comment: Comment):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수에 왜 _가 들어있나요?

name = self.get_display_name(_parent_article, _comment.created_by_id)
title = f"{name} 님이 새로운 댓글을 작성했습니다."

notification = Notification(
type="article_commented",
title=title,
content=_comment.content[:32],
related_article=_parent_article,
related_comment=None,
)
notification.save()

NotificationReadLog.objects.create(
read_by=_parent_article.created_by,
notification=notification,
)

related_article = comment.parent_article if comment.parent_article else parent_comment.parent_article
fcm_notify_comment(
_parent_article.created_by,
title,
_comment.content[:32],
f"post/{_parent_article.id}",
)

title = f"{article.title}에 새로운 {'대댓글' if parent_comment else '댓글'}이 작성되었습니다."
content = comment.content[:32]
def notify_comment_commented(_parent_article: Article, _comment: Comment):
name = self.get_display_name(_parent_article, _comment.created_by_id)
title = f"{name} 님이 새로운 대댓글을 작성했습니다."

Notification.objects.create(
type="comment_commented" if parent_comment else "article_commented",
title=title,
content=content,
related_article=related_article,
related_comment=related_comment,
notification = Notification(
type="comment_commented",
title=title,
content=_comment.content[:32],
related_article=_parent_article,
related_comment=_comment.parent_comment,
)
notification.save()

NotificationReadLog.objects.create(
read_by=_comment.parent_comment.created_by,
notification=notification,
)

fcm_notify_comment(
_comment.parent_comment.created_by,
title,
_comment.content[:32],
f"post/{_parent_article.id}",
)

article = (
comment.parent_article
if comment.parent_article
else comment.parent_comment.parent_article
)
"""

if comment.created_by != article.created_by:
notify_article_commented(article, comment)

if (
comment.parent_comment
and comment.created_by != comment.parent_comment.created_by
):
notify_comment_commented(article, comment)
Loading
Loading