Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class InboxViewControllerContainer: TableNodeViewController {
}

private func fetchInboxFolder() {
folderService.fetchFolders()
folderService.fetchFolders(isForceReload: true)
.then(on: .main) { [weak self] folders in
self?.handleFetched(folders: folders)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class MyMenuViewController: ASDKViewController<ASDisplayNode> {

private var folders: [FolderViewModel] = []
private var serviceItems: [FolderViewModel] { FolderViewModel.menuItems }
private var accounts: [User] { dataService.users.filter { !$0.isActive } }
private var accounts: [User] { dataService.validAccounts() }

private let tableNode: ASTableNode

Expand Down Expand Up @@ -161,7 +161,7 @@ extension MyMenuViewController: ASTableDataSource, ASTableDelegate {
extension MyMenuViewController {
private func fetchFolders() {
showSpinner()
foldersProvider.fetchFolders()
foldersProvider.fetchFolders(isForceReload: false)
.then(on: .main) { [weak self] folders in
self?.handleNewFolders(with: folders)
}
Expand Down
7 changes: 5 additions & 2 deletions FlowCrypt/Core/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ final class Core: KeyDecrypter {
return try r.json.decodeJson(as: CoreRes.ZxcvbnStrengthBar.self)
}

public func startInBackgroundIfNotAlreadyRunning(_ completion: (() -> Void)? = nil) {
public func startInBackgroundIfNotAlreadyRunning(_ completion: @escaping (() -> Void)) {
if self.ready {
completion()
}
if !started {
started = true
DispatchQueue.global(qos: .default).async { [weak self] in
Expand All @@ -127,7 +130,7 @@ final class Core: KeyDecrypter {
self.context!.setObject(unsafeBitCast(cb_last_value_filler, to: AnyObject.self), forKeyedSubscript: "engine_host_cb_catcher" as (NSCopying & NSObjectProtocol)?)
self.ready = true
self.logger.logInfo("JsContext took \(trace.finish()) to start")
completion?()
completion()
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions FlowCrypt/Functionality/DataManager/DataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ protocol DataServiceType: EmailProviderType {
var token: String? { get }

var users: [User] { get }

func validAccounts() -> [User]
}

protocol ImapSessionProvider {
Expand Down Expand Up @@ -73,13 +75,7 @@ extension DataService: DataServiceType {
guard let currentUser = currentUser else {
return false
}

let isAnyKeysForCurrentUser = encryptedStorage.keysInfo()
.map(\.account)
.map { $0.contains(currentUser.email) }
.contains(true)

return isAnyKeysForCurrentUser
return encryptedStorage.isAnyKey(for: currentUser.email)
}

var isLoggedIn: Bool {
Expand Down Expand Up @@ -116,6 +112,13 @@ extension DataService: DataServiceType {
return nil
}
}

func validAccounts() -> [User] {
encryptedStorage.getAllUsers()
.filter { encryptedStorage.isAnyKey(for: $0.email) }
.filter { $0.email != currentUser?.email }
.map(User.init)
}
}

// MARK: - Migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protocol EncryptedStorageType: KeyStorageType {
func getAllUsers() -> [UserObject]
func saveActiveUser(with user: UserObject)
var activeUser: UserObject? { get }
func isAnyKey(for email: String) -> Bool

func cleanup()
}
Expand Down Expand Up @@ -180,6 +181,13 @@ extension EncryptedStorage {
.map(\.public)
.first
}

func isAnyKey(for email: String) -> Bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should rename isAnyKey to doesAnyKeyExist - filing another issue

keysInfo()
.map(\.account)
.map { $0.contains(email) }
.contains(true)
}
}

// MARK: - PassPhrase
Expand Down Expand Up @@ -223,6 +231,7 @@ extension EncryptedStorage {
self.getAllUsers().forEach {
$0.isActive = false
}
user.isActive = true
self.storage.add(user, update: .all)
}
}
Expand Down
18 changes: 18 additions & 0 deletions FlowCrypt/Functionality/DataManager/UserAccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protocol UserAccountServiceType {
func startSessionFor(user type: SessionType)
func switchActiveSessionFor(user: User) -> SessionType?
func startActiveSessionForNextUser() -> SessionType?
func cleanupSessions()
func cleanup()
}

Expand Down Expand Up @@ -95,6 +96,20 @@ extension UserAccountService: UserAccountServiceType {
return session
}

func cleanupSessions() {
encryptedStorage.getAllUsers()
.filter { !encryptedStorage.isAnyKey(for: $0.email) }
.map(\.email)
.forEach(logOut)

let users = encryptedStorage.getAllUsers()

if !users.contains(where: { $0.isActive }), let user = users.first(where: { encryptedStorage.isAnyKey(for: $0.email ) }) {
switchActiveSession(for: user)
}
}

@discardableResult
private func switchActiveSession(for userObject: UserObject) -> SessionType? {
let sessionType: SessionType
switch userObject.authType {
Expand All @@ -117,7 +132,10 @@ extension UserAccountService: UserAccountServiceType {
logger.logWarning("User is not logged in. Can't log out")
return
}
logOut(user: email)
}

private func logOut(user email: String) {
switch dataService.currentAuthType {
case .oAuthGmail:
googleService.signOut(user: email)
Expand Down
10 changes: 7 additions & 3 deletions FlowCrypt/Functionality/Services/AppStartup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct AppStartup {
window.rootViewController = BootstrapViewController()
window.makeKeyAndVisible()
Promise<Void> {
self.setupCore()
try awaitPromise(self.setupCore())
try self.setupMigrationIfNeeded()
try self.setupSession()
}.then(on: .main) {
Expand All @@ -29,8 +29,12 @@ struct AppStartup {
}
}

private func setupCore() {
Core.shared.startInBackgroundIfNotAlreadyRunning()
private func setupCore() -> Promise<Void> {
Promise { resolve, _ in
Core.shared.startInBackgroundIfNotAlreadyRunning {
resolve(())
}
}
}

private func setupMigrationIfNeeded() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol TrashFolderProviderType {
}

protocol FoldersServiceType {
func fetchFolders() -> Promise<[FolderViewModel]>
func fetchFolders(isForceReload: Bool) -> Promise<[FolderViewModel]>
}

final class FoldersService: FoldersServiceType {
Expand All @@ -34,7 +34,11 @@ final class FoldersService: FoldersServiceType {
self.localStorage = localStorage
}

func fetchFolders() -> Promise<[FolderViewModel]> {
func fetchFolders(isForceReload: Bool) -> Promise<[FolderViewModel]> {
if isForceReload {
return getAndSaveFolders()
}

let localFolders = self.localFoldersProvider.fetchFolders()

if localFolders.isEmpty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension TrashFolderProvider: TrashFolderProviderType {
} else {
return Promise { resolve, _ in
// will get all folders
_ = try awaitPromise(folderProvider.fetchFolders())
_ = try awaitPromise(folderProvider.fetchFolders(isForceReload: true))
resolve(localStorage.trashFolderPath)
}
}
Expand Down
1 change: 1 addition & 0 deletions FlowCrypt/Functionality/Services/GlobalRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ final class GlobalRouter: GlobalRouterType {
extension GlobalRouter {
/// proceed to flow (signing/setup/app) depends on user status (isLoggedIn/isSetupFinished)
func proceed() {
userAccountService.cleanupSessions()
proceed(with: nil)
}

Expand Down