Skip to content

Commit

Permalink
feat(api): support annotations in fetch entries query
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 13, 2024
1 parent 937b561 commit a1646a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
43 changes: 22 additions & 21 deletions crates/synd_api/src/gql/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ impl From<feedrs::Link> for Link {
}

pub struct Entry<'a> {
meta: Cow<'a, types::FeedMeta>,
meta: Cow<'a, Annotated<types::FeedMeta>>,
entry: types::Entry,
}

#[Object]
impl<'a> Entry<'a> {
/// Feed of this entry
async fn feed(&'a self) -> FeedMeta<'a> {
self.meta.as_ref().into()
async fn feed(&'a self) -> FeedMeta {
self.meta.clone().into()
}
/// Entry title
async fn title(&self) -> Option<&str> {
Expand All @@ -67,16 +67,13 @@ impl<'a> Entry<'a> {

/// Link to websiteurl at which this entry is published
async fn website_url(&self) -> Option<&str> {
self.entry.website_url(self.meta.r#type())
self.entry.website_url(self.meta.feed.r#type())
}
}

impl<'a> Entry<'a> {
pub fn new(meta: impl Into<Cow<'a, types::FeedMeta>>, entry: types::Entry) -> Self {
Self {
meta: meta.into(),
entry,
}
pub fn new(meta: Cow<'a, Annotated<types::FeedMeta>>, entry: types::Entry) -> Self {
Self { meta, entry }
}
}

Expand Down Expand Up @@ -134,12 +131,12 @@ impl Feed {
> {
#[allow(clippy::cast_sign_loss)]
let first = first.unwrap_or(5).max(0) as usize;
let meta = self.0.feed.meta();
let meta = self.0.project(|feed| feed.meta().clone());
let entries = self
.0
.feed
.entries()
.map(|entry| Entry::new(meta, entry.clone()))
.map(move |entry| Entry::new(Cow::Owned(meta.clone()), entry.clone()))
.take(first)
.collect::<Vec<_>>();

Expand Down Expand Up @@ -230,29 +227,33 @@ impl From<Annotated<Arc<types::Feed>>> for Feed {
}
}

pub(super) struct FeedMeta<'a>(Cow<'a, types::FeedMeta>);
pub(super) struct FeedMeta<'a>(Cow<'a, Annotated<types::FeedMeta>>);

#[Object]
impl<'a> FeedMeta<'a> {
/// Title of the feed
async fn title(&self) -> Option<&str> {
self.0.title()
self.0.feed.title()
}

/// Url of the feed
async fn url(&self) -> &str {
self.0.url()
self.0.feed.url()
}

/// Requirement Level for the feed
async fn requirement(&self) -> Option<Requirement> {
self.0.requirement
}
}

impl From<types::FeedMeta> for FeedMeta<'static> {
fn from(value: types::FeedMeta) -> Self {
Self(Cow::Owned(value))
/// Category of the feed
async fn category(&self) -> Option<&Category<'static>> {
self.0.category.as_ref()
}
}

impl<'a> From<&'a types::FeedMeta> for FeedMeta<'a> {
fn from(value: &'a types::FeedMeta) -> Self {
Self(Cow::Borrowed(value))
impl<'a> From<Cow<'a, Annotated<types::FeedMeta>>> for FeedMeta<'a> {
fn from(value: Cow<'a, Annotated<types::FeedMeta>>) -> Self {
Self(value)
}
}
4 changes: 3 additions & 1 deletion crates/synd_api/src/gql/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use async_graphql::{
connection::{Connection, Edge},
Context, Object, Result,
Expand Down Expand Up @@ -89,7 +91,7 @@ impl Subscription {
.expect("FeedMeta not found. this is a bug")
.clone();
let cursor = entry.id().into();
let node = Entry::new(meta, entry);
let node = Entry::new(Cow::Owned(meta), entry);
Edge::new(cursor, node)
});

Expand Down
18 changes: 15 additions & 3 deletions crates/synd_api/src/usecase/fetch_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{cmp::Ordering, collections::HashMap, sync::Arc};
use futures_util::{stream::FuturesUnordered, StreamExt};
use synd_feed::{
feed::{cache::FetchCachedFeed, parser::FetchFeedError},
types::{self, EntryId},
types::{self, Annotated, EntryId},
};
use thiserror::Error;

Expand All @@ -26,7 +26,7 @@ pub struct FetchEntriesInput {
#[derive(Default)]
pub struct FetchEntriesOutput {
pub entries: Vec<(types::Entry, types::FeedUrl)>,
pub feeds: HashMap<types::FeedUrl, types::FeedMeta>,
pub feeds: HashMap<types::FeedUrl, Annotated<types::FeedMeta>>,
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -66,7 +66,7 @@ impl Usecase for FetchEntries {
.user_id()
.expect("user id not found. this is a bug");

let feeds = self.repository.fetch_subscribed_feeds(user_id).await?;
let mut feeds = self.repository.fetch_subscribed_feeds(user_id).await?;

// TODO: refactor
let mut feed_metas = HashMap::with_capacity(feeds.urls.len());
Expand All @@ -82,6 +82,18 @@ impl Usecase for FetchEntries {

let meta = feed.meta().clone();
let feed_url = meta.url().to_owned();
let meta = match feeds
.annotations
.as_mut()
.and_then(|annotations| annotations.remove(&feed_url))
{
Some(feed_annotations) => Annotated {
feed: meta,
requirement: feed_annotations.requirement,
category: feed_annotations.category,
},
None => Annotated::new(meta),
};
feed_metas.insert(feed_url.clone(), meta);
entries.extend(
feed.entries()
Expand Down

0 comments on commit a1646a5

Please sign in to comment.