-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
854d802
commit 48eec32
Showing
12 changed files
with
361 additions
and
241 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { and, eq } from 'drizzle-orm'; | ||
import { database, PostComments, PostRevisions, Posts, Profiles, Spaces } from '$lib/server/database'; | ||
import { createNotification, useFirstRow } from '$lib/server/utils'; | ||
import type { NotificationMaker } from './type'; | ||
|
||
export const commentNotificationMaker: NotificationMaker = async (commentId) => { | ||
const comment = await database | ||
.select({ | ||
id: PostComments.id, | ||
content: PostComments.content, | ||
commenterId: PostComments.userId, | ||
profileId: PostComments.profileId, | ||
profileName: Profiles.name, | ||
spaceId: Posts.spaceId, | ||
spaceSlug: Spaces.slug, | ||
postPermalink: Posts.permalink, | ||
postWriterId: Posts.userId, | ||
postTitle: PostRevisions.title, | ||
parentId: PostComments.parentId, | ||
}) | ||
.from(PostComments) | ||
.innerJoin(Posts, eq(PostComments.postId, Posts.id)) | ||
.innerJoin(Spaces, eq(Posts.spaceId, Spaces.id)) | ||
.innerJoin(PostRevisions, eq(Posts.publishedRevisionId, PostRevisions.id)) | ||
.innerJoin(Profiles, eq(PostComments.profileId, Profiles.id)) | ||
.where(and(eq(PostComments.id, commentId), eq(PostComments.state, 'ACTIVE'))) | ||
.then(useFirstRow); | ||
|
||
if (!comment || !comment.spaceId) { | ||
return; | ||
} | ||
|
||
let notifiedUserId = comment.postWriterId; | ||
|
||
if (comment.parentId) { | ||
const parentComment = await database | ||
.select({ | ||
userId: PostComments.userId, | ||
}) | ||
.from(PostComments) | ||
.where(and(eq(PostComments.id, comment.parentId), eq(PostComments.state, 'ACTIVE'))) | ||
.then(useFirstRow); | ||
|
||
if (parentComment) { | ||
if (parentComment.userId === comment.commenterId) { | ||
return; | ||
} | ||
|
||
notifiedUserId = parentComment.userId; | ||
} | ||
} else if (notifiedUserId === comment.commenterId) { | ||
return; | ||
} | ||
|
||
await createNotification({ | ||
userId: notifiedUserId, | ||
category: 'COMMENT', | ||
actorId: comment.profileId, | ||
data: { | ||
commentId: comment.id, | ||
}, | ||
pushTitle: comment.postTitle ?? '(제목 없음)', | ||
pushBody: `${comment.profileName}님이 "${comment.content}" 댓글을 남겼어요.`, | ||
pushPath: `/${comment.spaceSlug}/${comment.postPermalink}`, | ||
}); | ||
}; |
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,43 @@ | ||
import { eq } from 'drizzle-orm'; | ||
import { database, PostReactions, PostRevisions, Posts, Spaces } from '$lib/server/database'; | ||
import { createNotification, getSpaceProfile, useFirstRow } from '$lib/server/utils'; | ||
import type { NotificationMaker } from './type'; | ||
|
||
export const emojiReactionNotificationMaker: NotificationMaker = async (reactionId) => { | ||
const reaction = await database | ||
.select({ | ||
emoji: PostReactions.emoji, | ||
postId: Posts.id, | ||
postPermalink: Posts.permalink, | ||
title: PostRevisions.title, | ||
postWriterId: Posts.userId, | ||
spaceId: Posts.spaceId, | ||
spaceSlug: Spaces.slug, | ||
emojigazerId: PostReactions.userId, | ||
}) | ||
.from(PostReactions) | ||
.innerJoin(Posts, eq(PostReactions.postId, Posts.id)) | ||
.innerJoin(PostRevisions, eq(Posts.publishedRevisionId, PostRevisions.id)) | ||
.innerJoin(Spaces, eq(Posts.spaceId, Spaces.id)) | ||
.where(eq(PostReactions.id, reactionId)) | ||
.then(useFirstRow); | ||
|
||
if (!reaction || !reaction.spaceId || reaction.postWriterId === reaction.emojigazerId) { | ||
return; | ||
} | ||
|
||
const profile = await getSpaceProfile({ spaceId: reaction.spaceId, userId: reaction.emojigazerId }); | ||
|
||
await createNotification({ | ||
userId: reaction.postWriterId, | ||
category: 'EMOJI_REACTION', | ||
actorId: profile.id, | ||
data: { | ||
postId: reaction.postId, | ||
emoji: reaction.emoji, | ||
}, | ||
pushTitle: reaction.title ?? '(제목 없음)', | ||
pushBody: `${profile.name}님이 이모지를 남겼어요.`, | ||
pushPath: `/${reaction.spaceSlug}/${reaction.postPermalink}`, | ||
}); | ||
}; |
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,11 @@ | ||
import { commentNotificationMaker } from './comment'; | ||
import { emojiReactionNotificationMaker } from './emoji-reaction'; | ||
import { purchaseNotificationMaker } from './purchase'; | ||
import { subscribeNotificationMaker } from './subscribe'; | ||
|
||
export const notificationMaker = { | ||
EMOJI_REACTION: emojiReactionNotificationMaker, | ||
COMMENT: commentNotificationMaker, | ||
PURCHASE: purchaseNotificationMaker, | ||
SUBSCRIBE: subscribeNotificationMaker, | ||
}; |
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,38 @@ | ||
import { eq } from 'drizzle-orm'; | ||
import { database, PostPurchases, PostRevisions, Posts } from '$lib/server/database'; | ||
import { createNotification, getSpaceProfile, useFirstRow } from '$lib/server/utils'; | ||
import type { NotificationMaker } from './type'; | ||
|
||
export const purchaseNotificationMaker: NotificationMaker = async (purchaseId) => { | ||
const purchase = await database | ||
.select({ | ||
postId: PostPurchases.postId, | ||
buyerId: PostPurchases.userId, | ||
postWriterId: Posts.userId, | ||
spaceId: Posts.spaceId, | ||
postTitle: PostRevisions.title, | ||
}) | ||
.from(PostPurchases) | ||
.innerJoin(Posts, eq(PostPurchases.postId, Posts.id)) | ||
.innerJoin(PostRevisions, eq(Posts.publishedRevisionId, PostRevisions.id)) | ||
.where(eq(PostPurchases.id, purchaseId)) | ||
.then(useFirstRow); | ||
|
||
if (!purchase || !purchase.spaceId) { | ||
return; | ||
} | ||
|
||
const profile = await getSpaceProfile({ spaceId: purchase.spaceId, userId: purchase.buyerId }); | ||
|
||
await createNotification({ | ||
userId: purchase.postWriterId, | ||
category: 'PURCHASE', | ||
actorId: profile.id, | ||
data: { | ||
postId: purchase.postId, | ||
}, | ||
pushTitle: purchase.postTitle ?? '(제목 없음)', | ||
pushBody: `${profile.name}님이 포스트를 구매했어요.`, | ||
pushPath: '/me/revenue', | ||
}); | ||
}; |
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,48 @@ | ||
import { eq } from 'drizzle-orm'; | ||
import { database, SpaceFollows, SpaceMembers, Spaces } from '$lib/server/database'; | ||
import { createNotification, getSpaceProfile, useFirstRow } from '$lib/server/utils'; | ||
import type { NotificationMaker } from './type'; | ||
|
||
export const subscribeNotificationMaker: NotificationMaker = async (spaceFollowId) => { | ||
const spaceFollow = await database | ||
.select({ | ||
followerId: SpaceFollows.userId, | ||
spaceId: SpaceFollows.spaceId, | ||
spaceSlug: Spaces.slug, | ||
spaceName: Spaces.name, | ||
}) | ||
.from(SpaceFollows) | ||
.innerJoin(Spaces, eq(SpaceFollows.spaceId, Spaces.id)) | ||
.where(eq(SpaceFollows.id, spaceFollowId)) | ||
.then(useFirstRow); | ||
|
||
if (!spaceFollow) { | ||
return; | ||
} | ||
|
||
const profile = await getSpaceProfile({ spaceId: spaceFollow.spaceId, userId: spaceFollow.followerId }); | ||
|
||
const spaceMemberIds = await database | ||
.select({ | ||
userId: SpaceMembers.userId, | ||
}) | ||
.from(SpaceMembers) | ||
.where(eq(SpaceMembers.spaceId, spaceFollow.spaceId)) | ||
.then((rows) => rows.map((row) => row.userId)); | ||
|
||
await Promise.all( | ||
spaceMemberIds.map(async (userId) => { | ||
await createNotification({ | ||
userId, | ||
category: 'SUBSCRIBE', | ||
actorId: profile.id, | ||
data: { | ||
spaceId: spaceFollow.spaceId, | ||
}, | ||
pushTitle: spaceFollow.spaceName, | ||
pushBody: `${profile.name}님이 스페이스를 구독했어요.`, | ||
pushPath: `/${spaceFollow.spaceSlug}`, | ||
}); | ||
}), | ||
); | ||
}; |
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,3 @@ | ||
import type { MaybePromise } from '$lib/types'; | ||
|
||
export type NotificationMaker = (targetId: string) => MaybePromise<void>; |
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.