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

Add Ability to Toggle Posts as Answered Vs. Unanswered #17

Merged
merged 5 commits into from
Sep 25, 2024
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
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/PostObject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ PostObject:
type: number
deleted:
type: boolean
answered:
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
answered:
type: number
upvotes:
type: number
downvotes:
Expand All @@ -75,6 +77,8 @@ get:
type: number
deleterUid:
type: number
answererUid:
type: number
edited:
type: number
timestampISO:
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
answered:
type: number
upvotes:
type: number
downvotes:
type: number
deleterUid:
type: number
answererUid:
type: number
edited:
type: number
replies:
Expand Down
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
answered:
type: number
upvotes:
type: number
downvotes:
Expand Down
7 changes: 7 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.answer': toggleAnswered,
'posts.unanswer': toggleAnswered,

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

function toggleAnswered() {
console.log('Toggling answered field.');
}

function togglePostVote(data) {
const post = $('[data-pid="' + data.post.pid + '"]');
post.find('[component="post/upvote"]').filter(function (index, el) {
Expand Down
8 changes: 8 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ postsAPI.edit = async function (caller, data) {
return returnData;
};

postsAPI.answer = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'answer', 'answered', '', data);
};

postsAPI.unanswer = async function (caller, data) {
return await apiHelpers.postCommand(caller, 'unanswer', 'answered', '', data);
};

postsAPI.delete = async function (caller, data) {
await deleteOrRestore(caller, data, {
command: 'delete',
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 @@ Posts.unbookmark = async (req, res) => {
helpers.formatApiResponse(200, res);
};

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

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

Posts.getDiffs = async (req, res) => {
helpers.formatApiResponse(200, res, await api.posts.getDiffs(req, { ...req.params }));
};
Expand Down
27 changes: 27 additions & 0 deletions src/posts/answer.js
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.answer = async function (pid, uid) {
return await toggleAnswered('answer', pid, uid);
};

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

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

await Posts.setPostFields(pid, {
answered: isAnswering ? 1 : 0,
answererUid: isAnswering ? uid : 0,
});

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

return postData;
}
};
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', 'answered', 'answererUid',
];

module.exports = function (Posts) {
Expand Down
1 change: 1 addition & 0 deletions src/posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require('./recent')(Posts);
require('./tools')(Posts);
require('./votes')(Posts);
require('./bookmarks')(Posts);
require('./answer')(Posts);
require('./queue')(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', 'answered'].concat(options.extraFields);

let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
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('answering', async () => {
it('should mark post answered', async () => {
const data = await apiPosts.answer({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
const isAnswered = await posts.getPostField(postData.pid, 'answered');
assert.strictEqual(isAnswered, 1);
});
});

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