Skip to content

Commit

Permalink
add notification for post lock/hide/ comment hide
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub committed Feb 2, 2024
1 parent ba909f4 commit b1a2eee
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/controller/inn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions src/controller/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub enum NtType {
Message = 7,
SoloDelete = 8,
ImageDelete = 9,
PostLock = 10,
PostHide = 11,
CommentHide = 12,
}

impl From<u8> for NtType {
Expand All @@ -57,6 +60,9 @@ impl From<u8> for NtType {
7 => Self::Message,
8 => Self::SoloDelete,
9 => Self::ImageDelete,
10 => Self::PostLock,
11 => Self::PostHide,
12 => Self::CommentHide,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -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 <a href='/post/{}/{}?nid={}'>{}</a>",
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 <a href='/post/{}/{}?nid={}'>{}</a>",
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 <a href='/post/{}/{}?nid={}#{}'>{}</a> 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]);
Expand Down

0 comments on commit b1a2eee

Please sign in to comment.