-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: clegirar <clemntgirard@gmail.com>
- Loading branch information
Showing
5 changed files
with
202 additions
and
193 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
83 changes: 83 additions & 0 deletions
83
js/packages/store/messenger-effects/finishPreparingAccount.effect.ts
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,83 @@ | ||
import { Dictionary } from '@reduxjs/toolkit' | ||
import i18next from 'i18next' | ||
|
||
import beapi from '@berty/api' | ||
import { ServiceClientType } from '@berty/grpc-bridge/welsh-clients.gen' | ||
import { detectOSLanguage } from '@berty/i18n' | ||
import { MessengerState } from '@berty/redux/reducers/messenger.reducer' | ||
import { resetTheme } from '@berty/redux/reducers/theme.reducer' | ||
import { setStateReady, setStateSetupFinished, UiState } from '@berty/redux/reducers/ui.reducer' | ||
import { AppDispatch } from '@berty/redux/store' | ||
import { storageGet, storageRemove } from '@berty/utils/accounts/accountClient' | ||
import { updateAccount } from '@berty/utils/accounts/accountUtils' | ||
import { GlobalPersistentOptionsKeys } from '@berty/utils/persistent-options/types' | ||
|
||
const closeConvos = async ( | ||
client: ServiceClientType<beapi.messenger.MessengerService> | null, | ||
conversations: { [key: string]: beapi.messenger.IConversation | undefined }, | ||
) => { | ||
if (client === null) { | ||
console.warn('client is null') | ||
return | ||
} | ||
|
||
for (const conv of Object.values(conversations).filter(conv => conv?.isOpen) as any) { | ||
client.conversationClose({ groupPk: conv.publicKey }).catch((e: any) => { | ||
console.warn(`failed to close conversation "${conv.displayName}",`, e) | ||
}) | ||
} | ||
} | ||
|
||
const updateAccountOnClients = async ( | ||
messengerClient: ServiceClientType<beapi.messenger.MessengerService> | null, | ||
selectedAccount: string | null, | ||
account: beapi.messenger.IAccount | null | undefined, | ||
) => { | ||
try { | ||
// remove the displayName value that was set at the creation of the account | ||
const displayName = await storageGet(GlobalPersistentOptionsKeys.DisplayName) | ||
await storageRemove(GlobalPersistentOptionsKeys.DisplayName) | ||
if (displayName) { | ||
// update account in messenger client | ||
await messengerClient?.accountUpdate({ displayName }) | ||
// update account in account client | ||
await updateAccount({ | ||
accountName: displayName, | ||
accountId: selectedAccount, | ||
publicKey: account?.publicKey, | ||
}) | ||
} | ||
} catch (err) { | ||
console.warn(err) | ||
} | ||
|
||
// TODO: fix flow of asking permissions | ||
// const config = await accountService.networkConfigGet({ accountId: selectedAccount }) | ||
// if (config.currentConfig?.staticRelay && config.currentConfig?.staticRelay[0] === ':default:') { | ||
// await servicesAuthViaURL(protocolClient, bertyOperatedServer) | ||
// } | ||
} | ||
|
||
export const finishPreparingAccount = async ( | ||
ui: UiState, | ||
messenger: MessengerState, | ||
conversations: Dictionary<beapi.messenger.IConversation>, | ||
dispatch: AppDispatch, | ||
) => { | ||
try { | ||
await closeConvos(ui.messengerClient, conversations) | ||
|
||
await i18next.changeLanguage(detectOSLanguage()) | ||
|
||
if (ui.isNewAccount) { | ||
await updateAccountOnClients(ui.messengerClient, ui.selectedAccount, messenger.account) | ||
// reset ui theme | ||
dispatch(resetTheme()) | ||
dispatch(setStateSetupFinished()) | ||
} else { | ||
dispatch(setStateReady()) | ||
} | ||
} catch (err) { | ||
console.warn(err) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
js/packages/store/messenger-effects/initialLaunch.effect.ts
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,58 @@ | ||
import RNFS from 'react-native-fs' | ||
|
||
import { GoBridge } from '@berty/native-modules/GoBridge' | ||
import { setNextAccount, setStateOnBoardingReady } from '@berty/redux/reducers/ui.reducer' | ||
import { AppDispatch } from '@berty/redux/store' | ||
import { refreshAccountList } from '@berty/utils/accounts/accountUtils' | ||
|
||
const initBridge = async () => { | ||
try { | ||
console.log('bridge methods: ', Object.keys(GoBridge)) | ||
await GoBridge.initBridge() | ||
console.log('bridge init done') | ||
} catch (err: any) { | ||
if (err?.message?.indexOf('already started') === -1) { | ||
console.error('unable to init bridge: ', err) | ||
} else { | ||
console.log('bridge already started: ', err) | ||
} | ||
} | ||
} | ||
|
||
export const initialLaunch = async (dispatch: AppDispatch) => { | ||
await initBridge() | ||
const f = async () => { | ||
const accounts = await refreshAccountList() | ||
|
||
if (Object.keys(accounts).length > 0) { | ||
let accountSelected: any = null | ||
Object.values(accounts).forEach(account => { | ||
if (!accountSelected) { | ||
accountSelected = account | ||
} else if (accountSelected && accountSelected.lastOpened < (account.lastOpened || 0)) { | ||
accountSelected = account | ||
} | ||
}) | ||
|
||
// Delete berty-backup account | ||
const outFile = | ||
RNFS.TemporaryDirectoryPath + `/berty-backup-${accountSelected.accountId}` + '.tar' | ||
RNFS.unlink(outFile) | ||
.then(() => { | ||
console.log('File deleted') | ||
}) | ||
.catch(() => { | ||
console.log('File berty backup does not exist') // here | ||
}) | ||
|
||
dispatch(setNextAccount(accountSelected.accountId)) | ||
|
||
return | ||
} else { | ||
// this is the first account that will be created | ||
dispatch(setStateOnBoardingReady()) | ||
} | ||
} | ||
|
||
f().catch(e => console.warn(e)) | ||
} |
Oops, something went wrong.