-
Notifications
You must be signed in to change notification settings - Fork 1
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
Qndndn
wants to merge
7
commits into
develop
Choose a base branch
from
fastapi/notification
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a47a2e8
feat: Creat notification FastAPI
c8b7d65
notification_infra
7298e37
feat: notifiction_infra
f562ccc
feat: create_notification
32cba7f
feat: notification_service
41da2ef
feat: testcase_notification_infra
c228e10
feat: Docker Compose v2 syntax
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from ara.controller.authentication import get_current_user | ||
from ara.service.notification_service import NotificationService | ||
from ara.domain.user import User | ||
from ara.domain.exceptions import EntityDoesNotExist | ||
from pydantic import BaseModel | ||
|
||
router = APIRouter() | ||
notification_service = NotificationService() | ||
|
||
class NotificationRead(BaseModel): | ||
notification_id: int | ||
|
||
@router.get("/notifications") | ||
async def list_notifications(current_user: User = Depends(get_current_user)): | ||
notifications = notification_service.get_notifications_for_user(current_user) | ||
return notifications | ||
|
||
@router.post("/notifications/{notification_id}/read") | ||
async def mark_notification_as_read(notification_id: int, current_user: User = Depends(get_current_user)): | ||
try: | ||
notification_service.mark_notification_as_read(notification_id, current_user) | ||
except EntityDoesNotExist: | ||
raise HTTPException(status_code=404, detail="Notification not found") | ||
except PermissionError: | ||
raise HTTPException(status_code=403, detail="You are not allowed to mark this notification as read") | ||
return {"message": "Notification marked as read successfully"} | ||
|
||
@router.post("/notifications/read-all") | ||
async def mark_all_notifications_as_read(current_user: User = Depends(get_current_user)): | ||
notification_service.mark_all_notifications_as_read(current_user) | ||
return {"message": "All notifications marked as read successfully"} | ||
|
||
@router.post("/notifications/send-push-notification") | ||
async def send_push_notification(notification: NotificationRead, current_user: User = Depends(get_current_user)): | ||
try: | ||
notification_service.send_push_notification(notification.notification_id, current_user) | ||
except EntityDoesNotExist: | ||
raise HTTPException(status_code=404, detail="Notification not found") | ||
except PermissionError: | ||
raise HTTPException(status_code=403, detail="You are not allowed to send push notification for this notification") | ||
return {"message": "Push notification sent successfully"} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class EntityDoesNotExist(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
|
||
class Notification(BaseModel): | ||
id: int | ||
type: str | ||
title: str | ||
content: str | ||
related_article_id: Optional[int] | ||
related_comment_id: Optional[int] | ||
is_read: bool | ||
|
||
class Config: | ||
orm_mode = True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from apps.core.models import Comment | ||
from ara.domain.notification.type import NotificationInfo | ||
from ara.infra.notification.notification_infra import NotificationInfra | ||
|
||
|
||
class NotificationDomain: | ||
def __init__(self) -> None: | ||
self.notification_infra = NotificationInfra() | ||
|
||
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]: | ||
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, comment: Comment): | ||
return self.notification_infra.create_notification(comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
from apps.core.models import Article, Comment, Notification | ||
|
||
|
||
class NotificationReadLogInfo(BaseModel): | ||
is_read: bool | ||
read_by: int | ||
notification: Notification | ||
|
||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
|
||
class NotificationInfo(BaseModel): | ||
id: int | ||
type: str | ||
title: str | ||
content: str | ||
related_article: Article | None | ||
related_comment: Optional[Comment] | ||
|
||
class Config: | ||
arbitrary_types_allowed = True |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
from django.db.models import Prefetch | ||
|
||
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) -> None: | ||
super().__init__(Notification) | ||
|
||
def get_all_notifications(self, user_id: int) -> list[NotificationInfo]: | ||
queryset = Notification.objects.select_related( | ||
"related_article", | ||
"related_comment", | ||
).prefetch_related( | ||
"related_article__attachments", | ||
NotificationReadLog.prefetch_my_notification_read_log(user_id), | ||
) | ||
return [self._to_notification_info(notification) for notification in queryset] | ||
|
||
def get_unread_notifications(self, user_id: int) -> list[NotificationInfo]: | ||
queryset = Notification.objects.select_related( | ||
"related_article", | ||
"related_comment", | ||
).prefetch_related("related_article__attachments") | ||
|
||
unread_notifications = [ | ||
self._to_notification_info(notification) | ||
for notification in queryset | ||
if NotificationReadLog.objects.filter( | ||
notification=notification, read_by_id=user_id, is_read=False | ||
).exists() | ||
] | ||
|
||
return unread_notifications | ||
|
||
def _to_notification_info(self, notification: Notification) -> NotificationInfo: | ||
return NotificationInfo( | ||
id=notification.id, | ||
type=notification.type, | ||
title=notification.title, | ||
content=notification.content, | ||
related_article=notification.related_article, | ||
related_comment=notification.related_comment, | ||
) | ||
|
||
def read_all_notifications(self, user_id: int) -> None: | ||
unread_notifications = self.get_unread_notifications(user_id) | ||
notification_ids = [notification.id for notification in unread_notifications] | ||
NotificationReadLog.objects.filter( | ||
notification__in=notification_ids, read_by=user_id | ||
).update(is_read=True) | ||
|
||
def _get_display_name(self, article: Article, profile: int): | ||
if article.name_type == NameType.REALNAME: | ||
return "실명" | ||
elif article.name_type == NameType.REGULAR: | ||
return "nickname" | ||
else: | ||
return "익명" | ||
|
||
def create_notification(self, comment: Comment) -> None: | ||
def notify_article_commented(_parent_article: Article, _comment: Comment): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} 님이 새로운 댓글을 작성했습니다." | ||
|
||
print(f"Creating notification for article commented: {title}") | ||
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, | ||
) | ||
|
||
fcm_notify_comment( | ||
_parent_article.created_by, | ||
title, | ||
_comment.content[:32], | ||
f"post/{_parent_article.id}", | ||
) | ||
|
||
def notify_comment_commented(_parent_article: Article, _comment: Comment): | ||
name = self._get_display_name(_parent_article, _comment.created_by_id) | ||
title = f"{name} 님이 새로운 대댓글을 작성했습니다." | ||
|
||
print(f"Creating notification for comment commented: {title}") | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from apps.core.models import Comment | ||
from ara.domain.notification.notification_domain import NotificationDomain | ||
from ara.domain.notification.type import NotificationInfo | ||
|
||
|
||
class NotificationService: | ||
def __init__(self) -> None: | ||
self.notification_infra = NotificationDomain() | ||
|
||
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]: | ||
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, comment: Comment): | ||
return self.notification_infra.create_notification(comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 transaction이 붙어야 할거 같아요 음... 근데 이거는 어려울 수도 있으니까 같이 한번 봐요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵~!