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 implementation of isAnswered feature, including isAnswered field in posts, storing answered status, adding logic/property to edit post data, and API endpoints #27

Open
wants to merge 12 commits into
base: f24
Choose a base branch
from
Open
Binary file added dump.rdb
Binary file not shown.
16 changes: 16 additions & 0 deletions src/controllers/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ const relative_path = nconf.get('relative_path');
const upload_url = nconf.get('upload_url');
const validSorts = ['oldest_to_newest', 'newest_to_oldest', 'most_votes'];

// Assisted by Copilot
topicsController.updateAnsweredStatus = async function (req, res) {
const { pid } = req.params; // Updated with destructuring // Post ID from URL params
const { isAnswered } = req.body; // Extract the 'isAnswered' value from the request body
if (!req.loggedIn) {
return res.status(403).json({ error: 'User not logged in.' });
}
try {
await topics.setPostAnsweredStatus(pid, isAnswered);
res.status(200).json({ message: 'Post answered status updated successfully.' });
} catch (error) { // Corrected brace-style
console.error(`Failed to update answered status: ${error.message}`);
res.status(500).json({ error: 'Failed to update answered status.' });
}
};

topicsController.get = async function getTopic(req, res, next) {
const tid = req.params.topic_id;
if (
Expand Down
13 changes: 13 additions & 0 deletions src/posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ module.exports = function (Posts) {
const oldContent = postData.content; // for diffing purposes
const editPostData = getEditPostData(data, topicData, postData);

// Logic to handle `isAnswered`
if (data.hasOwnProperty('isAnswered')) {
editPostData.isAnswered = data.isAnswered;
}

if (data.handle) {
editPostData.handle = data.handle;
}
Expand Down Expand Up @@ -197,6 +202,14 @@ module.exports = function (Posts) {
editor: data.uid,
};

if (data.hasOwnProperty('isAnswered')) {
editPostData.isAnswered = data.isAnswered;
}

if (data.hasOwnProperty('isAnswered')) {
editPostData.isAnswered = data.isAnswered;
}

// For posts in scheduled topics, if edited before, use edit timestamp
editPostData.edited = topicData.scheduled ? (postData.edited || postData.timestamp) + 1 : Date.now();

Expand Down
11 changes: 10 additions & 1 deletion src/posts/topics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

// Assisted by Copilot
'use strict';

Check failure on line 2 in src/posts/topics.js

View workflow job for this annotation

GitHub Actions / test

Expected newline before "use strict" directive

const topics = require('../topics');
const user = require('../user');
Expand All @@ -9,6 +9,10 @@
Posts.getPostsFromSet = async function (set, start, stop, uid, reverse) {
const pids = await Posts.getPidsFromSet(set, start, stop, reverse);
const posts = await Posts.getPostsByPids(pids, uid);
// Logic to retrieve the `isAnswered` field
posts.forEach((post) => {
post.isAnswered = post.isAnswered || false; // Default to false if not set
});
return await user.blocks.filter(uid, posts);
};

Expand All @@ -21,6 +25,11 @@
return isArray ? result : result[0];
};

Posts.setPostAnsweredStatus = async function (pid, isAnswered) {
// Store the answered status for the post
await Posts.setPostField(pid, 'isAnswered', isAnswered);
};

Posts.getTopicFields = async function (pid, fields) {
const tid = await Posts.getPostField(pid, 'tid');
return await topics.getTopicFields(tid, fields);
Expand Down
3 changes: 2 additions & 1 deletion src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const express = require('express');

const uploadsController = require('../controllers/uploads');
const topicsController = require('../controllers/topics'); // topics controller added
const helpers = require('./helpers');

module.exports = function (app, middleware, controllers) {
Expand Down Expand Up @@ -32,7 +33,7 @@ module.exports = function (app, middleware, controllers) {
middleware.uploads.ratelimit,
middleware.applyCSRF,
];

router.get('/post/:pid/answered', [...middlewares, middleware.ensureLoggedIn], helpers.tryRoute(topicsController.updateAnsweredStatus));
router.post('/post/upload', postMiddlewares, helpers.tryRoute(uploadsController.uploadPost));
router.post('/user/:userslug/uploadpicture', [
...middlewares,
Expand Down
39 changes: 39 additions & 0 deletions test/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,45 @@
});
});

describe('isAnswered', () => {
let testTopic;
let replyPid;
let fooUid;

before(async () => {
fooUid = await User.create({ username: 'foo_test_user', password: '123456' });

const testCategory = await categories.create({
name: 'Answered Test Category',
description: 'Category for testing the isAnswered feature',
});

const topicResponse = await topics.post({
uid: fooUid,
title: 'Test Topic for isAnswered',
content: 'Initial content of the test topic',
cid: testCategory.cid,
});
testTopic = topicResponse.topicData;
it('should default to isAnswered as false for a new topic', async () => {
const topicData = await topics.getTopicData(testTopic.tid);
assert.strictEqual(topicData.isAnswered, false, 'New topics should have isAnswered set to false by default');
});

Check failure on line 2533 in test/topics.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
it('should correctly set isAnswered to true when a post is marked as an answer', async () => {
await topics.setIsAnswered(testTopic.tid, replyPid, true);
const topicData = await topics.getTopicData(testTopic.tid);
assert.strictEqual(topicData.isAnswered, true, 'isAnswered should be set to true when an answer is marked');
});

Check failure on line 2539 in test/topics.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
it('should correctly set isAnswered back to false when the answer is unmarked', async () => {
await topics.setIsAnswered(testTopic.tid, replyPid, false);
const topicData = await topics.getTopicData(testTopic.tid);
assert.strictEqual(topicData.isAnswered, false, 'isAnswered should be set to false when the answer is unmarked');
});
});
});

describe('Topics\'', async () => {
let files;

Expand Down
Loading