Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth on addons or library failed and lock addons if failed #634

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 1 addition & 45 deletions src/models/ctx/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::constants::LIBRARY_COLLECTION_NAME;
use crate::models::common::{DescriptorLoadable, Loadable, ResourceLoadable};
use crate::models::ctx::{
update_events, update_library, update_notifications, update_profile, update_search_history,
Expand All @@ -7,8 +6,7 @@ use crate::models::ctx::{
use crate::runtime::msg::{Action, ActionCtx, Event, Internal, Msg};
use crate::runtime::{Effect, EffectFuture, Effects, Env, EnvFutureExt, Update};
use crate::types::api::{
fetch_api, APIRequest, APIResult, AuthRequest, AuthResponse, CollectionResponse,
DatastoreCommand, DatastoreRequest, LibraryItemsResponse, SuccessResponse,
fetch_api, APIRequest, APIResult, AuthRequest, AuthResponse, SuccessResponse,
};
use crate::types::events::{DismissedEventsBucket, Events};
use crate::types::library::LibraryBucket;
Expand Down Expand Up @@ -240,48 +238,6 @@ fn authenticate<E: Env + 'static>(auth_request: &AuthRequest) -> Effect {
APIResult::Err { error } => future::err(CtxError::from(error)),
})
.map_ok(|AuthResponse { key, user }| Auth { key, user })
.and_then(|auth| {
let addon_collection_fut = {
let request = APIRequest::AddonCollectionGet {
auth_key: auth.key.to_owned(),
update: true,
};
fetch_api::<E, _, _, _>(&request)
.inspect(move |result| {
trace!(?result, ?request, "Get user's Addon Collection request")
})
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result),
APIResult::Err { error } => future::err(CtxError::from(error)),
})
.map_ok(|CollectionResponse { addons, .. }| addons)
};

let datastore_library_fut = {
let request = DatastoreRequest {
auth_key: auth.key.to_owned(),
collection: LIBRARY_COLLECTION_NAME.to_owned(),
command: DatastoreCommand::Get {
ids: vec![],
all: true,
},
};

fetch_api::<E, _, _, LibraryItemsResponse>(&request)
.inspect(move |result| {
trace!(?result, ?request, "Get user's Addon Collection request")
})
.map_err(CtxError::from)
.and_then(|result| match result {
APIResult::Ok { result } => future::ok(result.0),
APIResult::Err { error } => future::err(CtxError::from(error)),
})
};

future::try_join(addon_collection_fut, datastore_library_fut)
.map_ok(move |(addons, library_items)| (auth, addons, library_items))
})
.map(enclose!((auth_request) move |result| {
let internal_msg = Msg::Internal(Internal::CtxAuthResult(auth_request, result));

Expand Down
3 changes: 3 additions & 0 deletions src/models/ctx/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum OtherError {
AddonNotInstalled,
AddonIsProtected,
AddonConfigurationRequired,
AddonsAreLocked,
}

impl OtherError {
Expand All @@ -49,6 +50,7 @@ impl OtherError {
OtherError::AddonNotInstalled => "Addon is not installed".to_owned(),
OtherError::AddonIsProtected => "Addon is protected".to_owned(),
OtherError::AddonConfigurationRequired => "Addon requires configuration".to_owned(),
OtherError::AddonsAreLocked => "Addons are locked".to_owned(),
}
}
pub fn code(&self) -> u64 {
Expand All @@ -59,6 +61,7 @@ impl OtherError {
OtherError::AddonNotInstalled => 4,
OtherError::AddonIsProtected => 5,
OtherError::AddonConfigurationRequired => 6,
OtherError::AddonsAreLocked => 7,
}
}
}
Expand Down
32 changes: 14 additions & 18 deletions src/models/ctx/update_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ pub fn update_library<E: Env + 'static>(
let auth_key = profile.auth_key();
match msg {
Msg::Action(Action::Ctx(ActionCtx::Logout)) | Msg::Internal(Internal::Logout) => {
let next_library = LibraryBucket::default();
if *library != next_library {
*library = next_library;
Effects::msg(Msg::Internal(Internal::LibraryChanged(false)))
} else {
Effects::none().unchanged()
}
*library = LibraryBucket::default();
Effects::msg(Msg::Internal(Internal::LibraryChanged(false)))
}
Msg::Action(Action::Ctx(ActionCtx::AddToLibrary(meta_preview))) => {
let mut library_item = match library.items.get(&meta_preview.id) {
Expand Down Expand Up @@ -175,17 +170,13 @@ pub fn update_library<E: Env + 'static>(
Effects::one(push_library_to_storage::<E>(library)).unchanged()
}
Msg::Internal(Internal::CtxAuthResult(auth_request, result)) => match (status, result) {
(CtxStatus::Loading(loading_auth_request), Ok((auth, _, library_items)))
(CtxStatus::Loading(loading_auth_request), Ok(auth))
if loading_auth_request == auth_request =>
{
let next_library =
LibraryBucket::new(Some(auth.user.id.to_owned()), library_items.to_owned());
if *library != next_library {
*library = next_library;
Effects::msg(Msg::Internal(Internal::LibraryChanged(false)))
} else {
Effects::none().unchanged()
}
*library = LibraryBucket::new(Some(auth.user.id.to_owned()), vec![]);
let changed_effects = Effects::msg(Msg::Internal(Internal::LibraryChanged(false)));
let pull_effects = Effects::one(pull_items_from_api::<E>(vec![], true, &auth.key));
changed_effects.join(pull_effects)
}
_ => Effects::none().unchanged(),
},
Expand Down Expand Up @@ -214,6 +205,7 @@ pub fn update_library<E: Env + 'static>(
} else {
Effects::one(pull_items_from_api::<E>(
pull_ids.to_owned(),
false,
loading_auth_key,
))
.unchanged()
Expand Down Expand Up @@ -370,11 +362,15 @@ fn push_items_to_api<E: Env + 'static>(items: Vec<LibraryItem>, auth_key: &AuthK
.into()
}

fn pull_items_from_api<E: Env + 'static>(ids: Vec<String>, auth_key: &AuthKey) -> Effect {
fn pull_items_from_api<E: Env + 'static>(
ids: Vec<String>,
all: bool,
auth_key: &AuthKey,
) -> Effect {
let request = DatastoreRequest {
auth_key: auth_key.to_owned(),
collection: LIBRARY_COLLECTION_NAME.to_owned(),
command: DatastoreCommand::Get { ids, all: false },
command: DatastoreCommand::Get { ids, all },
};
EffectFuture::Concurrent(
fetch_api::<E, _, _, LibraryItemsResponse>(&request)
Expand Down
Loading