Skip to content

Commit

Permalink
Merge pull request #514 from Stremio/feat/track-activate-and-deactivate
Browse files Browse the repository at this point in the history
feat: trakt uninstall addon on log out
  • Loading branch information
elpiel authored Sep 8, 2023
2 parents 96636cd + 56e7666 commit d1a6b71
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/models/ctx/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct Ctx {
#[derivative(Default(value = "CtxStatus::Ready"))]
pub status: CtxStatus,
#[serde(skip)]
/// Used only for loading the Descriptor and then the descriptor will be discarded
pub trakt_addon: Option<DescriptorLoadable>,
#[serde(skip)]
pub notification_catalogs: Vec<ResourceLoadable<Vec<MetaItem>>>,
Expand Down
8 changes: 7 additions & 1 deletion src/models/ctx/update_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ pub fn update_profile<E: Env + 'static>(
Msg::Action(Action::Ctx(ActionCtx::InstallAddon(addon))) => {
Effects::msg(Msg::Internal(Internal::InstallAddon(addon.to_owned()))).unchanged()
}
Msg::Action(Action::Ctx(ActionCtx::UninstallAddon(addon))) => {
Effects::msg(Msg::Internal(Internal::UninstallAddon(addon.to_owned()))).unchanged()
}
Msg::Action(Action::Ctx(ActionCtx::UpgradeAddon(addon))) => {
if profile.addons.contains(addon) {
return addon_upgrade_error_effects(addon, OtherError::AddonAlreadyInstalled);
Expand Down Expand Up @@ -147,7 +150,7 @@ pub fn update_profile<E: Env + 'static>(
.join(push_to_api_effects)
.join(Effects::msg(Msg::Internal(Internal::ProfileChanged)))
}
Msg::Action(Action::Ctx(ActionCtx::UninstallAddon(addon))) => {
Msg::Internal(Internal::UninstallAddon(addon)) => {
let addon_position = profile
.addons
.iter()
Expand Down Expand Up @@ -183,8 +186,11 @@ pub fn update_profile<E: Env + 'static>(
user.trakt = None;
let push_to_api_effects =
Effects::one(push_user_to_api::<E>(user.to_owned(), key));

Effects::msg(Msg::Event(Event::TraktLoggedOut { uid: profile.uid() }))
.join(push_to_api_effects)
// first uninstall the trakt addon
.join(Effects::msg(Msg::Internal(Internal::UninstallTraktAddon)))
.join(Effects::msg(Msg::Internal(Internal::ProfileChanged)))
} else {
Effects::msg(Msg::Event(Event::TraktLoggedOut { uid: profile.uid() }))
Expand Down
42 changes: 36 additions & 6 deletions src/models/ctx/update_trakt_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ use crate::types::profile::Profile;
use percent_encoding::utf8_percent_encode;
use url::Url;

pub fn build_track_addon_url(uid: &str) -> Result<Url, url::ParseError> {
Url::parse(&format!(
"https://www.strem.io/trakt/addon/{}/manifest.json",
utf8_percent_encode(uid, URI_COMPONENT_ENCODE_SET)
))
}

pub fn update_trakt_addon<E: Env + 'static>(
trakt_addon: &mut Option<DescriptorLoadable>,
profile: &Profile,
Expand All @@ -20,16 +27,16 @@ pub fn update_trakt_addon<E: Env + 'static>(
eq_update(trakt_addon, None)
}
Msg::Action(Action::Ctx(ActionCtx::InstallTraktAddon)) => {
Effects::msg(Msg::Internal(Internal::InstallTraktAddon))
}
Msg::Internal(Internal::InstallTraktAddon) => {
let uid = profile.uid();
match uid {
Some(uid) => descriptor_update::<E>(
trakt_addon,
DescriptorAction::DescriptorRequested {
transport_url: &Url::parse(&format!(
"https://www.strem.io/trakt/addon/{}/manifest.json",
utf8_percent_encode(&uid, URI_COMPONENT_ENCODE_SET)
))
.expect("Failed to parse trakt addon transport url"),
transport_url: &build_track_addon_url(&uid)
.expect("Failed to parse trakt addon transport url"),
},
),
_ => Effects::msg(Msg::Event(Event::Error {
Expand All @@ -39,6 +46,25 @@ pub fn update_trakt_addon<E: Env + 'static>(
.unchanged(),
}
}
Msg::Internal(Internal::UninstallTraktAddon) => {
// make sure we uninstall the addon from the user profile too!
let trakt_uninstall_effects = match profile.uid() {
Some(uid) => {
match profile.addons.iter().find(|addon| {
addon.transport_url
== build_track_addon_url(&uid).expect("Should be valid url")
}) {
Some(trakt_addon) => Effects::msg(Msg::Internal(Internal::UninstallAddon(
trakt_addon.clone(),
))),
None => Effects::none().unchanged(),
}
}
None => Effects::none().unchanged(),
};

trakt_uninstall_effects.join(eq_update(trakt_addon, None))
}
Msg::Internal(Internal::ManifestRequestResult(transport_url, result)) => {
let trakt_addon_effects = descriptor_update::<E>(
trakt_addon,
Expand All @@ -47,6 +73,7 @@ pub fn update_trakt_addon<E: Env + 'static>(
result,
},
);

let trakt_addon_events_effects = match trakt_addon {
Some(DescriptorLoadable {
content: Loadable::Ready(addon),
Expand All @@ -73,7 +100,10 @@ pub fn update_trakt_addon<E: Env + 'static>(
(CtxStatus::Loading(loading_auth_request), Ok(_))
if loading_auth_request == auth_request =>
{
eq_update(trakt_addon, None)
// clear the trakt_addon from Ctx when a new Auth request has been initialised
*trakt_addon = None;

Effects::none().unchanged()
}
_ => Effects::none().unchanged(),
},
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ pub enum Internal {
LibraryPullResult(DatastoreRequest, Result<Vec<LibraryItem>, CtxError>),
/// Dispatched when expired session is detected
Logout,
/// Internal event dispatched on user action or login
/// to install the addon if it's not present
InstallTraktAddon,
/// Dispatched when addons needs to be installed.
InstallAddon(Descriptor),
/// Dispatched when addons needs to be uninstalled.
UninstallAddon(Descriptor),
UninstallTraktAddon,
/// Dispatched when a new stream is loaded into the Player.
StreamLoaded {
stream: Stream,
Expand Down
3 changes: 3 additions & 0 deletions src/types/profile/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ impl Profile {
pub fn auth_key(&self) -> Option<&AuthKey> {
self.auth.as_ref().map(|auth| &auth.key)
}

/// check whether the user has Trakt authentication token
/// will return `false` if the token has expired
pub fn has_trakt<E: Env>(&self) -> bool {
self.auth
.as_ref()
Expand Down

0 comments on commit d1a6b71

Please sign in to comment.