Koucha/koucha/src/db/item.rs

145 lines
3.7 KiB
Rust
Raw Normal View History

use crate::{
Result,
AdapterPool,
db::{
2026-02-04 15:49:55 -08:00
ChannelKey,
ItemKey,
2026-01-22 15:55:31 -08:00
},
fetch::FetchedRSSItem,
};
use chrono::{DateTime, Utc};
pub struct UnparsedItem {
pub id: i64,
pub channel_id: i64,
2026-01-22 15:55:31 -08:00
pub fetched_at: Option<String>,
pub title: Option<String>,
pub description: Option<String>,
pub content: Option<String>,
}
impl UnparsedItem {
pub fn parse(self) -> Result<Item> {
Ok(Item {
2026-02-04 15:49:55 -08:00
id: ItemKey(self.id),
channel_id: ChannelKey(self.channel_id),
2026-01-23 16:35:43 -08:00
fetched_at: match self.fetched_at {
Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)?
.with_timezone(&Utc)),
None => None,
},
title: self.title,
description: self.description,
content: self.content,
})
}
}
pub struct Item {
2026-02-04 15:49:55 -08:00
id: ItemKey,
channel_id: ChannelKey,
2026-01-23 16:35:43 -08:00
#[allow(dead_code)] // TODO: Use for score decay calculations later
2026-01-22 15:55:31 -08:00
fetched_at: Option<DateTime<Utc>>,
title: Option<String>,
description: Option<String>,
content: Option<String>,
}
impl Item {
2026-02-04 15:49:55 -08:00
pub fn id(&self) -> ItemKey { self.id }
pub fn channel(&self) -> ChannelKey { self.channel_id }
2026-01-22 15:55:31 -08:00
pub fn title(&self) -> Option<&str> { self.title.as_deref() }
pub fn description(&self) -> Option<&str> { self.description.as_deref() }
pub fn content(&self) -> Option<&str> { self.content.as_deref() }
pub async fn get_or_create(
2026-02-04 15:49:55 -08:00
pool: &AdapterPool, from_channel: ChannelKey, guid: &str
) -> Result<Self> {
let item = sqlx::query_as!(
UnparsedItem,
2026-01-22 15:55:31 -08:00
"INSERT INTO items (channel_id, guid)
2026-01-23 16:35:43 -08:00
VALUES (?, ?)
ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id
2026-01-22 15:55:31 -08:00
RETURNING id as `id!`, channel_id, fetched_at, title, description,
content",
2026-02-04 13:59:54 -08:00
from_channel.0, guid
).fetch_one(&pool.0).await?.parse();
item
}
2026-01-22 15:55:31 -08:00
pub async fn update_content(
&self, pool: &AdapterPool, fetched: &FetchedRSSItem, fetched_at: &DateTime<Utc>
) -> Result<()> {
let title = fetched.title();
let description = fetched.description();
let content = fetched.content();
let string_fetched_at = fetched_at.to_rfc2822();
sqlx::query!(
"UPDATE items
SET title = ?, description = ?, content = ?,
fetched_at = ?
WHERE id = ?",
title, description, content, string_fetched_at,
self.id.0
).execute(&pool.0).await?;
Ok(())
}
}
2026-01-23 16:35:43 -08:00
#[cfg(test)]
mod tests {
use super::*;
2026-01-26 15:00:52 -08:00
use crate::test_utils::{
ITEM_GUID, ITEM_TITLE, ITEM_DESC, ITEM_CONT,
setup_adapter,
setup_channel,
2026-01-23 16:35:43 -08:00
};
2026-01-26 15:00:52 -08:00
use chrono::TimeZone;
// UnparsedItem tests
#[test]
fn parse_unparsed_item() {
const ITEM_ID: i64 = 1;
const CHANNEL_ID: i64 = 1;
let date: DateTime<Utc> = Utc.with_ymd_and_hms(2020,1,1,0,0,0).unwrap();
let raw_item = UnparsedItem {
id: ITEM_ID,
channel_id: CHANNEL_ID,
fetched_at: Some(date.to_rfc2822()),
title: Some(ITEM_TITLE.to_string()),
description: Some(ITEM_DESC.to_string()),
content: Some(ITEM_CONT.to_string()),
};
let item = raw_item.parse().unwrap();
assert_eq!(item.id.0, ITEM_ID);
assert_eq!(item.channel_id.0, CHANNEL_ID);
assert_eq!(item.fetched_at, Some(date));
assert_eq!(item.title, Some(ITEM_TITLE.to_string()));
assert_eq!(item.description, Some(ITEM_DESC.to_string()));
assert_eq!(item.content, Some(ITEM_CONT.to_string()));
2026-01-23 16:35:43 -08:00
}
// Item Tests
#[tokio::test]
2026-01-26 15:00:52 -08:00
async fn get_or_create_duplicate() {
2026-01-23 16:35:43 -08:00
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
let channel = setup_channel(pool).await;
let item1 = Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap();
let item2 = Item::get_or_create(pool, channel.id(), ITEM_GUID).await.unwrap();
assert_eq!(item1.id(), item2.id());
}
}