Skip to content

Commit

Permalink
fix story upvote should happen at end of stream
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Jun 26, 2015
1 parent 9818186 commit 8570f9b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions server/boot/story.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var Rx = require('rx'),
sanitizeHtml = require('sanitize-html'),
moment = require('moment'),
mongodb = require('mongodb'),
// debug = require('debug')('freecc:cntr:story'),
debug = require('debug')('freecc:cntr:story'),
utils = require('../utils'),
observeMethod = require('../utils/rx').observeMethod,
saveUser = require('../utils/rx').saveUser,
Expand Down Expand Up @@ -251,18 +251,9 @@ module.exports = function(app) {

function upvote(req, res, next) {
var id = req.body.data.id;
var savedStory = findStoryById(id)
.flatMap(function(story) {
story.rank += 1;
story.upVotes.push({
upVotedBy: req.user.id,
upVotedByUsername: req.user.username
});
return saveInstance(story);
})
.shareReplay();
var story$ = findStoryById(id).shareReplay();

savedStory.flatMap(function(story) {
story$.flatMap(function(story) {
// find story author
return findUserById(story.author.userId);
})
Expand All @@ -277,7 +268,18 @@ module.exports = function(app) {
req.user.progressTimestamps.push(Date.now());
return saveUser(req.user);
})
.flatMap(savedStory)
.flatMap(function() {
return story$;
})
.flatMap(function(story) {
debug('upvoting');
story.rank += 1;
story.upVotes.push({
upVotedBy: req.user.id,
upVotedByUsername: req.user.username
});
return saveInstance(story);
})
.subscribe(
function(story) {
return res.send(story);
Expand Down

0 comments on commit 8570f9b

Please sign in to comment.