feat: stub code for v3 chats api

isekai-main
Julian Lam 3 years ago
parent a115b771f0
commit 02e878be72

@ -0,0 +1,49 @@
/* eslint-disable */
'use strict';
const api = require('../../api');
const helpers = require('../helpers');
const Chats = module.exports;
Chats.list = async (req, res) => {
// ...
};
Chats.create = async (req, res) => {
// ...
};
Chats.exists = async (req, res) => {
helpers.formatApiResponse(200, res);
};
Chats.get = async (req, res) => {
// ...
};
Chats.post = async (req, res) => {
// ...
};
Chats.users = async (req, res) => {
// ...
};
Chats.invite = async (req, res) => {
// ...
};
Chats.kick = async (req, res) => {
// ...
};
Chats.message = {};
Chats.messages.edit = async (req, res) => {
// ...
};
Chats.messages.delete = async (req, res) => {
// ...
};

@ -14,6 +14,7 @@ const user = require('../user');
const groups = require('../groups');
const topics = require('../topics');
const posts = require('../posts');
const messaging = require('../messaging');
const slugify = require('../slugify');
const helpers = require('./helpers');
@ -104,3 +105,20 @@ Assert.folderName = helpers.try(async (req, res, next) => {
next();
});
Assert.room = helpers.try(async (req, res, next) => {
const [exists, inRoom] = await Promise.all([
await messaging.roomExists(req.params.roomId),
await messaging.isUserInRoom(req.uid, req.params.roomId),
]);
if (!exists) {
return controllerHelpers.formatApiResponse(404, res, new Error('[[error:chat-room-does-not-exist]]'));
}
if (!inRoom) {
return controllerHelpers.formatApiResponse(403, res, new Error('[[error:no-privileges]]'));
}
next();
});

@ -133,6 +133,14 @@ module.exports = function (middleware) {
controllers.helpers.notAllowed(req, res);
});
middleware.canChat = helpers.try(async (req, res, next) => {
const canChat = await privileges.global.can('chat', req.uid);
if (canChat) {
return next();
}
controllers.helpers.notAllowed(req, res);
});
middleware.checkAccountPermissions = helpers.try(async (req, res, next) => {
// This middleware ensures that only the requested user and admins can pass

@ -0,0 +1,29 @@
'use strict';
const router = require('express').Router();
const middleware = require('../../middleware');
const controllers = require('../../controllers');
const routeHelpers = require('../helpers');
const { setupApiRoute } = routeHelpers;
module.exports = function () {
const middlewares = [middleware.ensureLoggedIn, middleware.canChat];
setupApiRoute(router, 'get', '/', [...middlewares], controllers.write.chats.list);
setupApiRoute(router, 'post', '/', [...middlewares, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.create);
setupApiRoute(router, 'head', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.exists);
setupApiRoute(router, 'get', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.get);
setupApiRoute(router, 'post', '/:roomId', [...middlewares, middleware.assert.room], controllers.write.chats.post);
// no route for room deletion, reserved just in case...
setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users);
setupApiRoute(router, 'put', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.invite);
setupApiRoute(router, 'delete', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.kick);
setupApiRoute(router, 'put', '/:roomId/:mid', [...middlewares, middleware.assert.room], controllers.write.chats.messages.edit);
setupApiRoute(router, 'delete', '/:roomId/:mid', [...middlewares, middleware.assert.room], controllers.write.chats.messages.delete);
return router;
};

@ -37,6 +37,7 @@ Write.reload = async (params) => {
router.use('/api/v3/categories', require('./categories')());
router.use('/api/v3/topics', require('./topics')());
router.use('/api/v3/posts', require('./posts')());
router.use('/api/v3/chats', require('./chats')());
router.use('/api/v3/flags', require('./flags')());
router.use('/api/v3/admin', require('./admin')());
router.use('/api/v3/files', require('./files')());

Loading…
Cancel
Save