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.
15 changes: 15 additions & 0 deletions src/controllers/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ const relative_path = nconf.get('relative_path');
const upload_url = nconf.get('upload_url');
const validSorts = ['oldest_to_newest', 'newest_to_oldest', 'most_votes'];

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
9 changes: 9 additions & 0 deletions src/posts/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ module.exports = function (Posts) {
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 @@ module.exports = function (Posts) {
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
Loading