From f73531703f90b6920e11d222e07d04de3f172e9d Mon Sep 17 00:00:00 2001 From: Camille Day Date: Tue, 24 Sep 2024 02:03:35 -0400 Subject: [PATCH 1/5] Adding answered field to post objects --- public/openapi/components/schemas/PostObject.yaml | 2 ++ public/openapi/read/topic/topic_id.yaml | 4 ++++ public/openapi/write/posts/pid.yaml | 4 ++++ public/openapi/write/posts/pid/replies.yaml | 2 ++ src/posts/data.js | 2 +- src/posts/summary.js | 2 +- 6 files changed, 14 insertions(+), 2 deletions(-) diff --git a/public/openapi/components/schemas/PostObject.yaml b/public/openapi/components/schemas/PostObject.yaml index ea91579cc6..f49297e938 100644 --- a/public/openapi/components/schemas/PostObject.yaml +++ b/public/openapi/components/schemas/PostObject.yaml @@ -16,6 +16,8 @@ PostObject: type: number deleted: type: boolean + answered: + type: number upvotes: type: number downvotes: diff --git a/public/openapi/read/topic/topic_id.yaml b/public/openapi/read/topic/topic_id.yaml index f0fde1b6c0..977eef98ab 100644 --- a/public/openapi/read/topic/topic_id.yaml +++ b/public/openapi/read/topic/topic_id.yaml @@ -67,6 +67,8 @@ get: type: number deleted: type: number + answered: + type: number upvotes: type: number downvotes: @@ -75,6 +77,8 @@ get: type: number deleterUid: type: number + answererUid: + type: number edited: type: number timestampISO: diff --git a/public/openapi/write/posts/pid.yaml b/public/openapi/write/posts/pid.yaml index 593a7acd01..8ba44a033a 100644 --- a/public/openapi/write/posts/pid.yaml +++ b/public/openapi/write/posts/pid.yaml @@ -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: diff --git a/public/openapi/write/posts/pid/replies.yaml b/public/openapi/write/posts/pid/replies.yaml index b021eec14e..78042a6afd 100644 --- a/public/openapi/write/posts/pid/replies.yaml +++ b/public/openapi/write/posts/pid/replies.yaml @@ -45,6 +45,8 @@ get: type: number deleted: type: number + answered: + type: number upvotes: type: number downvotes: diff --git a/src/posts/data.js b/src/posts/data.js index 3a4d303ff5..b2bcb8555c 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -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) { diff --git a/src/posts/summary.js b/src/posts/summary.js index 364baad1f7..f27c8c56b1 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -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); From 57eec9ce6c37d05bb26015c5499f666f53a38945 Mon Sep 17 00:00:00 2001 From: Camille Day Date: Tue, 24 Sep 2024 02:05:36 -0400 Subject: [PATCH 2/5] Adding API to toggle if a post is answered or not --- public/src/client/topic/events.js | 7 +++++++ src/api/posts.js | 8 ++++++++ src/controllers/write/posts.js | 12 ++++++++++++ src/posts/answer.js | 28 ++++++++++++++++++++++++++++ src/posts/index.js | 1 + 5 files changed, 56 insertions(+) create mode 100644 src/posts/answer.js diff --git a/public/src/client/topic/events.js b/public/src/client/topic/events.js index e091dd69c8..6d08192d8d 100644 --- a/public/src/client/topic/events.js +++ b/public/src/client/topic/events.js @@ -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, @@ -222,6 +225,10 @@ define('forum/topic/events', [ el.find('[component="post/bookmark/off"]').toggleClass('hidden', data.isBookmarked); } + function toggleAnswered(data) { + console.log('hi'); + } + function togglePostVote(data) { const post = $('[data-pid="' + data.post.pid + '"]'); post.find('[component="post/upvote"]').filter(function (index, el) { diff --git a/src/api/posts.js b/src/api/posts.js index 4e3917a008..80ff4360a8 100644 --- a/src/api/posts.js +++ b/src/api/posts.js @@ -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', diff --git a/src/controllers/write/posts.js b/src/controllers/write/posts.js index 1dc8cf6800..5e592fdeb8 100644 --- a/src/controllers/write/posts.js +++ b/src/controllers/write/posts.js @@ -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 })); }; diff --git a/src/posts/answer.js b/src/posts/answer.js new file mode 100644 index 0000000000..13586206f7 --- /dev/null +++ b/src/posts/answer.js @@ -0,0 +1,28 @@ +'use strict'; + +const db = require('../database'); +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; + }; +} diff --git a/src/posts/index.js b/src/posts/index.js index 9db52c6b27..9a055ead2c 100644 --- a/src/posts/index.js +++ b/src/posts/index.js @@ -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); From cd7b505a0215291ecbd9eab5e1910996748ef7b8 Mon Sep 17 00:00:00 2001 From: Camille Day Date: Tue, 24 Sep 2024 02:06:10 -0400 Subject: [PATCH 3/5] Adding unit testing for the answering API --- test/posts.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/posts.js b/test/posts.js index 20403e24cf..4b78fa378f 100644 --- a/test/posts.js +++ b/test/posts.js @@ -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) => { From 53ac399c51a34970a1b27df435058421fc41c76f Mon Sep 17 00:00:00 2001 From: Camille Day Date: Tue, 24 Sep 2024 14:51:59 -0400 Subject: [PATCH 4/5] Fixing lint errors --- public/src/client/topic/events.js | 2 +- src/posts/answer.js | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/public/src/client/topic/events.js b/public/src/client/topic/events.js index 6d08192d8d..9f7f14175e 100644 --- a/public/src/client/topic/events.js +++ b/public/src/client/topic/events.js @@ -225,7 +225,7 @@ define('forum/topic/events', [ el.find('[component="post/bookmark/off"]').toggleClass('hidden', data.isBookmarked); } - function toggleAnswered(data) { + function toggleAnswered() { console.log('hi'); } diff --git a/src/posts/answer.js b/src/posts/answer.js index 13586206f7..3eae8148ac 100644 --- a/src/posts/answer.js +++ b/src/posts/answer.js @@ -1,6 +1,5 @@ 'use strict'; -const db = require('../database'); const plugins = require('../plugins'); module.exports = function (Posts) { @@ -8,15 +7,15 @@ module.exports = function (Posts) { return await toggleAnswered('answer', pid, uid); }; - Posts.unanswer = async function (pid, uid) { - return await toggleAnswered('unanswer', 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 }); + const isAnswering = type === 'answer'; + await plugins.hooks.fire(`filter:post.${type}`, { pid: pid, uid: uid }); - await Posts.setPostFields(pid, { + await Posts.setPostFields(pid, { answered: isAnswering ? 1 : 0, answererUid: isAnswering ? uid : 0, }); @@ -24,5 +23,5 @@ module.exports = function (Posts) { const postData = await Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp']); return postData; - }; -} + } +}; From 5bca90aea32738d201a099d538352fc800e0464a Mon Sep 17 00:00:00 2001 From: Camille Day Date: Tue, 24 Sep 2024 22:29:29 -0400 Subject: [PATCH 5/5] Adding meaningful message to console log in toggleAnswered event --- public/src/client/topic/events.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/src/client/topic/events.js b/public/src/client/topic/events.js index 9f7f14175e..2f52e25446 100644 --- a/public/src/client/topic/events.js +++ b/public/src/client/topic/events.js @@ -226,7 +226,7 @@ define('forum/topic/events', [ } function toggleAnswered() { - console.log('hi'); + console.log('Toggling answered field.'); } function togglePostVote(data) {