Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
const validator = require('validator'); | ||
const _ = require('lodash'); | ||
|
||
const meta = require('../../meta'); | ||
const groups = require('../../groups'); | ||
const posts = require('../../posts'); | ||
const events = require('../../events'); | ||
const utils = require('../../utils'); | ||
|
||
const helpers = require('../helpers'); | ||
const sockets = require('../../socket.io'); | ||
|
||
const Posts = module.exports; | ||
|
||
Posts.edit = async (req, res) => { | ||
if (meta.config.minimumPostLength !== 0 && !req.body.content) { | ||
throw new Error('[[error:invalid-data]]'); | ||
} | ||
|
||
// Trim and remove HTML (latter for composers that send in HTML, like redactor) | ||
var contentLen = utils.stripHTMLTags(req.body.content).trim().length; | ||
|
||
if (req.body.title && req.body.title.length < meta.config.minimumTitleLength) { | ||
throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]'); | ||
} else if (req.body.title && req.body.title.length > meta.config.maximumTitleLength) { | ||
throw new Error('[[error:title-too-long, ' + meta.config.maximumTitleLength + ']]'); | ||
} else if (meta.config.minimumPostLength !== 0 && contentLen < meta.config.minimumPostLength) { | ||
throw new Error('[[error:content-too-short, ' + meta.config.minimumPostLength + ']]'); | ||
} else if (contentLen > meta.config.maximumPostLength) { | ||
throw new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]'); | ||
} | ||
|
||
// Payload construction | ||
var payload = { | ||
req, | ||
uid: req.user.uid, | ||
pid: req.params.pid, | ||
content: req.body.content, | ||
options: {}, | ||
}; | ||
['handle', 'title'].forEach((prop) => { | ||
if (req.body.hasOwnProperty(prop)) { | ||
payload[prop] = req.body[prop]; | ||
} | ||
}); | ||
['topic_thumb', 'tags'].forEach((prop) => { | ||
if (req.body.hasOwnProperty(prop)) { | ||
payload.options[prop] = req.body[prop]; | ||
} | ||
}); | ||
|
||
const editResult = await posts.edit(payload); | ||
helpers.formatApiResponse(200, res, await posts.getPostSummaryByPids([editResult.pid], req.user.uid, {})); | ||
|
||
if (editResult.topic.renamed) { | ||
await events.log({ | ||
type: 'topic-rename', | ||
uid: req.user.uid, | ||
ip: req.ip, | ||
tid: editResult.topic.tid, | ||
oldTitle: validator.escape(String(editResult.topic.oldTitle)), | ||
newTitle: validator.escape(String(editResult.topic.title)), | ||
}); | ||
} | ||
|
||
if (!editResult.post.deleted) { | ||
sockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult); | ||
} | ||
|
||
const memberData = await groups.getMembersOfGroups([ | ||
'administrators', | ||
'Global Moderators', | ||
'cid:' + editResult.topic.cid + ':privileges:moderate', | ||
'cid:' + editResult.topic.cid + ':privileges:groups:moderate', | ||
]); | ||
|
||
const uids = _.uniq(_.flatten(memberData).concat(req.user.uid.toString())); | ||
uids.forEach(uid => sockets.in('uid_' + uid).emit('event:post_edited', editResult)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
const router = require('express').Router(); | ||
const middleware = require('../../middleware'); | ||
const controllers = require('../../controllers'); | ||
const routeHelpers = require('../helpers'); | ||
|
||
const setupApiRoute = routeHelpers.setupApiRoute; | ||
|
||
module.exports = function () { | ||
const middlewares = [middleware.authenticate]; | ||
|
||
setupApiRoute(router, '/:pid', middleware, [...middlewares, middleware.checkRequired.bind(null, ['content'])], 'put', controllers.write.posts.edit); | ||
|
||
// app.route('/:pid') | ||
// .put(apiMiddleware.requireUser, function(req, res) { | ||
// if (!utils.checkRequired(['content'], req, res)) { | ||
// return false; | ||
// } | ||
|
||
// var payload = { | ||
// uid: req.user.uid, | ||
// pid: req.params.pid, | ||
// content: req.body.content, | ||
// options: {} | ||
// }; | ||
|
||
// if (req.body.handle) { payload.handle = req.body.handle; } | ||
// if (req.body.title) { payload.title = req.body.title; } | ||
// if (req.body.topic_thumb) { payload.options.topic_thumb = req.body.topic_thumb; } | ||
// if (req.body.tags) { payload.options.tags = req.body.tags; } | ||
|
||
// posts.edit(payload, function(err) { | ||
// errorHandler.handle(err, res); | ||
// }) | ||
// }) | ||
// .delete(apiMiddleware.requireUser, apiMiddleware.validatePid, function(req, res) { | ||
// posts.purge(req.params.pid, req.user.uid, function(err) { | ||
// errorHandler.handle(err, res); | ||
// }); | ||
// }); | ||
|
||
// app.route('/:pid/state') | ||
// .put(apiMiddleware.requireUser, apiMiddleware.validatePid, function (req, res) { | ||
// posts.restore(req.params.pid, req.user.uid, function (err) { | ||
// errorHandler.handle(err, res); | ||
// }); | ||
// }) | ||
// .delete(apiMiddleware.requireUser, apiMiddleware.validatePid, function (req, res) { | ||
// posts.delete(req.params.pid, req.user.uid, function (err) { | ||
// errorHandler.handle(err, res); | ||
// }); | ||
// }); | ||
|
||
// app.route('/:pid/vote') | ||
// .post(apiMiddleware.requireUser, function(req, res) { | ||
// if (!utils.checkRequired(['delta'], req, res)) { | ||
// return false; | ||
// } | ||
|
||
// if (req.body.delta > 0) { | ||
// posts.upvote(req.params.pid, req.user.uid, function(err, data) { | ||
// errorHandler.handle(err, res, data); | ||
// }) | ||
// } else if (req.body.delta < 0) { | ||
// posts.downvote(req.params.pid, req.user.uid, function(err, data) { | ||
// errorHandler.handle(err, res, data); | ||
// }) | ||
// } else { | ||
// posts.unvote(req.params.pid, req.user.uid, function(err, data) { | ||
// errorHandler.handle(err, res, data); | ||
// }) | ||
// } | ||
// }) | ||
// .delete(apiMiddleware.requireUser, function(req, res) { | ||
// posts.unvote(req.params.pid, req.user.uid, function(err, data) { | ||
// errorHandler.handle(err, res, data); | ||
// }) | ||
// }); | ||
|
||
// app.route('/:pid/bookmark') | ||
// .post(apiMiddleware.requireUser, function(req, res) { | ||
// posts.bookmark(req.params.pid, req.user.uid, function (err) { | ||
// errorHandler.handle(err, res); | ||
// }); | ||
// }) | ||
// .delete(apiMiddleware.requireUser, apiMiddleware.validatePid, function (req, res) { | ||
// posts.unbookmark(req.params.pid, req.user.uid, function (err) { | ||
// errorHandler.handle(err, res); | ||
// }); | ||
// }); | ||
|
||
return router; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters