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

Post Endorsement Feature [Resolves issues #34 #6 #33 #26] #36

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/PostObject.yaml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix linter errors

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ PostObject:
type: number
deleted:
type: boolean
endorse:
type: number
upvotes:
type: number
downvotes:
Expand Down
4 changes: 4 additions & 0 deletions public/openapi/read/topic/topic_id.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ get:
type: number
deleted:
type: number
endorse:
type: number
upvotes:
type: number
downvotes:
Expand All @@ -75,6 +77,8 @@ get:
type: number
deleterUid:
type: number
endorseUid:
type: number
edited:
type: number
timestampISO:
Expand Down
2 changes: 2 additions & 0 deletions public/openapi/write.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ paths:
$ref: 'write/posts/pid/upvoters.yaml'
/posts/{pid}/bookmark:
$ref: 'write/posts/pid/bookmark.yaml'
/posts/{pid}/endorse:
$ref: 'write/posts/pid/endorse.yaml'
/posts/{pid}/diffs:
$ref: 'write/posts/pid/diffs.yaml'
/posts/{pid}/diffs/{since}:
Expand Down
4 changes: 4 additions & 0 deletions public/openapi/write/posts/pid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ get:
type: number
deleted:
type: number
endorse:
type: number
upvotes:
type: number
downvotes:
type: number
deleterUid:
type: number
endorseUid:
type: number
edited:
type: number
replies:
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: {}
2 changes: 2 additions & 0 deletions public/openapi/write/posts/pid/replies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ get:
type: number
deleted:
type: number
endorse:
type: number
upvotes:
type: number
downvotes:
Expand Down
6 changes: 6 additions & 0 deletions public/src/client/topic/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ define('forum/topic/events', [
'posts.bookmark': togglePostBookmark,
'posts.unbookmark': togglePostBookmark,

'posts.endorse': toggleEndorse,
'posts.unendorse': toggleEndorse,

'posts.upvote': togglePostVote,
'posts.downvote': togglePostVote,
'posts.unvote': togglePostVote,
Expand Down Expand Up @@ -222,6 +225,9 @@ define('forum/topic/events', [
el.find('[component="post/bookmark/off"]').toggleClass('hidden', data.isBookmarked);
}

function toggleEndorse() {
console.log('inside toggle endorse function');
}
function togglePostVote(data) {
const post = $('[data-pid="' + data.post.pid + '"]');
post.find('[component="post/upvote"]').filter(function (index, el) {
Expand Down
11 changes: 9 additions & 2 deletions public/src/client/topic/postTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@
return bookmarkPost($(this), getData($(this), 'data-pid'));
});

postContainer.on('click', '[component="post/endorse"]', function () {
return endorsePost($(this), getData($(this), 'data-pid'));
});

Check failure on line 135 in public/src/client/topic/postTools.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
postContainer.on('click', '[component="post/upvote"]', function () {
return votes.toggleVote($(this), '.upvoted', 1);
});
Expand Down Expand Up @@ -236,7 +240,7 @@
});

postContainer.on('click', '[component="post/purge"]', function () {
purgePost($(this));

Check failure on line 243 in public/src/client/topic/postTools.js

View workflow job for this annotation

GitHub Actions / test

'purgePost' is not defined
});

postContainer.on('click', '[component="post/move"]', function () {
Expand Down Expand Up @@ -404,8 +408,11 @@
postAction(action, pid);
}

function purgePost(button) {
postAction('purge', getData(button, 'data-pid'));
async function endorsePost(button, pid) {
const method = 'put';
const action = 'endorse';
console.log('attempting API call');
api[method](`/posts/${pid}/${action}`).catch(alerts.error);
}

async function postAction(action, pid) {
Expand Down
7 changes: 7 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ postsAPI.edit = async function (caller, data) {
return returnData;
};

postsAPI.endorse = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'endorse', 'endorsed', '', data);
};
postsAPI.unendorse = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'unendorse', 'unendorsed', '', data);
};

postsAPI.delete = async function (caller, data) {
await deleteOrRestore(caller, data, {
command: 'delete',
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/write/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ Posts.unbookmark = async (req, res) => {
helpers.formatApiResponse(200, res);
};

Posts.endorse = async (req, res) => {
const data = await mock(req);
await api.posts.endorse(req, data);
helpers.formatApiResponse(200, res);
};
Posts.unendorse = async (req, res) => {
const data = await mock(req);
await api.posts.unendorse(req, data);
helpers.formatApiResponse(200, res);
};

Posts.getDiffs = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.getDiffs(req, { ...req.params }));
};
Expand Down
2 changes: 1 addition & 1 deletion src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const utils = require('../utils');
const intFields = [
'uid', 'pid', 'tid', 'deleted', 'timestamp',
'upvotes', 'downvotes', 'deleterUid', 'edited',
'replies', 'bookmarks',
'replies', 'bookmarks', 'endorse', 'endorseUid',
];

module.exports = function (Posts) {
Expand Down
27 changes: 27 additions & 0 deletions src/posts/endorse.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear logic!

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const plugins = require('../plugins');

module.exports = function (Posts) {
Posts.endorse = async function (pid, uid) {
return await toggleEndorse('endorse', pid, uid);
};

Posts.unendorse = async function (pid, uid) {
return await toggleEndorse('unendorse', pid, uid);
};

async function toggleEndorse(type, pid, uid) {
const isEndorsing = type === 'endorse';
await plugins.hooks.fire(`filter:post.${type}`, { pid: pid, uid: uid });

await Posts.setPostFields(pid, {
endorse: isEndorsing ? 1 : 0,
endorseUid: isEndorsing ? uid : 0,
});

const postData = await Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp']);

return postData;
}
};
1 change: 1 addition & 0 deletions src/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require('./tools')(Posts);
require('./votes')(Posts);
require('./bookmarks')(Posts);
require('./queue')(Posts);
require('./endorse')(Posts);
require('./diffs')(Posts);
require('./uploads')(Posts);

Expand Down
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', 'endorse'].concat(options.extraFields);

let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
Expand Down
3 changes: 3 additions & 0 deletions src/routes/write/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ 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, 'put', '/:pid/endorse', middlewares, controllers.write.posts.endorse);
setupApiRoute(router, 'put', '/:pid/endorse', 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
8 changes: 8 additions & 0 deletions test/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ describe('Post\'s', () => {
});
});

describe('endorsing', () => {
it('should endorse a post', async () => {
const data = await apiPosts.endorse({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
const isEndorsed = await posts.getPostField(postData.pid, 'endorse');
assert.strictEqual(isEndorsed, 1);
});
});

describe('post tools', () => {
it('should error if data is invalid', (done) => {
socketPosts.loadPostTools({ uid: globalModUid }, null, (err) => {
Expand Down
Loading