Skip to content

Commit

Permalink
fix: refacto messenger effects
Browse files Browse the repository at this point in the history
Signed-off-by: clegirar <clemntgirard@gmail.com>
  • Loading branch information
clegirar committed May 13, 2022
1 parent 2a1479f commit 93208e0
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 193 deletions.
2 changes: 1 addition & 1 deletion js/packages/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export {
useReadEffect,
useMountEffect,
} from './hooks'
export * from './MessengerEffects'
export { MessengerEffects } from './messenger-effects/MessengerEffects'
export * from './types.gen'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { EventEmitterContext } from '@berty/contexts/eventEmitter.context'
import { useAppDispatch, useAppSelector, useConversationsDict } from '@berty/hooks'
import { selectAppState } from '@berty/redux/reducers/ui.reducer'

import { initialLaunch, openingDaemonAndClients, finishPreparingAccount } from './effectsImplem'
import { finishPreparingAccount } from './finishPreparingAccount.effect'
import { initialLaunch } from './initialLaunch.effect'
import { openingDaemonAndClients } from './openingDaemonAndClients.effect'

export const MessengerEffects: React.FC = () => {
const dispatch = useAppDispatch()
Expand Down
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 js/packages/store/messenger-effects/initialLaunch.effect.ts
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))
}

0 comments on commit 93208e0

Please sign in to comment.