Skip to content

Commit

Permalink
Sort status posts by time (latest first)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Feb 17, 2025
1 parent 4772a8d commit fe74531
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ impl Status {
Self(Some(StatusInner { kind, source }))
}

pub fn sort(&mut self) {
if let Some(StatusInner {
kind: StatusKind::Posts(posts),
..
}) = &mut self.0
{
// Latest first
posts.0.sort_by(|l, r| r.time.cmp(&l.time));
}
}

pub fn generate_notifications<'a>(&'a self, last_status: &'a Status) -> Vec<Notification<'a>> {
self.0
.as_ref()
Expand Down Expand Up @@ -446,6 +457,8 @@ pub fn fetcher(platform: &platform::Config) -> Box<dyn FetcherTrait> {

#[cfg(test)]
mod tests {
use chrono::Days;

use super::*;

#[test]
Expand Down Expand Up @@ -710,4 +723,63 @@ mod tests {
},
));
}

#[test]
fn status_posts_sort() {
let mut status = Status::new(
StatusKind::Posts(Posts(vec![
Post {
user: None,
content: PostContent::plain("content2"),
urls: PostUrls::new(PostUrl::Identity("id2".into())),
time: DateTime::UNIX_EPOCH
.checked_add_days(Days::new(1))
.unwrap()
.into(),
is_pinned: true,
repost_from: None,
attachments: vec![],
},
Post {
user: None,
content: PostContent::plain("content3"),
urls: PostUrls::new(PostUrl::Identity("id3".into())),
time: DateTime::UNIX_EPOCH
.checked_add_days(Days::new(2))
.unwrap()
.into(),
is_pinned: false,
repost_from: None,
attachments: vec![],
},
Post {
user: None,
content: PostContent::plain("content1"),
urls: PostUrls::new(PostUrl::Identity("id1".into())),
time: DateTime::UNIX_EPOCH.into(),
is_pinned: false,
repost_from: None,
attachments: vec![],
},
])),
StatusSource {
platform: PlatformMetadata {
display_name: "test",
},
user: Some(StatusSourceUser {
display_name: "user2".into(),
profile_url: "profile1".into(),
}),
},
);

status.sort();

let StatusKind::Posts(posts) = status.0.unwrap().kind else {
panic!()
};
assert_eq!(posts.0[0].content.fallback(), "content3");
assert_eq!(posts.0[1].content.fallback(), "content2");
assert_eq!(posts.0[2].content.fallback(), "content1");
}
}
4 changes: 3 additions & 1 deletion src/task/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ impl TaskSubscription {
}

async fn run_once(&mut self) -> anyhow::Result<()> {
let status = self.fetcher.fetch_status().await.map_err(|err| {
let mut status = self.fetcher.fetch_status().await.map_err(|err| {
anyhow!(
"failed to fetch status for '{}' on '{}': {err}",
self.name,
self.fetcher
)
})?;

status.sort();

trace!(
"status of '{}' on '{}' now is '{status:?}'",
self.name,
Expand Down

0 comments on commit fe74531

Please sign in to comment.