Skip to content

Commit

Permalink
Merge pull request #540 from Stremio/feat/streams-loading-in-meta-det…
Browse files Browse the repository at this point in the history
…ails

feat: MetaDetails - guess streams only when a flag is present
  • Loading branch information
elpiel authored Oct 23, 2023
2 parents 0859574 + 3ee8b34 commit a8a24d0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
76 changes: 53 additions & 23 deletions src/models/meta_details.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
use crate::constants::{LIBRARY_COLLECTION_NAME, META_RESOURCE_NAME, STREAM_RESOURCE_NAME};
use crate::models::common::{
eq_update, resources_update, resources_update_with_vector_content, Loadable, ResourceLoadable,
ResourcesAction,
};
use crate::models::ctx::Ctx;
use crate::runtime::msg::{Action, ActionLoad, ActionMetaDetails, Internal, Msg};
use crate::runtime::{Effects, Env, UpdateWithCtx};
use crate::types::addon::{AggrRequest, ResourcePath, ResourceRequest};
use crate::types::api::{DatastoreCommand, DatastoreRequest};
use crate::types::library::{LibraryBucket, LibraryItem};
use crate::types::profile::Profile;
use crate::types::resource::{MetaItem, Stream};
use std::{borrow::Cow, marker::PhantomData};

use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::marker::PhantomData;

use stremio_watched_bitfield::WatchedBitField;

use crate::{
constants::{LIBRARY_COLLECTION_NAME, META_RESOURCE_NAME, STREAM_RESOURCE_NAME},
models::{
common::{
eq_update, resources_update, resources_update_with_vector_content, Loadable,
ResourceLoadable, ResourcesAction,
},
ctx::Ctx,
},
runtime::{
msg::{Action, ActionLoad, ActionMetaDetails, Internal, Msg},
Effects, Env, UpdateWithCtx,
},
types::{
addon::{AggrRequest, ResourcePath, ResourceRequest},
api::{DatastoreCommand, DatastoreRequest},
library::{LibraryBucket, LibraryItem},
profile::Profile,
resource::{MetaItem, Stream},
},
};

#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Selected {
pub meta_path: ResourcePath,
pub stream_path: Option<ResourcePath>,
#[serde(default)]
/// if `stream_path` is `None` then we try to guess the video and make a request
/// to the addons to load the streams for that video id
pub guess_stream: bool,
}

#[derive(Default, Serialize, Clone, Debug)]
Expand All @@ -43,7 +57,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
let meta_items_effects =
meta_items_update::<E>(&mut self.meta_items, &self.selected, &ctx.profile);
let selected_override_effects =
selected_override_update(&mut self.selected, &self.meta_items);
selected_guess_stream_update(&mut self.selected, &self.meta_items);
let meta_streams_effects =
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let streams_effects =
Expand Down Expand Up @@ -129,7 +143,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
ResourcesAction::ResourceRequestResult { request, result },
);
let selected_override_effects =
selected_override_update(&mut self.selected, &self.meta_items);
selected_guess_stream_update(&mut self.selected, &self.meta_items);
let streams_effects = if selected_override_effects.has_changed {
streams_update::<E>(&mut self.streams, &self.selected, &ctx.profile)
} else {
Expand Down Expand Up @@ -214,26 +228,39 @@ fn library_item_sync(library_item: &Option<LibraryItem>, profile: &Profile) -> E
}
}

fn selected_override_update(
/// If `Selected::guess_stream` is `true` then we will override the selected stream
/// no matter if it's set (`Some`) or not (`None`).
///
/// How we override the stream:
///
/// 1. We find the first `MetaItem` that's successfully loaded from the addons.
/// 2. Selecting the video id for the stream request:
/// 2.1 If there's a `MetaItem.preview.behavior_hints.default_video_id`
/// we use it for the request
/// 2.2 If there's no `default_video_id` and no `MetaItem.videos` returned by the addon,
/// we use the `MetaItem.preview.id`
///
/// If we haven't found a suitable `video_id`, then we do not override the `Selected::stream_path`.
fn selected_guess_stream_update(
selected: &mut Option<Selected>,
meta_items: &[ResourceLoadable<MetaItem>],
) -> Effects {
let meta_path = match &selected {
Some(Selected {
meta_path,
// guess the stream only if `stream_path` is `None`!
stream_path: None,
guess_stream: true,
}) => meta_path,
_ => return Effects::default(),
};
let meta_item = match meta_items
.iter()
.find_map(|meta_item| match &meta_item.content {
Some(Loadable::Ready(meta_item)) => Some(Some(meta_item)),
Some(Loadable::Loading) => Some(None),
Some(Loadable::Ready(meta_item)) => Some(meta_item),
Some(Loadable::Loading) => None,
_ => None,
})
.flatten()
{
}) {
Some(meta_item) => meta_item,
_ => return Effects::default(),
};
Expand All @@ -255,6 +282,9 @@ fn selected_override_update(
id: video_id,
extra: vec![],
}),
// we must set the `guess_stream` to `false` after we've overridden it
// to make it consistent
guess_stream: false,
}),
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/unit_tests/meta_details/override_selected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ fn override_selected_default_video_id() {
id: "tt1".to_owned(),
extra: vec![]
},
stream_path: None
stream_path: None,
guess_stream: true,
})),
});
}),
Expand Down Expand Up @@ -173,7 +174,8 @@ fn override_selected_meta_id() {
id: "tt1".to_owned(),
extra: vec![]
},
stream_path: None
stream_path: None,
guess_stream: true,
})),
});
}),
Expand Down

0 comments on commit a8a24d0

Please sign in to comment.