From 920141e0ce04c8055a979f8ca7fb69fd3fb9e343 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 23 Aug 2022 14:13:27 +0100 Subject: [PATCH] implement a method for post type --- src/db.rs | 23 +++++++++++++++++++++++ src/sbot.rs | 20 ++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/db.rs b/src/db.rs index de98e54..68ede5e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -73,6 +73,29 @@ pub struct Post { pub subject: Option, } +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, + ) -> Post { + Post { + key, + text, + date, + sequence, + timestamp, + subject, + read: false, + } + } +} + /// An instance of the key-value database and relevant trees. #[allow(dead_code)] #[derive(Clone)] diff --git a/src/sbot.rs b/src/sbot.rs index 214a361..c5b7134 100644 --- a/src/sbot.rs +++ b/src/sbot.rs @@ -183,24 +183,24 @@ pub async fn get_root_posts( let content = msg.value.content.to_owned(); if let Value::Object(content_map) = content { 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 datetime = NaiveDateTime::from_timestamp(timestamp, 0); 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()); - latest_sequence = msg.value.sequence; - - posts.push(Post { - key: msg.key.to_owned(), + let post = Post::new( + msg.key.to_owned(), text, - timestamp, date, - sequence: msg.value.sequence, - read: false, + msg.value.sequence, + timestamp, subject, - }) + ); + + posts.push(post) } } }