Skip to content

Commit

Permalink
feat: 帖子点赞/加精/置顶/审核通过、评论点赞/置顶 消息通知
Browse files Browse the repository at this point in the history
  • Loading branch information
linfaxin committed Jun 8, 2024
1 parent d9322cc commit f8e4a6f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
26 changes: 26 additions & 0 deletions server/routes/bbs/controllers/PostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ export default class PostController {
throw new UIError('无权限点赞');
}
await setUserLikePost(db, post, currentUser.id, isLike);

// 评论点赞消息提示
if (isLike && currentUser.id !== post.user_id) {
insertUserMessage(db, {
title: `你的评论"${formatSubString(markdownToPureText(post.content), 10)}"被点赞了`,
content: `用户"${formatSubString(currentUser.nickname, 15)}"点赞了你的评论`,
link: `/#/thread/detail/${thread.id}`,
user_id: post.user_id,
from_user_id: currentUser.id,
unread_merge_key: `viewThread${thread.id}.post${post.id}.setLike`,
}).catch(noop);
}

userLikePostLogger.log({ postId, isLike });
return true;
}
Expand Down Expand Up @@ -74,6 +87,19 @@ export default class PostController {

post.is_sticky = isSticky;
await post.save();

// 评论置顶消息提示
if (currentUser.id !== post.user_id) {
insertUserMessage(db, {
title: `你的评论"${formatSubString(markdownToPureText(post.content), 10)}"被${isSticky ? '置顶' : '取消置顶'}了`,
content: `用户"${formatSubString(currentUser.nickname, 15)}"${isSticky ? '置顶' : '取消置顶'}了你的评论`,
link: `/#/thread/detail/${thread.id}`,
user_id: post.user_id,
from_user_id: currentUser.id,
unread_merge_key: `viewThread${thread.id}.post${post.id}.setSticky`,
}).catch(noop);
}

userStickyThreadLogger.log({ isSticky, threadId: thread.id, postId });
return true;
}
Expand Down
70 changes: 70 additions & 0 deletions server/routes/bbs/controllers/ThreadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export default class ThreadController {

// 数据处理
await setUserLikePost(db, firstPost, currentUser.id, isLike);

// 点赞消息提示
if (isLike && currentUser.id !== thread.user_id) {
insertUserMessage(db, {
title: `你的帖子"${formatSubString(thread.title, 15)}"被点赞了`,
content: `用户"${formatSubString(currentUser.nickname, 15)}"点赞了你的帖子`,
link: `/#/thread/detail/${thread.id}`,
user_id: thread.user_id,
from_user_id: currentUser.id,
unread_merge_key: `viewThread${thread.id}.setLike`,
}).catch(noop);
}

userLikeThreadLogger.log({ isLike, threadId, title: thread.title });
return true;
}
Expand Down Expand Up @@ -98,8 +111,35 @@ export default class ThreadController {
} else {
await thread.removeTag(THREAD_TAG_ID_STICKY, false);
}
const sticky_at_other_categories_before_set = thread.sticky_at_other_categories;
thread.sticky_at_other_categories = isSticky ? sticky_at_other_categories || null : null;
await thread.save();

// 置顶消息提示
if (currentUser.id !== thread.user_id) {
const belongCategoryIds = new Set(
[thread.category_id].concat(
((isSticky ? sticky_at_other_categories : sticky_at_other_categories_before_set) || '')
.split(',')
.filter(Boolean)
.map((id) => parseInt(id)),
),
);
const belongCategoryNames = await Promise.all(
Array.from(belongCategoryIds).map((cid) => getCategoryById(db, cid).then((c) => c.name)),
);
insertUserMessage(db, {
title: `你的帖子"${formatSubString(thread.title, 15)}"被${isSticky ? '置顶' : '取消置顶'}了`,
content: `管理员"${formatSubString(currentUser.nickname, 15)}"${
isSticky ? '置顶' : '取消置顶'
}了你的帖子\n板块:${belongCategoryNames.join(', ')}`,
link: `/#/thread/detail/${thread.id}`,
user_id: thread.user_id,
from_user_id: currentUser.id,
unread_merge_key: `viewThread${thread.id}.setSticky`,
}).catch(noop);
}

userStickyThreadLogger.log({ isSticky, threadId });
return true;
}
Expand All @@ -120,13 +160,28 @@ export default class ThreadController {
if (!hasPermission) throw new UIError('无权设置精华');
if (thread.deleted_at) throw new UIError('帖子已被删除');

if (thread.is_essence === isEssence) return true; // 状态未变

thread.is_essence = isEssence;
if (isEssence) {
await thread.addTag(THREAD_TAG_ID_ESSENCE, false);
} else {
await thread.removeTag(THREAD_TAG_ID_ESSENCE, false);
}
await thread.save();

// 加精消息提示
if (currentUser.id !== thread.user_id) {
insertUserMessage(db, {
title: `你的帖子"${formatSubString(thread.title, 15)}"被${isEssence ? '加精' : '取消加精'}了`,
content: `管理员"${formatSubString(currentUser.nickname, 15)}"${isEssence ? '' : '取消'}设置该贴为精华帖子`,
link: `/#/thread/detail/${thread.id}`,
user_id: thread.user_id,
from_user_id: currentUser.id,
unread_merge_key: `viewThread${thread.id}.setEssence`,
}).catch(noop);
}

userEssenceThreadLogger.log({ isEssence, threadId });
return true;
}
Expand All @@ -143,8 +198,23 @@ export default class ThreadController {
const thread = await getThread(db, threadId);
if (thread == null) throw new UIError('帖子未找到');

if (thread.is_approved === isApproved) return true; // 状态未变

thread.is_approved = isApproved;
await thread.saveAndUpdateThreadCount();

// 审核消息提示
if (isApproved === ThreadIsApproved.ok && currentUser.id !== thread.user_id) {
insertUserMessage(db, {
title: `你的帖子"${formatSubString(thread.title, 15)}"被审核通过了`,
content: `管理员"${formatSubString(currentUser.nickname, 15)}"审核通过了该贴`,
link: `/#/thread/detail/${thread.id}`,
user_id: thread.user_id,
from_user_id: currentUser.id,
unread_merge_key: `viewThread${thread.id}.setApproved`,
}).catch(noop);
}

userApprovedThreadLogger.log({ isApproved, threadId });
return true;
}
Expand Down

0 comments on commit f8e4a6f

Please sign in to comment.