diff options
author | cel 🌸 <cel@blos.sm> | 2023-06-20 23:04:56 +0100 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-06-20 23:04:56 +0100 |
commit | c24e154bb2b2299c68abbff8356e58008c875dbe (patch) | |
tree | 90c818a8e008898bbe8a2c988dc82ae731e220a9 /src | |
parent | 2059f378f33d19d7ded5a91947c1d4289f7b3be3 (diff) | |
download | blossom-c24e154bb2b2299c68abbff8356e58008c875dbe.tar.gz blossom-c24e154bb2b2299c68abbff8356e58008c875dbe.tar.bz2 blossom-c24e154bb2b2299c68abbff8356e58008c875dbe.zip |
improve skweet widget, namely time formatting
Diffstat (limited to '')
-rw-r--r-- | src/skweets.rs | 61 |
1 files changed, 58 insertions, 3 deletions
diff --git a/src/skweets.rs b/src/skweets.rs index 6434569..78994b1 100644 --- a/src/skweets.rs +++ b/src/skweets.rs @@ -1,10 +1,65 @@ -use crate::error::BlossomError; +use chrono::{offset::LocalResult, TimeZone, Utc}; +use chrono_humanize::{Accuracy, HumanTime, Tense}; use mastodon_async::entities::status::Status; use mastodon_async::prelude::*; +use serde::Serialize; +use time::OffsetDateTime; -pub async fn get_recents(client: &Mastodon) -> Result<Vec<Status>, BlossomError> { +use crate::Result; + +#[derive(Serialize)] +pub struct Skweet { + reblog: bool, + avatar: String, + username: String, + url: String, + time_since: String, + content: String, + media_attachments: Vec<Attachment>, +} + +pub async fn get_recents(client: &Mastodon) -> Result<Vec<Skweet>> { Ok(client .statuses(&AccountId::new("cel"), Default::default()) .await? - .initial_items) + .initial_items + .into_iter() + .map(|status| Skweet::from(status)) + .collect()) +} + +impl From<Status> for Skweet { + fn from(value: Status) -> Self { + let url = value.url.unwrap_or_default(); + fn time_since(time: OffsetDateTime) -> String { + let time = + if let LocalResult::Single(time) = Utc.timestamp_opt(time.unix_timestamp(), 0) { + time + } else { + return "oops".to_owned(); + }; + let duration = Utc::now() - time; + HumanTime::from(duration).to_text_en(Accuracy::Rough, Tense::Present) + } + match value.reblog { + Some(original) => Self { + reblog: true, + avatar: original.account.avatar, + username: original.account.acct, + url, + time_since: time_since(original.created_at), + content: value.content, + media_attachments: value.media_attachments, + }, + None => Self { + reblog: false, + avatar: value.account.avatar, + username: value.account.acct, + url, + time_since: time_since(value.created_at), + content: value.content, + media_attachments: value.media_attachments, + }, + } + } } |