Skip to content
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

Backend for endorsement feature (do not accept) #20

Open
wants to merge 2 commits into
base: f24
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions public/openapi/components/schemas/PostObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ PostObject:
type: number
votes:
type: number
endorsedBy:
type: number
nullable: true
timestampISO:
type: string
description: An ISO 8601 formatted date string (complementing `timestamp`)
Expand Down
15 changes: 15 additions & 0 deletions public/openapi/write/posts/pid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ get:
type: boolean
downvoted:
type: boolean
endorsedBy:
type: number
description: ID of the user who endorsed the post
required:
- pid
- uid
- tid
- content
- timestamp
- upvotes
- downvotes
- timestampISO
- upvoted
- downvoted
- endorsedBy
put:
tags:
- posts
Expand Down
26 changes: 26 additions & 0 deletions public/openapi/write/posts/pid/endorse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
put:
tags:
- posts
summary: endorse a post
description: This operation endorses a post.
parameters:
- in: path
name: pid
schema:
type: string
required: true
description: a valid post id
example: 2
responses:
'200':
description: Post successfully endorsed
content:
application/json:
schema:
type: object
properties:
status:
$ref: ../../../components/schemas/Status.yaml#/Status
response:
type: object
properties: {}
14 changes: 14 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,20 @@
return await apiHelpers.postCommand(caller, 'unvote', 'voted', '', data);
};

postsAPI.endorse = async function (caller, data) {
if (!data || !data.pid || !data.uid) {
throw new Error('[[error:invalid-data]]');
}
await posts.endorse(data.pid, data.uid);
};

postsAPI.unendorse = async function (caller, data) {
if (!data || !data.pid || !data.uid) {

Check failure on line 319 in src/api/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
throw new Error('[[error:invalid-data]]');

Check failure on line 320 in src/api/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
}

Check failure on line 321 in src/api/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
await posts.unendorse(data.pid, data.uid);

Check failure on line 322 in src/api/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
};

postsAPI.getVoters = async function (caller, data) {
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
Expand Down
12 changes: 12 additions & 0 deletions src/controllers/write/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@
helpers.formatApiResponse(200, res);
};

Posts.endorse = async (req, res) => {
const { pid, uid } = req.body;

Check failure on line 157 in src/controllers/write/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
await api.posts.endorse(req, { pid, uid });

Check failure on line 158 in src/controllers/write/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
helpers.formatApiResponse(200, res);

Check failure on line 159 in src/controllers/write/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
};

Posts.unendorse = async (req, res) => {
const { pid, uid } = req.body;

Check failure on line 163 in src/controllers/write/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
await api.posts.unendorse(req, { pid, uid });

Check failure on line 164 in src/controllers/write/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
helpers.formatApiResponse(200, res);

Check failure on line 165 in src/controllers/write/posts.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
};

Posts.getDiffs = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.getDiffs(req, { ...req.params }));
};
Expand Down
3 changes: 3 additions & 0 deletions src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,8 @@ function modifyPost(post, fields) {
if (post.hasOwnProperty('edited')) {
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
}
if (post.hasOwnProperty('endorsedBy')) {
post.endorsedBy = post.endorsedBy ? post.endorsedBy : null;
}
}
}
20 changes: 20 additions & 0 deletions src/posts/endorsements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

module.exports = function (Posts) {
Posts.endorse = async function (pid, uid) {
const postData = await Posts.getPostFields(pid, ['endorsedBy']);
if (postData.endorsedBy) {
throw new Error('[[error:post-already-endorsed]]');
}
await Posts.setPostField(pid, 'endorsedBy', uid);
};

Posts.unendorse = async function (pid, uid) {
const postData = await Posts.getPostFields(pid, ['endorsedBy']);
if (postData.endorsedBy === uid) {
await Posts.setPostField(pid, 'endorsedBy', null);
} else {
throw new Error('[[error:not-endorsed-by-user]]');
}
};
};
2 changes: 1 addition & 1 deletion src/posts/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (Posts) {
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];

const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);
const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle', 'endorsedBy'].concat(options.extraFields);

let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
Expand Down
17 changes: 17 additions & 0 deletions src/posts/votes.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ module.exports = function (Posts) {
return await db.getSetsMembers(pids.map(pid => `pid:${pid}:upvote`));
};

Posts.endorse = async function (pid, uid) {
const postData = await Posts.getPostFields(pid, ['endorsedBy']);
if (postData.endorsedBy) {
throw new Error('[[error:post-already-endorsed]]');
}
await Posts.setPostField(pid, 'endorsedBy', uid);
};

