fix sync issue, frontend improvement
This commit is contained in:
+87
-11
@@ -64,6 +64,27 @@ fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) {
|
||||
let item_title = item.title.clone().unwrap();
|
||||
log::info!("Create feed item: {}", item_title);
|
||||
|
||||
// Resolve the publication date before any HTML parsing or DB work so we can
|
||||
// bail out early for old articles. Items without a pub_date are treated as
|
||||
// current (inserted unconditionally) — feeds that don't publish dates are
|
||||
// typically small/curated enough that this is fine.
|
||||
let mut time: NaiveDateTime = Local::now().naive_local();
|
||||
if let Some(pub_date) = item.pub_date() {
|
||||
time = match get_date(pub_date) {
|
||||
Ok(date) => date,
|
||||
Err(err) => {
|
||||
log::error!("could not parse pub date: {}", err);
|
||||
time
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let cutoff = Local::now().naive_local() - Duration::days(14);
|
||||
if time < cutoff {
|
||||
log::info!("Skipping item {} (older than 2 weeks).", item_title);
|
||||
return;
|
||||
}
|
||||
|
||||
let base_content: &str = item.content().or(item.description()).unwrap_or_default();
|
||||
|
||||
let frag = Html::parse_fragment(base_content);
|
||||
@@ -96,17 +117,6 @@ fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) {
|
||||
.unwrap();
|
||||
|
||||
if existing_item.is_empty() {
|
||||
log::info!("{:?}", item.pub_date());
|
||||
let mut time: NaiveDateTime = Local::now().naive_local();
|
||||
if item.pub_date().is_some() {
|
||||
time = match get_date(item.pub_date().unwrap()) {
|
||||
Ok(date) => date,
|
||||
Err(err) => {
|
||||
log::error!("could not unwrap pub date: {}", err);
|
||||
time
|
||||
}
|
||||
};
|
||||
}
|
||||
let new_feed_item = NewFeedItem::new(
|
||||
feed.id,
|
||||
content.clone(),
|
||||
@@ -331,6 +341,72 @@ mod tests {
|
||||
.ok();
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn create_feed_item_skips_articles_older_than_two_weeks() {
|
||||
let mut connection = establish_connection();
|
||||
let suffix = unique_suffix();
|
||||
|
||||
let new_user = NewUser::new(
|
||||
format!("age_skip_test_{suffix}"),
|
||||
format!("age_skip_{suffix}@example.test"),
|
||||
"secret".to_string(),
|
||||
);
|
||||
let user: User = diesel::insert_into(users::table)
|
||||
.values(&new_user)
|
||||
.get_result(&mut connection)
|
||||
.unwrap();
|
||||
|
||||
let new_feed = NewFeed::new(
|
||||
format!("Age skip test feed {suffix}"),
|
||||
format!("https://example.test/feed/{suffix}"),
|
||||
user.id,
|
||||
);
|
||||
let feed: Feed = diesel::insert_into(feed::table)
|
||||
.values(&new_feed)
|
||||
.get_result(&mut connection)
|
||||
.unwrap();
|
||||
|
||||
// Item with a pub_date 20 days ago — should be ignored by create_feed_item.
|
||||
let old_date = (Local::now() - Duration::days(20))
|
||||
.format("%a, %d %b %Y %H:%M:%S %z")
|
||||
.to_string();
|
||||
let mut old_item = Item::default();
|
||||
old_item.set_title(Some(format!("Old article {suffix}")));
|
||||
old_item.set_link(Some(format!("https://example.test/old/{suffix}")));
|
||||
old_item.set_pub_date(Some(old_date));
|
||||
old_item.set_content(Some("<p>old</p>".to_string()));
|
||||
|
||||
// Item without a pub_date — treated as current, should be inserted.
|
||||
let mut fresh_item = Item::default();
|
||||
fresh_item.set_title(Some(format!("Fresh article {suffix}")));
|
||||
fresh_item.set_link(Some(format!("https://example.test/fresh/{suffix}")));
|
||||
fresh_item.set_content(Some("<p>fresh</p>".to_string()));
|
||||
|
||||
create_feed_item(old_item, &feed, &mut connection);
|
||||
create_feed_item(fresh_item, &feed, &mut connection);
|
||||
|
||||
let items: Vec<FeedItem> = feed_item::table
|
||||
.filter(feed_id.eq(feed.id))
|
||||
.load(&mut connection)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(1, items.len(), "old item should have been skipped");
|
||||
assert!(
|
||||
items[0].title.contains("Fresh article"),
|
||||
"only the fresh item should be present"
|
||||
);
|
||||
|
||||
diesel::delete(feed_item::table.filter(feed_id.eq(feed.id)))
|
||||
.execute(&mut connection)
|
||||
.ok();
|
||||
diesel::delete(feed::table.filter(feed::id.eq(feed.id)))
|
||||
.execute(&mut connection)
|
||||
.ok();
|
||||
diesel::delete(users::table.filter(users::id.eq(user.id)))
|
||||
.execute(&mut connection)
|
||||
.ok();
|
||||
}
|
||||
|
||||
#[actix_web::test]
|
||||
async fn create_feed_item_does_not_duplicate_existing_items() {
|
||||
let mut connection = establish_connection();
|
||||
|
||||
Reference in New Issue
Block a user