diff --git a/src/event_related.rs b/src/event_related.rs new file mode 100644 index 00000000..b36b4905 --- /dev/null +++ b/src/event_related.rs @@ -0,0 +1,86 @@ +use crate::db::DbEvent; +use crate::error::Error; +use nostr_proto::{Event, EventKind, Id}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct Reactions { + pub upvotes: u64, + pub downvotes: u64, + pub emojis: Vec<(char, u64)>, +} + +/// This contains event-related data that is relevant at the time of +/// rendering the event, most of which is gathered from other related +/// events. +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct EventRelated { + pub id: Id, + pub feed_related: bool, + pub replies: Vec, + pub in_reply_to: Option, + pub reactions: Reactions, + pub deleted_reason: Option, + pub client: Option, + pub hashtags: Vec, + pub subject: Option, + pub urls: Vec, + pub last_reply_at: Option, +} + +impl EventRelated { + #[allow(dead_code)] + pub fn new(id: Id) -> EventRelated { + EventRelated { + id, + feed_related: false, + replies: Vec::new(), + in_reply_to: None, + reactions: Default::default(), + deleted_reason: None, + client: None, + hashtags: Vec::new(), + subject: None, + urls: Vec::new(), + last_reply_at: None, + } + } +} + +impl From<&Event> for EventRelated { + fn from(event: &Event) -> EventRelated { + EventRelated { + id: event.id, + feed_related: event.kind == EventKind::TextNote, + replies: Vec::new(), + in_reply_to: None, + reactions: Default::default(), + deleted_reason: None, + client: None, + hashtags: Vec::new(), + subject: None, + urls: Vec::new(), + last_reply_at: Some(event.created_at.0), + } + } +} + +impl TryFrom<&DbEvent> for EventRelated { + type Error = Error; + + fn try_from(dbevent: &DbEvent) -> Result { + Ok(EventRelated { + id: dbevent.id.clone().try_into()?, + feed_related: dbevent.kind == 1, + replies: Vec::new(), + in_reply_to: None, + reactions: Default::default(), + deleted_reason: None, + client: None, + hashtags: Vec::new(), + subject: None, + urls: Vec::new(), + last_reply_at: Some(dbevent.created_at), + }) + } +} diff --git a/src/main.rs b/src/main.rs index 1b58aad7..0f12b4a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate lazy_static; mod comms; mod db; mod error; +mod event_related; mod globals; mod settings; mod ui;