Skip to content

Commit

Permalink
resolve suggested stream in meta details model
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBeastLT committed Sep 26, 2023
1 parent c23a026 commit 1d88e85
Showing 1 changed file with 100 additions and 4 deletions.
104 changes: 100 additions & 4 deletions src/models/meta_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::types::api::{DatastoreCommand, DatastoreRequest};
use crate::types::library::{LibraryBucket, LibraryItem};
use crate::types::profile::Profile;
use crate::types::resource::{MetaItem, Stream};
use crate::types::streams::{StreamsBucket, StreamsItemKey};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::marker::PhantomData;
Expand All @@ -30,6 +31,7 @@ pub struct MetaDetails {
pub meta_items: Vec<ResourceLoadable<MetaItem>>,
pub meta_streams: Vec<ResourceLoadable<Vec<Stream>>>,
pub streams: Vec<ResourceLoadable<Vec<Stream>>>,
pub suggested_stream: Option<Stream>,
pub library_item: Option<LibraryItem>,
#[serde(skip_serializing)]
pub watched: Option<WatchedBitField>,
Expand All @@ -48,6 +50,14 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let streams_effects =
streams_update::<E>(&mut self.streams, &self.selected, &ctx.profile);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
&self.streams,
&ctx.streams,
);
let library_item_effects = library_item_update::<E>(
&mut self.library_item,
&self.selected,
Expand All @@ -63,6 +73,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
.join(meta_items_effects)
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand All @@ -72,11 +83,13 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
let meta_streams_effects = eq_update(&mut self.meta_streams, vec![]);
let streams_effects = eq_update(&mut self.streams, vec![]);
let library_item_effects = eq_update(&mut self.library_item, None);
let suggested_stream_effects = eq_update(&mut self.suggested_stream, None);
let watched_effects = eq_update(&mut self.watched, None);
selected_effects
.join(meta_items_effects)
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand Down Expand Up @@ -137,6 +150,14 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
};
let meta_streams_effects =
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
&self.streams,
&ctx.streams,
);
let library_item_effects = library_item_update::<E>(
&mut self.library_item,
&self.selected,
Expand All @@ -149,6 +170,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
.join(meta_items_effects)
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand Down Expand Up @@ -178,6 +200,14 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let streams_effects =
streams_update::<E>(&mut self.streams, &self.selected, &ctx.profile);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
&self.streams,
&ctx.streams,
);
let library_item_effects = library_item_update::<E>(
&mut self.library_item,
&self.selected,
Expand All @@ -189,6 +219,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
meta_items_effects
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand Down Expand Up @@ -219,10 +250,7 @@ fn selected_override_update(
meta_items: &[ResourceLoadable<MetaItem>],
) -> Effects {
let meta_path = match &selected {
Some(Selected {
meta_path,
stream_path: None,
}) => meta_path,
Some(Selected { meta_path, .. }) => meta_path,
_ => return Effects::default(),
};
let meta_item = match meta_items
Expand Down Expand Up @@ -355,6 +383,74 @@ fn streams_update<E: Env + 'static>(
}
}

fn suggested_stream_update(
suggested_stream: &mut Option<Stream>,
selected: &Option<Selected>,
meta_items: &[ResourceLoadable<MetaItem>],
meta_streams: &[ResourceLoadable<Vec<Stream>>],
streams: &[ResourceLoadable<Vec<Stream>>],
stream_bucket: &StreamsBucket,
) -> Effects {
let next_suggested_stream = match selected {
Some(Selected {
meta_path,
stream_path: Some(stream_path),
}) => {
meta_items
.iter()
.find_map(|meta_item| match &meta_item.content {
Some(Loadable::Ready(meta_item)) => Some(&meta_item.videos),
_ => None,
})
.and_then(|videos| {
// Check saved stream only for last 30 videos starting from the current video
videos
.iter()
.position(|video| video.id == stream_path.id)
.and_then(|max_index| {
videos[max_index.saturating_sub(30)..=max_index]
.iter()
.rev()
.find_map(|video| {
stream_bucket.items.get(&StreamsItemKey {
meta_id: meta_path.id.to_string(),
video_id: video.id.to_owned(),
})
})
})
})
.and_then(|stream_item| {
[meta_streams, streams]
.concat()
.iter()
.find(|resource| resource.request.base == stream_item.stream_transport_url)
.and_then(|resource| match &resource.content {
Some(Loadable::Ready(streams)) => Some(streams),
_ => None,
})
.and_then(|streams| {
streams
.iter()
.find(|stream| *stream == &stream_item.stream)
.or_else(|| {
streams.iter().find(|stream| {
stream.behavior_hints.binge_group.as_deref()
== stream_item
.stream
.behavior_hints
.binge_group
.as_deref()
})
})
.cloned()
})
})
}
_ => None,
};
eq_update(suggested_stream, next_suggested_stream)
}

fn library_item_update<E: Env + 'static>(
library_item: &mut Option<LibraryItem>,
selected: &Option<Selected>,
Expand Down

0 comments on commit 1d88e85

Please sign in to comment.