RSS feed entries
This commit is contained in:
parent
18f51ed741
commit
c0898fb78c
21
src/poll.rs
21
src/poll.rs
@ -1,5 +1,7 @@
|
|||||||
use crate::user::AuthenticatedUser;
|
use crate::user::AuthenticatedUser;
|
||||||
use crate::{feed_utils::fetch_feed, Db};
|
use crate::{feed_utils::fetch_feed, Db};
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use feed_rs::model::Text;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::serde::uuid::Uuid;
|
use rocket::serde::uuid::Uuid;
|
||||||
use rocket::serde::{json::Json, Serialize};
|
use rocket::serde::{json::Json, Serialize};
|
||||||
@ -17,6 +19,10 @@ pub struct FeedPollResponse {
|
|||||||
struct Entry {
|
struct Entry {
|
||||||
id: String,
|
id: String,
|
||||||
title: String,
|
title: String,
|
||||||
|
published: Option<DateTime<Utc>>,
|
||||||
|
updated: Option<DateTime<Utc>>,
|
||||||
|
summary: String,
|
||||||
|
link: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/poll/<feed_id>")]
|
#[post("/poll/<feed_id>")]
|
||||||
@ -43,18 +49,23 @@ pub async fn poll_feed(
|
|||||||
let url = url::Url::parse(&feed_url).map_err(|_| Status::InternalServerError)?;
|
let url = url::Url::parse(&feed_url).map_err(|_| Status::InternalServerError)?;
|
||||||
|
|
||||||
let feed_data = fetch_feed(&url).await.map_err(|_| Status::BadGateway)?;
|
let feed_data = fetch_feed(&url).await.map_err(|_| Status::BadGateway)?;
|
||||||
|
|
||||||
let count = feed_data.entries.len();
|
let count = feed_data.entries.len();
|
||||||
|
|
||||||
|
fn get(item: Option<Text>, name: &'static str) -> String {
|
||||||
|
item.map(|t| t.content.to_string())
|
||||||
|
.unwrap_or(format!("<no {name}>"))
|
||||||
|
}
|
||||||
|
|
||||||
let entries = feed_data
|
let entries = feed_data
|
||||||
.entries
|
.entries
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|feed_entry| Entry {
|
.map(|feed_entry| Entry {
|
||||||
id: feed_entry.id,
|
id: feed_entry.id,
|
||||||
title: feed_entry
|
title: get(feed_entry.title, "title"),
|
||||||
.title
|
summary: get(feed_entry.summary, "summary"),
|
||||||
.map(|t| t.content)
|
published: feed_entry.published,
|
||||||
.unwrap_or("<no title>".to_string()),
|
updated: feed_entry.updated,
|
||||||
|
link: feed_entry.links.first().map(|l| l.href.clone()),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user