mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat(writeapi): topic posting and replying
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
PostsObject:
|
||||
description: One of the objects in the array returned from `Posts.getPostSummaryByPids`
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
|
||||
@@ -456,6 +456,84 @@ paths:
|
||||
response:
|
||||
type: object
|
||||
properties: {}
|
||||
/topics:
|
||||
post:
|
||||
tags:
|
||||
- topics
|
||||
summary: Create a new topic
|
||||
description: This operation creates a new topic with a post. Topic creation without a post is not allowed via the Write API as it is an internal-only method.
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
cid:
|
||||
type: number
|
||||
title:
|
||||
type: string
|
||||
content:
|
||||
type: string
|
||||
tags:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
required:
|
||||
- cid
|
||||
- title
|
||||
- content
|
||||
example:
|
||||
cid: 1
|
||||
title: Test topic
|
||||
content: This is the test topic's content
|
||||
responses:
|
||||
'200':
|
||||
description: topic successfully created
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: '#/components/schemas/Status'
|
||||
response:
|
||||
$ref: components/schemas/TopicObject.yaml#/TopicObject
|
||||
/topics/{tid}:
|
||||
post:
|
||||
tags:
|
||||
- topics
|
||||
summary: Reply to a topic
|
||||
description: This operation creates a new reply to an existing topic.
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
content:
|
||||
type: string
|
||||
timestamp:
|
||||
type: number
|
||||
toPid:
|
||||
type: number
|
||||
required:
|
||||
- content
|
||||
example:
|
||||
content: This is a test reply
|
||||
responses:
|
||||
'200':
|
||||
description: post successfully created
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: '#/components/schemas/Status'
|
||||
response:
|
||||
$ref: components/schemas/PostsObject.yaml#/PostsObject
|
||||
components:
|
||||
schemas:
|
||||
Status:
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
const nconf = require('nconf');
|
||||
const validator = require('validator');
|
||||
const querystring = require('querystring');
|
||||
const url = require('url');
|
||||
const _ = require('lodash');
|
||||
|
||||
const user = require('../user');
|
||||
@@ -413,4 +414,28 @@ helpers.generateError = (statusCode, message) => {
|
||||
return payload;
|
||||
};
|
||||
|
||||
helpers.buildReqObject = (req) => {
|
||||
var headers = req.headers;
|
||||
var encrypted = !!req.connection.encrypted;
|
||||
var host = headers.host;
|
||||
var referer = headers.referer || '';
|
||||
if (!host) {
|
||||
host = url.parse(referer).host || '';
|
||||
}
|
||||
|
||||
return {
|
||||
uid: req.uid,
|
||||
params: req.params,
|
||||
method: req.method,
|
||||
body: req.body,
|
||||
ip: req.ip,
|
||||
host: host,
|
||||
protocol: encrypted ? 'https' : 'http',
|
||||
secure: encrypted,
|
||||
url: referer,
|
||||
path: referer.substr(referer.indexOf(host) + host.length),
|
||||
headers: headers,
|
||||
};
|
||||
};
|
||||
|
||||
require('../promisify')(helpers);
|
||||
|
||||
@@ -3,4 +3,6 @@
|
||||
const Write = module.exports;
|
||||
|
||||
Write.users = require('./users');
|
||||
Write.groups = require('./groups');
|
||||
Write.categories = require('./categories');
|
||||
Write.topics = require('./topics');
|
||||
|
||||
@@ -6,13 +6,23 @@
|
||||
*/
|
||||
|
||||
const groups = require('../groups');
|
||||
const topics = require('../topics');
|
||||
|
||||
const helpers = require('../controllers/helpers');
|
||||
|
||||
module.exports = function (middleware) {
|
||||
middleware.assertGroup = async (req, res, next) => {
|
||||
const name = await groups.getGroupNameByGroupSlug(req.params.slug);
|
||||
const exists = await groups.exists(name);
|
||||
if (!exists) {
|
||||
throw new Error('[[error:no-group]]');
|
||||
if (!name || await groups.exists(name)) {
|
||||
return helpers.formatApiResponse(404, res, new Error('[[error:no-group]]'));
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
middleware.assertTopic = async (req, res, next) => {
|
||||
if (!await topics.exists(req.params.tid)) {
|
||||
return helpers.formatApiResponse(404, res, new Error('[[error:no-topic]]'));
|
||||
}
|
||||
|
||||
next();
|
||||
|
||||
@@ -21,11 +21,11 @@ Write.reload = (params) => {
|
||||
});
|
||||
|
||||
router.use('/api/v1/users', require('./users')());
|
||||
// router.use('/groups', require('./groups')(coreMiddleware));
|
||||
// router.use('/posts', require('./posts')(coreMiddleware));
|
||||
// router.use('/topics', require('./topics')(coreMiddleware));
|
||||
router.use('/api/v1/groups', require('./groups')());
|
||||
router.use('/api/v1/categories', require('./categories')());
|
||||
// router.use('/util', require('./util')(coreMiddleware));
|
||||
router.use('/api/v1/topics', require('./topics')());
|
||||
// router.use('/api/v1/posts', require('./posts')());
|
||||
// router.use('/api/v1/util', require('./util')());
|
||||
|
||||
router.get('/api/v1/ping', function (req, res) {
|
||||
helpers.formatApiResponse(200, res, {
|
||||
|
||||
@@ -13,6 +13,7 @@ const utils = require('../utils');
|
||||
|
||||
const apiController = require('../controllers/api');
|
||||
|
||||
const sockets = require('.');
|
||||
const SocketPosts = module.exports;
|
||||
|
||||
require('./posts/edit')(SocketPosts);
|
||||
@@ -23,6 +24,8 @@ require('./posts/tools')(SocketPosts);
|
||||
require('./posts/diffs')(SocketPosts);
|
||||
|
||||
SocketPosts.reply = async function (socket, data) {
|
||||
sockets.warnDeprecated(socket, 'POST /api/v1/topics/:tid');
|
||||
|
||||
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ const user = require('../user');
|
||||
const meta = require('../meta');
|
||||
const apiController = require('../controllers/api');
|
||||
const privileges = require('../privileges');
|
||||
const sockets = require('.');
|
||||
const socketHelpers = require('./helpers');
|
||||
|
||||
const SocketTopics = module.exports;
|
||||
@@ -18,6 +19,8 @@ require('./topics/tags')(SocketTopics);
|
||||
require('./topics/merge')(SocketTopics);
|
||||
|
||||
SocketTopics.post = async function (socket, data) {
|
||||
sockets.warnDeprecated(socket, 'POST /api/v1/topics');
|
||||
|
||||
if (!data) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user