This commit is contained in:
“spicecat” 2022-08-18 11:37:11 -04:00
commit 55e47f3552
6 changed files with 109 additions and 9 deletions

View file

@ -0,0 +1,18 @@
const mongoose = require('mongoose');
const Answer = mongoose.Schema(
{
question_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Question' },
answer_id: { type: String, required: true },
creator: { type: String, required: true },
text: { type: String, required: true },
upvotes: { type: Number, required: true, default: 0 },
downvotes: { type: Number, required: true, default: 0 },
accepted: { type: Boolean, required: true, default: false },
createdAt: { type: Date, required: true },
lastCommentFetch: { type: Date, default: new Date(0) },
},
{ timestamps: { createdAt: false, updatedAt: true } }
);
module.exports = mongoose.model('Answer', Answer);

View file

@ -3,7 +3,6 @@ const mongoose = require('mongoose');
const Question = mongoose.Schema(
{
answers: { type: Number, required: true, default: 0 },
close: [String],
comments: { type: Number, required: true, default: 0 },
createdAt: { type: Date, required: true, default: Date.now },
creator: { type: String, required: true },
@ -12,8 +11,12 @@ const Question = mongoose.Schema(
lastAnswerFetch: { type: Date, default: new Date(0) },
lastCommentFetch: { type: Date, default: new Date(0) },
protect: [String],
question_id: { type: String, required: true },
reopen: [String],
close: [String],
edit: [String],
editText: { type: String },
tags: [String],
question_id: { type: String, required: true },
status: { type: String, required: true, default: 'open' },
text: { type: String, required: true },
title: { type: String, required: true },

View file

@ -5,8 +5,10 @@ const tokenAuth = require('server/middleware/tokenAuth');
const GetMail = require('./mail/Get');
const SendMail = require('./mail/Send');
const ReadMail = require('./mail/Read');
router.get('/', tokenAuth, GetMail);
router.post('/', tokenAuth, SendMail);
router.patch('/', tokenAuth, ReadMail);
module.exports = router;

View file

@ -1,5 +1,3 @@
const createRequest = require('server/utils/api');
const deriveKeyFromPassword = require('server/utils/auth');
const Mail = require('server/db/models/Mail');
const config = require('server/config.json');
@ -11,4 +9,4 @@ async function Read(req, res) {
return cachedMail ? res.sendStatus(200) : res.status(404).send(config.errorNotFound);
}
module.exports = Edit;
module.exports = Read;

View file

@ -3,7 +3,6 @@ const router = express.Router();
const tokenAuth = require('server/middleware/tokenAuth');
const CreateAnswer = require('./question/CreateAnswer');
const CreateAnswerComment = require('./question/CreateAnswerComment');
const CreateComment = require('./question/CreateComment');
@ -16,6 +15,7 @@ const EditAnswerCommentVote = require('./question/EditAnswerCommentVote');
const EditAnswerVote = require('./question/EditAnswerVote');
const EditCommentVote = require('./question/EditCommentVote');
const EditQuestion = require('./question/EditQuestion');
const EditQuestionBody = require('./question/EditQuestionBody');
const EditQuestionStatusClosed = require('./question/EditQuestionStatusClosed');
const EditQuestionStatusProtected = require('./question/EditQuestionStatusProtected');
const EditQuestionStatusReopened = require('./question/EditQuestionStatusReopened');
@ -35,6 +35,7 @@ router.get('/search', Search);
router.post('/', tokenAuth, CreateQuestion);
router.get('/:question_id', GetQuestion);
router.patch('/:question_id', tokenAuth, EditQuestion);
router.patch('/:question_id/edit', tokenAuth, EditQuestionBody);
router.patch('/:question_id/close', tokenAuth, EditQuestionStatusClosed);
router.patch('/:question_id/protect', tokenAuth, EditQuestionStatusProtected);
router.patch('/:question_id/reopen', tokenAuth, EditQuestionStatusReopened);
@ -56,8 +57,20 @@ router.patch('/:question_id/answers/:answer_id/vote', tokenAuth, EditAnswerVote)
router.get('/:question_id/answers/:answer_id/comments', GetAnswerComments);
router.post('/:question_id/answers/:answer_id/comments', tokenAuth, CreateAnswerComment);
router.delete('/:question_id/answers/:answer_id/comments/:comment_id', tokenAuth, DeleteAnswerComment);
router.get('/:question_id/answers/:answer_id/comments/:comment_id/vote', tokenAuth, GetAnswerCommentVote);
router.patch('/:question_id/answers/:answer_id/comments/:comment_id/vote', tokenAuth, EditAnswerCommentVote);
router.delete(
'/:question_id/answers/:answer_id/comments/:comment_id',
tokenAuth,
DeleteAnswerComment
);
router.get(
'/:question_id/answers/:answer_id/comments/:comment_id/vote',
tokenAuth,
GetAnswerCommentVote
);
router.patch(
'/:question_id/answers/:answer_id/comments/:comment_id/vote',
tokenAuth,
EditAnswerCommentVote
);
module.exports = router;

View file

@ -0,0 +1,66 @@
const config = require('server/config.json');
const Question = require('server/db/models/Question');
const { getQuestion, refreshQuestion } = require('server/utils/question');
const createRequest = require('server/utils/api');
const getUserLevel = require('server/utils/getUserLevel');
async function EditQuestionStatusClosed(req, res) {
const { user } = req;
const { question_id } = req.params;
const { text } = req.body;
// Verify user has required level
if (getUserLevel(user.points) < 7) {
return res.status(403).send(config.errorForbidden);
}
// Get cached question and make sure it exists
const question = await getQuestion(question_id);
if (!question) return res.status(404).send(config.errorNotFound);
// Toggle question vote
if (question.edit.length === 0) {
if (!text) {
return res.status(400).send(config.errorIncomplete);
}
await Question.findByIdAndUpdate(question_id, {
editText: text,
$push: { edit: user.username },
});
return res.sendStatus(200);
}
if (question.edit.includes(user.username)) {
await Question.findByIdAndUpdate(question_id, {
$pull: { edit: user.username },
});
return res.sendStatus(200);
} else {
await Question.findByIdAndUpdate(question_id, {
$push: { close: user.username },
});
}
// Patch question status if required
if (question.edit.length === 2) {
const question = await Question.findByIdAndUpdate(question_id, {
edit: [],
text,
});
const { success } = await createRequest('patch', `/questions/${question_id}`, {
text,
});
return success
? res.send({ text: question.text })
: res.status(500).send(config.errorGeneric);
}
return res.sendStatus(200);
}
module.exports = EditQuestionStatusClosed;