Skip to content

Commit

Permalink
refactor: skip deleting session with a flag for internal logout message
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Dec 31, 2024
1 parent d312900 commit e0d777c
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 41 deletions.
9 changes: 7 additions & 2 deletions src/models/ctx/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,15 @@ impl<E: Env + 'static> Update<E> for Ctx {
self.status = CtxStatus::Loading(auth_request.to_owned());
Effects::one(authenticate::<E>(auth_request)).unchanged()
}
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Action(Action::Ctx(ActionCtx::Logout)) => {
Effects::msg(Msg::Internal(Internal::Logout(false))).unchanged()
}
Msg::Internal(Internal::Logout(deleted)) => {
let uid = self.profile.uid();
let session_effects = match self.profile.auth_key() {
Some(auth_key) => Effects::one(delete_session::<E>(auth_key)).unchanged(),
Some(auth_key) if !deleted => {
Effects::one(delete_session::<E>(auth_key)).unchanged()
}
_ => Effects::none().unchanged(),
};
let profile_effects =
Expand Down
2 changes: 1 addition & 1 deletion src/models/ctx/update_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn update_events<E: Env + 'static>(
msg: &Msg,
) -> Effects {
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Internal(Internal::Logout(_)) => {
let next_dismissed_events = DismissedEventsBucket::default();
*dismissed_events = next_dismissed_events;
Effects::msg(Msg::Internal(Internal::DismissedEventsChanged))
Expand Down
2 changes: 1 addition & 1 deletion src/models/ctx/update_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn update_library<E: Env + 'static>(
) -> Effects {
let auth_key = profile.auth_key();
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Internal(Internal::Logout(_)) => {
let next_library = LibraryBucket::default();
if *library != next_library {
*library = next_library;
Expand Down
2 changes: 1 addition & 1 deletion src/models/ctx/update_notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn update_notifications<E: Env + 'static>(
Msg::Internal(Internal::DismissNotificationItem(id.to_owned())),
)
.unchanged(),
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Internal(Internal::Logout(_)) => {
let notification_catalogs_effects = eq_update(notification_catalogs, vec![]);
let next_notifications = NotificationsBucket::new::<E>(profile.uid(), vec![]);
let notifications_effects = if *notifications != next_notifications {
Expand Down
6 changes: 3 additions & 3 deletions src/models/ctx/update_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn update_profile<E: Env + 'static>(
msg: &Msg,
) -> Effects {
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Internal(Internal::Logout(_)) => {
let next_profile = Profile::default();
if *profile != next_profile {
*profile = next_profile;
Expand Down Expand Up @@ -377,7 +377,7 @@ pub fn update_profile<E: Env + 'static>(
Err(error) => {
let session_expired_effects = match error {
CtxError::API(APIError { code, .. }) if *code == 1 => {
Effects::msg(Msg::Internal(Internal::Logout)).unchanged()
Effects::msg(Msg::Internal(Internal::Logout(false))).unchanged()
}
_ => Effects::none().unchanged(),
};
Expand All @@ -394,7 +394,7 @@ pub fn update_profile<E: Env + 'static>(
APIRequest::DeleteAccount { auth_key, .. },
result,
)) if profile.auth_key() == Some(auth_key) => match result {
Ok(_) => Effects::msg(Msg::Internal(Internal::Logout)).unchanged(),
Ok(_) => Effects::msg(Msg::Internal(Internal::Logout(true))).unchanged(),
Err(error) => Effects::msg(Msg::Event(Event::Error {
error: error.to_owned(),
source: Box::new(Event::UserAccountDeleted { uid: profile.uid() }),
Expand Down
2 changes: 1 addition & 1 deletion src/models/ctx/update_search_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn update_search_history<E: Env + 'static>(
msg: &Msg,
) -> Effects {
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Internal(Internal::Logout(_)) => {
let next_search_history = SearchHistoryBucket::default();
*search_history = next_search_history;
Effects::msg(Msg::Internal(Internal::SearchHistoryChanged))
Expand Down
4 changes: 2 additions & 2 deletions src/models/ctx/update_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::hash_map::Entry;
use crate::constants::STREAMS_STORAGE_KEY;
use crate::models::common::{Loadable, ResourceLoadable};
use crate::models::ctx::{CtxError, CtxStatus};
use crate::runtime::msg::{Action, ActionCtx, CtxAuthResponse, Event, Internal, Msg};
use crate::runtime::msg::{CtxAuthResponse, Event, Internal, Msg};
use crate::runtime::{Effect, EffectFuture, Effects, Env, EnvFutureExt};
use crate::types::streams::{StreamsBucket, StreamsItem, StreamsItemKey};

Expand All @@ -15,7 +15,7 @@ pub fn update_streams<E: Env + 'static>(
msg: &Msg,
) -> Effects {
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
Msg::Internal(Internal::Logout(_)) => {
let next_streams = StreamsBucket::default();
if *streams != next_streams {
*streams = next_streams;
Expand Down
4 changes: 1 addition & 3 deletions src/models/ctx/update_trakt_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ pub fn update_trakt_addon<E: Env + 'static>(
msg: &Msg,
) -> Effects {
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
eq_update(trakt_addon, None)
}
Msg::Internal(Internal::Logout(_)) => eq_update(trakt_addon, None),
Msg::Action(Action::Ctx(ActionCtx::InstallTraktAddon)) => {
Effects::msg(Msg::Internal(Internal::InstallTraktAddon))
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub enum Internal {
LibrarySyncPlanResult(DatastoreRequest, Result<LibraryPlanResponse, CtxError>),
/// Result for pull library items from API.
LibraryPullResult(DatastoreRequest, Result<Vec<LibraryItem>, CtxError>),
/// Dispatched when expired session is detected
Logout,
/// Dispatched when the user session needs to be cleared with a flag if the session was already deleted server-side
Logout(bool),
/// Internal event dispatched on user action or login
/// to install the addon if it's not present
InstallTraktAddon,
Expand Down
27 changes: 2 additions & 25 deletions src/unit_tests/ctx/delete_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ fn actionctx_delete_account() {
)
.boxed_env()
}
Request {
url, method, body, ..
} if url == "https://api.strem.io/api/logout"
&& method == "POST"
&& body == "{\"type\":\"Logout\",\"authKey\":\"auth_key\"}" =>
{
future::ok(
Box::new(APIResult::Ok(SuccessResponse { success: True {} }))
as Box<dyn Any + Send>,
)
.boxed_env()
}
_ => default_fetch_handler(request),
}
}
Expand Down Expand Up @@ -133,8 +121,8 @@ fn actionctx_delete_account() {

assert_eq!(
REQUESTS.read().unwrap().len(),
2,
"Two requests have been sent"
1,
"One request have been sent"
);

assert_eq!(
Expand All @@ -148,15 +136,4 @@ fn actionctx_delete_account() {
},
"Delete account request has been sent"
);

assert_eq!(
REQUESTS.read().unwrap().get(1).unwrap().to_owned(),
Request {
url: "https://api.strem.io/api/logout".to_owned(),
method: "POST".to_owned(),
body: "{\"type\":\"Logout\",\"authKey\":\"auth_key\"}".to_owned(),
..Default::default()
},
"Logout request has been sent"
);
}

0 comments on commit e0d777c

Please sign in to comment.