Skip to content

Commit b63499f

Browse files
committed
Code refactor and some tests
1 parent b188fff commit b63499f

File tree

2 files changed

+308
-85
lines changed

2 files changed

+308
-85
lines changed

packages/bitcore-wallet-service/src/lib/pushnotificationsservice.ts

+88-85
Original file line numberDiff line numberDiff line change
@@ -188,62 +188,49 @@ export class PushNotificationsService {
188188
async.waterfall(
189189
[
190190
next => {
191-
notification.type === 'NewBlock'
192-
? next(null, [])
193-
: this._readAndApplyTemplates(notification, notifType, recipientsList, next);
191+
notification.type !== 'NewBlock'
192+
? this._readAndApplyTemplates(notification, notifType, recipientsList, next)
193+
: next(null, []);
194194
},
195195
(contents, next) => {
196-
async.map(
197-
recipientsList,
198-
(recipient: IPreferences, next) => {
199-
const content = contents ? contents[recipient.language] : null;
200-
201-
this.storage.fetchPushNotificationSubs(recipient.copayerId, (err, subs) => {
202-
if (err) return next(err);
203-
204-
const notifications = _.map(subs, sub => {
205-
const tokenAddress =
206-
notification.data && notification.data.tokenAddress ? notification.data.tokenAddress : null;
207-
const multisigContractAddress =
208-
notification.data && notification.data.multisigContractAddress
209-
? notification.data.multisigContractAddress
210-
: null;
211-
212-
const notificationData: any = {
213-
to: sub.token,
214-
priority: 'high',
215-
restricted_package_name: sub.packageName,
216-
data: {
217-
walletId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(recipient.walletId)),
218-
tokenAddress,
219-
multisigContractAddress,
220-
copayerId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(recipient.copayerId)),
221-
title: content?.plain?.subject,
222-
body: content?.plain?.body,
223-
notification_type: notification.type
224-
}
225-
};
226-
227-
if (notifType.notifyOthers || notifType.notifyCreator) {
228-
notificationData.notification = {
229-
title: content.plain.subject,
230-
body: content.plain.body,
231-
sound: 'default',
232-
click_action: 'FCM_PLUGIN_ACTIVITY',
233-
icon: 'fcm_push_icon'
234-
};
235-
}
236-
237-
return notificationData;
238-
});
239-
return next(err, notifications);
240-
});
241-
},
242-
(err, allNotifications) => {
243-
if (err) return next(err);
244-
return next(null, _.flatten(allNotifications));
196+
this._getSubscriptions(notification, recipientsList, contents, next);
197+
},
198+
(subs, next) => {
199+
const notifications = _.map(subs, sub => {
200+
const tokenAddress =
201+
notification.data && notification.data.tokenAddress ? notification.data.tokenAddress : null;
202+
const multisigContractAddress =
203+
notification.data && notification.data.multisigContractAddress
204+
? notification.data.multisigContractAddress
205+
: null;
206+
207+
const notificationData: any = {
208+
to: sub.token,
209+
priority: 'high',
210+
restricted_package_name: sub.packageName,
211+
data: {
212+
walletId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(notification.walletId || sub.walletId)),
213+
tokenAddress,
214+
multisigContractAddress,
215+
copayerId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(sub.copayerId)),
216+
title: sub?.plain?.subject,
217+
body: sub?.plain?.body,
218+
notification_type: notification.type
219+
}
220+
};
221+
222+
if (notifType.notifyOthers || notifType.notifyCreator) {
223+
notificationData.notification = {
224+
title: sub?.plain?.subject,
225+
body: sub?.plain?.body,
226+
sound: 'default',
227+
click_action: 'FCM_PLUGIN_ACTIVITY',
228+
icon: 'fcm_push_icon'
229+
};
245230
}
246-
);
231+
return notificationData;
232+
});
233+
return next(err, notifications);
247234
},
248235
(notifications, next) => {
249236
async.each(
@@ -277,43 +264,14 @@ export class PushNotificationsService {
277264
}
278265

279266
_checkShouldSendNotif(notification, cb) {
280-
if (notification.type != 'NewTxProposal' || notification.type == 'NewBlock') return cb(null, true);
267+
if (notification.type != 'NewTxProposal') return cb(null, true);
281268
this.storage.fetchWallet(notification.walletId, (err, wallet) => {
282269
return cb(err, wallet && wallet.m > 1);
283270
});
284271
}
285272

286273
_getRecipientsList(notification, notificationType, cb) {
287-
if (notification.type === 'NewBlock') {
288-
this.storage.fetchLatestPushNotificationSubs((err, subs) => {
289-
if (err) return cb(err);
290-
291-
logger.info(
292-
`Sending NewBlock [${notification.data.coin}/${notification.data.network}] notifications to: ${subs.length} subscribers`
293-
);
294-
async.map(
295-
subs,
296-
(sub: any, next) => {
297-
this.storage.fetchCopayerLookup(sub.copayerId, (err, wallet) => {
298-
if (err) return cb(err);
299-
300-
const recipient: any = {
301-
walletId: wallet.walletId,
302-
copayerId: sub.copayerId,
303-
language: this.defaultLanguage,
304-
unit: this.defaultUnit
305-
};
306-
307-
return next(err, recipient);
308-
});
309-
},
310-
(err, recipientList) => {
311-
if (err) return cb(err);
312-
return cb(null, _.flatten(recipientList));
313-
}
314-
);
315-
});
316-
} else {
274+
if (notification.type !== 'NewBlock') {
317275
this.storage.fetchWallet(notification.walletId, (err, wallet) => {
318276
if (err) return cb(err);
319277

@@ -369,7 +327,7 @@ export class PushNotificationsService {
369327
return cb(null, recipientsList);
370328
});
371329
});
372-
}
330+
} else return cb(null, []);
373331
}
374332

375333
_readAndApplyTemplates(notification, notifType, recipientsList, cb) {
@@ -524,6 +482,51 @@ export class PushNotificationsService {
524482
};
525483
}
526484

485+
_getSubscriptions(notification, recipientsList, contents, cb) {
486+
if (notification.type !== 'NewBlock') {
487+
async.map(
488+
recipientsList,
489+
(recipient: IPreferences, next) => {
490+
const content = contents ? contents[recipient.language] : null;
491+
492+
this.storage.fetchPushNotificationSubs(recipient.copayerId, (err, subs) => {
493+
if (err) return next(err);
494+
495+
subs[0].plain = content.plain;
496+
return next(err, subs);
497+
});
498+
},
499+
(err, allSubs) => {
500+
if (err) return cb(err);
501+
return cb(null, _.flatten(allSubs));
502+
}
503+
);
504+
} else {
505+
this.storage.fetchLatestPushNotificationSubs((err, subs) => {
506+
if (err) return cb(err);
507+
508+
logger.info(
509+
`Sending NewBlock [${notification.data.coin}/${notification.data.network}] notifications to: ${subs.length} subscribers`
510+
);
511+
async.map(
512+
subs,
513+
(sub: any, next) => {
514+
this.storage.fetchCopayerLookup(sub.copayerId, (err, wallet) => {
515+
if (err) return cb(err);
516+
517+
sub.walletId = wallet.walletId;
518+
return next(err, sub);
519+
});
520+
},
521+
(err, subs) => {
522+
if (err) return cb(err);
523+
return cb(null, _.flatten(subs));
524+
}
525+
);
526+
});
527+
}
528+
}
529+
527530
_makeRequest(opts, cb) {
528531
this.request(
529532
{

0 commit comments

Comments
 (0)