feat(api): #9123, migrate rest of the getObject controllers to Write API

v1.18.x
Julian Lam 4 years ago
parent cdff8d286a
commit 9ecfac9b68

@ -2,9 +2,22 @@
const categories = require('../categories');
const events = require('../events');
const privileges = require('../privileges');
const categoriesAPI = module.exports;
categoriesAPI.get = async function (caller, data) {
const [userPrivileges, category] = await Promise.all([
privileges.categories.get(data.cid, caller.uid),
categories.getCategoryData(data.cid),
]);
if (!category || !userPrivileges.read) {
return null;
}
return category;
};
categoriesAPI.create = async function (caller, data) {
const response = await categories.create(data);
const categoryObjs = await categories.getCategories([response.cid], caller.uid);

@ -4,6 +4,7 @@ const user = require('../user');
const topics = require('../topics');
const posts = require('../posts');
const meta = require('../meta');
const privileges = require('../privileges');
const apiHelpers = require('./helpers');
const doTopicAction = apiHelpers.doTopicAction;
@ -13,6 +14,18 @@ const socketHelpers = require('../socket.io/helpers');
const topicsAPI = module.exports;
topicsAPI.get = async function (caller, data) {
const [userPrivileges, topic] = await Promise.all([
privileges.topics.get(data.tid, caller.uid),
topics.getTopicData(data.tid),
]);
if (!topic || !userPrivileges.read || !userPrivileges['topics:read'] || (topic.deleted && !userPrivileges.view_deleted)) {
return null;
}
return topic;
};
topicsAPI.create = async function (caller, data) {
if (!data) {
throw new Error('[[error:invalid-data]]');

@ -6,9 +6,7 @@ const winston = require('winston');
const meta = require('../meta');
const user = require('../user');
const topics = require('../topics');
const categories = require('../categories');
const privileges = require('../privileges');
const plugins = require('../plugins');
const translator = require('../translator');
const languages = require('../languages');
@ -120,29 +118,8 @@ apiController.getConfig = async function (req, res) {
// TODO: Deprecate these four controllers in 1.17.0
apiController.getPostData = async (pid, uid) => api.posts.get({ uid }, { pid });
apiController.getTopicData = async function (tid, uid) {
const [userPrivileges, topic] = await Promise.all([
privileges.topics.get(tid, uid),
topics.getTopicData(tid),
]);
if (!topic || !userPrivileges.read || !userPrivileges['topics:read'] || (topic.deleted && !userPrivileges.view_deleted)) {
return null;
}
return topic;
};
apiController.getCategoryData = async function (cid, uid) {
const [userPrivileges, category] = await Promise.all([
privileges.categories.get(cid, uid),
categories.getCategoryData(cid),
]);
if (!category || !userPrivileges.read) {
return null;
}
return category;
};
apiController.getTopicData = async (tid, uid) => api.topics.get({ uid }, { tid });
apiController.getCategoryData = async (cid, uid) => api.categories.get({ uid }, { cid });
apiController.getObject = async function (req, res, next) {
const methods = {
post: apiController.getPostData,

@ -4,7 +4,8 @@ const categories = require('../categories');
const privileges = require('../privileges');
const user = require('../user');
const topics = require('../topics');
const apiController = require('../controllers/api');
const api = require('../api');
const sockets = require('.');
const SocketCategories = module.exports;
@ -147,7 +148,9 @@ SocketCategories.isModerator = async function (socket, cid) {
};
SocketCategories.getCategory = async function (socket, cid) {
return await apiController.getCategoryData(cid, socket.uid);
sockets.warnDeprecated(socket, 'GET /api/v3/categories/:tid');
return await api.categories.get(socket, { cid });
// return await apiController.getCategoryData(cid, socket.uid);
};
require('../promisify')(SocketCategories);

@ -4,7 +4,6 @@ const api = require('../api');
const topics = require('../topics');
const user = require('../user');
const meta = require('../meta');
const apiController = require('../controllers/api');
const privileges = require('../privileges');
const sockets = require('.');
@ -96,7 +95,8 @@ SocketTopics.isModerator = async function (socket, tid) {
};
SocketTopics.getTopic = async function (socket, tid) {
return await apiController.getTopicData(tid, socket.uid);
sockets.warnDeprecated(socket, 'GET /api/v3/topics/:tid');
return await api.topics.get(socket, { tid });
};
require('../promisify')(SocketTopics);

Loading…
Cancel
Save