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-04 19:15:29 +00:00
|
|
|
import { getDeviceToken } from '../../../notifications/push';
|
|
|
|
import { extractHostname } from '../../../utils/server';
|
|
|
|
import { BASIC_AUTH_KEY } from '../../../utils/fetch';
|
|
|
|
import database, { getDatabase } from '../../database';
|
|
|
|
import RocketChat from '..';
|
|
|
|
import { useSsl } from '../../../utils/url';
|
|
|
|
import log from '../../../utils/log';
|
|
|
|
import { E2E_PRIVATE_KEY, E2E_PUBLIC_KEY, E2E_RANDOM_PASSWORD_KEY } from '../../constants';
|
|
|
|
import UserPreferences from '../../userPreferences';
|
|
|
|
import { ICertificate, IRocketChat } from '../../../definitions';
|
|
|
|
import sdk from '../services/sdk';
|
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 }) {
|
|
|
|
UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`);
|
2022-02-09 20:37:08 +00:00
|
|
|
if (userId) {
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.removeItem(`${RocketChat.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-03-09 19:41:26 +00:00
|
|
|
const userId = UserPreferences.getString(`${RocketChat.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() {
|
|
|
|
UserPreferences.removeItem(RocketChat.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-03-09 19:41:26 +00:00
|
|
|
const userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${server}`);
|
2020-05-04 20:20:45 +00:00
|
|
|
if (userId) {
|
2022-03-09 19:41:26 +00:00
|
|
|
const resume = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${userId}`);
|
2020-05-04 20:20:45 +00:00
|
|
|
|
|
|
|
const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: useSsl(server) });
|
|
|
|
await sdk.login({ resume });
|
|
|
|
|
|
|
|
const token = getDeviceToken();
|
|
|
|
if (token) {
|
|
|
|
await sdk.del('push.token', { token });
|
|
|
|
}
|
|
|
|
|
|
|
|
await sdk.logout();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-04 19:15:29 +00:00
|
|
|
export async function logout(this: IRocketChat, { server }: { server: string }): Promise<void> {
|
2020-05-04 20:20:45 +00:00
|
|
|
if (this.roomsSub) {
|
|
|
|
this.roomsSub.stop();
|
|
|
|
this.roomsSub = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.activeUsersSubTimeout) {
|
|
|
|
clearTimeout(this.activeUsersSubTimeout);
|
|
|
|
this.activeUsersSubTimeout = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.removePushToken();
|
|
|
|
} 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 });
|
|
|
|
}
|