Make titles optional on feeds and entries
This commit is contained in:
@@ -32,7 +32,7 @@ enum Commands {
|
||||
struct AddFeed {
|
||||
#[argh(option)]
|
||||
/// title of the feed (max 255 characters)
|
||||
title: String,
|
||||
title: Option<String>,
|
||||
#[argh(option)]
|
||||
/// URL of the feed (max 2048 characters)
|
||||
url: String,
|
||||
@@ -59,7 +59,7 @@ struct DeleteFeed {
|
||||
struct AddEntry {
|
||||
#[argh(option)]
|
||||
/// title of the entry (max 255 characters)
|
||||
title: String,
|
||||
title: Option<String>,
|
||||
#[argh(option)]
|
||||
/// URL of the entry (max 2048 characters)
|
||||
url: String,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use feed_rs::parser;
|
||||
use reqwest::Client;
|
||||
use sqlx::PgPool;
|
||||
use tracing::info;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use crate::models::feed::get_feeds;
|
||||
use crate::models::entry::{upsert_entries, CreateEntry};
|
||||
@@ -16,18 +16,17 @@ pub async fn crawl(pool: &PgPool) -> anyhow::Result<()> {
|
||||
let parsed_feed = parser::parse(&bytes[..])?;
|
||||
let mut payload = Vec::with_capacity(parsed_feed.entries.len());
|
||||
for entry in parsed_feed.entries {
|
||||
let entry = CreateEntry {
|
||||
title: entry
|
||||
.title
|
||||
.map_or_else(|| "No title".to_string(), |t| t.content),
|
||||
url: entry
|
||||
.links
|
||||
.get(0)
|
||||
.map_or_else(|| "https://example.com".to_string(), |l| l.href.clone()),
|
||||
description: entry.summary.map(|s| s.content),
|
||||
feed_id: feed.id,
|
||||
};
|
||||
payload.push(entry);
|
||||
if let Some(link) = entry.links.get(0) {
|
||||
let entry = CreateEntry {
|
||||
title: entry.title.map(|t| t.content),
|
||||
url: link.href.clone(),
|
||||
description: entry.summary.map(|s| s.content),
|
||||
feed_id: feed.id,
|
||||
};
|
||||
payload.push(entry);
|
||||
} else {
|
||||
warn!("Feed entry has no links: {:?}", entry);
|
||||
}
|
||||
}
|
||||
let entries = upsert_entries(pool, payload).await?;
|
||||
info!("Created {} entries for feed {}", entries.len(), feed.id);
|
||||
|
||||
@@ -8,7 +8,7 @@ use crate::error::{Error, Result};
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Entry {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub title: Option<String>,
|
||||
pub url: String,
|
||||
pub description: Option<String>,
|
||||
pub feed_id: i32,
|
||||
@@ -20,7 +20,7 @@ pub struct Entry {
|
||||
#[derive(Debug, Deserialize, Validate)]
|
||||
pub struct CreateEntry {
|
||||
#[validate(length(max = 255))]
|
||||
pub title: String,
|
||||
pub title: Option<String>,
|
||||
#[validate(url)]
|
||||
pub url: String,
|
||||
#[validate(length(max = 524288))]
|
||||
@@ -73,7 +73,7 @@ pub async fn create_entry(pool: &PgPool, payload: CreateEntry) -> Result<Entry>
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn create_entries(pool: &PgPool, payload: Vec<Create<Entry>) -> Result<Vec<Entry>> {
|
||||
pub async fn create_entries(pool: &PgPool, payload: Vec<CreateEntry>) -> Result<Vec<Entry>> {
|
||||
let mut titles = Vec::with_capacity(payload.len());
|
||||
let mut urls = Vec::with_capacity(payload.len());
|
||||
let mut descriptions: Vec<Option<String>> = Vec::with_capacity(payload.len());
|
||||
@@ -91,7 +91,7 @@ pub async fn create_entries(pool: &PgPool, payload: Vec<Create<Entry>) -> Result
|
||||
title, url, description, feed_id, created_at, updated_at
|
||||
) SELECT *, now(), now() FROM UNNEST($1::text[], $2::text[], $3::text[], $4::int[])
|
||||
RETURNING *",
|
||||
titles.as_slice(),
|
||||
titles.as_slice() as &[Option<String>],
|
||||
urls.as_slice(),
|
||||
descriptions.as_slice() as &[Option<String>],
|
||||
feed_ids.as_slice(),
|
||||
@@ -127,7 +127,7 @@ pub async fn upsert_entries(pool: &PgPool, payload: Vec<CreateEntry>) -> Result<
|
||||
) SELECT *, now(), now() FROM UNNEST($1::text[], $2::text[], $3::text[], $4::int[])
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING *",
|
||||
titles.as_slice(),
|
||||
titles.as_slice() as &[Option<String>],
|
||||
urls.as_slice(),
|
||||
descriptions.as_slice() as &[Option<String>],
|
||||
feed_ids.as_slice(),
|
||||
|
||||
@@ -29,7 +29,7 @@ impl FromStr for FeedType {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Feed {
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
pub title: Option<String>,
|
||||
pub url: String,
|
||||
#[serde(rename = "type")]
|
||||
pub feed_type: FeedType,
|
||||
@@ -42,7 +42,7 @@ pub struct Feed {
|
||||
#[derive(Debug, Deserialize, Validate)]
|
||||
pub struct CreateFeed {
|
||||
#[validate(length(max = 255))]
|
||||
pub title: String,
|
||||
pub title: Option<String>,
|
||||
#[validate(url)]
|
||||
pub url: String,
|
||||
#[serde(rename = "type")]
|
||||
|
||||
Reference in New Issue
Block a user