Skip to content

Commit

Permalink
feat: 이런저런 알림 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
CChuYong committed Jul 26, 2023
1 parent fb29942 commit 6ef1e27
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import co.bearus.magcloud.domain.entity.diary.DiaryCommentEntity
import co.bearus.magcloud.domain.repository.JPADiaryCommentRepository
import co.bearus.magcloud.domain.repository.QUserDiaryRepository
import co.bearus.magcloud.domain.service.diary.UserDiaryService
import co.bearus.magcloud.domain.service.notification.NotificationService
import co.bearus.magcloud.domain.type.ContextLanguage
import co.bearus.magcloud.util.ULIDUtils
import org.springframework.data.repository.findByIdOrNull
Expand All @@ -29,6 +30,7 @@ class DiaryCommentController(
private val diaryCommentRepository: JPADiaryCommentRepository,
private val diaryService: UserDiaryService,
private val qUserDiaryRepository: QUserDiaryRepository,
private val notificationService: NotificationService,
) {
@Transactional
@PostMapping
Expand All @@ -44,7 +46,9 @@ class DiaryCommentController(
userId = user.userId,
content = body.content,
)
return diaryCommentRepository.save(comment).toDto()
val result = diaryCommentRepository.save(comment).toDto()
notificationService.sendCommentCreateNotification(result)
return result
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import co.bearus.magcloud.domain.repository.JPADiaryLikeRepository
import co.bearus.magcloud.domain.repository.JPAUserDiaryRepository
import co.bearus.magcloud.domain.repository.JPAUserRepository
import co.bearus.magcloud.domain.repository.QUserDiaryRepository
import co.bearus.magcloud.domain.service.notification.NotificationService
import co.bearus.magcloud.domain.type.Emotion
import co.bearus.magcloud.util.DateUtils
import co.bearus.magcloud.util.DateUtils.Companion.toEpochMillis
Expand All @@ -29,6 +30,7 @@ class UserDiaryService(
private val diaryRepository: JPAUserDiaryRepository,
private val diaryLikeRepository: JPADiaryLikeRepository,
private val qDiaryRepository: QUserDiaryRepository,
private val notificationService: NotificationService,
) {
@Transactional
fun createDiary(
Expand Down Expand Up @@ -95,6 +97,7 @@ class UserDiaryService(
DiaryLikeEntity.of(diaryId, userId)
)
diary.likeCount = diaryLikeRepository.countByDiaryId(diaryId).toInt()
notificationService.sendDiaryLikedNotification(diaryId)
return diaryRepository.save(diary).toDto()
}
return diary.toDto()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package co.bearus.magcloud.domain.service.notification

import co.bearus.magcloud.controller.dto.request.DiaryCommentDTO
import co.bearus.magcloud.controller.dto.response.DiaryResponseDTO
import co.bearus.magcloud.domain.repository.*
import co.bearus.magcloud.domain.type.NotificationType
import co.bearus.magcloud.util.StringUtils
import com.google.auth.oauth2.GoogleCredentials
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
Expand All @@ -22,6 +24,7 @@ class NotificationService(
private val userNotificationConfigRepository: JPAUserNotificationConfigRepository,
private val qUserFriendRepository: QUserFriendRepository,
private val userRepository: JPAUserRepository,
private val diaryRepository: JPAUserDiaryRepository,
) {
init {
val credentials = ByteArrayInputStream(Base64.getDecoder().decode(secretValue))
Expand Down Expand Up @@ -51,6 +54,8 @@ class NotificationService(
fun sendDiaryCreateNotification(diary: DiaryResponseDTO) {
val requester = userRepository.findByIdOrNull(diary.userId) ?: return
val friends = qUserFriendRepository.getFriends(diary.userId)
val taggedPeoples = StringUtils.extractULIDFromString(diary.content)
friends.removeIf { taggedPeoples.contains(it.userId) } //TODO: O(n^2)
friends.forEach { user ->
sendMessageToUser(
notificationType = NotificationType.FEED,
Expand All @@ -59,6 +64,50 @@ class NotificationService(
routePath = "/feed"
)
}

taggedPeoples.forEach {
val taggedUser = userRepository.findByIdOrNull(it) ?: return@forEach
sendMessageToUser(
notificationType = NotificationType.SOCIAL,
userId = taggedUser.userId,
description = "${requester.name}님이 당신을 일기에서 언급했어요!",
routePath = "/feed"
)
}
}

@Async
fun sendCommentCreateNotification(comment: DiaryCommentDTO) {
val diary = diaryRepository.findByIdOrNull(comment.diaryId) ?: return
val writer = userRepository.findByIdOrNull(comment.userId) ?: return
sendMessageToUser(
notificationType = NotificationType.SOCIAL,
userId = diary.userId,
description = "${writer.name}님이 내 일기에 댓글을 달았어요!",
routePath = "/comment/${comment.diaryId}"
)

val taggedPeoples = StringUtils.extractULIDFromString(comment.content)
taggedPeoples.forEach {
val taggedUser = userRepository.findByIdOrNull(it) ?: return@forEach
sendMessageToUser(
notificationType = NotificationType.SOCIAL,
userId = taggedUser.userId,
description = "${writer.name}님이 당신을 댓글에서 언급했어요!",
routePath = "/comment/${comment.diaryId}"
)
}
}

@Async
fun sendDiaryLikedNotification(diaryId: String) {
val diary = diaryRepository.findByIdOrNull(diaryId) ?: return
sendMessageToUser(
notificationType = NotificationType.SOCIAL,
userId = diary.userId,
description = "누군가 내 일기에 좋아요를 눌렀어요!",
routePath = "/feed"
)
}

@Async
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/co/bearus/magcloud/util/StringUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.bearus.magcloud.util

class StringUtils {
companion object {
fun extractULIDFromString(text: String): List<String> {
val regex = Regex("([@]\\S+)")
val result = mutableListOf<String>()

regex.findAll(text).forEach { match ->
result.add(match.value.substring(1))
}
return result.filter { ULIDUtils.isULID(it) }
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/co/bearus/magcloud/util/ULIDUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ class ULIDUtils {
fun generate(): String {
return UlidCreator.getMonotonicUlid().toString()
}

fun isULID(input: String): Boolean {
val ulidRegex = Regex("^[0-9a-z]{26}$")
return ulidRegex.matches(input)
}
}
}

0 comments on commit 6ef1e27

Please sign in to comment.