From 50aac3c6a4eec2d5b515f18911ec8357d4136306 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 27 Aug 2016 14:06:14 +0300 Subject: [PATCH] closes #4979 closes #4984 --- src/plugins/hooks.js | 3 ++- src/posts/create.js | 6 ++++- src/posts/edit.js | 48 ++++++++++++++++++------------------- src/socket.io/posts/edit.js | 13 +++------- src/socket.io/topics.js | 15 ++++-------- src/topics/create.js | 9 ++++--- 6 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index 3e0bdcc848..111a09a86d 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -5,7 +5,8 @@ var winston = require('winston'), module.exports = function(Plugins) { Plugins.deprecatedHooks = { - 'filter:user.custom_fields': null // remove in v1.1.0 + 'filter:user.custom_fields': null, // remove in v1.1.0 + 'filter:post.save': 'filter:post.create' }; /* `data` is an object consisting of (* is required): diff --git a/src/posts/create.js b/src/posts/create.js index e84fdaaf3d..8884a3d07e 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -52,13 +52,17 @@ module.exports = function(Posts) { postData.ip = data.ip; } - if (parseInt(uid, 10) === 0 && data.handle) { + if (data.handle && !parseInt(uid, 10)) { postData.handle = data.handle; } plugins.fireHook('filter:post.save', postData, next); }, function(postData, next) { + plugins.fireHook('filter:post.create', {post: postData, data: data}, next); + }, + function(data, next) { + postData = data.post; db.setObject('post:' + postData.pid, postData, next); }, function(next) { diff --git a/src/posts/edit.js b/src/posts/edit.js index e1df94aa9f..9d78cfc140 100644 --- a/src/posts/edit.js +++ b/src/posts/edit.js @@ -1,16 +1,17 @@ 'use strict'; -var async = require('async'), - validator = require('validator'), - _ = require('underscore'), - db = require('../database'), - topics = require('../topics'), - user = require('../user'), - privileges = require('../privileges'), - plugins = require('../plugins'), - cache = require('./cache'), - pubsub = require('../pubsub'), - utils = require('../../public/src/utils'); +var async = require('async'); +var validator = require('validator'); +var _ = require('underscore'); + +var db = require('../database'); +var topics = require('../topics'); +var user = require('../user'); +var privileges = require('../privileges'); +var plugins = require('../plugins'); +var cache = require('./cache'); +var pubsub = require('../pubsub'); +var utils = require('../../public/src/utils'); module.exports = function(Posts) { @@ -19,7 +20,6 @@ module.exports = function(Posts) { }); Posts.edit = function(data, callback) { - var now = Date.now(); var postData; var results; @@ -37,23 +37,19 @@ module.exports = function(Posts) { if (!_postData) { return next(new Error('[[error:no-post]]')); } + postData = _postData; postData.content = data.content; - postData.edited = now; + postData.edited = Date.now(); postData.editor = data.uid; - plugins.fireHook('filter:post.edit', {req: data.req, post: postData, uid: data.uid}, next); + if (data.handle) { + postData.handle = data.handle; + } + plugins.fireHook('filter:post.edit', {req: data.req, post: postData, data: data, uid: data.uid}, next); }, function (result, next) { postData = result.post; - var updateData = { - edited: postData.edited, - editor: postData.editor, - content: postData.content - }; - if (data.handle) { - updateData.handle = data.handle; - } - Posts.setPostFields(data.pid, updateData, next); + Posts.setPostFields(data.pid, postData, next); }, function (next) { async.parallel({ @@ -121,12 +117,14 @@ module.exports = function(Posts) { topicData.slug = tid + '/' + (utils.slugify(title) || 'topic'); } - topicData.thumb = data.topic_thumb || ''; + topicData.thumb = data.thumb || ''; data.tags = data.tags || []; async.waterfall([ - async.apply(plugins.fireHook, 'filter:topic.edit', {req: data.req, topic: topicData}), + function(next) { + plugins.fireHook('filter:topic.edit', {req: data.req, topic: topicData, data: data}, next); + }, function(results, next) { db.setObject('topic:' + tid, results.topic, next); }, diff --git a/src/socket.io/posts/edit.js b/src/socket.io/posts/edit.js index 41a7c1134f..22b7aa0ea2 100644 --- a/src/socket.io/posts/edit.js +++ b/src/socket.io/posts/edit.js @@ -31,16 +31,9 @@ module.exports = function(SocketPosts) { return callback(new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]')); } - posts.edit({ - uid: socket.uid, - handle: data.handle, - pid: data.pid, - title: data.title, - content: data.content, - topic_thumb: data.topic_thumb, - tags: data.tags, - req: websockets.reqFromSocket(socket) - }, function(err, result) { + data.uid = socket.uid; + data.req = websockets.reqFromSocket(socket); + posts.edit(data, function(err, result) { if (err) { return callback(err); } diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 48f24700b1..5887483242 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -22,16 +22,11 @@ SocketTopics.post = function(socket, data, callback) { return callback(new Error('[[error:invalid-data]]')); } - topics.post({ - uid: socket.uid, - handle: data.handle, - title: data.title, - content: data.content, - cid: data.category_id, - thumb: data.topic_thumb, - tags: data.tags, - req: websockets.reqFromSocket(socket) - }, function(err, result) { + data.uid = socket.uid; + data.req = websockets.reqFromSocket(socket); + data.timestamp = Date.now(); + + topics.post(data, function(err, result) { if (err) { return callback(err); } diff --git a/src/topics/create.js b/src/topics/create.js index ea7d35761a..5de7ac9fa2 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -45,7 +45,7 @@ module.exports = function(Topics) { topicData.thumb = data.thumb; } - plugins.fireHook('filter:topic.create', {topic: topicData}, next); + plugins.fireHook('filter:topic.create', {topic: topicData, data: data}, next); }, function(data, next) { topicData = data.topic; @@ -122,10 +122,13 @@ module.exports = function(Topics) { }, function(filteredData, next) { data = filteredData; - Topics.create({uid: data.uid, title: data.title, cid: data.cid, thumb: data.thumb, tags: data.tags, timestamp: data.timestamp}, next); + Topics.create(data, next); }, function(tid, next) { - posts.create({uid: data.uid, tid: tid, handle: data.handle, content: data.content, timestamp: data.timestamp, ip: data.req ? data.req.ip : null}, next); + var postData = data; + postData.tid = tid; + postData.ip = data.req ? data.req.ip : null; + posts.create(postData, next); }, function(postData, next) { onNewPost(postData, data, next);