Skip to content

Commit

Permalink
Merge pull request #456 from sparcs-kaist/fix/notification-user-name
Browse files Browse the repository at this point in the history
Fix notification displaying nickname on anonymous post
  • Loading branch information
injoonH committed Mar 12, 2024
2 parents 4d423fb + 60f9c2b commit ddc3b4c
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions apps/core/models/notification.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from django.db import models
from django.utils.functional import cached_property

from apps.core.models import Article, Comment
from apps.core.models.board import NameType
from ara.db.models import MetaDataModel
from ara.firebase import fcm_notify_comment

if TYPE_CHECKING:
from apps.user.models import UserProfile

TYPE_CHOICES = (
("default", "default"),
("article_commented", "article_commented"),
Expand All @@ -12,41 +21,41 @@


class Notification(MetaDataModel):
class Meta(MetaDataModel.Meta):
verbose_name = "알림"
verbose_name_plural = "알림 목록"

type = models.CharField(
verbose_name="알림 종류",
choices=TYPE_CHOICES,
default="default",
max_length=32,
verbose_name="알림 종류",
)
title = models.CharField(
max_length=256,
verbose_name="제목",
max_length=256,
)
content = models.TextField(
verbose_name="내용",
)

related_article = models.ForeignKey(
on_delete=models.CASCADE,
verbose_name="알림 관련 제보",
to="core.Article",
on_delete=models.CASCADE,
related_name="notification_set",
null=True,
db_index=True,
related_name="notification_set",
verbose_name="알림 관련 제보",
)
related_comment = models.ForeignKey(
on_delete=models.CASCADE,
verbose_name="알림 관련 댓글",
to="core.Comment",
on_delete=models.CASCADE,
related_name="notification_set",
null=True,
db_index=True,
related_name="notification_set",
verbose_name="알림 관련 댓글",
)

class Meta(MetaDataModel.Meta):
verbose_name = "알림"
verbose_name_plural = "알림 목록"

@cached_property
def data(self) -> dict:
return {
Expand All @@ -56,12 +65,23 @@ def data(self) -> dict:
"click_action": "",
}

@staticmethod
def get_display_name(article: Article, profile: UserProfile):
if article.name_type == NameType.REALNAME:
return profile.realname
elif article.name_type == NameType.REGULAR:
return profile.nickname
else:
return "익명"

@classmethod
def notify_commented(cls, comment):
from apps.core.models import NotificationReadLog

def notify_article_commented(_parent_article, _comment):
title = f"{_comment.created_by.profile.nickname} 님이 새로운 댓글을 작성했습니다."
def notify_article_commented(_parent_article: Article, _comment: Comment):
name = cls.get_display_name(_parent_article, _comment.created_by.profile)
title = f"{name} 님이 새로운 댓글을 작성했습니다."

NotificationReadLog.objects.create(
read_by=_parent_article.created_by,
notification=cls.objects.create(
Expand All @@ -79,8 +99,10 @@ def notify_article_commented(_parent_article, _comment):
f"post/{_parent_article.id}",
)

def notify_comment_commented(_parent_article, _comment):
title = f"{_comment.created_by.profile.nickname} 님이 새로운 대댓글을 작성했습니다."
def notify_comment_commented(_parent_article: Article, _comment: Comment):
name = cls.get_display_name(_parent_article, _comment.created_by.profile)
title = f"{name} 님이 새로운 대댓글을 작성했습니다."

NotificationReadLog.objects.create(
read_by=_comment.parent_comment.created_by,
notification=cls.objects.create(
Expand Down

0 comments on commit ddc3b4c

Please sign in to comment.