Convert to keys and setup feed_item

This commit is contained in:
Julia Lange 2026-02-04 15:49:55 -08:00
parent c3d9dff83f
commit f0a4e12f2b
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
8 changed files with 111 additions and 61 deletions

View file

@ -11,14 +11,23 @@ pub use channel::Channel;
mod item; mod item;
pub use item::Item; pub use item::Item;
macro_rules! define_id { macro_rules! define_key {
($name:ident) => { ($name:ident) => {
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(PartialEq, Debug, Copy, Clone)]
pub struct $name(i64); pub struct $name(i64);
}; };
($name:ident, $($field:ident),* $(,)?) => {
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct $name {
$($field: i64),*
}
};
} }
define_id!(UserId); define_key!(UserKey);
define_id!(FeedId); define_key!(FeedKey);
define_id!(ChannelId); define_key!(FeedChannelKey, feed_id, channel_id);
define_id!(ItemId); define_key!(FeedItemKey, feed_id, item_id);
define_key!(ChannelKey);
define_key!(ItemKey);

View file

@ -4,7 +4,7 @@ use crate::{
Result, Result,
AdapterPool, AdapterPool,
db::{ db::{
ChannelId, ChannelKey,
Item, Item,
FeedChannel, FeedChannel,
feed_channel::UnparsedFeedChannel, feed_channel::UnparsedFeedChannel,
@ -23,7 +23,7 @@ pub struct UnparsedChannel {
impl UnparsedChannel { impl UnparsedChannel {
pub fn parse(self) -> Result<Channel> { pub fn parse(self) -> Result<Channel> {
Ok(Channel { Ok(Channel {
id: ChannelId(self.id), id: ChannelKey(self.id),
title: self.title, title: self.title,
link: Url::parse(&self.link)?, link: Url::parse(&self.link)?,
description: self.description, description: self.description,
@ -36,7 +36,7 @@ impl UnparsedChannel {
} }
pub struct Channel { pub struct Channel {
id: ChannelId, id: ChannelKey,
title: String, title: String,
link: Url, link: Url,
description: Option<String>, description: Option<String>,
@ -44,7 +44,7 @@ pub struct Channel {
} }
impl Channel { impl Channel {
pub fn id(&self) -> ChannelId { self.id } pub fn id(&self) -> ChannelKey { self.id }
pub fn title(&self) -> &str { &self.title } pub fn title(&self) -> &str { &self.title }
pub fn link(&self) -> &Url { &self.link } pub fn link(&self) -> &Url { &self.link }
pub fn description(&self) -> Option<&str> { self.description.as_deref() } pub fn description(&self) -> Option<&str> { self.description.as_deref() }
@ -59,7 +59,7 @@ impl Channel {
channels channels
} }
pub async fn get(pool: &AdapterPool, id: ChannelId) -> Result<Self> { pub async fn get(pool: &AdapterPool, id: ChannelKey) -> Result<Self> {
let channel: Result<Self> = sqlx::query_as!( let channel: Result<Self> = sqlx::query_as!(
UnparsedChannel, UnparsedChannel,
"SELECT id, title, link, description, last_fetched "SELECT id, title, link, description, last_fetched

View file

@ -3,10 +3,10 @@ use crate::{
Result, Result,
db::{ db::{
Channel, Channel,
ChannelId, ChannelKey,
FeedId, FeedKey,
Item, Item,
UserId, UserKey,
channel::UnparsedChannel, channel::UnparsedChannel,
item::UnparsedItem, item::UnparsedItem,
}, },
@ -19,23 +19,23 @@ pub struct UnparsedFeed {
impl UnparsedFeed { impl UnparsedFeed {
pub fn parse(self) -> Result<Feed> { pub fn parse(self) -> Result<Feed> {
Ok(Feed { Ok(Feed {
id: FeedId(self.id), id: FeedKey(self.id),
title: self.title, title: self.title,
}) })
} }
} }
pub struct Feed { pub struct Feed {
id: FeedId, id: FeedKey,
title: String, title: String,
} }
impl Feed { impl Feed {
pub fn id(&self) -> FeedId { self.id } pub fn id(&self) -> FeedKey { self.id }
pub fn title(&self) -> &str { &self.title } pub fn title(&self) -> &str { &self.title }
pub async fn get( pub async fn get(
pool: &AdapterPool, id: FeedId pool: &AdapterPool, id: FeedKey
) -> Result<Self> { ) -> Result<Self> {
let feed = sqlx::query_as!( let feed = sqlx::query_as!(
UnparsedFeed, UnparsedFeed,
@ -47,7 +47,7 @@ impl Feed {
} }
pub async fn create( pub async fn create(
pool: &AdapterPool, user_id: UserId, title: &str pool: &AdapterPool, user_id: UserKey, title: &str
) -> Result<Self> { ) -> Result<Self> {
let new_feed = sqlx::query_as!( let new_feed = sqlx::query_as!(
UnparsedFeed, UnparsedFeed,
@ -61,7 +61,7 @@ impl Feed {
} }
pub async fn update_title( pub async fn update_title(
pool: &AdapterPool, id: FeedId, new_title: &str pool: &AdapterPool, id: FeedKey, new_title: &str
) -> Result<()> { ) -> Result<()> {
sqlx::query!( sqlx::query!(
"UPDATE feeds SET title = ? WHERE id = ?", "UPDATE feeds SET title = ? WHERE id = ?",
@ -72,7 +72,7 @@ impl Feed {
} }
pub async fn add_channel( pub async fn add_channel(
&self, pool: &AdapterPool, channel_id: ChannelId &self, pool: &AdapterPool, channel_id: ChannelKey
) -> Result<()> { ) -> Result<()> {
sqlx::query!( sqlx::query!(
"INSERT INTO feed_channels (feed_id, channel_id) "INSERT INTO feed_channels (feed_id, channel_id)

View file

@ -3,9 +3,9 @@ use crate::{
AdapterPool, AdapterPool,
db::{ db::{
Channel, Channel,
ChannelId, ChannelKey,
Feed, Feed,
FeedId, FeedKey,
Item, Item,
}, },
score::{ score::{
@ -26,8 +26,8 @@ pub struct UnparsedFeedChannel {
impl UnparsedFeedChannel { impl UnparsedFeedChannel {
pub fn parse(self) -> Result<FeedChannel> { pub fn parse(self) -> Result<FeedChannel> {
Ok(FeedChannel { Ok(FeedChannel {
channel_id: ChannelId(self.channel_id), channel_id: ChannelKey(self.channel_id),
feed_id: FeedId(self.feed_id), feed_id: FeedKey(self.feed_id),
initial_score: Score::new(self.initial_score), initial_score: Score::new(self.initial_score),
gravity: Gravity::new(self.gravity), gravity: Gravity::new(self.gravity),
boost: Boost::new(self.boost), boost: Boost::new(self.boost),
@ -36,8 +36,8 @@ impl UnparsedFeedChannel {
} }
pub struct FeedChannel { pub struct FeedChannel {
channel_id: ChannelId, channel_id: ChannelKey,
feed_id: FeedId, feed_id: FeedKey,
initial_score: Score, initial_score: Score,
gravity: Gravity, gravity: Gravity,
boost: Boost, boost: Boost,
@ -80,7 +80,7 @@ mod tests {
use crate::{ use crate::{
db::{ db::{
Channel, Channel,
FeedId, FeedKey,
User User
}, },
test_utils::{ test_utils::{
@ -123,7 +123,7 @@ mod tests {
let fc = FeedChannel { let fc = FeedChannel {
channel_id: channel.id(), channel_id: channel.id(),
feed_id: FeedId(1), // Fake Feed feed_id: FeedKey(1), // Fake Feed
initial_score: Score::new(None), initial_score: Score::new(None),
gravity: Gravity::new(None), gravity: Gravity::new(None),
boost: Boost::new(None), boost: Boost::new(None),
@ -142,7 +142,7 @@ mod tests {
let feed = Feed::create(pool, user.id(), "My Feed").await.unwrap(); let feed = Feed::create(pool, user.id(), "My Feed").await.unwrap();
let fc = FeedChannel { let fc = FeedChannel {
channel_id: ChannelId(1), // Fake Channel channel_id: ChannelKey(1), // Fake Channel
feed_id: feed.id(), feed_id: feed.id(),
initial_score: Score::new(None), initial_score: Score::new(None),
gravity: Gravity::new(None), gravity: Gravity::new(None),

View file

@ -3,12 +3,11 @@ use crate::{
Result, Result,
AdapterPool, AdapterPool,
db::{ db::{
ItemId, FeedItemKey,
FeedId,
}, },
score::{ score::{
TimedScore, TimedScore,
UnparsedTimedScore, UnparsedTimedScore
}, },
}; };
@ -22,8 +21,10 @@ pub struct UnparsedFeedItem {
impl UnparsedFeedItem { impl UnparsedFeedItem {
pub fn parse(self) -> Result<FeedItem> { pub fn parse(self) -> Result<FeedItem> {
Ok(FeedItem { Ok(FeedItem {
item_id: ItemId(self.item_id), key: FeedItemKey {
feed_id: FeedId(self.feed_id), feed_id: self.feed_id,
item_id: self.item_id,
},
score: (UnparsedTimedScore { score: (UnparsedTimedScore {
value: self.score, value: self.score,
last_updated: DateTime::parse_from_rfc2822(&self.last_updated)? last_updated: DateTime::parse_from_rfc2822(&self.last_updated)?
@ -38,19 +39,41 @@ impl UnparsedFeedItem {
} }
pub struct FeedItem { pub struct FeedItem {
item_id: ItemId, key: FeedItemKey,
feed_id: FeedId,
score: TimedScore, score: TimedScore,
} }
impl FeedItem { impl FeedItem {
// async fn boost(pool: &AdapterPool, boost: Boost) -> Result<()> { pub fn key(&self) -> FeedItemKey { self.key }
// self.boost_at(pool, boost, Utc::now()).await pub fn score(&self) -> TimedScore { self.score.clone() }
// }
// pub async fn archive(
// async fn boost_at( &self, pool: &AdapterPool
// &self, pool: &AdapterPool, boost: Boost, boost_at: DateTime<Utc> ) -> Result<()> {
// ) -> Result<()> { sqlx::query!(
// "UPDATE feed_items
// } SET archived = ?
WHERE feed_id = ? AND item_id = ?",
true, self.key.feed_id, self.key.item_id
).execute(&pool.0).await?;
Ok(())
}
pub async fn update_score(
pool: &AdapterPool, key: FeedItemKey, new_score: TimedScore
) -> Result<()> {
let unparsed_score = UnparsedTimedScore::unparse(new_score);
let last_updated = unparsed_score.last_updated.to_rfc2822();
let boosted_at = unparsed_score.last_boosted.map(|lb| lb.to_rfc2822());
sqlx::query!(
"UPDATE feed_items
SET score = ?, last_updated = ?, boosted_at = ?
WHERE feed_id = ? AND item_id = ?",
unparsed_score.value, last_updated, boosted_at, key.feed_id, key.item_id
).execute(&pool.0).await?;
Ok(())
}
} }

View file

@ -2,8 +2,8 @@ use crate::{
Result, Result,
AdapterPool, AdapterPool,
db::{ db::{
ChannelId, ChannelKey,
ItemId, ItemKey,
}, },
fetch::FetchedRSSItem, fetch::FetchedRSSItem,
}; };
@ -22,8 +22,8 @@ pub struct UnparsedItem {
impl UnparsedItem { impl UnparsedItem {
pub fn parse(self) -> Result<Item> { pub fn parse(self) -> Result<Item> {
Ok(Item { Ok(Item {
id: ItemId(self.id), id: ItemKey(self.id),
channel_id: ChannelId(self.channel_id), channel_id: ChannelKey(self.channel_id),
fetched_at: match self.fetched_at { fetched_at: match self.fetched_at {
Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)? Some(dt_str) => Some(DateTime::parse_from_rfc2822(&dt_str)?
.with_timezone(&Utc)), .with_timezone(&Utc)),
@ -38,8 +38,8 @@ impl UnparsedItem {
} }
pub struct Item { pub struct Item {
id: ItemId, id: ItemKey,
channel_id: ChannelId, channel_id: ChannelKey,
#[allow(dead_code)] // TODO: Use for score decay calculations later #[allow(dead_code)] // TODO: Use for score decay calculations later
fetched_at: Option<DateTime<Utc>>, fetched_at: Option<DateTime<Utc>>,
@ -49,14 +49,14 @@ pub struct Item {
} }
impl Item { impl Item {
pub fn id(&self) -> ItemId { self.id } pub fn id(&self) -> ItemKey { self.id }
pub fn channel(&self) -> ChannelId { self.channel_id } pub fn channel(&self) -> ChannelKey { self.channel_id }
pub fn title(&self) -> Option<&str> { self.title.as_deref() } pub fn title(&self) -> Option<&str> { self.title.as_deref() }
pub fn description(&self) -> Option<&str> { self.description.as_deref() } pub fn description(&self) -> Option<&str> { self.description.as_deref() }
pub fn content(&self) -> Option<&str> { self.content.as_deref() } pub fn content(&self) -> Option<&str> { self.content.as_deref() }
pub async fn get_or_create( pub async fn get_or_create(
pool: &AdapterPool, from_channel: ChannelId, guid: &str pool: &AdapterPool, from_channel: ChannelKey, guid: &str
) -> Result<Self> { ) -> Result<Self> {
let item = sqlx::query_as!( let item = sqlx::query_as!(

View file

@ -2,7 +2,7 @@ use crate::{
Result, Result,
AdapterPool, AdapterPool,
db::{ db::{
UserId, UserKey,
Feed, Feed,
feed::UnparsedFeed, feed::UnparsedFeed,
}, },
@ -15,22 +15,22 @@ pub struct UnparsedUser {
impl UnparsedUser { impl UnparsedUser {
pub fn parse(self) -> Result<User> { pub fn parse(self) -> Result<User> {
Ok(User { Ok(User {
id: UserId(self.id), id: UserKey(self.id),
name: self.name name: self.name
}) })
} }
} }
pub struct User { pub struct User {
id: UserId, id: UserKey,
name: String, name: String,
} }
impl User { impl User {
pub fn id(&self) -> UserId { self.id } pub fn id(&self) -> UserKey { self.id }
pub fn name(&self) -> &str { &self.name } pub fn name(&self) -> &str { &self.name }
pub async fn get(pool: &AdapterPool, id: UserId) -> Result<Self> { pub async fn get(pool: &AdapterPool, id: UserKey) -> Result<Self> {
let user = sqlx::query_as!( let user = sqlx::query_as!(
UnparsedUser, UnparsedUser,
"SELECT id, name FROM users WHERE id = ?", "SELECT id, name FROM users WHERE id = ?",
@ -58,13 +58,13 @@ impl User {
).fetch_one(&pool.0).await?; ).fetch_one(&pool.0).await?;
Ok(Self { Ok(Self {
id: UserId(result.id), id: UserKey(result.id),
name: result.name, name: result.name,
}) })
} }
pub async fn update_name( pub async fn update_name(
pool: &AdapterPool, id: UserId, new_name: &str pool: &AdapterPool, id: UserKey, new_name: &str
) -> Result<()> { ) -> Result<()> {
sqlx::query!( sqlx::query!(
"UPDATE users SET name = ? WHERE id = ?", "UPDATE users SET name = ? WHERE id = ?",

View file

@ -80,8 +80,24 @@ impl UnparsedTimedScore {
}), }),
} }
} }
pub fn unparse(ts: TimedScore) -> Self {
match ts {
TimedScore::Decaying(ds) => UnparsedTimedScore {
value: ds.value.into(),
last_updated: ds.last_updated,
last_boosted: None,
},
TimedScore::Boosted(bs) => UnparsedTimedScore {
value: bs.value.into(),
last_updated: bs.boosted_at,
last_boosted: Some(bs.boosted_at),
},
}
}
} }
#[derive(Clone)]
pub enum TimedScore { pub enum TimedScore {
Decaying(DecayingScore), Decaying(DecayingScore),
Boosted(BoostedScore), Boosted(BoostedScore),
@ -148,11 +164,13 @@ impl TimedScore {
} }
} }
#[derive(Clone)]
pub struct BoostedScore { pub struct BoostedScore {
value: Score, value: Score,
boosted_at: DateTime<Utc>, boosted_at: DateTime<Utc>,
} }
#[derive(Clone)]
pub struct DecayingScore { pub struct DecayingScore {
value: Score, value: Score,
last_updated: DateTime<Utc>, last_updated: DateTime<Utc>,