Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.4.3 /2020-03-17
=================
* Updated code to avoid duplicated subscriptions by adding a hash to subscription object

0.4.2 / 2020-02-16
==================

Expand Down
18 changes: 15 additions & 3 deletions logic/observer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const errors = require('../util/errors'),
const crypto = require ('crypto')
const errors = require('../util/errors'),
TransactionWatcher = require('./transaction-watcher'),
Notifier = require('./notifier'),
storage = require('./storage'),
Expand Down Expand Up @@ -38,10 +39,21 @@ class Observer {
}

subscribe(subscriptionParams, user) {
//TODO: prevent duplicate subscriptions by checking subscription hash (fields "account", "asset_type" etc.)
//https://www.npmjs.com/package/farmhash
return this.loadSubscriptions()
.then(() => {
// Create hash in the subscription to avoid duplication

let hashData = `${subscriptionParams.reaction_url} ${subscriptionParams.account} ${subscriptionParams.memo} ${subscriptionParams.operation_types} ${subscriptionParams.asset_code} ${subscriptionParams.asset_issuer} ${subscriptionParams.expires}`
let hash = crypto.createHash('md5').update(hashData).digest("hex").toString();
subscriptionParams.hash = hash

let subscription = this.subscriptions.find(s => s.hash == hash)

if(subscription){

return subscription
}

if (this.getActiveSubscriptionsCount() >= config.maxActiveSubscriptions) {
return Promise.reject(errors.forbidden('Max active subscriptions exceeded.'))
}
Expand Down
15 changes: 15 additions & 0 deletions logic/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class Storage {
})
}

/**
*
* @param {*} hash - the hash of the subscription
*/

async fetchSubscriptioHash(hash){
this.provider.fetchSubscriptioHash(hash)
.then(subscription =>{
return subscription
})
}


/**
* Load next notification from db
* @param {*} subscriptionId - subscription id
Expand Down Expand Up @@ -208,6 +221,8 @@ class Storage {
subscription.expires = expirationDate
}

subscription.hash = subscriptionParams.hash

return this.provider.saveSubscription(subscription)
}

Expand Down
6 changes: 6 additions & 0 deletions models/subscription-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class SubscriptionModel extends Model {
* Cached notifications, associated with the subscription
*/
notifications

/**
* Subscription hash to avoid duplicated subscriptions
*/
hash

}

module.exports = SubscriptionModel
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@stellar-expert/operations-notifier",
"license": "MIT",
"private": true,
"version": "0.4.2",
"version": "0.4.3",
"author": "orbitlens<orbit.lens@stellar.expert>",
"description": "Stellar operations observer and notifier.",
"main": "app.js",
Expand Down
4 changes: 4 additions & 0 deletions persistence-layer/mongodb-storage-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class MongoDBStorageProvider extends StorageProvider {
return Subscription.findById(id)
}

fetchSubscriptioHash(hash){
return Subscription.findOne({hash})
}

fetchNextNotification(subscriptionId) {
return Notification.findOne({subscriptions: toObjectId(subscriptionId)})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const subscriptionSchema = new Schema({
reaction_url: {type: String},
delivery_failures: {type: Number, default: 0},
sent: {type: Number, default: 0},
expires: {type: Date}
expires: {type: Date},
hash:{type:String}
},
{
timestamps: {createdAt: 'created', updatedAt: 'updated'}
Expand Down