add supporting methods for storing/fetching/removing subscriptions

This commit is contained in:
Ivan Socolsky 2017-02-02 15:18:16 -03:00
parent c65704a29c
commit bfcd14d297
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
2 changed files with 61 additions and 20 deletions

View File

@ -3055,36 +3055,39 @@ WalletService.prototype.getFiatRate = function(opts, cb) {
});
};
/**
* Subscribe this copayer to the Push Notifications service using the specified token.
* @param {Object} opts
* @param {string} opts.token - The token representing the app/device.
* @param {string} [opts.packageName] - The restricted_package_name option associated with this token.
* @param {string} [opts.platform] - The platform associated with this token.
*/
WalletService.prototype.pushNotificationsSubscribe = function(opts, cb) {
if (!checkRequired(opts, ['token'], cb)) return;
var self = this;
opts.user = self.walletId + '$' + self.copayerId;
request({
url: config.pushNotificationsOpts.pushServerUrl + '/subscribe',
method: 'POST',
json: true,
body: opts
}, function(err, response) {
return cb(err, response);
var sub = Model.PushNotificationSub.create({
copayerId: self.copayerId,
token: opts.token,
packageName: opts.packageName,
platform: opts.platform,
});
self.storage.storePushNotificationSub(sub, cb);
};
WalletService.prototype.pushNotificationsUnsubscribe = function(cb) {
/**
* Unsubscribe this copayer to the Push Notifications service using the specified token.
* @param {Object} opts
* @param {string} opts.token - The token representing the app/device.
*/
WalletService.prototype.pushNotificationsUnsubscribe = function(opts, cb) {
if (!checkRequired(opts, ['token'], cb)) return;
var self = this;
request({
url: config.pushNotificationsOpts.pushServerUrl + '/unsubscribe',
method: 'POST',
json: true,
body: {
user: self.walletId + '$' + self.copayerId
}
}, function(err, response) {
return cb(err, response);
});
self.storage.removePushNotificationSub(self.copayerId, opts.token, cb);
};
module.exports = WalletService;

View File

@ -24,6 +24,7 @@ var collections = {
FIAT_RATES: 'fiat_rates',
TX_NOTES: 'tx_notes',
SESSIONS: 'sessions',
PUSH_NOTIFICATION_SUBS: 'push_notification_subs',
};
var Storage = function(opts) {
@ -74,6 +75,9 @@ Storage.prototype._createIndexes = function() {
walletId: 1,
txid: 1,
});
this.db.collection(collections.PUSH_NOTIFICATION_TOKENS).createIndex({
copayerId: 1,
});
};
Storage.prototype.connect = function(opts, cb) {
@ -890,6 +894,40 @@ Storage.prototype.storeSession = function(session, cb) {
}, cb);
};
Storage.prototype.fetchPushNotificationSubs = function(copayerId, cb) {
this.db.collection(collections.PUSH_NOTIFICATION_SUBS).find({
copayerId: copayerId,
}).toArray(function(err, result) {
if (err) return cb(err);
if (!result) return cb();
var tokens = _.map([].concat(result), function(r) {
return Model.PushNotificationSub.fromObj(r);
});
return cb(null, tokens);
});
};
Storage.prototype.storePushNotificationSub = function(pushNotificationSub, cb) {
this.db.collection(collections.PUSH_NOTIFICATION_SUBS).update({
copayerId: pushNotificationSub.copayerId,
token: pushNotificationSub.token,
}, pushNotificationSub, {
w: 1,
upsert: true,
}, cb);
};
Storage.prototype.removePushNotificationSub = function(copayerId, token, cb) {
this.db.collection(collections.PUSH_NOTIFICATION_SUBS).remove({
copayerId: copayerId,
token: token,
}, {
w: 1
}, cb);
};
Storage.prototype._dump = function(cb, fn) {
fn = fn || console.log;
cb = cb || function() {};