Compare commits

..

2 commits

Author SHA1 Message Date
f0a4e12f2b
Convert to keys and setup feed_item 2026-02-04 15:49:55 -08:00
c3d9dff83f
Remove from<i64> from db_ids 2026-02-04 13:59:54 -08:00
8 changed files with 121 additions and 79 deletions

View file

@ -11,15 +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);
impl From<$name> for i64 { fn from(id: $name) -> Self { id.0 } } };
};
($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
@ -245,10 +245,7 @@ mod tests {
let channel1 = Channel::get_or_create(pool, url_feed.clone()).await.unwrap(); let channel1 = Channel::get_or_create(pool, url_feed.clone()).await.unwrap();
let channel2 = Channel::get_or_create(pool, url_feed).await.unwrap(); let channel2 = Channel::get_or_create(pool, url_feed).await.unwrap();
assert_eq!( assert_eq!(channel1.id(), channel2.id());
i64::from(channel1.id()),
i64::from(channel2.id())
);
} }
#[tokio::test] #[tokio::test]

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,22 +47,21 @@ 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 int_id = i64::from(user_id);
let new_feed = sqlx::query_as!( let new_feed = sqlx::query_as!(
UnparsedFeed, UnparsedFeed,
"INSERT INTO feeds (user_id, title) "INSERT INTO feeds (user_id, title)
VALUES (?, ?) VALUES (?, ?)
RETURNING id as `id!`, title", RETURNING id as `id!`, title",
int_id, title user_id.0, title
).fetch_one(&pool.0).await?.parse(); ).fetch_one(&pool.0).await?.parse();
new_feed new_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 = ?",
@ -73,13 +72,12 @@ 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<()> {
let int_channel_id = i64::from(channel_id);
sqlx::query!( sqlx::query!(
"INSERT INTO feed_channels (feed_id, channel_id) "INSERT INTO feed_channels (feed_id, channel_id)
VALUES (?, ?)", VALUES (?, ?)",
self.id.0, int_channel_id self.id.0, channel_id.0
).execute(&pool.0).await?; ).execute(&pool.0).await?;
Ok(()) Ok(())

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,
@ -60,15 +60,14 @@ impl FeedChannel {
async fn add_item_at( async fn add_item_at(
&self, pool: &AdapterPool, item: &Item, add_at: DateTime<Utc> &self, pool: &AdapterPool, item: &Item, add_at: DateTime<Utc>
) -> Result<()> { ) -> Result<()> {
let int_item_id = i64::from(item.id()); let int_item_id = item.id().0;
let int_feed_id = i64::from(self.feed_id);
let int_initial_score = i64::from(self.initial_score); let int_initial_score = i64::from(self.initial_score);
let string_last_updated = add_at.to_rfc2822(); let string_last_updated = add_at.to_rfc2822();
sqlx::query!( sqlx::query!(
"INSERT OR IGNORE INTO feed_items (feed_id, item_id, score, last_updated) "INSERT OR IGNORE INTO feed_items (feed_id, item_id, score, last_updated)
VALUES (?, ?, ?, ?)", VALUES (?, ?, ?, ?)",
int_feed_id, int_item_id, int_initial_score, string_last_updated self.feed_id.0, int_item_id, int_initial_score, string_last_updated
).execute(&pool.0).await?; ).execute(&pool.0).await?;
Ok(()) Ok(())
} }
@ -81,7 +80,7 @@ mod tests {
use crate::{ use crate::{
db::{ db::{
Channel, Channel,
FeedId, FeedKey,
User User
}, },
test_utils::{ test_utils::{
@ -124,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),
@ -143,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,16 +49,15 @@ 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 int_channel_id = i64::from(from_channel);
let item = sqlx::query_as!( let item = sqlx::query_as!(
UnparsedItem, UnparsedItem,
@ -67,7 +66,7 @@ impl Item {
ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id ON CONFLICT(channel_id, guid) DO UPDATE SET channel_id = channel_id
RETURNING id as `id!`, channel_id, fetched_at, title, description, RETURNING id as `id!`, channel_id, fetched_at, title, description,
content", content",
int_channel_id, guid from_channel.0, guid
).fetch_one(&pool.0).await?.parse(); ).fetch_one(&pool.0).await?.parse();
item item

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>,