Files
NodeBB/src/controllers/write/topics.js

137 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-05-01 15:58:01 -04:00
'use strict';
const validator = require('validator');
const api = require('../../api');
2020-05-01 15:58:01 -04:00
const topics = require('../../topics');
const privileges = require('../../privileges');
2020-05-01 15:58:01 -04:00
const helpers = require('../helpers');
const uploadsController = require('../uploads');
2020-05-01 15:58:01 -04:00
const Topics = module.exports;
Topics.create = async (req, res) => {
const payload = await api.topics.create(req, req.body);
if (payload.queued) {
helpers.formatApiResponse(202, res, payload);
} else {
helpers.formatApiResponse(200, res, payload);
2020-05-01 15:58:01 -04:00
}
};
Topics.reply = async (req, res) => {
const payload = await api.topics.reply(req, { ...req.body, tid: req.params.tid });
helpers.formatApiResponse(200, res, payload);
2020-05-01 15:58:01 -04:00
};
Topics.delete = async (req, res) => {
await api.topics.delete(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
Topics.restore = async (req, res) => {
await api.topics.restore(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
Topics.purge = async (req, res) => {
await api.topics.purge(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
Topics.pin = async (req, res) => {
// Pin expiry was not available w/ sockets hence not included in api lib method
if (req.body.expiry) {
await topics.tools.setPinExpiry(req.params.tid, req.body.expiry, req.uid);
}
await api.topics.pin(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
Topics.unpin = async (req, res) => {
await api.topics.unpin(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
Topics.lock = async (req, res) => {
await api.topics.lock(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
Topics.unlock = async (req, res) => {
await api.topics.unlock(req, { tids: [req.params.tid] });
helpers.formatApiResponse(200, res);
};
2020-10-06 11:36:52 -04:00
Topics.follow = async (req, res) => {
await api.topics.follow(req, req.params);
2020-10-06 11:36:52 -04:00
helpers.formatApiResponse(200, res);
};
Topics.ignore = async (req, res) => {
await api.topics.ignore(req, req.params);
2020-10-06 11:36:52 -04:00
helpers.formatApiResponse(200, res);
};
Topics.unfollow = async (req, res) => {
await api.topics.unfollow(req, req.params);
2020-10-06 11:36:52 -04:00
helpers.formatApiResponse(200, res);
};
2020-10-06 11:54:25 -04:00
Topics.addTags = async (req, res) => {
await topics.createTags(req.body.tags, req.params.tid, Date.now());
helpers.formatApiResponse(200, res);
};
Topics.deleteTags = async (req, res) => {
await topics.deleteTopicTags(req.params.tid);
helpers.formatApiResponse(200, res);
};
Topics.getThumbs = async (req, res) => {
helpers.formatApiResponse(200, res, await topics.thumbs.get(req.params.tid));
};
Topics.addThumb = async (req, res) => {
// req.params.tid could be either a tid (pushing a new thumb to an existing topic) or a post UUID (a new topic being composed)
const id = req.params.tid;
const isUUID = validator.isUUID(id);
// Sanity-check the tid if it's strictly not a uuid
if (!isUUID && (isNaN(parseInt(id, 10)) || !await topics.exists(req.params.tid))) {
return helpers.formatApiResponse(404, res, new Error('[[error:no-topic]]'));
}
// While drafts are not protected, tids are
if (!isUUID && !await privileges.topics.canEdit(req.params.tid, req.user.uid)) {
return helpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
}
/**
* todo test:
* - uuid
* - tid
* - number but not tid
* - random garbage
* - wrong caller uid (unpriv)
*/
const files = await uploadsController.uploadThumb(req, res); // response is handled here, fix this?
// Add uploaded files to topic zset
await Promise.all(files.map(async (fileObj) => {
await topics.thumbs.associate(id, fileObj.path);
}));
};
Topics.deleteThumb = async (req, res) => {
if (!await privileges.topics.canEdit(req.params.tid, req.user.uid)) {
return helpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
}
2020-12-03 13:50:30 -05:00
await topics.thumbs.delete(req.params.tid, req.body.path);
helpers.formatApiResponse(200, res, await topics.thumbs.get(req.params.tid));
};