diff --git a/api/src/api/me.js b/api/src/api/me.js index 7be2250..4cdadf5 100644 --- a/api/src/api/me.js +++ b/api/src/api/me.js @@ -51,6 +51,20 @@ function quizParse(html, check) { return items } +function quizResponseBuild(quiz) { + var responses = quiz + if (quiz.id !== undefined) + responses = quiz.responses + if (responses === undefined || + responses === null) + return false + html = '

' + for (var i = 0; i < responses.length; i++) + html += `${i+1}: ${responses[i]}
` + html += '

' + return html +} + async function quizCreate(content, from, to, replyTo) { var quiz = quizParse(content) if (quiz === false) @@ -117,8 +131,58 @@ module.exports = { return res.json(data) }], post: [auth.enforceSession, async (req, res) => { - console.log(req.body) - return res.json({}) + if (req.body.quiz_id === undefined) + return res.json({ error: 'invalid_params' }) + + const quiz = await getQuizs(apID(res.locals.user.username), + { id: req.body.quiz_id }) + if ([false, undefined, null].includes(quiz)) + return res.json({ error: 'invalid_params' }) + + if (quiz.responses !== undefined) + return res.json({ error: 'already_sent_quiz' }) + + var responses = [] + for (var i = 0; i < quiz.content.length; i++) { + const q = quiz.content[i] + if (q.options.length > 0) + if (!q.options.includes(req.body['question_'+i])) + return res.json({ error: 'invalid_params' }) + responses.push(req.body['question_'+i]) + } + if (responses.length !== quiz.content.length) + return res.json({ error: 'invalid_params' }) + + await db.table.quizs().updateOne({ _id: quiz._id }, { $set: { responses } }) + html = quizResponseBuild(responses) + if (html === false) + return res.json({ error: 'invalid_params' }) + + const id = utils.apRandomURL() + const actor = await apex.store.getObject( + apID(res.locals.user.username), true) + const payload = { + id: id+'#activity', + type: 'Create', + actor: actor.id, + to: quiz.from.url, + object: { + id, + type: 'Note', + content: html, + to: quiz.from.url, + attributedTo: actor.id, + inReplyTo: quiz.replyTo, + tag: [{ + type: 'Mention', + href: quiz.from.url, + name: quiz.from.acct, + }], + }, + } + await apex.addToOutbox(actor, payload) + await apex.store.saveObject(payload.object) + return res.json({ result: 1 }) }], }, pending_follows: { diff --git a/web/src/config/lang/en.php b/web/src/config/lang/en.php index aa6fa3d..470dbc0 100644 --- a/web/src/config/lang/en.php +++ b/web/src/config/lang/en.php @@ -52,6 +52,7 @@ $strings = [ 'follow_pending' => 'This person has not yet accepted to be your crush', 'follow_rejected_max' => 'You have reached the limit of crush requests on this person', 'user_blocked_you' => 'We are sorry, this person rejected to be your crush. You will find another love soon!', + 'already_sent_quiz' => 'Sorry, you can\'t send the Quiz again. It was already sent.', ], ], ]; diff --git a/web/src/config/lang/es.php b/web/src/config/lang/es.php index 79180f8..c336bec 100644 --- a/web/src/config/lang/es.php +++ b/web/src/config/lang/es.php @@ -52,6 +52,7 @@ $strings = [ 'follow_pending' => 'Esta persona todavía no ha aceptado ser tu crush', 'follow_rejected_max' => 'Has alcanzado el límite de peticiones de crush para esta persona', 'user_blocked_you' => 'Lo sentimos, esta persona ha rechazado ser su crush. ¡Encontrarás a otra persona pronto!', + 'already_sent_quiz' => 'Lo siento, no puedes enviar este Quiz de nuevo. Ya ha sido enviado antes.', ], ], ];