From c24e154bb2b2299c68abbff8356e58008c875dbe Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 20 Jun 2023 23:04:56 +0100 Subject: improve skweet widget, namely time formatting --- src/skweets.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'src') 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, 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, +} + +pub async fn get_recents(client: &Mastodon) -> Result> { Ok(client .statuses(&AccountId::new("cel"), Default::default()) .await? - .initial_items) + .initial_items + .into_iter() + .map(|status| Skweet::from(status)) + .collect()) +} + +impl From 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, + }, + } + } } -- cgit