diff --git a/src/controller/inn.rs b/src/controller/inn.rs index 6d0adf9f..8c81c1d5 100644 --- a/src/controller/inn.rs +++ b/src/controller/inn.rs @@ -1873,6 +1873,13 @@ pub(crate) async fn comment_hide( comment.is_hidden = !comment.is_hidden; set_one_with_key(&DB, "post_comments", k, &comment)?; + add_notification( + &DB, + comment.uid, + NtType::CommentHide, + comment.pid, + comment.cid, + )?; let target = format!("/post/{iid}/{pid}"); Ok(Redirect::to(&target)) @@ -2014,6 +2021,7 @@ pub(crate) async fn post_lock( if User::is_mod(&DB, claim.uid, iid)? || Role::from(claim.role) == Role::Admin { if post.status != PostStatus::LockedByMod { + add_notification(&DB, post.uid, NtType::PostLock, claim.uid, post.pid)?; post.status = PostStatus::LockedByMod } else if post.status == PostStatus::LockedByMod { post.status = PostStatus::Normal @@ -2047,6 +2055,7 @@ pub(crate) async fn post_hide( if User::is_mod(&DB, claim.uid, iid)? || Role::from(claim.role) == Role::Admin { if post.status != PostStatus::HiddenByMod { + add_notification(&DB, post.uid, NtType::PostHide, claim.uid, post.pid)?; post.status = PostStatus::HiddenByMod } else if post.status == PostStatus::HiddenByMod { post.status = PostStatus::Normal diff --git a/src/controller/notification.rs b/src/controller/notification.rs index 60a59410..7d4b4058 100644 --- a/src/controller/notification.rs +++ b/src/controller/notification.rs @@ -43,6 +43,9 @@ pub enum NtType { Message = 7, SoloDelete = 8, ImageDelete = 9, + PostLock = 10, + PostHide = 11, + CommentHide = 12, } impl From for NtType { @@ -57,6 +60,9 @@ impl From for NtType { 7 => Self::Message, 8 => Self::SoloDelete, 9 => Self::ImageDelete, + 10 => Self::PostLock, + 11 => Self::PostHide, + 12 => Self::CommentHide, _ => unreachable!(), } } @@ -204,6 +210,62 @@ pub(crate) async fn notification( tree.remove(&key)?; }; } + NtType::PostHide => { + let uid = u8_slice_to_u32(&value[0..4]); + let user: User = get_one(&DB, "users", uid)?; + let pid = u8_slice_to_u32(&value[4..8]); + let post: Post = get_one(&DB, "posts", pid)?; + let content2 = format!( + "{} has hidden your 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::PostLock => { + let uid = u8_slice_to_u32(&value[0..4]); + let user: User = get_one(&DB, "users", uid)?; + let pid = u8_slice_to_u32(&value[4..8]); + let post: Post = get_one(&DB, "posts", pid)?; + let content2 = format!( + "{} has locked your 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::CommentHide => { + 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)?; + let content1 = format!( + "Your comment on {} has been hidden", + post.iid, comment.pid, nid, comment.cid, post.title + ); + let notification = Notification { + nid, + uid: comment.uid, + content1, + content2: unescape(&comment.content).unwrap(), + is_read, + }; + notifications.push(notification); + } else { + tree.remove(&key)?; + }; + } NtType::SoloComment => { let sid1 = u8_slice_to_u32(&value[0..4]); let sid2 = u8_slice_to_u32(&value[4..8]);