From f1b0fbedd681bd78d575da290fd64e4dede1072f Mon Sep 17 00:00:00 2001 From: GitHub Date: Sat, 3 Feb 2024 15:13:27 +0800 Subject: [PATCH] fix post mention bug --- src/controller/inn.rs | 2 +- src/controller/notification.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/controller/inn.rs b/src/controller/inn.rs index 9d085617..2d33a2f3 100644 --- a/src/controller/inn.rs +++ b/src/controller/inn.rs @@ -1732,7 +1732,7 @@ pub(crate) async fn comment_post( // notify user to be mentioned in comment // prevent duplicate notifications if uid != post.uid { - add_notification(&DB, uid, NtType::PostMention, pid, cid)?; + add_notification(&DB, uid, NtType::CommentMention, pid, cid)?; } } diff --git a/src/controller/notification.rs b/src/controller/notification.rs index 7d4b4058..cc7c0058 100644 --- a/src/controller/notification.rs +++ b/src/controller/notification.rs @@ -46,6 +46,7 @@ pub enum NtType { PostLock = 10, PostHide = 11, CommentHide = 12, + CommentMention = 13, } impl From for NtType { @@ -63,6 +64,7 @@ impl From for NtType { 10 => Self::PostLock, 11 => Self::PostHide, 12 => Self::CommentHide, + 13 => Self::CommentMention, _ => unreachable!(), } } @@ -153,7 +155,7 @@ pub(crate) async fn notification( } let mut notifications = Vec::with_capacity(n); - for (idx, i) in tree.scan_prefix(&prefix).enumerate() { + for (idx, i) in tree.scan_prefix(&prefix).rev().enumerate() { if idx < anchor { continue; } @@ -190,6 +192,26 @@ pub(crate) async fn notification( }; } NtType::PostMention => { + let pid = u8_slice_to_u32(&value[0..4]); + let Ok(post) = get_one::(&DB, "posts", pid) else { + tree.remove(&key)?; + continue; + }; + let user: User = get_one(&DB, "users", post.uid)?; + let content2 = format!( + "{} mentioned you on post {}", + user.username, post.iid, pid, nid, post.title + ); + let notification = Notification { + nid, + uid: post.uid, + content1: String::new(), + content2, + is_read, + }; + notifications.push(notification); + } + NtType::CommentMention => { if let Some(v) = &DB.open_tree("post_comments")?.get(&value[0..8])? { let (comment, _): (Comment, usize) = bincode::decode_from_slice(v, standard())?; let post: Post = get_one(&DB, "posts", comment.pid)?; @@ -385,7 +407,6 @@ pub(crate) async fn notification( } } } - notifications.reverse(); let mut inn_notifications = Vec::new(); let mod_inns = get_ids_by_prefix(&DB, "mod_inns", prefix, None)?;