2026-01-23 16:35:43 -08:00
|
|
|
use crate::{
|
|
|
|
|
Result,
|
|
|
|
|
AdapterPool,
|
|
|
|
|
db::{
|
|
|
|
|
Channel,
|
2026-02-04 15:49:55 -08:00
|
|
|
ChannelKey,
|
2026-01-23 16:35:43 -08:00
|
|
|
Feed,
|
2026-02-04 15:49:55 -08:00
|
|
|
FeedKey,
|
2026-01-23 16:35:43 -08:00
|
|
|
Item,
|
|
|
|
|
},
|
2026-02-03 17:51:09 -08:00
|
|
|
score::{
|
|
|
|
|
Score,
|
|
|
|
|
Gravity,
|
|
|
|
|
Boost,
|
|
|
|
|
},
|
2026-01-23 16:35:43 -08:00
|
|
|
};
|
2026-02-03 17:51:09 -08:00
|
|
|
use chrono::{Utc, DateTime};
|
2026-01-23 16:35:43 -08:00
|
|
|
|
|
|
|
|
pub struct UnparsedFeedChannel {
|
|
|
|
|
pub channel_id: i64,
|
|
|
|
|
pub feed_id: i64,
|
2026-02-03 17:51:09 -08:00
|
|
|
pub initial_score: Option<i64>,
|
|
|
|
|
pub gravity: Option<i64>,
|
|
|
|
|
pub boost: Option<i64>,
|
2026-01-23 16:35:43 -08:00
|
|
|
}
|
|
|
|
|
impl UnparsedFeedChannel {
|
|
|
|
|
pub fn parse(self) -> Result<FeedChannel> {
|
|
|
|
|
Ok(FeedChannel {
|
2026-02-04 15:49:55 -08:00
|
|
|
channel_id: ChannelKey(self.channel_id),
|
|
|
|
|
feed_id: FeedKey(self.feed_id),
|
2026-02-03 17:51:09 -08:00
|
|
|
initial_score: Score::new(self.initial_score),
|
|
|
|
|
gravity: Gravity::new(self.gravity),
|
|
|
|
|
boost: Boost::new(self.boost),
|
2026-01-23 16:35:43 -08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct FeedChannel {
|
2026-02-04 15:49:55 -08:00
|
|
|
channel_id: ChannelKey,
|
|
|
|
|
feed_id: FeedKey,
|
2026-02-03 17:51:09 -08:00
|
|
|
initial_score: Score,
|
|
|
|
|
gravity: Gravity,
|
|
|
|
|
boost: Boost,
|
2026-01-23 16:35:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FeedChannel {
|
|
|
|
|
pub async fn get_channel(&self, pool: &AdapterPool) -> Result<Channel> {
|
|
|
|
|
Channel::get(pool, self.channel_id).await
|
|
|
|
|
}
|
|
|
|
|
pub async fn get_feed(&self, pool: &AdapterPool) -> Result<Feed> {
|
|
|
|
|
Feed::get(pool, self.feed_id).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn add_item(
|
|
|
|
|
&self, pool: &AdapterPool, item: &Item
|
2026-02-03 17:51:09 -08:00
|
|
|
) -> Result<()> {
|
|
|
|
|
self.add_item_at(pool, item, Utc::now()).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn add_item_at(
|
|
|
|
|
&self, pool: &AdapterPool, item: &Item, add_at: DateTime<Utc>
|
2026-01-23 16:35:43 -08:00
|
|
|
) -> Result<()> {
|
2026-02-04 13:59:54 -08:00
|
|
|
let int_item_id = item.id().0;
|
2026-02-03 17:51:09 -08:00
|
|
|
let int_initial_score = i64::from(self.initial_score);
|
|
|
|
|
let string_last_updated = add_at.to_rfc2822();
|
2026-01-23 16:35:43 -08:00
|
|
|
|
|
|
|
|
sqlx::query!(
|
2026-02-03 17:51:09 -08:00
|
|
|
"INSERT OR IGNORE INTO feed_items (feed_id, item_id, score, last_updated)
|
|
|
|
|
VALUES (?, ?, ?, ?)",
|
2026-02-04 13:59:54 -08:00
|
|
|
self.feed_id.0, int_item_id, int_initial_score, string_last_updated
|
2026-01-23 16:35:43 -08:00
|
|
|
).execute(&pool.0).await?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use reqwest::Url;
|
|
|
|
|
use crate::{
|
|
|
|
|
db::{
|
|
|
|
|
Channel,
|
2026-02-04 15:49:55 -08:00
|
|
|
FeedKey,
|
2026-01-23 16:35:43 -08:00
|
|
|
User
|
2026-01-26 15:00:52 -08:00
|
|
|
},
|
|
|
|
|
test_utils::{
|
2026-02-03 17:51:09 -08:00
|
|
|
FEED1, setup_adapter, get_datetime
|
2026-01-26 15:00:52 -08:00
|
|
|
},
|
2026-01-23 16:35:43 -08:00
|
|
|
};
|
|
|
|
|
|
2026-01-26 15:00:52 -08:00
|
|
|
#[test]
|
|
|
|
|
fn parse() {
|
|
|
|
|
const CID: i64 = 1;
|
2026-02-03 17:51:09 -08:00
|
|
|
const FID: i64 = 2;
|
|
|
|
|
const IS: i64 = 3;
|
|
|
|
|
const G: i64 = 4;
|
|
|
|
|
const B: i64 = 5;
|
2026-01-26 15:00:52 -08:00
|
|
|
let ufc = UnparsedFeedChannel {
|
|
|
|
|
channel_id: CID,
|
|
|
|
|
feed_id: FID,
|
2026-02-03 17:51:09 -08:00
|
|
|
initial_score: Some(IS),
|
|
|
|
|
gravity: Some(G),
|
|
|
|
|
boost: Some(B),
|
2026-01-26 15:00:52 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let fc = ufc.parse().unwrap();
|
2026-01-23 16:35:43 -08:00
|
|
|
|
2026-01-26 15:00:52 -08:00
|
|
|
assert_eq!(fc.channel_id.0, CID);
|
|
|
|
|
assert_eq!(fc.feed_id.0, FID);
|
2026-02-03 17:51:09 -08:00
|
|
|
assert_eq!(i64::from(fc.initial_score), IS);
|
|
|
|
|
assert_eq!(i64::from(fc.gravity), G);
|
|
|
|
|
assert_eq!(i64::from(fc.boost), B);
|
2026-01-23 16:35:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FeedChannel Tests
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn get_channel() {
|
|
|
|
|
let adapter = setup_adapter().await;
|
|
|
|
|
let pool = adapter.get_pool();
|
|
|
|
|
|
|
|
|
|
let url = Url::parse(FEED1).unwrap();
|
|
|
|
|
let channel = Channel::get_or_create(pool, url).await.unwrap();
|
|
|
|
|
|
|
|
|
|
let fc = FeedChannel {
|
|
|
|
|
channel_id: channel.id(),
|
2026-02-04 15:49:55 -08:00
|
|
|
feed_id: FeedKey(1), // Fake Feed
|
2026-02-03 17:51:09 -08:00
|
|
|
initial_score: Score::new(None),
|
|
|
|
|
gravity: Gravity::new(None),
|
|
|
|
|
boost: Boost::new(None),
|
2026-01-23 16:35:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let channel_from_fc = fc.get_channel(pool).await.unwrap();
|
|
|
|
|
assert_eq!(channel_from_fc.id(), channel.id());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn get_feed() {
|
|
|
|
|
let adapter = setup_adapter().await;
|
|
|
|
|
let pool = adapter.get_pool();
|
|
|
|
|
|
|
|
|
|
let user = User::create(pool, "Alice").await.unwrap();
|
|
|
|
|
let feed = Feed::create(pool, user.id(), "My Feed").await.unwrap();
|
|
|
|
|
|
|
|
|
|
let fc = FeedChannel {
|
2026-02-04 15:49:55 -08:00
|
|
|
channel_id: ChannelKey(1), // Fake Channel
|
2026-01-23 16:35:43 -08:00
|
|
|
feed_id: feed.id(),
|
2026-02-03 17:51:09 -08:00
|
|
|
initial_score: Score::new(None),
|
|
|
|
|
gravity: Gravity::new(None),
|
|
|
|
|
boost: Boost::new(None),
|
2026-01-23 16:35:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let feed_from_fc = fc.get_feed(pool).await.unwrap();
|
|
|
|
|
assert_eq!(feed_from_fc.id(), feed.id());
|
|
|
|
|
}
|
2026-02-03 17:51:09 -08:00
|
|
|
|
2026-01-23 16:35:43 -08:00
|
|
|
#[tokio::test]
|
|
|
|
|
pub async fn add_item() {
|
2026-02-03 17:51:09 -08:00
|
|
|
let dt = get_datetime();
|
2026-01-23 16:35:43 -08:00
|
|
|
let adapter = setup_adapter().await;
|
|
|
|
|
let pool = adapter.get_pool();
|
|
|
|
|
|
|
|
|
|
let user = User::create(pool, "Alice").await.unwrap();
|
|
|
|
|
let feed = Feed::create(pool, user.id(), "My Feed").await.unwrap();
|
|
|
|
|
let url = Url::parse(FEED1).unwrap();
|
|
|
|
|
let channel = Channel::get_or_create(pool, url).await.unwrap();
|
|
|
|
|
let fc = FeedChannel {
|
|
|
|
|
channel_id: channel.id(),
|
|
|
|
|
feed_id: feed.id(),
|
2026-02-03 17:51:09 -08:00
|
|
|
initial_score: Score::new(None),
|
|
|
|
|
gravity: Gravity::new(None),
|
|
|
|
|
boost: Boost::new(None),
|
2026-01-23 16:35:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let item = Item::get_or_create(pool, channel.id(), "item-guid").await.unwrap();
|
2026-02-03 17:51:09 -08:00
|
|
|
fc.add_item_at(pool, &item, dt).await.unwrap();
|
2026-01-23 16:35:43 -08:00
|
|
|
|
|
|
|
|
let items = feed.get_items(pool, 1, 0).await.unwrap();
|
|
|
|
|
assert_eq!(items[0].id(), item.id());
|
|
|
|
|
}
|
|
|
|
|
}
|