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: Notifications with Continue watching #423

Draft
wants to merge 6 commits into
base: development
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub const LIBRARY_STORAGE_KEY: &str = "library";
pub const LIBRARY_RECENT_STORAGE_KEY: &str = "library_recent";
pub const LIBRARY_COLLECTION_NAME: &str = "libraryItem";
pub const SEARCH_EXTRA_NAME: &str = "search";
/// `https://{ADDON_UR}/meta/...` resource
pub const META_RESOURCE_NAME: &str = "meta";
pub const STREAM_RESOURCE_NAME: &str = "stream";
/// `https://{ADDON_URL}/catalog/...` resource
pub const CATALOG_RESOURCE_NAME: &str = "catalog";
pub const SUBTITLES_RESOURCE_NAME: &str = "subtitles";
pub const ADDON_MANIFEST_PATH: &str = "/manifest.json";
Expand Down Expand Up @@ -47,6 +49,7 @@ lazy_static! {
pub static ref STREAMING_SERVER_URL: Url =
Url::parse("http://127.0.0.1:11470").expect("STREAMING_SERVER_URL parse failed");
pub static ref IMDB_URL: Url = Url::parse("https://imdb.com").expect("IMDB_URL parse failed");
/// Meta hub url used to show background images for Notifications.
pub static ref OFFICIAL_ADDONS: Vec<Descriptor> =
serde_json::from_slice(stremio_official_addons::ADDONS)
.expect("OFFICIAL_ADDONS parse failed");
Expand Down
89 changes: 63 additions & 26 deletions src/models/notifications.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
use crate::models::common::{resources_update, Loadable, ResourceLoadable, ResourcesAction};
use crate::models::ctx::Ctx;
use crate::runtime::msg::Internal::*;
use crate::runtime::msg::*;
use crate::runtime::*;
use crate::types::addon::{ExtraValue, ResourcePath, ResourceRequest};
use crate::types::resource::MetaItem;
use crate::{
constants::{CATALOG_RESOURCE_NAME, URI_COMPONENT_ENCODE_SET},
models::{
common::{resources_update, Loadable, ResourceLoadable, ResourcesAction},
ctx::Ctx,
},
runtime::{
msg::{Action, ActionLoad, Internal, Msg},
Effect, EffectFuture, Effects, Env, UpdateWithCtx, EnvFutureExt,
},
types::{
addon::{ExtraValue, ResourcePath, ResourceRequest},
resource::MetaItem,
},
};

use futures::FutureExt;
use lazysort::SortedBy;
use serde::*;
use percent_encoding::utf8_percent_encode;
use serde::Serialize;

// Cinemeta/Channels are curently limited to that many
// but in general, it's healthy to have some sort of a limit
/// Cinemeta/Channels are currently limited to that many
/// but in general, it's healthy to have some sort of a limit
const MAX_PER_REQUEST: usize = 50;

// The name of the extra property
const LAST_VID_IDS: &str = "lastVideosIds";
const EXTRA_LAST_VIDEOS_IDS: &str = "lastVideosIds";

/// The last videos catalog id should be `last-videos`
///
/// See [ManifestCatalog.id](crate::types::addon::ManifestCatalog.id)
const LIST_VIDEOS_CATALOG_ID: &str = "last-videos";

/// Notifications for new video for [`LibraryItem`]s with videos
/// (i.e. movie series with new episodes).
///
/// [`LibraryItem`]: crate::types::library::LibraryItem
#[derive(Default, Serialize)]
pub struct Notifications {
/// each addon has it's own group
pub groups: Vec<ResourceLoadable<Vec<MetaItem>>>,
}

impl<E: Env + 'static> UpdateWithCtx<E> for Notifications {
fn update(&mut self, msg: &Msg, ctx: &Ctx) -> Effects {
match msg {
Expand All @@ -29,13 +51,16 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Notifications {
.profile
.addons
.iter()
.filter(|addon| {
// skip the addon if it does not support `new_episode_notifications`
addon.manifest.behavior_hints.new_episode_notifications
})
.flat_map(|addon| {
// The catalog supports this property
let viable_catalogs = addon
.manifest
.catalogs
.iter()
.filter(|cat| cat.extra.iter().any(|e| e.name == LAST_VID_IDS));
// The catalogs that support the `lastVideosIds` property
let viable_catalogs = addon.manifest.catalogs.iter().filter(|cat| {

cat.extra.iter().any(|e| e.name == EXTRA_LAST_VIDEOS_IDS)
});

viable_catalogs.flat_map(move |cat| {
let relevant_items = library
Expand All @@ -46,12 +71,12 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Notifications {
.filter(|item| {
!item.state.no_notif
&& !item.removed
&& cat.r#type == item.r#type
&& cat.r#type == item.r#type // for example `series`
&& addon.manifest.is_resource_supported(
&ResourcePath::without_extra(
"meta",
CATALOG_RESOURCE_NAME,
&item.r#type,
&item.id,
&LIST_VIDEOS_CATALOG_ID,
),
)
})
Expand All @@ -63,14 +88,26 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Notifications {
relevant_items
.chunks(MAX_PER_REQUEST)
.map(|items_page| -> (_, Effect) {
let ids =
items_page.iter().map(|x| x.id.clone()).collect::<Vec<_>>();
let ordered_ids = {
let mut ids = items_page
.iter()
.map(|x| x.id.as_str())
.collect::<Vec<_>>();
// sort the ids alphabetically
ids.sort_unstable();
ids
};
let extra_props = [ExtraValue {
name: LAST_VID_IDS.into(),
value: ids.join(","),
name: EXTRA_LAST_VIDEOS_IDS.into(),
value: utf8_percent_encode(
&ordered_ids.join(","),
URI_COMPONENT_ENCODE_SET,
)
.to_string(),
}];

let path = ResourcePath::with_extra(
"catalog",
CATALOG_RESOURCE_NAME,
&cat.r#type,
&cat.id,
&extra_props,
Expand Down Expand Up @@ -105,7 +142,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Notifications {
self.groups = groups;
Effects::many(effects)
}
Msg::Internal(ResourceRequestResult(req, result)) => {
Msg::Internal(Internal::ResourceRequestResult(req, result)) => {
if let Some(idx) = self.groups.iter().position(|g| g.request == *req) {
resources_update::<E, _>(
&mut self.groups,
Expand Down
31 changes: 31 additions & 0 deletions src/types/addon/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,33 @@ impl Default for OptionsLimit {
}
}

///
/// # Examples
///
/// ```
/// use stremio_core::types::addon::ManifestBehaviorHints;
///
/// use serde_json::{from_value, json};
///
/// let json = json!({
/// "adult": true,
/// "p2p": true,
/// "configurable": true,
/// "configuration_required": true,
/// "new_episode_notifications": true,
/// "unknown_property": "unknown property data",
/// });
///
/// let actual = from_value::<ManifestBehaviorHints>(json).expect("Should deserialize");
///
/// let expected = ManifestBehaviorHints {
/// adult: true,
/// p2p: true,
/// configurable: true,
/// configuration_required: true,
/// new_episode_notifications: true,
/// };
/// ```
#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ManifestBehaviorHints {
Expand All @@ -285,4 +312,8 @@ pub struct ManifestBehaviorHints {
pub configurable: bool,
#[serde(default)]
pub configuration_required: bool,
/// Whether or not the add-on supports notifications for new episodes.
// TODO: Define the new catalog for Add-ons
#[serde(default)]
pub new_episode_notifications: bool,
}
4 changes: 4 additions & 0 deletions src/unit_tests/serde/manifest_behavior_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn manifest_behavior_hints() {
p2p: true,
configurable: true,
configuration_required: true,
new_episode_notifications: true,
},
&[
Token::Struct {
Expand All @@ -23,6 +24,8 @@ fn manifest_behavior_hints() {
Token::Bool(true),
Token::Str("configurationRequired"),
Token::Bool(true),
Token::Str("newEpisodeNotifications"),
Token::Bool(true),
Token::StructEnd,
],
);
Expand All @@ -32,6 +35,7 @@ fn manifest_behavior_hints() {
p2p: false,
configurable: false,
configuration_required: false,
new_episode_notifications: false,
},
&[
Token::Struct {
Expand Down