From c24e154bb2b2299c68abbff8356e58008c875dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?cel=20=F0=9F=8C=B8?= Date: Tue, 20 Jun 2023 23:04:56 +0100 Subject: [PATCH] improve skweet widget, namely time formatting --- Cargo.lock | 33 +++++++++++------ Cargo.toml | 2 + src/skweets.rs | 61 +++++++++++++++++++++++++++++-- static/style.css | 25 +++++++------ templates/base.html.tera | 2 +- templates/latestskweets.html.tera | 28 +++++++++++--- 6 files changed, 119 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67ba3e7..12f9291 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -260,6 +260,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "chrono-humanize" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32dce1ea1988dbdf9f9815ff11425828523bd2a134ec0805d2ac8af26ee6096e" +dependencies = [ + "chrono", +] + [[package]] name = "chrono-tz" version = "0.6.3" @@ -316,7 +325,7 @@ dependencies = [ "rand", "sha2", "subtle", - "time 0.3.19", + "time 0.3.22", "version_check", ] @@ -1194,7 +1203,7 @@ dependencies = [ "static_assertions", "tap-reader", "thiserror", - "time 0.3.19", + "time 0.3.22", "tokio", "tokio-util", "url", @@ -1212,7 +1221,7 @@ dependencies = [ "serde", "static_assertions", "thiserror", - "time 0.3.19", + "time 0.3.22", ] [[package]] @@ -1904,7 +1913,7 @@ dependencies = [ "serde_json", "state", "tempfile", - "time 0.3.19", + "time 0.3.22", "tokio", "tokio-stream", "tokio-util", @@ -1964,7 +1973,7 @@ dependencies = [ "smallvec", "stable-pattern", "state", - "time 0.3.19", + "time 0.3.22", "tokio", "uncased", ] @@ -2152,6 +2161,7 @@ name = "site" version = "0.1.0" dependencies = [ "chrono", + "chrono-humanize", "listenbrainz", "mastodon-async", "reqwest", @@ -2159,6 +2169,7 @@ dependencies = [ "rocket_dyn_templates", "serde", "serde_json", + "time 0.3.22", "tokio", ] @@ -2361,9 +2372,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.19" +version = "0.3.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53250a3b3fed8ff8fd988587d8925d26a83ac3845d9e03b220b37f34c2b8d6c2" +checksum = "ea9e1b3cf1243ae005d9e74085d4d542f3125458f3a81af210d901dcd7411efd" dependencies = [ "itoa", "serde", @@ -2373,15 +2384,15 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" +checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" [[package]] name = "time-macros" -version = "0.2.7" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a460aeb8de6dcb0f381e1ee05f1cd56fcf5a5f6eb8187ff3d8f0b11078d38b7c" +checksum = "372950940a5f07bf38dbe211d7283c9e6d7327df53794992d293e534c733d09b" dependencies = [ "time-core", ] diff --git a/Cargo.toml b/Cargo.toml index 57ea2d4..ba29610 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,5 @@ rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] } listenbrainz = "0.7" chrono = "0.4.23" serde_json = "1.0.97" +time = "0.3.22" +chrono-humanize = "0.2.2" 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, + }, + } + } } diff --git a/static/style.css b/static/style.css index 7098687..a912b53 100644 --- a/static/style.css +++ b/static/style.css @@ -231,6 +231,9 @@ iframe { border-color: #bf42a0; background-color: #28446c; z-index: -1; + display: flex; + flex-direction: column; + gap: 1em; } #skinnyverse h5 { @@ -247,10 +250,15 @@ iframe { color: #bf42a0; } +.boosted-skweet>h4 { + margin: 0.25em 0; + font-family: 'Cherry Bomb'; +} + .skweet { display: flex; gap: 2em; - margin: 2em 0; + margin: 0; } .skweet a { @@ -284,6 +292,7 @@ iframe { .skweet-info { display: flex; justify-content: space-between; + gap: 1em; } .skweet-text { @@ -291,17 +300,9 @@ iframe { margin-bottom: 0.8em; } -.skweet-gallery { - display: flex; - max-width: 100%; - gap: 1em; -} - -.skweet-gallery img, -.skweet-gallery video { - border: 4px solid #bf42a0; - flex: 1 1 auto; - width: 10px; +.skweet img, +.skweet video { + max-height: 80vh; } /* contact page */ diff --git a/templates/base.html.tera b/templates/base.html.tera index b48109c..081e0a1 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -40,7 +40,7 @@
  • girlblog
  • projetos
  • -
  • écoute
  • +
  • écoute
  • video
  • 🔴ライブ
  • pix
  • diff --git a/templates/latestskweets.html.tera b/templates/latestskweets.html.tera index d698eea..84798de 100644 --- a/templates/latestskweets.html.tera +++ b/templates/latestskweets.html.tera @@ -3,21 +3,39 @@ {% for skweet in skweets %} {% if not skweet.reblog %}
    - +
    - +

    {{ skweet.username }}

    {{ skweet.time_since }} ago
    {{ skweet.content }}
    {% if skweet.media_attachments[0] %} - +
    + {% elif skweet.reblog %} +
    +

    boosted:

    +
    + +
    +

    {{ skweet.username }}

    {{ skweet.time_since }} ago
    +
    {{ skweet.content }}
    + {% if skweet.media_attachments[0] %} {% for media_attachment in skweet.media_attachments %} {% if media_attachment.type == "image" %} - + {% elif media_attachment.type == "video" %} {% endif %} {% endfor %} + {% endif %}
    - {% endif %}
    {% endif %}