Skip to content

Commit

Permalink
Merge pull request #17 from CMU-313/answered-field
Browse files Browse the repository at this point in the history
Add Ability to Toggle Posts as Answered Vs. Unanswered
  • Loading branch information
katcday authored Sep 25, 2024
2 parents e0e965d + 5bca90a commit 64d9e6f
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 2 deletions.
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

0 comments on commit 64d9e6f

Please sign in to comment.