From 1b00594c35b2b30f19928668367f6c645dd5775a Mon Sep 17 00:00:00 2001 From: Overtorment Date: Thu, 30 Jul 2020 13:22:06 +0100 Subject: [PATCH] FIX: unsubscribe from push notifications when wallet is deleted --- blue_modules/notifications.js | 31 +++++++++++++++++++++++++++++++ screen/wallets/details.js | 4 +++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/blue_modules/notifications.js b/blue_modules/notifications.js index fe2d42359..a98097ae5 100644 --- a/blue_modules/notifications.js +++ b/blue_modules/notifications.js @@ -173,6 +173,36 @@ const majorTomToGroundControl = async function (addresses, hashes, txids) { ); }; +/** + * The opposite of `majorTomToGroundControl` call. + * + * @param addresses {string[]} + * @param hashes {string[]} + * @param txids {string[]} + * @returns {Promise} Response object from API rest call + */ +const unsubscribe = async function (addresses, hashes, txids) { + if (!Array.isArray(addresses) || !Array.isArray(hashes) || !Array.isArray(txids)) + throw new Error('no addresses or hashes or txids provided'); + const pushToken = await _getPushToken(); + if (!pushToken || !pushToken.token || !pushToken.os) return; + + const api = new Frisbee({ baseURI: constants.groundControlUri }); + + return await api.post( + '/unsubscribe', + Object.assign({}, _getHeaders(), { + body: { + addresses, + hashes, + txids, + token: pushToken.token, + os: pushToken.os, + }, + }), + ); +}; + // on app launch (load module): (async () => { if (!(await _getPushToken())) return; @@ -186,3 +216,4 @@ PushNotification.setApplicationIconBadgeNumber(0); module.exports.tryToObtainPermissions = tryToObtainPermissions; module.exports.majorTomToGroundControl = majorTomToGroundControl; +module.exports.unsubscribe = unsubscribe; diff --git a/screen/wallets/details.js b/screen/wallets/details.js index e5d37b8bf..c17947663 100644 --- a/screen/wallets/details.js +++ b/screen/wallets/details.js @@ -30,8 +30,8 @@ import loc from '../../loc'; import { BlueCurrentTheme } from '../../components/themes'; const EV = require('../../blue_modules/events'); const prompt = require('../../blue_modules/prompt'); -/** @type {AppStorage} */ const BlueApp = require('../../BlueApp'); +const notifications = require('../../blue_modules/notifications'); const styles = StyleSheet.create({ root: { @@ -149,6 +149,7 @@ export default class WalletDetails extends Component { if (Number(walletBalanceConfirmation) === this.state.wallet.getBalance()) { this.props.navigation.setParams({ isLoading: true }); this.setState({ isLoading: true }, async () => { + notifications.unsubscribe(this.state.wallet.getAllExternalAddresses(), [], []); BlueApp.deleteWallet(this.state.wallet); ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false }); await BlueApp.saveToDisk(); @@ -356,6 +357,7 @@ export default class WalletDetails extends Component { } else { this.props.navigation.setParams({ isLoading: true }); this.setState({ isLoading: true }, async () => { + notifications.unsubscribe(this.state.wallet.getAllExternalAddresses(), [], []); BlueApp.deleteWallet(this.state.wallet); ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false }); await BlueApp.saveToDisk();