Skip to content
Snippets Groups Projects
Commit b390a3cf authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

small code refactoring

parent 1ad68b35
No related branches found
No related tags found
No related merge requests found
...@@ -108,16 +108,17 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es ...@@ -108,16 +108,17 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
}); });
} }
function getWalletEventsAsNotifications(options) { function getWalletNotifications(options) {
options = options || {}; options = options || {};
var wallet = options.wallet || csWallet; var wallet = options.wallet || csWallet;
if (!wallet.data.events ||!wallet.data.events.length) return []; return new Promise(function(resolve) {
if (!wallet.data || !wallet.data.events ||!wallet.data.events.length) return resolve([]);
// Add some wallet events as notifications // Add some wallet events as notifications
var time = csHttp.date.now() - filterTranslations.MEDIAN_TIME_OFFSET; var time = csHttp.date.now() - filterTranslations.MEDIAN_TIME_OFFSET;
return (wallet.data.events || []).reduce(function(res, event) { var result = (wallet.data.events || []).reduce(function(res, event) {
if (event.type != "warn") return res; // Skip NOT warn events if (event.type != "warn" && event.type != "error") return res; // Keep only warn and error events
var notification = new EsNotification({}, function(self) { var notification = new EsNotification({}, function(self) {
if (!self.read) { if (!self.read) {
self.read = true; self.read = true;
...@@ -136,6 +137,10 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es ...@@ -136,6 +137,10 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
notification.messageParams = event.messageParams; notification.messageParams = event.messageParams;
return res.concat(notification); return res.concat(notification);
}, []); }, []);
resolve(result);
});
} }
// Load user notifications // Load user notifications
...@@ -146,7 +151,6 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es ...@@ -146,7 +151,6 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
} }
options.from = options.from || 0; options.from = options.from || 0;
options.size = options.size || constants.DEFAULT_LOAD_SIZE; options.size = options.size || constants.DEFAULT_LOAD_SIZE;
var wallet = options.wallet || csWallet;
var request = { var request = {
query: createFilterQuery(options.pubkey, options), query: createFilterQuery(options.pubkey, options),
sort : [ sort : [
...@@ -157,18 +161,24 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es ...@@ -157,18 +161,24 @@ angular.module('cesium.es.notification.services', ['cesium.platform', 'cesium.es
_source: fields.commons _source: fields.commons
}; };
return that.raw.postSearch(request) return $q.all([
.then(function(res) {
// Get wallet events (as notifications) // Get wallet events (as notifications)
var walletEvents = getWalletEventsAsNotifications(options); getWalletNotifications(options),
// Load notification from ES node
that.raw.postSearch(request)
]).then(function(res) {
var walletNotifs = res[0] || [];
res = res[1];
if (!res.hits || !res.hits.total) return walletEvents; if (!res.hits || !res.hits.total) return walletNotifs;
var notifications = res.hits.hits.reduce(function(res, hit) { var notifications = res.hits.hits.reduce(function(res, hit) {
var item = new EsNotification(hit._source, markNotificationAsRead); var item = new EsNotification(hit._source, markNotificationAsRead);
item.id = hit._id; item.id = hit._id;
return res.concat(item); return res.concat(item);
}, walletEvents || []); }, walletNotifs);
return csWot.extendAll(notifications); return csWot.extendAll(notifications);
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment