From 1f06f90a501d5ef1a31085537448939f489cd3a7 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 20 Apr 2015 17:56:43 -0400 Subject: [PATCH] closes #3051 updated lru to latest created new files posts/cache.js posts/parse.js posts/edit.js --- package.json | 2 +- public/src/client/topic/events.js | 31 ++++-- src/controllers/accounts.js | 3 +- src/postTools.js | 163 +----------------------------- src/posts.js | 5 +- src/posts/cache.js | 11 ++ src/posts/edit.js | 146 ++++++++++++++++++++++++++ src/posts/parse.js | 33 ++++++ src/posts/summary.js | 3 +- src/posts/user.js | 3 +- src/socket.io/admin.js | 4 +- src/socket.io/posts.js | 32 ++---- src/topics/create.js | 4 +- src/topics/teaser.js | 3 +- 14 files changed, 238 insertions(+), 205 deletions(-) create mode 100644 src/posts/cache.js create mode 100644 src/posts/edit.js create mode 100644 src/posts/parse.js diff --git a/package.json b/package.json index 37335702b1..9efc0b144d 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "heapdump": "^0.3.0", "less": "^2.0.0", "logrotate-stream": "^0.2.3", - "lru-cache": "^2.5.0", + "lru-cache": "^2.6.1", "mime": "^1.3.4", "mkdirp": "~0.5.0", "mmmagic": "^0.3.13", diff --git a/public/src/client/topic/events.js b/public/src/client/topic/events.js index 1013f49ff0..80df64dc6d 100644 --- a/public/src/client/topic/events.js +++ b/public/src/client/topic/events.js @@ -97,20 +97,24 @@ define('forum/topic/events', [ } function onPostEdited(data) { - var editedPostEl = components.get('post/content', data.pid), + if (!data || !data.post) { + return; + } + var editedPostEl = components.get('post/content', data.post.pid), + editorEl = $('[data-pid="' + data.post.pid + '"] [component="post/editor"]'), topicTitle = components.get('topic/title'); - if (topicTitle.length && data.title) { - var newUrl = 'topic/' + data.slug + (window.location.search ? window.location.search : ''); + if (topicTitle.length && data.topic.title) { + var newUrl = 'topic/' + data.topic.slug + (window.location.search ? window.location.search : ''); history.replaceState({url: newUrl}, null, window.location.protocol + '//' + window.location.host + config.relative_path + '/' + newUrl); topicTitle.fadeOut(250, function() { - topicTitle.html(data.title).fadeIn(250); + topicTitle.html(data.topic.title).fadeIn(250); }); } editedPostEl.fadeOut(250, function() { - editedPostEl.html(data.content); + editedPostEl.html(data.post.content); editedPostEl.find('img').addClass('img-responsive'); app.replaceSelfLinks(editedPostEl.find('a')); editedPostEl.fadeIn(250); @@ -118,8 +122,21 @@ define('forum/topic/events', [ $(window).trigger('action:posts.edited', data); }); - if (data.tags && tagsUpdated(data.tags)) { - templates.parse('partials/post_bar', 'tags', {tags: data.tags}, function(html) { + var editData = { + editor: data.editor, + relativeEditTime: utils.toISOString(data.post.edited) + }; + + templates.parse('partials/topic/post-editor', editData, function(html) { + translator.translate(html, function(translated) { + html = $(translated); + editorEl.replaceWith(html); + html.find('.timeago').timeago(); + }); + }); + + if (data.topic.tags && tagsUpdated(data.topic.tags)) { + templates.parse('partials/post_bar', 'tags', {tags: data.topic.tags}, function(html) { var tags = $('.tags'); tags.fadeOut(250, function() { diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index b845586620..536241dba1 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -13,7 +13,6 @@ var fs = require('fs'), topics = require('../topics'), groups = require('../groups'), messaging = require('../messaging'), - postTools = require('../postTools'), utils = require('../../public/src/utils'), meta = require('../meta'), plugins = require('../plugins'), @@ -154,7 +153,7 @@ accountsController.getAccount = function(req, res, next) { posts.getPostsFromSet('uid:' + userData.theirid + ':posts', req.uid, 0, 9, next); }, signature: function(next) { - postTools.parseSignature(userData, req.uid, next); + posts.parseSignature(userData, req.uid, next); } }, function(err, results) { if(err) { diff --git a/src/postTools.js b/src/postTools.js index 82f3e3604d..d702274abf 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -1,143 +1,15 @@ 'use strict'; -var winston = require('winston'), - async = require('async'), - nconf = require('nconf'), - validator = require('validator'), +var async = require('async'), - db = require('./database'), posts = require('./posts'), - topics = require('./topics'), - threadTools = require('./threadTools'), privileges = require('./privileges'), - user = require('./user'), - utils = require('../public/src/utils'), - plugins = require('./plugins'), - events = require('./events'), - meta = require('./meta'), - LRU = require('lru-cache'); - -var cache = LRU({ - max: 1048576, - length: function (n) { return n.length; }, - maxAge: 1000 * 60 * 60 -}); + cache = require('./posts/cache'); (function(PostTools) { PostTools.edit = function(data, callback) { - var options = data.options || {}, - title = data.title.trim(); - - async.waterfall([ - function (next) { - privileges.posts.canEdit(data.pid, data.uid, next); - }, - function(canEdit, next) { - if (!canEdit) { - return next(new Error('[[error:no-privileges]]')); - } - posts.getPostData(data.pid, next); - }, - function(postData, next) { - postData.content = data.content; - plugins.fireHook('filter:post.edit', {post: postData, uid: data.uid}, next); - } - ], function(err, result) { - if (err) { - return callback(err); - } - - var postData = result.post; - async.parallel({ - post: function(next) { - var d = { - edited: Date.now(), - editor: data.uid, - content: postData.content - }; - if (data.handle) { - d.handle = data.handle; - } - posts.setPostFields(data.pid, d, next); - }, - topic: function(next) { - var tid = postData.tid; - async.parallel({ - cid: function(next) { - topics.getTopicField(tid, 'cid', next); - }, - isMain: function(next) { - posts.isMain(data.pid, next); - } - }, function(err, results) { - if (err) { - return next(err); - } - - options.tags = options.tags || []; - - if (!results.isMain) { - return next(null, { - tid: tid, - cid: results.cid, - isMainPost: false - }); - } - - var topicData = { - tid: tid, - cid: results.cid, - uid: postData.uid, - mainPid: data.pid - }; - - if (title) { - topicData.title = title; - topicData.slug = tid + '/' + utils.slugify(title); - } - - if (options.topic_thumb) { - topicData.thumb = options.topic_thumb; - } - - db.setObject('topic:' + tid, topicData, function(err) { - plugins.fireHook('action:topic.edit', topicData); - }); - - topics.updateTags(tid, options.tags, function(err) { - if (err) { - return next(err); - } - topics.getTopicTagsObjects(tid, function(err, tags) { - next(err, { - tid: tid, - cid: results.cid, - uid: postData.uid, - title: validator.escape(title), - slug: topicData.slug, - isMainPost: results.isMain, - tags: tags - }); - }); - }); - }); - }, - postData: function(next) { - cache.del(postData.pid); - PostTools.parsePost(postData, next); - } - }, function(err, results) { - if (err) { - return callback(err); - } - postData.cid = results.topic.cid; - results.content = results.postData.content; - - plugins.fireHook('action:post.edit', postData); - callback(null, results); - }); - }); + posts.edit(data, callback); }; PostTools.delete = function(uid, pid, callback) { @@ -181,7 +53,7 @@ var cache = LRU({ if (err) { return callback(err); } - PostTools.parsePost(postData, callback); + posts.parsePost(postData, callback); }); } }); @@ -197,32 +69,5 @@ var cache = LRU({ }); }; - PostTools.parsePost = function(postData, callback) { - postData.content = postData.content || ''; - - var cachedContent = cache.get(postData.pid); - if (cachedContent) { - postData.content = cachedContent; - return callback(null, postData); - } - - plugins.fireHook('filter:parse.post', {postData: postData}, function(err, data) { - if (err) { - return callback(err); - } - cache.set(data.postData.pid, data.postData.content); - callback(null, data.postData); - }); - }; - - PostTools.parseSignature = function(userData, uid, callback) { - userData.signature = userData.signature || ''; - - plugins.fireHook('filter:parse.signature', {userData: userData, uid: uid}, callback); - }; - - PostTools.resetCache = function() { - cache.reset(); - }; }(exports)); diff --git a/src/posts.js b/src/posts.js index 97c490f015..c2e252302d 100644 --- a/src/posts.js +++ b/src/posts.js @@ -7,7 +7,6 @@ var async = require('async'), utils = require('../public/src/utils'), user = require('./user'), topics = require('./topics'), - postTools = require('./postTools'), privileges = require('./privileges'), plugins = require('./plugins'); @@ -15,6 +14,8 @@ var async = require('async'), require('./posts/create')(Posts); require('./posts/delete')(Posts); + require('./posts/edit')(Posts); + require('./posts/parse')(Posts); require('./posts/user')(Posts); require('./posts/topics')(Posts); require('./posts/category')(Posts); @@ -52,7 +53,7 @@ var async = require('async'), post.relativeTime = utils.toISOString(post.timestamp); post.relativeEditTime = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : ''; - postTools.parsePost(post, next); + Posts.parsePost(post, next); }, next); }, function(posts, next) { diff --git a/src/posts/cache.js b/src/posts/cache.js new file mode 100644 index 0000000000..592d313f48 --- /dev/null +++ b/src/posts/cache.js @@ -0,0 +1,11 @@ +var LRU = require('lru-cache'); + +var cache = LRU({ + max: 1048576, + length: function (n) { return n.length; }, + maxAge: 1000 * 60 * 60 +}); + + + +module.exports = cache; \ No newline at end of file diff --git a/src/posts/edit.js b/src/posts/edit.js new file mode 100644 index 0000000000..e96714af96 --- /dev/null +++ b/src/posts/edit.js @@ -0,0 +1,146 @@ +'use strict'; + +var async = require('async'), + validator = require('validator'), + db = require('../database'), + topics = require('../topics'), + user = require('../user'), + privileges = require('../privileges'), + plugins = require('../plugins'), + cache = require('./cache'), + utils = require('../../public/src/utils'); + +module.exports = function(Posts) { + + Posts.edit = function(data, callback) { + var now = Date.now(); + var postData; + + async.waterfall([ + function (next) { + privileges.posts.canEdit(data.pid, data.uid, next); + }, + function(canEdit, next) { + if (!canEdit) { + return next(new Error('[[error:no-privileges]]')); + } + Posts.getPostData(data.pid, next); + }, + function(_postData, next) { + postData = _postData; + postData.content = data.content; + postData.edited = now; + postData.editor = data.uid; + plugins.fireHook('filter:post.edit', {post: postData, 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); + } + ], function(err, result) { + if (err) { + return callback(err); + } + + async.parallel({ + editor: function(next) { + user.getUserFields(data.uid, ['username', 'userslug'], next); + }, + topic: function(next) { + editMainPost(data, postData, next); + }, + post: function(next) { + cache.del(postData.pid); + Posts.parsePost(postData, next); + } + }, function(err, results) { + if (err) { + return callback(err); + } + postData.cid = results.topic.cid; + plugins.fireHook('action:post.edit', postData); + + callback(null, results); + }); + }); + }; + + function editMainPost(data, postData, callback) { + var tid = postData.tid; + var title = data.title.trim(); + + async.parallel({ + cid: function(next) { + topics.getTopicField(tid, 'cid', next); + }, + isMain: function(next) { + Posts.isMain(data.pid, next); + } + }, function(err, results) { + if (err) { + return callback(err); + } + + if (!results.isMain) { + return callback(null, { + tid: tid, + cid: results.cid, + isMainPost: false + }); + } + + var topicData = { + tid: tid, + cid: results.cid, + uid: postData.uid, + mainPid: data.pid + }; + + if (title) { + topicData.title = title; + topicData.slug = tid + '/' + utils.slugify(title); + } + + if (data.topic_thumb) { + topicData.thumb = data.topic_thumb; + } + + data.tags = data.tags || []; + + async.waterfall([ + function(next) { + db.setObject('topic:' + tid, topicData, next); + }, + function(next) { + topics.updateTags(tid, data.tags, next); + }, + function(next) { + topics.getTopicTagsObjects(tid, next); + }, + function(tags, next) { + topicData.tags = data.tags; + plugins.fireHook('action:topic.edit', topicData); + next(null, { + tid: tid, + cid: results.cid, + uid: postData.uid, + title: validator.escape(title), + slug: topicData.slug, + isMainPost: results.isMain, + tags: tags + }); + } + ], callback); + }); + } + + +}; diff --git a/src/posts/parse.js b/src/posts/parse.js new file mode 100644 index 0000000000..d2f3278aca --- /dev/null +++ b/src/posts/parse.js @@ -0,0 +1,33 @@ + +'use strict'; + +var cache = require('./cache'), + plugins = require('../plugins'); + +module.exports = function(Posts) { + + Posts.parsePost = function(postData, callback) { + postData.content = postData.content || ''; + + var cachedContent = cache.get(postData.pid); + if (cachedContent) { + postData.content = cachedContent; + return callback(null, postData); + } + + plugins.fireHook('filter:parse.post', {postData: postData}, function(err, data) { + if (err) { + return callback(err); + } + cache.set(data.postData.pid, data.postData.content); + + callback(null, data.postData); + }); + }; + + Posts.parseSignature = function(userData, uid, callback) { + userData.signature = userData.signature || ''; + + plugins.fireHook('filter:parse.signature', {userData: userData, uid: uid}, callback); + }; +}; \ No newline at end of file diff --git a/src/posts/summary.js b/src/posts/summary.js index 4b972dfec2..b41519473a 100644 --- a/src/posts/summary.js +++ b/src/posts/summary.js @@ -9,7 +9,6 @@ var async = require('async'), user = require('../user'), plugins = require('../plugins'), categories = require('../categories'), - postTools = require('../postTools'), utils = require('../../public/src/utils'); @@ -86,7 +85,7 @@ module.exports = function(Posts) { return next(null, post); } - postTools.parsePost(post, function(err, post) { + Posts.parsePost(post, function(err, post) { if (err) { return next(err); } diff --git a/src/posts/user.js b/src/posts/user.js index 8d64aeb621..46e132a186 100644 --- a/src/posts/user.js +++ b/src/posts/user.js @@ -6,7 +6,6 @@ var async = require('async'), user = require('../user'), groups = require('../groups'), meta = require('../meta'), - postTools = require('../postTools'), plugins = require('../plugins'); @@ -64,7 +63,7 @@ module.exports = function(Posts) { userData.signature = ''; return next(); } - postTools.parseSignature(userData, uid, next); + Posts.parseSignature(userData, uid, next); }, customProfileInfo: function(next) { plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next); diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 937f5637f6..3ca18e6e7f 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -104,12 +104,12 @@ SocketAdmin.themes.updateBranding = function(socket, data, callback) { }; SocketAdmin.plugins.toggleActive = function(socket, plugin_id, callback) { - require('../postTools').resetCache(); + require('../posts/cache').reset(); plugins.toggleActive(plugin_id, callback); }; SocketAdmin.plugins.toggleInstall = function(socket, data, callback) { - require('../postTools').resetCache(); + require('../posts/cache').reset(); plugins.toggleInstall(data.id, data.version, callback); }; diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 1dfe59bfb7..314028b14c 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -290,36 +290,22 @@ SocketPosts.edit = function(socket, data, callback) { return callback(new Error('[[error:content-too-long, ' + meta.config.maximumPostLength + ']]')); } - // uid, pid, title, content, options postTools.edit({ uid: socket.uid, handle: data.handle, pid: data.pid, title: data.title, content: data.content, - options: { - topic_thumb: data.topic_thumb, - tags: data.tags - } - }, function(err, results) { + topic_thumb: data.topic_thumb, + tags: data.tags + }, function(err, result) { if (err) { return callback(err); } - var result = { - pid: data.pid, - handle: data.handle, - title: results.topic.title, - slug: results.topic.slug, - isMainPost: results.topic.isMainPost, - tags: results.topic.tags, - content: results.content - }; - - if (parseInt(results.postData.deleted) !== 1) { - websockets.in('topic_' + results.topic.tid).emit('event:post_edited', result); - callback(); - return; + if (parseInt(result.post.deleted) !== 1) { + websockets.in('topic_' + result.topic.tid).emit('event:post_edited', result); + return callback(); } socket.emit('event:post_edited', result); @@ -327,8 +313,8 @@ SocketPosts.edit = function(socket, data, callback) { async.parallel({ admins: async.apply(groups.getMembers, 'administrators', 0, -1), - moderators: async.apply(groups.getMembers, 'cid:' + results.topic.cid + ':privileges:mods', 0, -1), - uidsInTopic: async.apply(websockets.getUidsInRoom, 'topic_' + results.topic.tid) + moderators: async.apply(groups.getMembers, 'cid:' + result.topic.cid + ':privileges:mods', 0, -1), + uidsInTopic: async.apply(websockets.getUidsInRoom, 'topic_' + result.topic.tid) }, function(err, results) { if (err) { return winston.error(err); @@ -507,7 +493,7 @@ SocketPosts.flag = function(socket, pid, callback) { function(topic, next) { post.topic = topic; message = '[[notifications:user_flagged_post_in, ' + userName + ', ' + topic.title + ']]'; - postTools.parsePost(post, next); + posts.parsePost(post, next); }, function(post, next) { async.parallel({ diff --git a/src/topics/create.js b/src/topics/create.js index ec1bf21d0d..5f5ea7b129 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -9,8 +9,6 @@ var async = require('async'), user = require('../user'), meta = require('../meta'), posts = require('../posts'), - threadTools = require('../threadTools'), - postTools = require('../postTools'), privileges = require('../privileges'), categories = require('../categories'); @@ -256,7 +254,7 @@ module.exports = function(Topics) { posts.getPidIndex(postData.pid, uid, next); }, content: function(next) { - postTools.parsePost(postData, next); + posts.parsePost(postData, next); } }, next); }, diff --git a/src/topics/teaser.js b/src/topics/teaser.js index 1726f7c770..4574151554 100644 --- a/src/topics/teaser.js +++ b/src/topics/teaser.js @@ -9,7 +9,6 @@ var async = require('async'), user = require('../user'), posts = require('../posts'), plugins = require('../plugins'), - postTools = require('../postTools'), utils = require('../../public/src/utils'); @@ -56,7 +55,7 @@ module.exports = function(Topics) { post.user = users[post.uid]; post.timestamp = utils.toISOString(post.timestamp); tidToPost[post.tid] = post; - postTools.parsePost(post, next); + posts.parsePost(post, next); }, function(err) { if (err) { return callback(err);