diff --git a/stremio-core-web/src/model/serialize_player.rs b/stremio-core-web/src/model/serialize_player.rs index 2d7c56289..b6326ca5d 100644 --- a/stremio-core-web/src/model/serialize_player.rs +++ b/stremio-core-web/src/model/serialize_player.rs @@ -49,7 +49,7 @@ mod model { pub video: &'a stremio_core::types::resource::Video, pub upcoming: bool, pub watched: bool, - pub progress: Option, + pub progress: Option, pub scheduled: bool, pub deep_links: VideoDeepLinks, } @@ -134,30 +134,56 @@ pub fn serialize_player( .map(|ResourceLoadable { request, content }| match &content { Some(Loadable::Loading) | None => Loadable::Loading, Some(Loadable::Err(error)) => Loadable::Err(error), - Some(Loadable::Ready(meta_item)) => { - Loadable::Ready(model::MetaItem { - meta_item, - videos: meta_item - .videos - .iter() - .map(|video| model::Video { + Some(Loadable::Ready(meta_item)) => Loadable::Ready(model::MetaItem { + meta_item, + videos: meta_item + .videos + .iter() + .map(|video| model::Video { + video, + upcoming: meta_item.preview.behavior_hints.has_scheduled_videos + && video.released > Some(E::now()), + watched: ctx + .library + .items + .get(&meta_item.preview.id) + .map(|library_item| { + library_item + .state + .watched_bitfield(&meta_item.videos) + .get_video(&video.id) + }) + .unwrap_or_default(), + // only the currently playing video can have the progress + // as we keep that information in the LibraryItem + progress: ctx.library.items.get(&meta_item.preview.id).and_then( + |library_item| { + // only set up the progress for the current video + // for series, the selected stream path ID should be the video id! + if player + .selected + .as_ref() + .and_then(|selected| selected.stream_request.as_ref()) + .map(|stream_request| stream_request.path.id == video.id) + .unwrap_or_default() + { + Some(library_item.progress()) + } else { + None + } + }, + ), + scheduled: meta_item.preview.behavior_hints.has_scheduled_videos, + deep_links: VideoDeepLinks::from(( video, - upcoming: meta_item.preview.behavior_hints.has_scheduled_videos - && video.released > Some(E::now()), - watched: false, // TODO use library - progress: None, // TODO use library, - scheduled: meta_item.preview.behavior_hints.has_scheduled_videos, - deep_links: VideoDeepLinks::from(( - video, - request, - &streaming_server.base_url, - &ctx.profile.settings, - )) - .into_web_deep_links(), - }) - .collect(), - }) - } + request, + &streaming_server.base_url, + &ctx.profile.settings, + )) + .into_web_deep_links(), + }) + .collect(), + }), }), subtitles: player .subtitles @@ -214,8 +240,35 @@ pub fn serialize_player( && video.released > Some(E::now()) }) .unwrap_or_default(), - watched: false, // TODO use library - progress: None, // TODO use library, + watched: player + .meta_item + .as_ref() + .and_then(|meta_item| match meta_item { + ResourceLoadable { + content: Some(Loadable::Ready(meta_item)), + .. + } => Some(meta_item), + _ => None, + }) + .and_then(|meta_item| { + ctx.library + .items + .get(&meta_item.preview.id) + .map(|library_item| { + library_item + .state + .watched_bitfield(&meta_item.videos) + .get_video(&video.id) + }) + }) + .unwrap_or_default(), + // We do not have information about other videos in the LibraryItem + // apart from the currently playing one. + // We could eventually use e.g. StreamsBucket to get local streams + // and match the next video with existing stream, however, we only use this next_video + // for generating the Deep links + // Will always be None! + progress: None, scheduled: player .meta_item .as_ref()