Skip to content

Commit

Permalink
feat(writeapi): topic posting and replying
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Oct 8, 2020
1 parent 40dc1c3 commit 4c833d0
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 7 deletions.
1 change: 1 addition & 0 deletions public/openapi/components/schemas/PostsObject.yaml
@@ -1,4 +1,5 @@
PostsObject:
description: One of the objects in the array returned from `Posts.getPostSummaryByPids`
type: array
items:
type: object
Expand Down
78 changes: 78 additions & 0 deletions public/openapi/write.yaml
Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions src/controllers/helpers.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
2 changes: 2 additions & 0 deletions src/controllers/write/index.js
Expand Up @@ -3,4 +3,6 @@
const Write = module.exports;

Write.users = require('./users');
Write.groups = require('./groups');
Write.categories = require('./categories');
Write.topics = require('./topics');
16 changes: 13 additions & 3 deletions src/middleware/assert.js
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/routes/write/index.js
Expand Up @@ -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, {
Expand Down
3 changes: 3 additions & 0 deletions src/socket.io/posts.js
Expand Up @@ -13,6 +13,7 @@ const utils = require('../utils');

const apiController = require('../controllers/api');

const sockets = require('.');
const SocketPosts = module.exports;

require('./posts/edit')(SocketPosts);
Expand All @@ -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]]');
}
Expand Down
3 changes: 3 additions & 0 deletions src/socket.io/topics.js
Expand Up @@ -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;
Expand All @@ -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]]');
}
Expand Down

0 comments on commit 4c833d0

Please sign in to comment.