-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Bruno Dantas <oliveiradantas96@gmail.com> Co-authored-by: Calebe Rios <calebersmendes@gmail.com> [NEW] Remove push token & logout [NEW] Remove server database [FIX] Superfluous trailing arguments
- Loading branch information
1 parent
df0a385
commit cff6e34
Showing
9 changed files
with
278 additions
and
170 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import RNUserDefaults from 'rn-user-defaults'; | ||
import * as FileSystem from 'expo-file-system'; | ||
import { Rocketchat as RocketchatClient } from '@rocket.chat/sdk'; | ||
|
||
import { SERVERS, SERVER_URL } from '../../constants/userDefaults'; | ||
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 '../rocketchat'; | ||
import { useSsl } from '../../utils/url'; | ||
|
||
async function removeServerKeys({ server, userId }) { | ||
await RNUserDefaults.clear(`${ RocketChat.TOKEN_KEY }-${ server }`); | ||
await RNUserDefaults.clear(`${ RocketChat.TOKEN_KEY }-${ userId }`); | ||
await RNUserDefaults.clear(`${ BASIC_AUTH_KEY }-${ server }`); | ||
} | ||
|
||
async function removeSharedCredentials({ server }) { | ||
try { | ||
const servers = await RNUserDefaults.objectForKey(SERVERS); | ||
await RNUserDefaults.setObjectForKey(SERVERS, servers && servers.filter(srv => srv[SERVER_URL] !== server)); | ||
|
||
// clear certificate for server - SSL Pinning | ||
const certificate = await RNUserDefaults.objectForKey(extractHostname(server)); | ||
if (certificate && certificate.path) { | ||
await RNUserDefaults.clear(extractHostname(server)); | ||
await FileSystem.deleteAsync(certificate.path); | ||
} | ||
} catch (e) { | ||
console.log('removeSharedCredentials', e); | ||
} | ||
} | ||
|
||
async function removeServerData({ server }) { | ||
try { | ||
const batch = []; | ||
const serversDB = database.servers; | ||
const userId = await RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ server }`); | ||
|
||
const usersCollection = serversDB.collections.get('users'); | ||
if (userId) { | ||
const userRecord = await usersCollection.find(userId); | ||
batch.push(userRecord.prepareDestroyPermanently()); | ||
} | ||
const serverCollection = serversDB.collections.get('servers'); | ||
const serverRecord = await serverCollection.find(server); | ||
batch.push(serverRecord.prepareDestroyPermanently()); | ||
|
||
await serversDB.action(() => serversDB.batch(...batch)); | ||
await removeSharedCredentials({ server }); | ||
await removeServerKeys({ server }); | ||
} catch (e) { | ||
console.log('removeServerData', e); | ||
} | ||
} | ||
|
||
async function removeCurrentServer() { | ||
await RNUserDefaults.clear('currentServer'); | ||
await RNUserDefaults.clear(RocketChat.TOKEN_KEY); | ||
} | ||
|
||
async function removeServerDatabase({ server }) { | ||
try { | ||
const db = getDatabase(server); | ||
await db.action(() => db.unsafeResetDatabase()); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
export async function removeServer({ server }) { | ||
try { | ||
const userId = await RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ server }`); | ||
if (userId) { | ||
const resume = await RNUserDefaults.get(`${ RocketChat.TOKEN_KEY }-${ userId }`); | ||
|
||
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) { | ||
console.log('removePush', e); | ||
} | ||
} | ||
|
||
export default async function logout({ server }) { | ||
if (this.roomsSub) { | ||
this.roomsSub.stop(); | ||
} | ||
|
||
if (this.activeUsersSubTimeout) { | ||
clearTimeout(this.activeUsersSubTimeout); | ||
this.activeUsersSubTimeout = false; | ||
} | ||
|
||
try { | ||
await this.removePushToken(); | ||
} catch (e) { | ||
console.log('removePushToken', e); | ||
} | ||
|
||
try { | ||
// RC 0.60.0 | ||
await this.sdk.logout(); | ||
} catch (e) { | ||
console.log('logout', e); | ||
} | ||
|
||
if (this.sdk) { | ||
this.sdk = null; | ||
} | ||
|
||
await removeServerData({ server }); | ||
await removeCurrentServer(); | ||
await removeServerDatabase({ server }); | ||
} |
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
Oops, something went wrong.