Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CatalogsWithExtra - serialize model #700

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stremio-core-web/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod deep_links_ext;

mod serialize_catalogs_with_extra;
use serialize_catalogs_with_extra::*;
pub use serialize_catalogs_with_extra::*;

mod serialize_continue_watching_preview;
use serialize_continue_watching_preview::*;
Expand Down
22 changes: 17 additions & 5 deletions stremio-core-web/src/model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ use stremio_core::{
use crate::{
env::WebEnv,
model::{
serialize_catalogs_with_extra, serialize_continue_watching_preview, serialize_ctx,
serialize_data_export, serialize_discover, serialize_installed_addons, serialize_library,
serialize_local_search, serialize_meta_details, serialize_player, serialize_remote_addons,
serialize_continue_watching_preview, serialize_ctx, serialize_data_export,
serialize_discover, serialize_installed_addons, serialize_library, serialize_local_search,
serialize_meta_details, serialize_player, serialize_remote_addons,
serialize_streaming_server,
},
};

use super::SerializeModel;

#[derive(Model, Clone)]
#[cfg_attr(debug_assertions, derive(Serialize))]
#[model(WebEnv)]
Expand Down Expand Up @@ -133,7 +135,12 @@ impl WebModel {
self.streaming_server.base_url.as_ref(),
&self.ctx.profile.settings,
),
WebModelField::Board => serialize_catalogs_with_extra(&self.board, &self.ctx),
WebModelField::Board => {
// let old = serialize_catalogs_with_extra(&self.board, &self.ctx);
crate::model::CatalogsWithExtra::new(&self.board, &self.ctx)
.serialize_model()
.expect("JsValue from model::CatalogsWithExtra")
}
WebModelField::Discover => {
serialize_discover(&self.discover, &self.ctx, &self.streaming_server)
}
Expand All @@ -149,7 +156,12 @@ impl WebModel {
self.streaming_server.base_url.as_ref(),
"continuewatching".to_owned(),
),
WebModelField::Search => serialize_catalogs_with_extra(&self.search, &self.ctx),
WebModelField::Search => {
// let old = serialize_catalogs_with_extra(&self.search, &self.ctx)
crate::model::CatalogsWithExtra::new(&self.search, &self.ctx)
.serialize_model()
.expect("JsValue from model::CatalogsWithExtra")
}
WebModelField::LocalSearch => serialize_local_search(&self.local_search),
WebModelField::MetaDetails => {
serialize_meta_details(&self.meta_details, &self.ctx, &self.streaming_server)
Expand Down
193 changes: 112 additions & 81 deletions stremio-core-web/src/model/serialize_catalogs_with_extra.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,91 @@
use crate::model::deep_links_ext::DeepLinksExt;
use gloo_utils::format::JsValueSerdeExt;
use itertools::Itertools;
use serde::Serialize;
use stremio_core::deep_links::{DiscoverDeepLinks, MetaItemDeepLinks};
use stremio_core::models::catalogs_with_extra::{CatalogsWithExtra, Selected};
use stremio_core::models::common::Loadable;
use stremio_core::models::ctx::Ctx;
use stremio_core::types::resource::PosterShape;
use wasm_bindgen::JsValue;

mod model {
use crate::model::deep_links_ext::DeepLinksExt;

use super::*;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ManifestPreview<'a> {
pub id: &'a str,
pub name: &'a str,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DescriptorPreview<'a> {
pub manifest: ManifestPreview<'a>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MetaItemPreview<'a> {
#[serde(flatten)]
pub meta_item: &'a stremio_core::types::resource::MetaItemPreview,
pub poster_shape: &'a PosterShape,
pub watched: bool,
pub deep_links: MetaItemDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceLoadable<'a> {
pub id: String,
pub name: String,
pub r#type: String,
pub addon: DescriptorPreview<'a>,
pub content: Option<Loadable<Vec<MetaItemPreview<'a>>, String>>,
pub deep_links: DiscoverDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CatalogsWithExtra<'a> {
pub selected: &'a Option<Selected>,
pub catalogs: Vec<ResourceLoadable<'a>>,
}
pub use stremio_core::{
deep_links::{DiscoverDeepLinks, MetaItemDeepLinks},
models::{catalogs_with_extra::Selected, common::Loadable, ctx::Ctx},
types::resource::PosterShape,
};

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ManifestPreview<'a> {
pub id: &'a str,
pub name: &'a str,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DescriptorPreview<'a> {
pub manifest: ManifestPreview<'a>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MetaItemPreview<'a> {
#[serde(flatten)]
pub meta_item: &'a stremio_core::types::resource::MetaItemPreview,
pub poster_shape: &'a PosterShape,
pub watched: bool,
pub deep_links: MetaItemDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceLoadable<'a> {
pub id: String,
pub name: String,
pub r#type: String,
pub addon: DescriptorPreview<'a>,
pub content: Option<Loadable<Vec<MetaItemPreview<'a>>, String>>,
pub deep_links: DiscoverDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CatalogsWithExtra<'a> {
pub selected: &'a Option<Selected>,
pub catalogs: Vec<ResourceLoadable<'a>>,
}

pub fn serialize_catalogs_with_extra(
catalogs_with_extra: &CatalogsWithExtra,
ctx: &Ctx,
) -> JsValue {
<JsValue as JsValueSerdeExt>::from_serde(&model::CatalogsWithExtra {
selected: &catalogs_with_extra.selected,
catalogs: catalogs_with_extra
.catalogs
.iter()
.filter_map(|catalog| catalog.first())
.filter_map(|catalog| {
ctx.profile
.addons
.iter()
.find(|addon| addon.transport_url == catalog.request.base)
.and_then(|addon| {
addon
.manifest
.catalogs
.iter()
.find(|manifest_catalog| {
manifest_catalog.id == catalog.request.path.id
&& manifest_catalog.r#type == catalog.request.path.r#type
})
.map(|manifest_catalog| (addon, manifest_catalog, catalog))
})
})
.map(
|(addon, manifest_catalog, catalog)| model::ResourceLoadable {
impl<'a> CatalogsWithExtra<'a> {
pub fn new(
catalogs_with_extra: &'a stremio_core::models::catalogs_with_extra::CatalogsWithExtra,
ctx: &'a Ctx,
) -> Self {
Self {
selected: &catalogs_with_extra.selected,
catalogs: catalogs_with_extra
.catalogs
.iter()
.filter_map(|catalog| catalog.first())
.filter_map(|catalog| {
ctx.profile
.addons
.iter()
.find(|addon| addon.transport_url == catalog.request.base)
.and_then(|addon| {
addon
.manifest
.catalogs
.iter()
.find(|manifest_catalog| {
manifest_catalog.id == catalog.request.path.id
&& manifest_catalog.r#type == catalog.request.path.r#type
})
.map(|manifest_catalog| (addon, manifest_catalog, catalog))
})
})
.map(|(addon, manifest_catalog, catalog)| ResourceLoadable {
id: manifest_catalog.id.to_string(),
name: manifest_catalog
.name
.as_ref()
.unwrap_or(&addon.manifest.name)
.to_string(),
r#type: manifest_catalog.r#type.to_string(),
addon: model::DescriptorPreview {
manifest: model::ManifestPreview {
addon: DescriptorPreview {
manifest: ManifestPreview {
id: &addon.manifest.id,
name: &addon.manifest.name,
},
Expand All @@ -101,7 +99,7 @@ pub fn serialize_catalogs_with_extra(
.iter()
.unique_by(|meta_item| &meta_item.id)
.take(10)
.map(|meta_item| model::MetaItemPreview {
.map(|meta_item| crate::model::MetaItemPreview {
meta_item,
poster_shape: poster_shape
.unwrap_or(&meta_item.poster_shape),
Expand All @@ -125,9 +123,42 @@ pub fn serialize_catalogs_with_extra(
None => None,
},
deep_links: DiscoverDeepLinks::from(&catalog.request).into_web_deep_links(),
},
)
.collect::<Vec<_>>(),
})
.expect("JsValue from model::CatalogsWithExtra")
})
.collect::<Vec<_>>(),
}
}
}

pub struct AppleKotlinStruct {}

pub trait SerializeModel<Out> {
type Error;

fn serialize_model(&self) -> Result<Out, Self::Error>;
}

impl<'a> SerializeModel<JsValue> for CatalogsWithExtra<'a> {
type Error = serde_json::Error;
// type Out = JsValue;

fn serialize_model(&self) -> Result<JsValue, Self::Error> {
JsValue::try_from(self)
}
}

impl<'a> SerializeModel<AppleKotlinStruct> for CatalogsWithExtra<'a> {
type Error = serde_json::Error;
// type Out = JsValue;

fn serialize_model(&self) -> Result<AppleKotlinStruct, Self::Error> {
Ok(AppleKotlinStruct {})
}
}

impl<'a> TryFrom<&CatalogsWithExtra<'a>> for JsValue {
type Error = serde_json::Error;

fn try_from(catalogs: &CatalogsWithExtra<'a>) -> Result<Self, Self::Error> {
<JsValue as JsValueSerdeExt>::from_serde(&catalogs)
}
}
Loading