clean up 'update_items'

This commit is contained in:
Julia Lange 2026-01-22 15:55:31 -08:00
parent 3748606e21
commit 0bb9a81d60
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
4 changed files with 104 additions and 34 deletions

View file

@ -6,6 +6,7 @@ use crate::{
db::{
ChannelId,
Item,
FeedId,
item::UnparsedItem,
},
fetch::FetchedRSSChannel,
@ -33,6 +34,40 @@ impl UnparsedChannel {
}
}
pub struct UnparsedFeedChannel {
pub channel_id: i64,
pub feed_id: i64,
}
impl UnparsedFeedChannel {
pub fn parse(self) -> Result<FeedChannel> {
Ok(FeedChannel {
channel_id: ChannelId(self.channel_id),
feed_id: FeedId(self.feed_id)
})
}
}
pub struct FeedChannel {
channel_id: ChannelId,
feed_id: FeedId,
}
impl FeedChannel {
pub async fn add_item(
&self, pool: &AdapterPool, item: &Item
) -> Result<()> {
let int_item_id = i64::from(item.id());
let int_feed_id = i64::from(self.feed_id);
sqlx::query!(
"INSERT OR IGNORE INTO feed_items (feed_id, item_id, score)
VALUES (?, ?, 5)", // TODO: Add in scoring featuress
int_feed_id, int_item_id
).execute(&pool.0).await?;
Ok(())
}
}
pub struct Channel {
id: ChannelId,
title: String,
@ -108,24 +143,34 @@ impl Channel {
Ok(())
}
async fn get_feed_channels(
&self, pool: &AdapterPool
) -> Result<Vec<FeedChannel>> {
let feeds: Result<Vec<FeedChannel>> = sqlx::query_as!(
UnparsedFeedChannel,
"SELECT channel_id, feed_id
FROM feed_channels
WHERE channel_id = ?",
self.id.0
).fetch_all(&pool.0).await?.into_iter()
.map(UnparsedFeedChannel::parse).collect();
feeds
}
pub async fn update_items(
&self, pool: &AdapterPool, fetched: FetchedRSSChannel
) -> Result<()> {
let fetched_at = fetched.fetched_at().to_rfc2822();
let fetched_at = fetched.fetched_at();
for item in fetched.items() {
let guid = item.guid();
let title = item.title();
let description = item.description();
let content = item.content();
sqlx::query!(
"INSERT OR IGNORE INTO items
(channel_id, guid, fetched_at, title, description, content)
VALUES (?, ?, ?, ?, ?, ?)",
self.id.0, guid, fetched_at, title, description, content
)
.execute(&pool.0)
.await?;
let feed_channels = self.get_feed_channels(pool).await?;
for rss_item in fetched.items() {
let new_item = Item::get_or_create(pool, self.id, rss_item.guid()).await?;
new_item.update_content(pool, rss_item, &fetched_at).await?;
for feed_channel in &feed_channels {
feed_channel.add_item(pool, &new_item).await?;
}
}
Ok(())
@ -134,9 +179,9 @@ impl Channel {
pub async fn get_items(&self, pool: &AdapterPool) -> Result<Vec<Item>> {
let items: Result<Vec<Item>> = sqlx::query_as!(
UnparsedItem,
"SELECT id as `id!`, channel_id, guid, fetched_at, title, description,
"SELECT id as `id!`, channel_id, fetched_at, title, description,
content
FROM items
FROM items
WHERE channel_id = ?",
self.id.0
).fetch_all(&pool.0).await?.into_iter().map(UnparsedItem::parse).collect();