webcomment/src/helpers.rs

141 lines
3.2 KiB
Rust

use crate::db::*;
use log::error;
pub fn new_pending_comment(comment: &Comment, dbs: &Dbs) -> Result<CommentId, sled::Error> {
let comment_id = CommentId::new();
dbs.comment.insert(&comment_id, comment)?;
dbs.comment_pending.insert(
&(
comment.topic_hash.clone(),
comment.post_time,
comment_id.clone(),
),
&(),
)?;
Ok(comment_id)
}
pub fn approve_comment(comment_id: CommentId, dbs: &Dbs) -> Result<(), sled::Error> {
if let Some(comment) = dbs.comment.get(&comment_id)? {
dbs.comment_pending.remove(&(
comment.topic_hash.clone(),
comment.post_time,
comment_id.clone(),
))?;
dbs.comment_approved
.insert(&(comment.topic_hash, comment.post_time, comment_id), &())?;
}
Ok(())
}
pub fn remove_pending_comment(
comment_id: CommentId,
dbs: &Dbs,
) -> Result<Option<Comment>, sled::Error> {
if let Some(comment) = dbs.comment.remove(&comment_id)? {
dbs.comment_pending
.remove(&(comment.topic_hash.clone(), comment.post_time, comment_id))?;
return Ok(Some(comment));
}
Ok(None)
}
pub fn iter_comments_by_topic<'a>(
topic_hash: TopicHash,
tree: &'a Tree<(TopicHash, Time, CommentId), ()>,
dbs: &'a Dbs,
) -> impl Iterator<Item = (CommentId, Comment)> + 'a {
tree.range(
(topic_hash.clone(), 0, CommentId::zero())..=(topic_hash, Time::MAX, CommentId::max()),
)
.filter_map(|entry| {
let ((_topic_hash, _time, comment_id), ()) = entry
.map_err(|e| error!("Reading comment_by_topic_and_time: {:?}", e))
.ok()?;
let comment = dbs
.comment
.get(&comment_id)
.map_err(|e| error!("Reading comment: {:?}", e))
.ok()?
.or_else(|| {
error!("Comment not found");
None
})?;
Some((comment_id, comment))
})
}
pub fn iter_approved_comments_by_topic(
topic_hash: TopicHash,
dbs: &Dbs,
) -> impl Iterator<Item = (CommentId, Comment)> + '_ {
iter_comments_by_topic(topic_hash, &dbs.comment_approved, dbs)
}
pub fn iter_pending_comments_by_topic(
topic_hash: TopicHash,
dbs: &Dbs,
) -> impl Iterator<Item = (CommentId, Comment)> + '_ {
iter_comments_by_topic(topic_hash, &dbs.comment_pending, dbs)
}
//pub enum DbHelperError {}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_comment() {
let comment = Comment {
topic_hash: TopicHash::from_topic("test"),
author: String::from("Emmanuel Goldstein"),
email: None,
last_edit_time: None,
post_time: 42,
text: String::from("Hello world!"),
};
let dbs = load_dbs(None);
let comment_id = new_pending_comment(&comment, &dbs).unwrap();
let mut iter = dbs.comment.iter();
assert_eq!(iter.next(), Some(Ok((comment_id.clone(), comment.clone()))));
assert_eq!(iter.next(), None);
let mut iter = dbs.comment_pending.iter();
assert_eq!(
iter.next(),
Some(Ok((
(
comment.topic_hash.clone(),
comment.post_time,
comment_id.clone()
),
()
)))
);
assert_eq!(iter.next(), None);
approve_comment(comment_id.clone(), &dbs).unwrap();
let mut iter = dbs.comment_pending.iter();
assert_eq!(iter.next(), None);
let mut iter = dbs.comment_approved.iter();
assert_eq!(
iter.next(),
Some(Ok((
(
comment.topic_hash.clone(),
comment.post_time,
comment_id.clone()
),
()
)))
);
assert_eq!(iter.next(), None);
}
}