Extract API endpoints for quick replies

This commit is contained in:
Cohee 2024-03-20 00:46:46 +02:00
parent d448d4f65b
commit 7dcd39c806
3 changed files with 45 additions and 28 deletions

View File

@ -177,7 +177,7 @@ export class QuickReplySet {
async performSave() {
const response = await fetch('/savequickreply', {
const response = await fetch('/api/quick-replies/save', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(this),
@ -191,7 +191,7 @@ export class QuickReplySet {
}
async delete() {
const response = await fetch('/deletequickreply', {
const response = await fetch('/api/quick-replies/delete', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(this),

View File

@ -244,32 +244,6 @@ app.post('/savemovingui', jsonParser, (request, response) => {
return response.sendStatus(200);
});
app.post('/savequickreply', jsonParser, (request, response) => {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
const filename = path.join(DIRECTORIES.quickreplies, sanitize(request.body.name) + '.json');
writeFileAtomicSync(filename, JSON.stringify(request.body, null, 4), 'utf8');
return response.sendStatus(200);
});
app.post('/deletequickreply', jsonParser, (request, response) => {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
const filename = path.join(DIRECTORIES.quickreplies, sanitize(request.body.name) + '.json');
if (fs.existsSync(filename)) {
fs.unlinkSync(filename);
}
return response.sendStatus(200);
});
/**
* Ensure the directory for the provided file path exists.
* If not, it will recursively create the directory.
@ -444,6 +418,13 @@ redirect('/getuseravatars', '/api/avatars/get');
redirect('/deleteuseravatar', '/api/avatars/delete');
redirect('/uploaduseravatar', '/api/avatars/upload');
// Redirect deprecated quick reply endpoints
redirect('/deletequickreply', '/api/quick-replies/delete');
redirect('/savequickreply', '/api/quick-replies/save');
// Quick reply management
app.use('/api/quick-replies', require('./src/endpoints/quick-replies').router);
// Avatar management
app.use('/api/avatars', require('./src/endpoints/avatars').router);

View File

@ -0,0 +1,36 @@
const fs = require('fs');
const path = require('path');
const express = require('express');
const sanitize = require('sanitize-filename');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser } = require('../express-common');
const { DIRECTORIES } = require('../constants');
const router = express.Router();
router.post('/save', jsonParser, (request, response) => {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
const filename = path.join(DIRECTORIES.quickreplies, sanitize(request.body.name) + '.json');
writeFileAtomicSync(filename, JSON.stringify(request.body, null, 4), 'utf8');
return response.sendStatus(200);
});
router.post('/delete', jsonParser, (request, response) => {
if (!request.body || !request.body.name) {
return response.sendStatus(400);
}
const filename = path.join(DIRECTORIES.quickreplies, sanitize(request.body.name) + '.json');
if (fs.existsSync(filename)) {
fs.unlinkSync(filename);
}
return response.sendStatus(200);
});
module.exports = { router };