mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-24 01:10:31 +01:00
closes #3051
updated lru to latest created new files posts/cache.js posts/parse.js posts/edit.js
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
163
src/postTools.js
163
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));
|
||||
|
||||
@@ -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) {
|
||||
|
||||
11
src/posts/cache.js
Normal file
11
src/posts/cache.js
Normal file
@@ -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;
|
||||
146
src/posts/edit.js
Normal file
146
src/posts/edit.js
Normal file
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
33
src/posts/parse.js
Normal file
33
src/posts/parse.js
Normal file
@@ -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);
|
||||
};
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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) {
|
||||
}, 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({
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user