Score refactor, add scores to feed_channels

This commit is contained in:
Julia Lange 2026-02-03 17:51:09 -08:00
parent 91229287a8
commit 25f00d1665
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
4 changed files with 166 additions and 78 deletions

View file

@ -8,17 +8,29 @@ use crate::{
FeedId,
Item,
},
score::{
Score,
Gravity,
Boost,
},
};
use chrono::{Utc, DateTime};
pub struct UnparsedFeedChannel {
pub channel_id: i64,
pub feed_id: i64,
pub initial_score: Option<i64>,
pub gravity: Option<i64>,
pub boost: Option<i64>,
}
impl UnparsedFeedChannel {
pub fn parse(self) -> Result<FeedChannel> {
Ok(FeedChannel {
channel_id: ChannelId(self.channel_id),
feed_id: FeedId(self.feed_id)
feed_id: FeedId(self.feed_id),
initial_score: Score::new(self.initial_score),
gravity: Gravity::new(self.gravity),
boost: Boost::new(self.boost),
})
}
}
@ -26,6 +38,9 @@ impl UnparsedFeedChannel {
pub struct FeedChannel {
channel_id: ChannelId,
feed_id: FeedId,
initial_score: Score,
gravity: Gravity,
boost: Boost,
}
impl FeedChannel {
@ -38,17 +53,26 @@ impl FeedChannel {
pub async fn add_item(
&self, pool: &AdapterPool, item: &Item
) -> Result<()> {
self.add_item_at(pool, item, Utc::now()).await
}
async fn add_item_at(
&self, pool: &AdapterPool, item: &Item, add_at: DateTime<Utc>
) -> Result<()> {
let int_item_id = i64::from(item.id());
let int_feed_id = i64::from(self.feed_id);
let int_initial_score = i64::from(self.initial_score);
let string_last_updated = add_at.to_rfc2822();
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
"INSERT OR IGNORE INTO feed_items (feed_id, item_id, score, last_updated)
VALUES (?, ?, ?, ?)",
int_feed_id, int_item_id, int_initial_score, string_last_updated
).execute(&pool.0).await?;
Ok(())
}
}
#[cfg(test)]
@ -62,23 +86,32 @@ mod tests {
User
},
test_utils::{
FEED1, setup_adapter,
FEED1, setup_adapter, get_datetime
},
};
#[test]
fn parse() {
const CID: i64 = 1;
const FID: i64 = 1;
const FID: i64 = 2;
const IS: i64 = 3;
const G: i64 = 4;
const B: i64 = 5;
let ufc = UnparsedFeedChannel {
channel_id: CID,
feed_id: FID,
initial_score: Some(IS),
gravity: Some(G),
boost: Some(B),
};
let fc = ufc.parse().unwrap();
assert_eq!(fc.channel_id.0, CID);
assert_eq!(fc.feed_id.0, FID);
assert_eq!(i64::from(fc.initial_score), IS);
assert_eq!(i64::from(fc.gravity), G);
assert_eq!(i64::from(fc.boost), B);
}
// FeedChannel Tests
@ -93,6 +126,9 @@ mod tests {
let fc = FeedChannel {
channel_id: channel.id(),
feed_id: FeedId(1), // Fake Feed
initial_score: Score::new(None),
gravity: Gravity::new(None),
boost: Boost::new(None),
};
let channel_from_fc = fc.get_channel(pool).await.unwrap();
@ -110,14 +146,18 @@ mod tests {
let fc = FeedChannel {
channel_id: ChannelId(1), // Fake Channel
feed_id: feed.id(),
initial_score: Score::new(None),
gravity: Gravity::new(None),
boost: Boost::new(None),
};
let feed_from_fc = fc.get_feed(pool).await.unwrap();
assert_eq!(feed_from_fc.id(), feed.id());
}
#[tokio::test]
pub async fn add_item() {
let dt = get_datetime();
let adapter = setup_adapter().await;
let pool = adapter.get_pool();
@ -128,10 +168,13 @@ mod tests {
let fc = FeedChannel {
channel_id: channel.id(),
feed_id: feed.id(),
initial_score: Score::new(None),
gravity: Gravity::new(None),
boost: Boost::new(None),
};
let item = Item::get_or_create(pool, channel.id(), "item-guid").await.unwrap();
fc.add_item(pool, &item).await.unwrap();
fc.add_item_at(pool, &item, dt).await.unwrap();
let items = feed.get_items(pool, 1, 0).await.unwrap();
assert_eq!(items[0].id(), item.id());