-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #724 from Stremio/feat/streming-server-urls-bucket
feat: streaming-server urls bucket
- Loading branch information
Showing
33 changed files
with
378 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use futures::FutureExt; | ||
|
||
use crate::constants::STREAMING_SERVER_URLS_STORAGE_KEY; | ||
use crate::runtime::msg::{Action, ActionCtx, CtxAuthResponse}; | ||
use crate::runtime::{ | ||
msg::{Event, Internal, Msg}, | ||
Effect, EffectFuture, Effects, Env, EnvFutureExt, | ||
}; | ||
use crate::types::server_urls::ServerUrlsBucket; | ||
|
||
use super::{CtxError, CtxStatus}; | ||
|
||
pub fn update_streaming_server_urls<E: Env + 'static>( | ||
streaming_server_urls: &mut ServerUrlsBucket, | ||
status: &CtxStatus, | ||
msg: &Msg, | ||
) -> Effects { | ||
match msg { | ||
Msg::Action(Action::Ctx(ActionCtx::AddServerUrl(url))) => { | ||
streaming_server_urls.add_url::<E>(url.clone()); | ||
Effects::msg(Msg::Internal(Internal::StreamingServerUrlsBucketChanged)) | ||
} | ||
Msg::Action(Action::Ctx(ActionCtx::DeleteServerUrl(url))) => { | ||
streaming_server_urls.delete_url(url); | ||
Effects::msg(Msg::Internal(Internal::StreamingServerUrlsBucketChanged)) | ||
} | ||
Msg::Internal(Internal::CtxAuthResult(auth_request, result)) => match (status, result) { | ||
(CtxStatus::Loading(loading_auth_request), Ok(CtxAuthResponse { auth, .. })) | ||
if loading_auth_request == auth_request => | ||
{ | ||
let next_server_urls = ServerUrlsBucket::new::<E>(Some(auth.user.id.to_owned())); | ||
*streaming_server_urls = next_server_urls; | ||
Effects::msg(Msg::Internal(Internal::StreamingServerUrlsBucketChanged)) | ||
} | ||
_ => Effects::none().unchanged(), | ||
}, | ||
Msg::Internal(Internal::StreamingServerUrlsBucketChanged) => { | ||
Effects::one(push_server_urls_to_storage::<E>(streaming_server_urls)).unchanged() | ||
} | ||
_ => Effects::none().unchanged(), | ||
} | ||
} | ||
|
||
fn push_server_urls_to_storage<E: Env + 'static>( | ||
streaming_server_urls: &ServerUrlsBucket, | ||
) -> Effect { | ||
let uid: Option<String> = streaming_server_urls.uid.clone(); | ||
|
||
EffectFuture::Sequential( | ||
E::set_storage( | ||
STREAMING_SERVER_URLS_STORAGE_KEY, | ||
Some(streaming_server_urls), | ||
) | ||
.map(move |result| match result { | ||
Ok(_) => Msg::Event(Event::StreamingServerUrlsPushedToStorage { uid: uid.clone() }), | ||
Err(error) => Msg::Event(Event::Error { | ||
error: CtxError::from(error), | ||
source: Box::new(Event::StreamingServerUrlsPushedToStorage { uid: uid.clone() }), | ||
}), | ||
}) | ||
.boxed_env(), | ||
) | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod server_urls_bucket; | ||
pub use server_urls_bucket::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{constants::STREAMING_SERVER_URL, runtime::Env, types::profile::UID}; | ||
use chrono::{DateTime, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use url::Url; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)] | ||
pub struct ServerUrlsBucket { | ||
/// User ID | ||
pub uid: UID, | ||
/// A map of the the server Url as a key and the modified/added datetime of that particular url. | ||
pub items: HashMap<Url, DateTime<Utc>>, | ||
} | ||
|
||
impl ServerUrlsBucket { | ||
/// Create a new `ServerUrlsBucket` with the base URL inserted. | ||
pub fn new<E: Env + 'static>(uid: UID) -> Self { | ||
let mut items = HashMap::new(); | ||
let base_url: &Url = &STREAMING_SERVER_URL; | ||
let mtime = E::now(); | ||
items.insert(base_url.clone(), mtime); | ||
|
||
ServerUrlsBucket { uid, items } | ||
} | ||
|
||
/// Add a URL to the bucket. | ||
pub fn add_url<E: Env + 'static>(&mut self, url: Url) { | ||
let mtime = E::now(); | ||
self.items.insert(url, mtime); | ||
} | ||
|
||
/// Delete a URL from the bucket. | ||
pub fn delete_url(&mut self, url: &Url) { | ||
let default_url: &Url = &STREAMING_SERVER_URL; | ||
if url != default_url { | ||
self.items.remove(url); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.