Skip to content

Instantly share code, notes, and snippets.

@LosantGists
Created April 12, 2016 15:09
Show Gist options
  • Select an option

  • Save LosantGists/46bbb145f2d7ee6fab95024004079387 to your computer and use it in GitHub Desktop.

Select an option

Save LosantGists/46bbb145f2d7ee6fab95024004079387 to your computer and use it in GitHub Desktop.
how-to-connect-discourse-to-slack
var result = {
newPost: null,
newPostId: null
};
// If this is the first time through, lastID will be undefined.
// Set it to the most recent post and return.
if(!payload.data.lastId) {
if(payload.data.latest.body.latest_posts.length) {
result.newPostId = payload.data.latest.body.latest_posts[0].id;
return result;
}
}
result.newPostId = Number.MAX_VALUE;
// Find the oldest post that is still newer than lastId.
payload.data.latest.body.latest_posts.forEach(function(post) {
if(post.id > payload.data.lastId) {
if(post.id < result.newPostId) {
result.newPost = post;
result.newPostId = post.id;
}
}
});
// If it didn't find one newer, set it back to lastId.
result.newPostId = result.newPost ? result.newPost.id : payload.data.lastId;
return result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment