From 93d22bfe3f1df3ebb95ba8a523cfd4d66ecdfa8c Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 10 Mar 2026 18:38:20 -0700 Subject: [PATCH] not working truncation --- src/lib.rs | 55 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9bbd05f..ca0db75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, time::Duration}; +use std::{collections::HashMap, fmt::Display, time::Duration}; use feed_rs::parser::parse; use html2md::{TagHandler, TagHandlerFactory, dummy::DummyHandler}; @@ -44,7 +44,7 @@ pub struct BlogdorTheAggregator { blogdor_token: String, // sent *from zulip* in POSTs *to us* } -#[derive(Debug, Default, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct FeedEntry { post_url: String, feed_url: String, @@ -54,7 +54,7 @@ pub struct FeedEntry { published: DateTime, received: DateTime, feed_description: Option, - body: Option, + body: PostBody, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -79,6 +79,26 @@ pub enum Action { List, } +#[derive(Debug, Clone, PartialEq, Eq)] +enum PostBody { + Full(String), + Truncated(String), + Empty, +} + +impl Display for PostBody { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let s = match self { + PostBody::Empty => "".to_string(), + PostBody::Full(s) => s.to_owned(), + PostBody::Truncated(s) => { + format!("{s}[...]") + } + }; + write!(f, "{s}") + } +} + #[derive(Debug, Clone)] pub struct UserRequest { command: FeedCommand, @@ -571,6 +591,24 @@ async fn check_feed( feed.entries.sort_by_key(|e| std::cmp::Reverse(e.posted())); for post in feed.entries.into_iter().take(5) { + let body = post + .content + .and_then(|c| match c.body { + None => PostBody::Empty, + Some(c) => { + let s = html2md::parse_html_custom(&c, &handlers) + .graphemes(false) + .take(ZULIP_MESSAGE_CUTOFF) + .collect::(); + if s.len() == c.len() { + PostBody::Full(s) + } else { + PostBody::Truncated(s) + } + } + }) + .unwrap_or(PostBody::Empty); + if post.posted().unwrap_or(LAST_FETCHED) > last_fetched { let entry = FeedEntry { post_url: post @@ -590,16 +628,9 @@ async fn check_feed( published: post.posted().unwrap_or(now), received: now, feed_description: feed.description.to_owned().map(|d| d.content), - body: post.content.and_then(|c| { - c.body.map(|f| { - let s = html2md::parse_html_custom(&f, &handlers) - .graphemes(false) - .take(ZULIP_MESSAGE_CUTOFF) - .collect::(); - s.to_string() - }) - }), + body, }; + entries.push(entry); } }