Skip to content

fix(): disappearing likes, ssl redirect #332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions server/controllers/like.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import algoliasearch from 'algoliasearch';
import Like from '../models/like.model';
import Vote from '../models/vote.model';
import Post from '../models/post.model';

function syncAlgolia(query) {
Expand Down Expand Up @@ -50,35 +50,41 @@ function syncAlgolia(query) {
async function likePost(req, res) {
const { postId } = req.params;
const { _id: userId } = req.user;
const query = { postId, userId };
const like = new Like(query);
const likeActive = await Like.findOne(query);
const likeCount = likeActive ? -1 : 1;
const query = {
postId,
userId,
direction: 'upvote',
active: true,
};

const like = new Vote(query);
const likeActive = await Vote.findOne(query);
const score = likeActive ? -1 : 1;

if (likeActive) {
await Like.deleteOne({ _id: likeActive._id });
await Vote.deleteOne({ _id: likeActive._id });
} else {
await like.save();
}

Post
.findOneAndUpdate(
{ _id: postId },
{ $inc: { likeCount } },
{ $inc: { score } },
{ new: true },
).exec(async (err, reply) => {
if (err) {
return res.status(500).json({ message: 'Error liking post' });
}

syncAlgolia({
likeCount: reply.likeCount,
score: reply.score,
objectID: reply.id,
});

return res.json({
likeCount: reply.likeCount,
likeActive: !(likeActive),
score: reply.score,
upvoted: !(likeActive),
});
});
}
Expand Down
10 changes: 5 additions & 5 deletions server/helpers/post.helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import algoliasearch from 'algoliasearch';
import Like from '../models/like.model';
import Vote from '../models/vote.model';
import Favorite from '../models/favorite.model';
import { getAdFreeMp3 } from '../helpers/mp3.helper';
import { getPrivateRss } from '../helpers/rss.helper';
Expand Down Expand Up @@ -46,20 +46,20 @@ async function addPostData(post, fullUser) {
const query = {
userId: fullUser ? fullUser._id : null,
postId: post._id,
active: true,
};

const likeActive = await Like
const upvoted = await Vote
.findOne(query)
.exec(l => Promise.resolve(l));

const bookmark = await Favorite
.findOne(query)
.exec(l => Promise.resolve(l));

post.likeCount = post.likeCount || 0 // eslint-disable-line
post.upvoted = !!(upvoted) // eslint-disable-line
post.totalFavorites = post.totalFavorites || 0 // eslint-disable-line
post.likeActive = !!(likeActive) // eslint-disable-line
post.bookmarkActive = !!(bookmark && bookmark.active) // eslint-disable-line
post.bookmarked = !!(bookmark && bookmark.active) // eslint-disable-line
post.rss = '/rss/public/all' // eslint-disable-line

if (fullUser && fullUser.subscription && fullUser.subscription.active) {
Expand Down
1 change: 0 additions & 1 deletion server/models/post.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ PostSchema.statics = {
*/
standardSelectForFind: [
'content',
'likeCount',
'totalFavorites',
'title',
'date',
Expand Down
45 changes: 22 additions & 23 deletions server/routes/post.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,31 @@ router.route('/search')

router.route('/recommendations')
.get(
expressJwt({ secret: config.jwtSecret })
, loadFullUser
, postCtrl.recommendations
expressJwt({ secret: config.jwtSecret }),
loadFullUser,
postCtrl.recommendations,
);

router.route('/:postId')
.get(
expressJwt({ secret: config.jwtSecret, credentialsRequired: false })
, loadFullUser
, postCtrl.get
expressJwt({ secret: config.jwtSecret, credentialsRequired: false }),
loadFullUser,
postCtrl.get,
);

// Get related links associated with postId
router.route('/:postId/related-links')
.get(
expressJwt({ secret: config.jwtSecret, credentialsRequired: false })
, relatedLinkCtrl.list
expressJwt({ secret: config.jwtSecret, credentialsRequired: false }),
relatedLinkCtrl.list,
);

// Add a related-link to postId:
router.route('/:postId/related-link')
.post(
expressJwt({ secret: config.jwtSecret })
, validate(paramValidation.relatedLinkCreate)
, relatedLinkCtrl.create
expressJwt({ secret: config.jwtSecret }),
validate(paramValidation.relatedLinkCreate),
relatedLinkCtrl.create,
);

router.route('/:postId/like')
Expand All @@ -77,22 +78,20 @@ router.route('/:postId/like')

router.route('/:postId/upvote')
.post(
expressJwt({ secret: config.jwtSecret })
, transferField({ source: 'post', target: 'entity' })
, voteCtrl.findVote
, voteCtrl.upvote // normal upvoting via vote model
, postCtrl.upvote // special-case: uses racoon upvoting just for posts.
, voteCtrl.finish
expressJwt({ secret: config.jwtSecret }),
transferField({ source: 'post', target: 'entity' }),
voteCtrl.findVote,
voteCtrl.upvote, // normal upvoting via vote model
voteCtrl.finish,
);

router.route('/:postId/downvote')
.post(
expressJwt({ secret: config.jwtSecret })
, transferField({ source: 'post', target: 'entity' })
, voteCtrl.findVote
, voteCtrl.downvote
, postCtrl.downvote
, voteCtrl.finish
expressJwt({ secret: config.jwtSecret }),
transferField({ source: 'post', target: 'entity' }),
voteCtrl.findVote,
voteCtrl.downvote,
voteCtrl.finish,
);

router.route('/:postId/bookmark')
Expand Down
12 changes: 6 additions & 6 deletions server/tests/post.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ describe('## Post APIs', () => {
.expect(httpStatus.OK)
.then((res) => {
const like = res.body;
expect(like).to.have.property('likeCount').that.is.a('number');
expect(like).to.have.property('likeActive').that.is.a('boolean');
expect(like.likeActive).to.be.true; //eslint-disable-line
expect(like).to.have.property('score').that.is.a('number');
expect(like).to.have.property('upvoted').that.is.a('boolean');
expect(like.upvoted).to.be.true; //eslint-disable-line
done();
})
.catch(done);
Expand All @@ -185,9 +185,9 @@ describe('## Post APIs', () => {
.expect(httpStatus.OK)
.then((res) => {
const like = res.body;
expect(like).to.have.property('likeCount').that.is.a('number');
expect(like).to.have.property('likeActive').that.is.a('boolean');
expect(like.likeActive).to.be.false; //eslint-disable-line
expect(like).to.have.property('score').that.is.a('number');
expect(like).to.have.property('upvoted').that.is.a('boolean');
expect(like.upvoted).to.be.false; //eslint-disable-line
done();
})
.catch(done);
Expand Down