2020-05-04 20:20:45 +00:00
|
|
|
import * as FileSystem from 'expo-file-system';
|
|
|
|
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk';
|
2022-02-09 20:37:08 +00:00
|
|
|
import Model from '@nozbe/watermelondb/Model';
|
2020-05-04 20:20:45 +00:00
|
|
|
|
2022-04-12 16:37:28 +00:00
|
|
|
import { getDeviceToken } from '../notifications';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { extractHostname, isSsl } from './helpers';
|
|
|
|
import { BASIC_AUTH_KEY } from './helpers/fetch';
|
2022-04-07 16:53:07 +00:00
|
|
|
import database, { getDatabase } from '../database';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from './helpers/log';
|
2022-05-13 14:52:37 +00:00
|
|
|
import { ICertificate } from '../../definitions';
|
2022-04-04 19:15:29 +00:00
|
|
|
import sdk from '../services/sdk';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { CURRENT_SERVER, E2E_PRIVATE_KEY, E2E_PUBLIC_KEY, E2E_RANDOM_PASSWORD_KEY, TOKEN_KEY } from '../constants';
|
2022-04-07 16:53:07 +00:00
|
|
|
import UserPreferences from './userPreferences';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../services';
|
2022-05-13 14:52:37 +00:00
|
|
|
import { roomsSubscription } from './subscriptions/rooms';
|
|
|
|
import { _activeUsersSubTimeout } from '.';
|
2020-05-04 20:20:45 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
function removeServerKeys({ server, userId }: { server: string; userId?: string | null }) {
|
2022-04-28 20:37:25 +00:00
|
|
|
UserPreferences.removeItem(`${TOKEN_KEY}-${server}`);
|
2022-02-09 20:37:08 +00:00
|
|
|
if (userId) {
|
2022-04-28 20:37:25 +00:00
|
|
|
UserPreferences.removeItem(`${TOKEN_KEY}-${userId}`);
|
2022-02-09 20:37:08 +00:00
|
|
|
}
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`);
|
|
|
|
UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`);
|
|
|
|
UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`);
|
|
|
|
UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 20:37:08 +00:00
|
|
|
async function removeSharedCredentials({ server }: { server: string }) {
|
2020-08-19 17:14:22 +00:00
|
|
|
// clear certificate for server - SSL Pinning
|
2020-05-04 20:20:45 +00:00
|
|
|
try {
|
2022-03-09 19:41:26 +00:00
|
|
|
const certificate = UserPreferences.getMap(extractHostname(server)) as ICertificate | null;
|
2022-02-09 20:37:08 +00:00
|
|
|
if (certificate?.path) {
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.removeItem(extractHostname(server));
|
2020-05-04 20:20:45 +00:00
|
|
|
await FileSystem.deleteAsync(certificate.path);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-02-09 20:37:08 +00:00
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 20:37:08 +00:00
|
|
|
async function removeServerData({ server }: { server: string }) {
|
2020-05-04 20:20:45 +00:00
|
|
|
try {
|
2022-02-09 20:37:08 +00:00
|
|
|
const batch: Model[] = [];
|
2020-05-04 20:20:45 +00:00
|
|
|
const serversDB = database.servers;
|
2022-04-28 20:37:25 +00:00
|
|
|
const userId = UserPreferences.getString(`${TOKEN_KEY}-${server}`);
|
2020-05-04 20:20:45 +00:00
|
|
|
|
2021-02-26 16:25:51 +00:00
|
|
|
const usersCollection = serversDB.get('users');
|
2020-05-04 20:20:45 +00:00
|
|
|
if (userId) {
|
|
|
|
const userRecord = await usersCollection.find(userId);
|
|
|
|
batch.push(userRecord.prepareDestroyPermanently());
|
|
|
|
}
|
2021-02-26 16:25:51 +00:00
|
|
|
const serverCollection = serversDB.get('servers');
|
2020-05-04 20:20:45 +00:00
|
|
|
const serverRecord = await serverCollection.find(server);
|
|
|
|
batch.push(serverRecord.prepareDestroyPermanently());
|
|
|
|
|
2022-02-09 20:37:08 +00:00
|
|
|
await serversDB.write(() => serversDB.batch(...batch));
|
2020-05-04 20:20:45 +00:00
|
|
|
await removeSharedCredentials({ server });
|
2022-03-09 19:41:26 +00:00
|
|
|
removeServerKeys({ server, userId });
|
2020-05-04 20:20:45 +00:00
|
|
|
} catch (e) {
|
2022-02-09 20:37:08 +00:00
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
function removeCurrentServer() {
|
2022-04-28 20:37:25 +00:00
|
|
|
UserPreferences.removeItem(CURRENT_SERVER);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 20:37:08 +00:00
|
|
|
async function removeServerDatabase({ server }: { server: string }) {
|
2020-05-04 20:20:45 +00:00
|
|
|
try {
|
|
|
|
const db = getDatabase(server);
|
2022-02-09 20:37:08 +00:00
|
|
|
await db.write(() => db.unsafeResetDatabase());
|
2020-05-04 20:20:45 +00:00
|
|
|
} catch (e) {
|
2022-02-09 20:37:08 +00:00
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 20:37:08 +00:00
|
|
|
export async function removeServer({ server }: { server: string }): Promise<void> {
|
2020-05-04 20:20:45 +00:00
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
const userId = UserPreferences.getString(`${TOKEN_KEY}-${server}`);
|
2020-05-04 20:20:45 +00:00
|
|
|
if (userId) {
|
2022-04-28 20:37:25 +00:00
|
|
|
const resume = UserPreferences.getString(`${TOKEN_KEY}-${userId}`);
|
2020-05-04 20:20:45 +00:00
|
|
|
|
2022-06-21 19:40:29 +00:00
|
|
|
try {
|
|
|
|
const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: isSsl(server) });
|
|
|
|
await sdk.login({ resume });
|
|
|
|
|
|
|
|
const token = getDeviceToken();
|
|
|
|
if (token) {
|
|
|
|
await sdk.del('push.token', { token });
|
|
|
|
}
|
|
|
|
|
|
|
|
await sdk.logout();
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await removeServerData({ server });
|
|
|
|
await removeServerDatabase({ server });
|
|
|
|
} catch (e) {
|
2022-02-09 20:37:08 +00:00
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 14:52:37 +00:00
|
|
|
export async function logout({ server }: { server: string }): Promise<void> {
|
|
|
|
if (roomsSubscription?.stop) {
|
|
|
|
roomsSubscription.stop();
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 14:52:37 +00:00
|
|
|
if (_activeUsersSubTimeout.activeUsersSubTimeout) {
|
|
|
|
clearTimeout(_activeUsersSubTimeout.activeUsersSubTimeout as number);
|
|
|
|
_activeUsersSubTimeout.activeUsersSubTimeout = false;
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-04-28 20:37:25 +00:00
|
|
|
await Services.removePushToken();
|
2020-05-04 20:20:45 +00:00
|
|
|
} catch (e) {
|
2022-02-09 20:37:08 +00:00
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// RC 0.60.0
|
2022-03-08 18:54:26 +00:00
|
|
|
await sdk.current.logout();
|
2020-05-04 20:20:45 +00:00
|
|
|
} catch (e) {
|
2022-02-09 20:37:08 +00:00
|
|
|
log(e);
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 18:54:26 +00:00
|
|
|
if (sdk.current) {
|
|
|
|
sdk.disconnect();
|
2020-05-04 20:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await removeServerData({ server });
|
|
|
|
await removeCurrentServer();
|
|
|
|
await removeServerDatabase({ server });
|
|
|
|
}
|