feat: add rss feeds and episode enclosure
This commit is contained in:
@@ -29,7 +29,14 @@ pub struct BangumiFilter {
|
||||
pub group: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize, SimpleObject)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "bangumi_type")]
|
||||
pub enum BangumiType {
|
||||
#[sea_orm(string_value = "mikan")]
|
||||
Mikan,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "bangumi")]
|
||||
pub struct Model {
|
||||
#[sea_orm(default_expr = "Expr::current_timestamp()")]
|
||||
@@ -39,6 +46,7 @@ pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub mikan_bangumi_id: Option<String>,
|
||||
pub bangumi_type: BangumiType,
|
||||
pub subscriber_id: i32,
|
||||
pub display_name: String,
|
||||
pub origin_name: String,
|
||||
@@ -50,7 +58,6 @@ pub struct Model {
|
||||
pub rss_link: Option<String>,
|
||||
pub poster_link: Option<String>,
|
||||
pub origin_poster_link: Option<String>,
|
||||
pub save_path: Option<String>,
|
||||
pub homepage: Option<String>,
|
||||
}
|
||||
|
||||
@@ -152,10 +159,7 @@ impl ActiveModel {
|
||||
season_raw: ActiveValue::Set(season_raw),
|
||||
fansub: ActiveValue::Set(Some(meta.fansub)),
|
||||
poster_link: ActiveValue::Set(poster_link),
|
||||
origin_poster_link: ActiveValue::Set(
|
||||
meta.origin_poster_src
|
||||
.map(|src| src[url::Position::BeforePath..].to_string()),
|
||||
),
|
||||
origin_poster_link: ActiveValue::Set(meta.origin_poster_src.map(|src| src.to_string())),
|
||||
homepage: ActiveValue::Set(Some(meta.homepage.to_string())),
|
||||
rss_link: ActiveValue::Set(Some(rss_url.to_string())),
|
||||
..Default::default()
|
||||
@@ -234,6 +238,7 @@ impl Model {
|
||||
Column::OriginName,
|
||||
Column::Fansub,
|
||||
Column::PosterLink,
|
||||
Column::OriginPosterLink,
|
||||
Column::Season,
|
||||
Column::SeasonRaw,
|
||||
Column::RssLink,
|
||||
|
||||
@@ -9,11 +9,19 @@ use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::RecorderResult,
|
||||
extract::{
|
||||
bittorrent::EpisodeEnclosureMeta,
|
||||
mikan::{MikanEpisodeHash, MikanEpisodeMeta, build_mikan_episode_homepage_url},
|
||||
origin::{OriginCompTrait, OriginNameRoot},
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "episode_type")]
|
||||
pub enum EpisodeType {
|
||||
#[sea_orm(string_value = "mikan")]
|
||||
Mikan,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "episodes")]
|
||||
pub struct Model {
|
||||
@@ -25,11 +33,15 @@ pub struct Model {
|
||||
pub id: i32,
|
||||
#[sea_orm(indexed)]
|
||||
pub mikan_episode_id: Option<String>,
|
||||
pub enclosure_torrent_link: Option<String>,
|
||||
pub enclosure_magnet_link: Option<String>,
|
||||
pub enclosure_pub_date: Option<DateTimeUtc>,
|
||||
pub enclosure_content_length: Option<u64>,
|
||||
pub episode_type: EpisodeType,
|
||||
pub origin_name: String,
|
||||
pub display_name: String,
|
||||
pub bangumi_id: i32,
|
||||
pub subscriber_id: i32,
|
||||
pub save_path: Option<String>,
|
||||
pub resolution: Option<String>,
|
||||
pub season: i32,
|
||||
pub season_raw: Option<String>,
|
||||
@@ -122,6 +134,7 @@ impl ActiveModel {
|
||||
ctx: &dyn AppContextTrait,
|
||||
bangumi: &bangumi::Model,
|
||||
episode: MikanEpisodeMeta,
|
||||
enclosure_meta: EpisodeEnclosureMeta,
|
||||
) -> RecorderResult<Self> {
|
||||
let mikan_base_url = ctx.mikan().base_url().clone();
|
||||
let episode_extention_meta = OriginNameRoot::parse_comp(&episode.episode_title)
|
||||
@@ -149,6 +162,10 @@ impl ActiveModel {
|
||||
poster_link: ActiveValue::Set(bangumi.poster_link.clone()),
|
||||
origin_poster_link: ActiveValue::Set(bangumi.origin_poster_link.clone()),
|
||||
episode_index: ActiveValue::Set(0),
|
||||
enclosure_torrent_link: ActiveValue::Set(enclosure_meta.torrent_link),
|
||||
enclosure_magnet_link: ActiveValue::Set(enclosure_meta.magnet_link),
|
||||
enclosure_pub_date: ActiveValue::Set(enclosure_meta.pub_date),
|
||||
enclosure_content_length: ActiveValue::Set(enclosure_meta.content_length),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -216,14 +233,19 @@ impl Model {
|
||||
|
||||
pub async fn add_mikan_episodes_for_subscription(
|
||||
ctx: &dyn AppContextTrait,
|
||||
creations: impl Iterator<Item = (&bangumi::Model, MikanEpisodeMeta)>,
|
||||
creations: impl Iterator<Item = (&bangumi::Model, MikanEpisodeMeta, EpisodeEnclosureMeta)>,
|
||||
subscriber_id: i32,
|
||||
subscription_id: i32,
|
||||
) -> RecorderResult<()> {
|
||||
let db = ctx.db();
|
||||
let new_episode_active_modes: Vec<ActiveModel> = creations
|
||||
.map(|(bangumi, episode_meta)| {
|
||||
ActiveModel::from_mikan_bangumi_and_episode_meta(ctx, bangumi, episode_meta)
|
||||
.map(|(bangumi, episode_meta, enclosure_meta)| {
|
||||
ActiveModel::from_mikan_bangumi_and_episode_meta(
|
||||
ctx,
|
||||
bangumi,
|
||||
episode_meta,
|
||||
enclosure_meta,
|
||||
)
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
@@ -234,7 +256,23 @@ impl Model {
|
||||
let new_episode_ids = Entity::insert_many(new_episode_active_modes)
|
||||
.on_conflict(
|
||||
OnConflict::columns([Column::MikanEpisodeId, Column::SubscriberId])
|
||||
.update_columns([Column::OriginName, Column::PosterLink, Column::Homepage])
|
||||
.update_columns([
|
||||
Column::OriginName,
|
||||
Column::PosterLink,
|
||||
Column::OriginPosterLink,
|
||||
Column::Homepage,
|
||||
Column::EnclosureContentLength,
|
||||
Column::EnclosurePubDate,
|
||||
Column::EnclosureTorrentLink,
|
||||
Column::EnclosureMagnetLink,
|
||||
Column::EpisodeIndex,
|
||||
Column::Subtitle,
|
||||
Column::Source,
|
||||
Column::Resolution,
|
||||
Column::Season,
|
||||
Column::SeasonRaw,
|
||||
Column::Fansub,
|
||||
])
|
||||
.to_owned(),
|
||||
)
|
||||
.exec_with_returning_columns(db, [Column::Id])
|
||||
|
||||
131
apps/recorder/src/models/feeds/mod.rs
Normal file
131
apps/recorder/src/models/feeds/mod.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
mod registry;
|
||||
mod rss;
|
||||
mod subscription_episodes_feed;
|
||||
|
||||
use ::rss::Channel;
|
||||
use async_trait::async_trait;
|
||||
pub use registry::Feed;
|
||||
pub use rss::{RssFeedItemTrait, RssFeedTrait};
|
||||
use sea_orm::{ActiveValue, DeriveEntityModel, entity::prelude::*};
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub use subscription_episodes_feed::SubscriptionEpisodesFeed;
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::{RecorderError, RecorderResult},
|
||||
};
|
||||
|
||||
#[derive(
|
||||
Debug, Serialize, Deserialize, Clone, PartialEq, Eq, DeriveActiveEnum, EnumIter, DeriveDisplay,
|
||||
)]
|
||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "feed_type")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum FeedType {
|
||||
#[sea_orm(string_value = "rss")]
|
||||
Rss,
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Debug, Serialize, Deserialize, Clone, PartialEq, Eq, DeriveActiveEnum, EnumIter, DeriveDisplay,
|
||||
)]
|
||||
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "feed_source")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum FeedSource {
|
||||
#[sea_orm(string_value = "subscription_episode")]
|
||||
SubscriptionEpisode,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "feeds")]
|
||||
pub struct Model {
|
||||
pub created_at: DateTimeUtc,
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
#[sea_orm(indexed)]
|
||||
pub token: String,
|
||||
#[sea_orm(indexed)]
|
||||
pub feed_type: FeedType,
|
||||
#[sea_orm(indexed)]
|
||||
pub feed_source: FeedSource,
|
||||
pub subscriber_id: Option<i32>,
|
||||
pub subscription_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::subscriptions::Entity",
|
||||
from = "Column::SubscriptionId",
|
||||
to = "super::subscriptions::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
Subscription,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::subscribers::Entity",
|
||||
from = "Column::SubscriberId",
|
||||
to = "super::subscribers::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
Subscriber,
|
||||
}
|
||||
|
||||
impl Related<super::subscriptions::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Subscription.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::subscribers::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Subscriber.def()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
|
||||
pub enum RelatedEntity {
|
||||
#[sea_orm(entity = "super::subscribers::Entity")]
|
||||
Subscriber,
|
||||
#[sea_orm(entity = "super::subscriptions::Entity")]
|
||||
Subscription,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ActiveModelBehavior for ActiveModel {
|
||||
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
|
||||
where
|
||||
C: ConnectionTrait,
|
||||
{
|
||||
if insert && let ActiveValue::NotSet = self.token {
|
||||
let token = nanoid::nanoid!(10);
|
||||
self.token = ActiveValue::Set(token);
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub async fn find_rss_feed_by_token(
|
||||
ctx: &dyn AppContextTrait,
|
||||
token: &str,
|
||||
api_base: &Url,
|
||||
) -> RecorderResult<Channel> {
|
||||
let db = ctx.db();
|
||||
|
||||
let feed_model = Entity::find()
|
||||
.filter(Column::Token.eq(token))
|
||||
.filter(Column::FeedType.eq(FeedType::Rss))
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or(RecorderError::ModelEntityNotFound {
|
||||
entity: "Feed".into(),
|
||||
})?;
|
||||
|
||||
let feed = Feed::from_model(ctx, feed_model).await?;
|
||||
|
||||
feed.into_rss_channel(ctx, api_base)
|
||||
}
|
||||
}
|
||||
54
apps/recorder/src/models/feeds/registry.rs
Normal file
54
apps/recorder/src/models/feeds/registry.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
use rss::Channel;
|
||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::{RecorderError, RecorderResult},
|
||||
models::{
|
||||
episodes,
|
||||
feeds::{self, FeedSource, RssFeedTrait, SubscriptionEpisodesFeed},
|
||||
subscriptions,
|
||||
},
|
||||
};
|
||||
|
||||
pub enum Feed {
|
||||
SubscritpionEpisodes(SubscriptionEpisodesFeed),
|
||||
}
|
||||
|
||||
impl Feed {
|
||||
pub async fn from_model(ctx: &dyn AppContextTrait, m: feeds::Model) -> RecorderResult<Self> {
|
||||
match m.feed_source {
|
||||
FeedSource::SubscriptionEpisode => {
|
||||
let db = ctx.db();
|
||||
let (subscription, episodes) = if let Some(subscription_id) = m.subscription_id
|
||||
&& let Some((subscription, episodes)) = subscriptions::Entity::find()
|
||||
.filter(subscriptions::Column::Id.eq(subscription_id))
|
||||
.find_with_related(episodes::Entity)
|
||||
.all(db)
|
||||
.await?
|
||||
.pop()
|
||||
{
|
||||
(subscription, episodes)
|
||||
} else {
|
||||
return Err(RecorderError::ModelEntityNotFound {
|
||||
entity: "Subscription".into(),
|
||||
});
|
||||
};
|
||||
Ok(Feed::SubscritpionEpisodes(
|
||||
SubscriptionEpisodesFeed::from_model(m, subscription, episodes),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_rss_channel(
|
||||
self,
|
||||
ctx: &dyn AppContextTrait,
|
||||
api_base: &Url,
|
||||
) -> RecorderResult<Channel> {
|
||||
match self {
|
||||
Self::SubscritpionEpisodes(feed) => feed.into_channel(ctx, api_base),
|
||||
}
|
||||
}
|
||||
}
|
||||
142
apps/recorder/src/models/feeds/rss.rs
Normal file
142
apps/recorder/src/models/feeds/rss.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use downloader::bittorrent::BITTORRENT_MIME_TYPE;
|
||||
use maplit::btreemap;
|
||||
use rss::{
|
||||
Channel, ChannelBuilder, EnclosureBuilder, GuidBuilder, Item, ItemBuilder,
|
||||
extension::{ExtensionBuilder, ExtensionMap},
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::{RecorderError, RecorderResult},
|
||||
};
|
||||
|
||||
pub trait RssFeedItemTrait: Sized {
|
||||
fn get_guid_value(&self) -> Cow<'_, str>;
|
||||
fn get_title(&self) -> Cow<'_, str>;
|
||||
fn get_description(&self) -> Cow<'_, str>;
|
||||
fn get_link(&self, ctx: &dyn AppContextTrait, api_base: &Url) -> Option<Cow<'_, str>>;
|
||||
fn get_enclosure_mime(&self) -> Option<Cow<'_, str>>;
|
||||
fn get_enclosure_link(&self, ctx: &dyn AppContextTrait, api_base: &Url)
|
||||
-> Option<Cow<'_, str>>;
|
||||
fn get_enclosure_pub_date(&self) -> Option<DateTime<Utc>>;
|
||||
fn get_enclosure_content_length(&self) -> Option<u64>;
|
||||
fn into_item(self, ctx: &dyn AppContextTrait, api_base: &Url) -> RecorderResult<Item> {
|
||||
let enclosure_mime_type =
|
||||
self.get_enclosure_mime()
|
||||
.ok_or_else(|| RecorderError::MikanRssInvalidFieldError {
|
||||
field: "enclosure_mime_type".into(),
|
||||
source: None.into(),
|
||||
})?;
|
||||
let enclosure_link = self.get_enclosure_link(ctx, api_base).ok_or_else(|| {
|
||||
RecorderError::MikanRssInvalidFieldError {
|
||||
field: "enclosure_link".into(),
|
||||
source: None.into(),
|
||||
}
|
||||
})?;
|
||||
let enclosure_content_length = self.get_enclosure_content_length().ok_or_else(|| {
|
||||
RecorderError::MikanRssInvalidFieldError {
|
||||
field: "enclosure_content_length".into(),
|
||||
source: None.into(),
|
||||
}
|
||||
})?;
|
||||
let enclosure_pub_date = self.get_enclosure_pub_date().ok_or_else(|| {
|
||||
RecorderError::MikanRssInvalidFieldError {
|
||||
field: "enclosure_pub_date".into(),
|
||||
source: None.into(),
|
||||
}
|
||||
})?;
|
||||
let link = self.get_link(ctx, api_base).ok_or_else(|| {
|
||||
RecorderError::MikanRssInvalidFieldError {
|
||||
field: "link".into(),
|
||||
source: None.into(),
|
||||
}
|
||||
})?;
|
||||
|
||||
let mut extensions = ExtensionMap::default();
|
||||
if enclosure_mime_type == BITTORRENT_MIME_TYPE {
|
||||
extensions.insert(
|
||||
"torrent".to_string(),
|
||||
btreemap! {
|
||||
"link".to_string() => vec![
|
||||
ExtensionBuilder::default().name(
|
||||
"link"
|
||||
).value(enclosure_link.to_string()).build()
|
||||
],
|
||||
"contentLength".to_string() => vec![
|
||||
ExtensionBuilder::default().name(
|
||||
"contentLength"
|
||||
).value(enclosure_content_length.to_string()).build()
|
||||
],
|
||||
"pubDate".to_string() => vec![
|
||||
ExtensionBuilder::default().name(
|
||||
"pubDate"
|
||||
).value(enclosure_pub_date.to_rfc3339()).build()
|
||||
],
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
let enclosure = EnclosureBuilder::default()
|
||||
.mime_type(enclosure_mime_type)
|
||||
.url(enclosure_link.to_string())
|
||||
.length(enclosure_content_length.to_string())
|
||||
.build();
|
||||
|
||||
let guid = GuidBuilder::default()
|
||||
.value(self.get_guid_value())
|
||||
.permalink(false)
|
||||
.build();
|
||||
|
||||
let item = ItemBuilder::default()
|
||||
.guid(guid)
|
||||
.title(self.get_title().to_string())
|
||||
.description(self.get_description().to_string())
|
||||
.link(link.to_string())
|
||||
.enclosure(enclosure)
|
||||
.pub_date(enclosure_pub_date.to_rfc3339())
|
||||
.extensions(extensions)
|
||||
.build();
|
||||
|
||||
Ok(item)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RssFeedTrait: Sized {
|
||||
type Item: RssFeedItemTrait;
|
||||
|
||||
fn get_description(&self) -> Cow<'_, str>;
|
||||
|
||||
fn get_title(&self) -> Cow<'_, str>;
|
||||
|
||||
fn get_link(&self, ctx: &dyn AppContextTrait, api_base: &Url) -> Option<Cow<'_, str>>;
|
||||
|
||||
fn items(&self) -> impl Iterator<Item = &Self::Item>;
|
||||
|
||||
fn into_items(self) -> impl Iterator<Item = Self::Item>;
|
||||
|
||||
fn into_channel(self, ctx: &dyn AppContextTrait, api_base: &Url) -> RecorderResult<Channel> {
|
||||
let link = self.get_link(ctx, api_base).ok_or_else(|| {
|
||||
RecorderError::MikanRssInvalidFieldError {
|
||||
field: "link".into(),
|
||||
source: None.into(),
|
||||
}
|
||||
})?;
|
||||
|
||||
let channel = ChannelBuilder::default()
|
||||
.title(self.get_title())
|
||||
.link(link.to_string())
|
||||
.description(self.get_description())
|
||||
.items({
|
||||
self.into_items()
|
||||
.map(|item| item.into_item(ctx, api_base))
|
||||
.collect::<RecorderResult<Vec<_>>>()?
|
||||
})
|
||||
.build();
|
||||
|
||||
Ok(channel)
|
||||
}
|
||||
}
|
||||
114
apps/recorder/src/models/feeds/subscription_episodes_feed.rs
Normal file
114
apps/recorder/src/models/feeds/subscription_episodes_feed.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use downloader::bittorrent::BITTORRENT_MIME_TYPE;
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
app::{AppContextTrait, PROJECT_NAME},
|
||||
models::{
|
||||
episodes,
|
||||
feeds::{
|
||||
self,
|
||||
rss::{RssFeedItemTrait, RssFeedTrait},
|
||||
},
|
||||
subscriptions,
|
||||
},
|
||||
web::controller,
|
||||
};
|
||||
|
||||
pub struct SubscriptionEpisodesFeed {
|
||||
pub feed: feeds::Model,
|
||||
pub subscription: subscriptions::Model,
|
||||
pub episodes: Vec<episodes::Model>,
|
||||
}
|
||||
|
||||
impl SubscriptionEpisodesFeed {
|
||||
pub fn from_model(
|
||||
feed: feeds::Model,
|
||||
subscription: subscriptions::Model,
|
||||
episodes: Vec<episodes::Model>,
|
||||
) -> Self {
|
||||
Self {
|
||||
feed,
|
||||
subscription,
|
||||
episodes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RssFeedItemTrait for episodes::Model {
|
||||
fn get_guid_value(&self) -> Cow<'_, str> {
|
||||
Cow::Owned(format!("{PROJECT_NAME}:episode:{}", self.id))
|
||||
}
|
||||
|
||||
fn get_title(&self) -> Cow<'_, str> {
|
||||
Cow::Borrowed(&self.display_name)
|
||||
}
|
||||
|
||||
fn get_description(&self) -> Cow<'_, str> {
|
||||
Cow::Borrowed(&self.display_name)
|
||||
}
|
||||
|
||||
fn get_link(&self, _ctx: &dyn AppContextTrait, _api_base: &Url) -> Option<Cow<'_, str>> {
|
||||
self.homepage.as_deref().map(Cow::Borrowed)
|
||||
}
|
||||
|
||||
fn get_enclosure_mime(&self) -> Option<Cow<'_, str>> {
|
||||
if self.enclosure_torrent_link.is_some() {
|
||||
Some(Cow::Borrowed(BITTORRENT_MIME_TYPE))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn get_enclosure_link(
|
||||
&self,
|
||||
_ctx: &dyn AppContextTrait,
|
||||
_api_base: &Url,
|
||||
) -> Option<Cow<'_, str>> {
|
||||
self.enclosure_torrent_link.as_deref().map(Cow::Borrowed)
|
||||
}
|
||||
|
||||
fn get_enclosure_pub_date(&self) -> Option<DateTime<Utc>> {
|
||||
self.enclosure_pub_date
|
||||
}
|
||||
|
||||
fn get_enclosure_content_length(&self) -> Option<u64> {
|
||||
self.enclosure_content_length
|
||||
}
|
||||
}
|
||||
|
||||
impl RssFeedTrait for SubscriptionEpisodesFeed {
|
||||
type Item = episodes::Model;
|
||||
|
||||
fn get_description(&self) -> Cow<'_, str> {
|
||||
Cow::Owned(format!(
|
||||
"{PROJECT_NAME} - episodes of subscription \"{}\"",
|
||||
self.subscription.display_name
|
||||
))
|
||||
}
|
||||
|
||||
fn get_title(&self) -> Cow<'_, str> {
|
||||
Cow::Owned(format!("{PROJECT_NAME} - subscription episodes"))
|
||||
}
|
||||
|
||||
fn get_link(&self, _ctx: &dyn AppContextTrait, api_base: &Url) -> Option<Cow<'_, str>> {
|
||||
let api_base = api_base
|
||||
.join(&format!(
|
||||
"{}/{}",
|
||||
controller::feeds::CONTROLLER_PREFIX,
|
||||
self.feed.token
|
||||
))
|
||||
.ok()?;
|
||||
Some(Cow::Owned(api_base.to_string()))
|
||||
}
|
||||
|
||||
fn items(&self) -> impl Iterator<Item = &Self::Item> {
|
||||
self.episodes.iter()
|
||||
}
|
||||
|
||||
fn into_items(self) -> impl Iterator<Item = Self::Item> {
|
||||
self.episodes.into_iter()
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ pub mod credential_3rd;
|
||||
pub mod downloaders;
|
||||
pub mod downloads;
|
||||
pub mod episodes;
|
||||
pub mod feeds;
|
||||
pub mod query;
|
||||
pub mod subscriber_tasks;
|
||||
pub mod subscribers;
|
||||
|
||||
@@ -3,11 +3,11 @@ use sea_orm::{ActiveValue, FromJsonQueryResult, TransactionTrait, entity::prelud
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
app::{AppContextTrait, PROJECT_NAME},
|
||||
errors::app_error::{RecorderError, RecorderResult},
|
||||
};
|
||||
|
||||
pub const SEED_SUBSCRIBER: &str = "konobangu";
|
||||
pub const SEED_SUBSCRIBER: &str = PROJECT_NAME;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
||||
pub struct SubscriberBangumiConfig {
|
||||
|
||||
@@ -1,311 +0,0 @@
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::{RecorderError, RecorderResult},
|
||||
extract::mikan::{
|
||||
MikanBangumiSubscription, MikanSeasonSubscription, MikanSubscriberSubscription,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize, DeriveDisplay,
|
||||
)]
|
||||
#[sea_orm(
|
||||
rs_type = "String",
|
||||
db_type = "Enum",
|
||||
enum_name = "subscription_category"
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SubscriptionCategory {
|
||||
#[sea_orm(string_value = "mikan_subscriber")]
|
||||
MikanSubscriber,
|
||||
#[sea_orm(string_value = "mikan_season")]
|
||||
MikanSeason,
|
||||
#[sea_orm(string_value = "mikan_bangumi")]
|
||||
MikanBangumi,
|
||||
#[sea_orm(string_value = "manual")]
|
||||
Manual,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "subscriptions")]
|
||||
pub struct Model {
|
||||
#[sea_orm(default_expr = "Expr::current_timestamp()")]
|
||||
pub created_at: DateTimeUtc,
|
||||
#[sea_orm(default_expr = "Expr::current_timestamp()")]
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub display_name: String,
|
||||
pub subscriber_id: i32,
|
||||
pub category: SubscriptionCategory,
|
||||
pub source_url: String,
|
||||
pub enabled: bool,
|
||||
pub credential_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::subscribers::Entity",
|
||||
from = "Column::SubscriberId",
|
||||
to = "super::subscribers::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
Subscriber,
|
||||
#[sea_orm(has_many = "super::bangumi::Entity")]
|
||||
Bangumi,
|
||||
#[sea_orm(has_many = "super::episodes::Entity")]
|
||||
Episodes,
|
||||
#[sea_orm(has_many = "super::subscription_episode::Entity")]
|
||||
SubscriptionEpisode,
|
||||
#[sea_orm(has_many = "super::subscription_bangumi::Entity")]
|
||||
SubscriptionBangumi,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::credential_3rd::Entity",
|
||||
from = "Column::CredentialId",
|
||||
to = "super::credential_3rd::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "SetNull"
|
||||
)]
|
||||
Credential3rd,
|
||||
}
|
||||
|
||||
impl Related<super::subscribers::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Subscriber.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::subscription_bangumi::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::SubscriptionBangumi.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::subscription_episode::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::SubscriptionEpisode.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::bangumi::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::subscription_bangumi::Relation::Bangumi.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(
|
||||
super::subscription_bangumi::Relation::Subscription
|
||||
.def()
|
||||
.rev(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::episodes::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::subscription_episode::Relation::Episode.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(
|
||||
super::subscription_episode::Relation::Subscription
|
||||
.def()
|
||||
.rev(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::credential_3rd::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Credential3rd.def()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
|
||||
pub enum RelatedEntity {
|
||||
#[sea_orm(entity = "super::subscribers::Entity")]
|
||||
Subscriber,
|
||||
#[sea_orm(entity = "super::bangumi::Entity")]
|
||||
Bangumi,
|
||||
#[sea_orm(entity = "super::episodes::Entity")]
|
||||
Episode,
|
||||
#[sea_orm(entity = "super::subscription_episode::Entity")]
|
||||
SubscriptionEpisode,
|
||||
#[sea_orm(entity = "super::subscription_bangumi::Entity")]
|
||||
SubscriptionBangumi,
|
||||
#[sea_orm(entity = "super::credential_3rd::Entity")]
|
||||
Credential3rd,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl ActiveModel {}
|
||||
|
||||
impl Model {
|
||||
pub async fn toggle_with_ids(
|
||||
ctx: &dyn AppContextTrait,
|
||||
ids: impl Iterator<Item = i32>,
|
||||
enabled: bool,
|
||||
) -> RecorderResult<()> {
|
||||
let db = ctx.db();
|
||||
Entity::update_many()
|
||||
.col_expr(Column::Enabled, Expr::value(enabled))
|
||||
.filter(Column::Id.is_in(ids))
|
||||
.exec(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_with_ids(
|
||||
ctx: &dyn AppContextTrait,
|
||||
ids: impl Iterator<Item = i32>,
|
||||
) -> RecorderResult<()> {
|
||||
let db = ctx.db();
|
||||
Entity::delete_many()
|
||||
.filter(Column::Id.is_in(ids))
|
||||
.exec(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn find_by_id_and_subscriber_id(
|
||||
ctx: &dyn AppContextTrait,
|
||||
subscriber_id: i32,
|
||||
subscription_id: i32,
|
||||
) -> RecorderResult<Self> {
|
||||
let db = ctx.db();
|
||||
let subscription_model = Entity::find_by_id(subscription_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| RecorderError::ModelEntityNotFound {
|
||||
entity: "Subscription".into(),
|
||||
})?;
|
||||
|
||||
if subscription_model.subscriber_id != subscriber_id {
|
||||
Err(RecorderError::ModelEntityNotFound {
|
||||
entity: "Subscription".into(),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(subscription_model)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait SubscriptionTrait: Sized + Debug {
|
||||
fn get_subscriber_id(&self) -> i32;
|
||||
|
||||
fn get_subscription_id(&self) -> i32;
|
||||
|
||||
async fn sync_feeds_incremental(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()>;
|
||||
|
||||
async fn sync_feeds_full(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()>;
|
||||
|
||||
async fn sync_sources(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()>;
|
||||
|
||||
fn try_from_model(model: &Model) -> RecorderResult<Self>;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "category")]
|
||||
pub enum Subscription {
|
||||
#[serde(rename = "mikan_subscriber")]
|
||||
MikanSubscriber(MikanSubscriberSubscription),
|
||||
#[serde(rename = "mikan_season")]
|
||||
MikanSeason(MikanSeasonSubscription),
|
||||
#[serde(rename = "mikan_bangumi")]
|
||||
MikanBangumi(MikanBangumiSubscription),
|
||||
#[serde(rename = "manual")]
|
||||
Manual,
|
||||
}
|
||||
|
||||
impl Subscription {
|
||||
pub fn category(&self) -> SubscriptionCategory {
|
||||
match self {
|
||||
Self::MikanSubscriber(_) => SubscriptionCategory::MikanSubscriber,
|
||||
Self::MikanSeason(_) => SubscriptionCategory::MikanSeason,
|
||||
Self::MikanBangumi(_) => SubscriptionCategory::MikanBangumi,
|
||||
Self::Manual => SubscriptionCategory::Manual,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SubscriptionTrait for Subscription {
|
||||
fn get_subscriber_id(&self) -> i32 {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.get_subscriber_id(),
|
||||
Self::MikanSeason(subscription) => subscription.get_subscriber_id(),
|
||||
Self::MikanBangumi(subscription) => subscription.get_subscriber_id(),
|
||||
Self::Manual => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_subscription_id(&self) -> i32 {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.get_subscription_id(),
|
||||
Self::MikanSeason(subscription) => subscription.get_subscription_id(),
|
||||
Self::MikanBangumi(subscription) => subscription.get_subscription_id(),
|
||||
Self::Manual => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_feeds_incremental(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()> {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.sync_feeds_incremental(ctx).await,
|
||||
Self::MikanSeason(subscription) => subscription.sync_feeds_incremental(ctx).await,
|
||||
Self::MikanBangumi(subscription) => subscription.sync_feeds_incremental(ctx).await,
|
||||
Self::Manual => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_feeds_full(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()> {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.sync_feeds_full(ctx).await,
|
||||
Self::MikanSeason(subscription) => subscription.sync_feeds_full(ctx).await,
|
||||
Self::MikanBangumi(subscription) => subscription.sync_feeds_full(ctx).await,
|
||||
Self::Manual => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_sources(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()> {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.sync_sources(ctx).await,
|
||||
Self::MikanSeason(subscription) => subscription.sync_sources(ctx).await,
|
||||
Self::MikanBangumi(subscription) => subscription.sync_sources(ctx).await,
|
||||
Self::Manual => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn try_from_model(model: &Model) -> RecorderResult<Self> {
|
||||
match model.category {
|
||||
SubscriptionCategory::MikanSubscriber => {
|
||||
MikanSubscriberSubscription::try_from_model(model).map(Self::MikanSubscriber)
|
||||
}
|
||||
SubscriptionCategory::MikanSeason => {
|
||||
MikanSeasonSubscription::try_from_model(model).map(Self::MikanSeason)
|
||||
}
|
||||
SubscriptionCategory::MikanBangumi => {
|
||||
MikanBangumiSubscription::try_from_model(model).map(Self::MikanBangumi)
|
||||
}
|
||||
SubscriptionCategory::Manual => Ok(Self::Manual),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&Model> for Subscription {
|
||||
type Error = RecorderError;
|
||||
|
||||
fn try_from(model: &Model) -> Result<Self, Self::Error> {
|
||||
Self::try_from_model(model)
|
||||
}
|
||||
}
|
||||
20
apps/recorder/src/models/subscriptions/core.rs
Normal file
20
apps/recorder/src/models/subscriptions/core.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{app::AppContextTrait, errors::RecorderResult, models::subscriptions};
|
||||
|
||||
#[async_trait]
|
||||
pub trait SubscriptionTrait: Sized + Debug {
|
||||
fn get_subscriber_id(&self) -> i32;
|
||||
|
||||
fn get_subscription_id(&self) -> i32;
|
||||
|
||||
async fn sync_feeds_incremental(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()>;
|
||||
|
||||
async fn sync_feeds_full(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()>;
|
||||
|
||||
async fn sync_sources(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()>;
|
||||
|
||||
fn try_from_model(model: &subscriptions::Model) -> RecorderResult<Self>;
|
||||
}
|
||||
185
apps/recorder/src/models/subscriptions/mod.rs
Normal file
185
apps/recorder/src/models/subscriptions/mod.rs
Normal file
@@ -0,0 +1,185 @@
|
||||
mod core;
|
||||
mod registry;
|
||||
pub use core::SubscriptionTrait;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use async_trait::async_trait;
|
||||
pub use registry::{
|
||||
Subscription, SubscriptionCategory, SubscriptionCategoryEnum, SubscriptionCategoryIter,
|
||||
SubscriptionCategoryVariant, SubscriptionCategoryVariantIter,
|
||||
};
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::{RecorderError, RecorderResult},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
#[sea_orm(table_name = "subscriptions")]
|
||||
pub struct Model {
|
||||
#[sea_orm(default_expr = "Expr::current_timestamp()")]
|
||||
pub created_at: DateTimeUtc,
|
||||
#[sea_orm(default_expr = "Expr::current_timestamp()")]
|
||||
pub updated_at: DateTimeUtc,
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub display_name: String,
|
||||
pub subscriber_id: i32,
|
||||
pub category: SubscriptionCategory,
|
||||
pub source_url: String,
|
||||
pub enabled: bool,
|
||||
pub credential_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::subscribers::Entity",
|
||||
from = "Column::SubscriberId",
|
||||
to = "super::subscribers::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
Subscriber,
|
||||
#[sea_orm(has_many = "super::bangumi::Entity")]
|
||||
Bangumi,
|
||||
#[sea_orm(has_many = "super::episodes::Entity")]
|
||||
Episodes,
|
||||
#[sea_orm(has_many = "super::subscription_episode::Entity")]
|
||||
SubscriptionEpisode,
|
||||
#[sea_orm(has_many = "super::subscription_bangumi::Entity")]
|
||||
SubscriptionBangumi,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::credential_3rd::Entity",
|
||||
from = "Column::CredentialId",
|
||||
to = "super::credential_3rd::Column::Id",
|
||||
on_update = "Cascade",
|
||||
on_delete = "SetNull"
|
||||
)]
|
||||
Credential3rd,
|
||||
}
|
||||
|
||||
impl Related<super::subscribers::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Subscriber.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::subscription_bangumi::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::SubscriptionBangumi.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::subscription_episode::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::SubscriptionEpisode.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::bangumi::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::subscription_bangumi::Relation::Bangumi.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(
|
||||
super::subscription_bangumi::Relation::Subscription
|
||||
.def()
|
||||
.rev(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::episodes::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
super::subscription_episode::Relation::Episode.def()
|
||||
}
|
||||
|
||||
fn via() -> Option<RelationDef> {
|
||||
Some(
|
||||
super::subscription_episode::Relation::Subscription
|
||||
.def()
|
||||
.rev(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::credential_3rd::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Credential3rd.def()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
|
||||
pub enum RelatedEntity {
|
||||
#[sea_orm(entity = "super::subscribers::Entity")]
|
||||
Subscriber,
|
||||
#[sea_orm(entity = "super::bangumi::Entity")]
|
||||
Bangumi,
|
||||
#[sea_orm(entity = "super::episodes::Entity")]
|
||||
Episode,
|
||||
#[sea_orm(entity = "super::subscription_episode::Entity")]
|
||||
SubscriptionEpisode,
|
||||
#[sea_orm(entity = "super::subscription_bangumi::Entity")]
|
||||
SubscriptionBangumi,
|
||||
#[sea_orm(entity = "super::credential_3rd::Entity")]
|
||||
Credential3rd,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
impl ActiveModel {}
|
||||
|
||||
impl Model {
|
||||
pub async fn toggle_with_ids(
|
||||
ctx: &dyn AppContextTrait,
|
||||
ids: impl Iterator<Item = i32>,
|
||||
enabled: bool,
|
||||
) -> RecorderResult<()> {
|
||||
let db = ctx.db();
|
||||
Entity::update_many()
|
||||
.col_expr(Column::Enabled, Expr::value(enabled))
|
||||
.filter(Column::Id.is_in(ids))
|
||||
.exec(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_with_ids(
|
||||
ctx: &dyn AppContextTrait,
|
||||
ids: impl Iterator<Item = i32>,
|
||||
) -> RecorderResult<()> {
|
||||
let db = ctx.db();
|
||||
Entity::delete_many()
|
||||
.filter(Column::Id.is_in(ids))
|
||||
.exec(db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn find_by_id_and_subscriber_id(
|
||||
ctx: &dyn AppContextTrait,
|
||||
subscriber_id: i32,
|
||||
subscription_id: i32,
|
||||
) -> RecorderResult<Self> {
|
||||
let db = ctx.db();
|
||||
let subscription_model = Entity::find_by_id(subscription_id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| RecorderError::ModelEntityNotFound {
|
||||
entity: "Subscription".into(),
|
||||
})?;
|
||||
|
||||
if subscription_model.subscriber_id != subscriber_id {
|
||||
Err(RecorderError::ModelEntityNotFound {
|
||||
entity: "Subscription".into(),
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(subscription_model)
|
||||
}
|
||||
}
|
||||
129
apps/recorder/src/models/subscriptions/registry.rs
Normal file
129
apps/recorder/src/models/subscriptions/registry.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use sea_orm::{DeriveActiveEnum, DeriveDisplay, EnumIter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
app::AppContextTrait,
|
||||
errors::{RecorderError, RecorderResult},
|
||||
extract::mikan::{
|
||||
MikanBangumiSubscription, MikanSeasonSubscription, MikanSubscriberSubscription,
|
||||
},
|
||||
models::subscriptions::{self, SubscriptionTrait},
|
||||
};
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize, DeriveDisplay,
|
||||
)]
|
||||
#[sea_orm(
|
||||
rs_type = "String",
|
||||
db_type = "Enum",
|
||||
enum_name = "subscription_category"
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum SubscriptionCategory {
|
||||
#[sea_orm(string_value = "mikan_subscriber")]
|
||||
MikanSubscriber,
|
||||
#[sea_orm(string_value = "mikan_season")]
|
||||
MikanSeason,
|
||||
#[sea_orm(string_value = "mikan_bangumi")]
|
||||
MikanBangumi,
|
||||
#[sea_orm(string_value = "manual")]
|
||||
Manual,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "category")]
|
||||
pub enum Subscription {
|
||||
#[serde(rename = "mikan_subscriber")]
|
||||
MikanSubscriber(MikanSubscriberSubscription),
|
||||
#[serde(rename = "mikan_season")]
|
||||
MikanSeason(MikanSeasonSubscription),
|
||||
#[serde(rename = "mikan_bangumi")]
|
||||
MikanBangumi(MikanBangumiSubscription),
|
||||
#[serde(rename = "manual")]
|
||||
Manual,
|
||||
}
|
||||
|
||||
impl Subscription {
|
||||
pub fn category(&self) -> SubscriptionCategory {
|
||||
match self {
|
||||
Self::MikanSubscriber(_) => SubscriptionCategory::MikanSubscriber,
|
||||
Self::MikanSeason(_) => SubscriptionCategory::MikanSeason,
|
||||
Self::MikanBangumi(_) => SubscriptionCategory::MikanBangumi,
|
||||
Self::Manual => SubscriptionCategory::Manual,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl SubscriptionTrait for Subscription {
|
||||
fn get_subscriber_id(&self) -> i32 {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.get_subscriber_id(),
|
||||
Self::MikanSeason(subscription) => subscription.get_subscriber_id(),
|
||||
Self::MikanBangumi(subscription) => subscription.get_subscriber_id(),
|
||||
Self::Manual => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_subscription_id(&self) -> i32 {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.get_subscription_id(),
|
||||
Self::MikanSeason(subscription) => subscription.get_subscription_id(),
|
||||
Self::MikanBangumi(subscription) => subscription.get_subscription_id(),
|
||||
Self::Manual => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_feeds_incremental(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()> {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.sync_feeds_incremental(ctx).await,
|
||||
Self::MikanSeason(subscription) => subscription.sync_feeds_incremental(ctx).await,
|
||||
Self::MikanBangumi(subscription) => subscription.sync_feeds_incremental(ctx).await,
|
||||
Self::Manual => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_feeds_full(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()> {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.sync_feeds_full(ctx).await,
|
||||
Self::MikanSeason(subscription) => subscription.sync_feeds_full(ctx).await,
|
||||
Self::MikanBangumi(subscription) => subscription.sync_feeds_full(ctx).await,
|
||||
Self::Manual => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn sync_sources(&self, ctx: Arc<dyn AppContextTrait>) -> RecorderResult<()> {
|
||||
match self {
|
||||
Self::MikanSubscriber(subscription) => subscription.sync_sources(ctx).await,
|
||||
Self::MikanSeason(subscription) => subscription.sync_sources(ctx).await,
|
||||
Self::MikanBangumi(subscription) => subscription.sync_sources(ctx).await,
|
||||
Self::Manual => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn try_from_model(model: &subscriptions::Model) -> RecorderResult<Self> {
|
||||
match model.category {
|
||||
SubscriptionCategory::MikanSubscriber => {
|
||||
MikanSubscriberSubscription::try_from_model(model).map(Self::MikanSubscriber)
|
||||
}
|
||||
SubscriptionCategory::MikanSeason => {
|
||||
MikanSeasonSubscription::try_from_model(model).map(Self::MikanSeason)
|
||||
}
|
||||
SubscriptionCategory::MikanBangumi => {
|
||||
MikanBangumiSubscription::try_from_model(model).map(Self::MikanBangumi)
|
||||
}
|
||||
SubscriptionCategory::Manual => Ok(Self::Manual),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&subscriptions::Model> for Subscription {
|
||||
type Error = RecorderError;
|
||||
|
||||
fn try_from(model: &subscriptions::Model) -> Result<Self, Self::Error> {
|
||||
Self::try_from_model(model)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user