implement a method for post type

This commit is contained in:
glyph 2022-08-23 14:13:27 +01:00
parent d7c654fcb4
commit 920141e0ce
2 changed files with 33 additions and 10 deletions

View File

@ -73,6 +73,29 @@ pub struct Post {
pub subject: Option<String>, pub subject: Option<String>,
} }
impl Post {
/// Create a new instance of the Post struct. A default value of `false` is
/// set for `read`.
pub fn new(
key: String,
text: String,
date: String,
sequence: u64,
timestamp: i64,
subject: Option<String>,
) -> Post {
Post {
key,
text,
date,
sequence,
timestamp,
subject,
read: false,
}
}
}
/// An instance of the key-value database and relevant trees. /// An instance of the key-value database and relevant trees.
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Clone)] #[derive(Clone)]

View File

@ -183,24 +183,24 @@ pub async fn get_root_posts(
let content = msg.value.content.to_owned(); let content = msg.value.content.to_owned();
if let Value::Object(content_map) = content { if let Value::Object(content_map) = content {
if !content_map.contains_key("root") { if !content_map.contains_key("root") {
latest_sequence = msg.value.sequence;
let text = content_map.get_key_value("text").unwrap().1.to_string();
let timestamp = msg.value.timestamp.round() as i64 / 1000; let timestamp = msg.value.timestamp.round() as i64 / 1000;
let datetime = NaiveDateTime::from_timestamp(timestamp, 0); let datetime = NaiveDateTime::from_timestamp(timestamp, 0);
let date = datetime.format("%d %b %Y").to_string(); let date = datetime.format("%d %b %Y").to_string();
let text = content_map.get_key_value("text").unwrap().1.to_string();
let subject = text.get(0..52).map(|s| s.to_string()); let subject = text.get(0..52).map(|s| s.to_string());
latest_sequence = msg.value.sequence; let post = Post::new(
msg.key.to_owned(),
posts.push(Post {
key: msg.key.to_owned(),
text, text,
timestamp,
date, date,
sequence: msg.value.sequence, msg.value.sequence,
read: false, timestamp,
subject, subject,
}) );
posts.push(post)
} }
} }
} }