diff --git a/app/definitions/ICertificate.ts b/app/definitions/ICertificate.ts new file mode 100644 index 000000000..025b6a96f --- /dev/null +++ b/app/definitions/ICertificate.ts @@ -0,0 +1,4 @@ +export interface ICertificate { + path: string; + password: string; +} diff --git a/app/definitions/IRocketChat.ts b/app/definitions/IRocketChat.ts index 319664700..199cf4891 100644 --- a/app/definitions/IRocketChat.ts +++ b/app/definitions/IRocketChat.ts @@ -4,4 +4,6 @@ type TRocketChat = typeof rocketchat; export interface IRocketChat extends TRocketChat { sdk: any; + activeUsersSubTimeout: any; + roomsSub: any; } diff --git a/app/definitions/index.ts b/app/definitions/index.ts index 1c154388e..9512c7cf9 100644 --- a/app/definitions/index.ts +++ b/app/definitions/index.ts @@ -21,6 +21,8 @@ export * from './IUser'; export * from './IServer'; export * from './ILoggedUser'; export * from './IServerHistory'; +export * from './IRocketChat'; +export * from './ICertificate'; export interface IBaseScreen, S extends string> { navigation: StackNavigationProp; diff --git a/app/lib/methods/logout.js b/app/lib/methods/logout.ts similarity index 72% rename from app/lib/methods/logout.js rename to app/lib/methods/logout.ts index c108a9965..fefd1769b 100644 --- a/app/lib/methods/logout.js +++ b/app/lib/methods/logout.ts @@ -1,5 +1,6 @@ import * as FileSystem from 'expo-file-system'; import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk'; +import Model from '@nozbe/watermelondb/Model'; import { getDeviceToken } from '../../notifications/push'; import { extractHostname } from '../../utils/server'; @@ -7,34 +8,38 @@ import { BASIC_AUTH_KEY } from '../../utils/fetch'; import database, { getDatabase } from '../database'; import RocketChat from '../rocketchat'; import { useSsl } from '../../utils/url'; +import log from '../../utils/log'; import { E2E_PRIVATE_KEY, E2E_PUBLIC_KEY, E2E_RANDOM_PASSWORD_KEY } from '../encryption/constants'; import UserPreferences from '../userPreferences'; +import { ICertificate, IRocketChat } from '../../definitions'; -async function removeServerKeys({ server, userId }) { +async function removeServerKeys({ server, userId }: { server: string; userId: string | null }) { await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`); - await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`); + if (userId) { + await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`); + } await UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`); await UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`); await UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`); await UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`); } -async function removeSharedCredentials({ server }) { +async function removeSharedCredentials({ server }: { server: string }) { // clear certificate for server - SSL Pinning try { - const certificate = await UserPreferences.getMapAsync(extractHostname(server)); - if (certificate && certificate.path) { + const certificate = (await UserPreferences.getMapAsync(extractHostname(server))) as ICertificate | null; + if (certificate?.path) { await UserPreferences.removeItem(extractHostname(server)); await FileSystem.deleteAsync(certificate.path); } } catch (e) { - console.log('removeSharedCredentials', e); + log(e); } } -async function removeServerData({ server }) { +async function removeServerData({ server }: { server: string }) { try { - const batch = []; + const batch: Model[] = []; const serversDB = database.servers; const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`); @@ -47,11 +52,11 @@ async function removeServerData({ server }) { const serverRecord = await serverCollection.find(server); batch.push(serverRecord.prepareDestroyPermanently()); - await serversDB.action(() => serversDB.batch(...batch)); + await serversDB.write(() => serversDB.batch(...batch)); await removeSharedCredentials({ server }); - await removeServerKeys({ server }); + await removeServerKeys({ server, userId }); } catch (e) { - console.log('removeServerData', e); + log(e); } } @@ -59,16 +64,16 @@ async function removeCurrentServer() { await UserPreferences.removeItem(RocketChat.CURRENT_SERVER); } -async function removeServerDatabase({ server }) { +async function removeServerDatabase({ server }: { server: string }) { try { const db = getDatabase(server); - await db.action(() => db.unsafeResetDatabase()); + await db.write(() => db.unsafeResetDatabase()); } catch (e) { - console.log(e); + log(e); } } -export async function removeServer({ server }) { +export async function removeServer({ server }: { server: string }): Promise { try { const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`); if (userId) { @@ -88,11 +93,11 @@ export async function removeServer({ server }) { await removeServerData({ server }); await removeServerDatabase({ server }); } catch (e) { - console.log('removeServer', e); + log(e); } } -export default async function logout({ server }) { +export default async function logout(this: IRocketChat, { server }: { server: string }): Promise { if (this.roomsSub) { this.roomsSub.stop(); this.roomsSub = null; @@ -106,14 +111,14 @@ export default async function logout({ server }) { try { await this.removePushToken(); } catch (e) { - console.log('removePushToken', e); + log(e); } try { // RC 0.60.0 await this.sdk.logout(); } catch (e) { - console.log('logout', e); + log(e); } if (this.sdk) { diff --git a/app/utils/sslPinning.ts b/app/utils/sslPinning.ts index 42245c98a..27b100228 100644 --- a/app/utils/sslPinning.ts +++ b/app/utils/sslPinning.ts @@ -5,6 +5,7 @@ import * as FileSystem from 'expo-file-system'; import UserPreferences from '../lib/userPreferences'; import I18n from '../i18n'; import { extractHostname } from './server'; +import { ICertificate } from '../definitions'; const { SSLPinning } = NativeModules; const { documentDirectory } = FileSystem; @@ -13,11 +14,6 @@ const extractFileScheme = (path: string) => path.replace('file://', ''); // file const getPath = (name: string) => `${documentDirectory}/${name}`; -interface ICertificate { - path: string; - password: string; -} - const persistCertificate = async (name: string, password: string) => { const certificatePath = getPath(name); const certificate: ICertificate = {