Posts.unendorse = async function (pid, uid) {
const postData = await Posts.getPostFields(pid, ['endorsedBy']);
if (postData.endorsedBy === uid) {
await Posts.setPostField(pid, 'endorsedBy', null);
} else {
throw new Error('[[error:not-endorsed-by-user]]');
}
};

function voteInProgress(pid, uid) {
return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].includes(parseInt(pid, 10));
}
Expand Down
2 changes: 2 additions & 0 deletions src/routes/write/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module.exports = function () {
setupApiRoute(router, 'put', '/:pid/bookmark', middlewares, controllers.write.posts.bookmark);
setupApiRoute(router, 'delete', '/:pid/bookmark', middlewares, controllers.write.posts.unbookmark);

setupApiRoute(router, 'post', '/:pid/endorse', middlewares, controllers.write.posts.endorse);
setupApiRoute(router, 'post', '/:pid/unendorse', middlewares, controllers.write.posts.unendorse);
setupApiRoute(router, 'get', '/:pid/diffs', [middleware.assert.post], controllers.write.posts.getDiffs);
setupApiRoute(router, 'get', '/:pid/diffs/:since', [middleware.assert.post], controllers.write.posts.loadDiff);
setupApiRoute(router, 'put', '/:pid/diffs/:since', middlewares, controllers.write.posts.restoreDiff);
Expand Down
1 change: 1 addition & 0 deletions src/socket.io/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SocketPosts = module.exports;

require('./posts/votes')(SocketPosts);
require('./posts/tools')(SocketPosts);
require('./posts/endorsements')(SocketPosts);

SocketPosts.getRawPost = async function (socket, pid) {
sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/raw');
Expand Down
22 changes: 22 additions & 0 deletions src/socket.io/posts/endorsements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const api = require('../../api');
const sockets = require('../index');

module.exports = function (SocketPosts) {
SocketPosts.endorse = async function (socket, data) {
if (!data || !data.pid || !data.uid) {
throw new Error('[[error:invalid-data]]');
}
sockets.warnDeprecated(socket, 'POST /api/v3/posts/:pid/endorse');
return await api.posts.endorse(socket, { pid: data.pid, uid: data.uid });
};

SocketPosts.unendorse = async function (socket, data) {
if (!data || !data.pid || !data.uid) {
throw new Error('[[error:invalid-data]]');
}
sockets.warnDeprecated(socket, 'POST /api/v3/posts/:pid/unendorse');
return await api.posts.unendorse(socket, { pid: data.pid, uid: data.uid });
};
};
40 changes: 40 additions & 0 deletions test/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,46 @@ describe('Post\'s', () => {
});
});

describe('endorsing', () => {
it('should allow a user to endorse a post', async () => {
await posts.endorse(postData.pid, voterUid);
const updatedPost = await posts.getPostFields(postData.pid, ['endorsedBy']);
assert.equal(updatedPost.endorsedBy, voterUid);
});

it('should not allow a user to endorse the same post again', async () => {
try {
await posts.endorse(postData.pid, voterUid);
assert.fail('Expected error not thrown');
} catch (err) {
assert.equal(err.message, '[[error:post-already-endorsed]]');
}
});

it('should allow the user to unendorse the post', async () => {
await posts.unendorse(postData.pid, voterUid);
const updatedPost = await posts.getPostFields(postData.pid, ['endorsedBy']);
console.log(updatedPost);
assert.equal(updatedPost.endorsedBy, null);
});

it('should not allow a user to unendorse a post they have not endorsed', async () => {
try {
await posts.unendorse(postData.pid, voterUid);
assert.fail('Expected error not thrown');
} catch (err) {
assert.equal(err.message, '[[error:not-endorsed-by-user]]');
}
});

it('should allow a different user to endorse the post after it has been unendorsed', async () => {
const newVoterUid = await user.create({ username: 'newEndorser' });
await posts.endorse(postData.pid, newVoterUid);
const updatedPost = await posts.getPostFields(postData.pid, ['endorsedBy']);
assert.equal(updatedPost.endorsedBy, newVoterUid);
});
});

describe('voting', () => {
it('should fail to upvote post if group does not have upvote permission', async () => {
await privileges.categories.rescind(['groups:posts:upvote', 'groups:posts:downvote'], cid, 'registered-users');
Expand Down