This repository has been archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserialize_discover.rs
204 lines (202 loc) · 8.03 KB
/
serialize_discover.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use crate::model::deep_links_ext::DeepLinksExt;
use boolinator::Boolinator;
use serde::Serialize;
use stremio_core::deep_links::{DiscoverDeepLinks, MetaItemDeepLinks, StreamDeepLinks};
use stremio_core::models::catalog_with_filters::{
CatalogWithFilters, Selected as CatalogWithFiltersSelected,
};
use stremio_core::models::common::Loadable;
use stremio_core::models::ctx::Ctx;
use stremio_core::types::resource::MetaItemPreview;
use wasm_bindgen::JsValue;
mod model {
use super::*;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ManifestPreview<'a> {
pub name: &'a String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DescriptorPreview<'a> {
pub manifest: ManifestPreview<'a>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectableExtraOption<'a> {
pub value: &'a Option<String>,
pub selected: &'a bool,
pub deep_links: DiscoverDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectableExtra<'a> {
pub name: &'a String,
pub is_required: &'a bool,
pub options: Vec<SelectableExtraOption<'a>>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectableCatalog<'a> {
pub id: &'a String,
pub name: &'a String,
pub addon: DescriptorPreview<'a>,
pub selected: &'a bool,
pub deep_links: DiscoverDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectableType<'a> {
pub r#type: &'a String,
pub selected: &'a bool,
pub deep_links: DiscoverDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Selectable<'a> {
pub types: Vec<SelectableType<'a>>,
pub catalogs: Vec<SelectableCatalog<'a>>,
pub extra: Vec<SelectableExtra<'a>>,
pub next_page: bool,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Stream<'a> {
#[serde(flatten)]
pub stream: &'a stremio_core::types::resource::Stream,
pub deep_links: StreamDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MetaItemPreview<'a> {
#[serde(flatten)]
pub meta_item: &'a stremio_core::types::resource::MetaItemPreview,
pub trailer_streams: Vec<Stream<'a>>,
pub in_library: bool,
pub deep_links: MetaItemDeepLinks,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceLoadable<'a> {
pub content: Loadable<Vec<MetaItemPreview<'a>>, String>,
pub installed: bool,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CatalogWithFilters<'a> {
pub selected: &'a Option<CatalogWithFiltersSelected>,
pub selectable: Selectable<'a>,
pub catalog: Option<ResourceLoadable<'a>>,
}
}
pub fn serialize_discover(discover: &CatalogWithFilters<MetaItemPreview>, ctx: &Ctx) -> JsValue {
JsValue::from_serde(&model::CatalogWithFilters {
selected: &discover.selected,
selectable: model::Selectable {
types: discover
.selectable
.types
.iter()
.map(|selectable_type| model::SelectableType {
r#type: &selectable_type.r#type,
selected: &selectable_type.selected,
deep_links: DiscoverDeepLinks::from(&selectable_type.request)
.into_web_deep_links(),
})
.collect(),
catalogs: discover
.selectable
.catalogs
.iter()
.filter_map(|selectable_catalog| {
ctx.profile
.addons
.iter()
.find(|addon| addon.transport_url == selectable_catalog.request.base)
.map(|addon| (addon, selectable_catalog))
})
.map(|(addon, selectable_catalog)| model::SelectableCatalog {
id: &selectable_catalog.request.path.id,
name: &selectable_catalog.catalog,
addon: model::DescriptorPreview {
manifest: model::ManifestPreview {
name: &addon.manifest.name,
},
},
selected: &selectable_catalog.selected,
deep_links: DiscoverDeepLinks::from(&selectable_catalog.request)
.into_web_deep_links(),
})
.collect(),
extra: discover
.selectable
.extra
.iter()
.map(|selectable_extra| model::SelectableExtra {
name: &selectable_extra.name,
is_required: &selectable_extra.is_required,
options: selectable_extra
.options
.iter()
.map(|option| model::SelectableExtraOption {
value: &option.value,
selected: &option.selected,
deep_links: DiscoverDeepLinks::from(&option.request)
.into_web_deep_links(),
})
.collect(),
})
.collect(),
next_page: discover.selectable.next_page.is_some(),
},
catalog: (!discover.catalog.is_empty()).as_option().map(|_| {
let first_page = discover.catalog.first().unwrap();
model::ResourceLoadable {
content: match &first_page.content {
Some(Loadable::Ready(_)) => Loadable::Ready(
discover
.catalog
.iter()
.filter_map(|page| page.content.as_ref())
.filter_map(|page_content| page_content.ready())
.flat_map(|meta_items| {
meta_items.iter().map(|meta_item| model::MetaItemPreview {
meta_item,
trailer_streams: meta_item
.trailer_streams
.iter()
.take(1)
.map(|stream| model::Stream {
stream,
deep_links: StreamDeepLinks::from(stream)
.into_web_deep_links(),
})
.collect::<Vec<_>>(),
in_library: ctx
.library
.items
.get(&meta_item.id)
.map(|library_item| !library_item.removed)
.unwrap_or_default(),
deep_links: MetaItemDeepLinks::from((
meta_item,
&first_page.request,
))
.into_web_deep_links(),
})
})
.collect::<Vec<_>>(),
),
Some(Loadable::Loading) | None => Loadable::Loading,
Some(Loadable::Err(error)) => Loadable::Err(error.to_string()),
},
installed: ctx
.profile
.addons
.iter()
.any(|addon| addon.transport_url == first_page.request.base),
}
}),
})
.unwrap()
}