Skip to content

Instantly share code, notes, and snippets.

@matthisk
Last active June 22, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthisk/ffc48a36608013b657ff1a39443daa81 to your computer and use it in GitHub Desktop.
Save matthisk/ffc48a36608013b657ff1a39443daa81 to your computer and use it in GitHub Desktop.
Reproducing an issue with the Notification feed
var stream = require('getstream');
var client = stream.connect('API_KEY', 'API_SECRET');
var feed = client.feed('notification', 'bandsintown-' + Date.now());
function errorHandler(error) {
console.error('Error: ', error['error']);
}
var DEBUG = !!process.env.DEBUG;
var NUM_ACTIVITIES = 10;
var id = null;
var activities = [];
for (var i = 0; i < NUM_ACTIVITIES; i++) {
activities[i] = {
verb: 'noti',
object: i,
actor: 'matthisk'
};
}
function debug() {
if (DEBUG) {
console.log.apply(console, arguments);
}
}
var sequential = [
function() {
// 1. API Call: add an activity to feed X (notification) (direct insert?)
return feed.addActivities(activities.slice(0,3))
.then(function(res) {
debug('1: ', res);
id = res['activities'][0].id;
}, errorHandler);
},
function() {
// 2. API Call: read feed X. The aggregated activity is marked unread and unseen
return feed.get({ limit: 10 })
.then(function(res) {
debug('2: ', res);
}, errorHandler);
},
function() {
// 3. API Call: mark the activity as seen
return feed.get({ limit: 10, mark_seen: true, mark_read: true })
.then(function(res) {
debug('3.1: ', res);
debug('3.2: ', res['results'][0]['activities']);
}, errorHandler);
},
function() {
// 4. API Call: read feed X. The aggregated activity is marked as seen
return feed.get({ limit: 10 })
.then(function(res) {
debug('4: ', res);
console.log('IS_SEEN', res['results'][0]['is_seen']);
console.log('IS_READ', res['results'][0]['is_read']);
}, errorHandler);
},
function() {
// 5. API Call: delete the activity
return feed.removeActivity(id)
.then(function(res) {
debug('5: ', res);
}, errorHandler);
},
function() {
// 6. API Call: read feed X. The aggregated activity is marked as not seen
return feed.get({ limit: 10 })
.then(function(res) {
debug('6.1: ', res);
debug('6.2: ', res['results'][0]['activities']);
console.log('IS_SEEN', res['results'][0]['is_seen']);
console.log('IS_READ', res['results'][0]['is_read']);
}, errorHandler);
}
];
function sleep(t) {
return function() {
return new Promise(function(resolve, reject) {
debug('SLEEP FOR', t);
setTimeout(resolve, t);
});
};
}
sequential.reduce(function(mem, fn) {
if (mem) {
return mem.then(fn, errorHandler).then(sleep(3000));
} else {
return fn.call();
}
}, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment