Skip to content

Instantly share code, notes, and snippets.

@dypsilon
Created March 24, 2017 19:11
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 dypsilon/b500cd1192114db887bf565ecc3ae249 to your computer and use it in GitHub Desktop.
Save dypsilon/b500cd1192114db887bf565ecc3ae249 to your computer and use it in GitHub Desktop.
function addComment(comment) {
return fetchPostBySlug(comment.postSlug)
.then((post) => {
if (!post.isCommentingEnabled) {
return Promise.reject(new Error('Commenting is disabled.'));
}
return isUserAllowedToComment(post, comment.user)
.then((isAllowed) => ({ isAllowed, post }));
}).then((input) => {
if (!input.isAllowed) {
return Promise.reject(
new Error('The user is not allowed to comment on this post.')
);
}
return insertComment(input.post.id, comment.user, comment.message);
}).then((result) => {
return result.record;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment