Skip to content

Commit

Permalink
refactor: merge post.edit
Browse files Browse the repository at this point in the history
fix: dont fadeout/fadeint if title/post didnt change
  • Loading branch information
barisusakli committed Oct 17, 2020
1 parent bb85c05 commit 9738e20
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 136 deletions.
42 changes: 22 additions & 20 deletions public/src/client/topic/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ define('forum/topic/events', [
var navbarTitle = components.get('navbar/title').find('span');
var breadCrumb = components.get('breadcrumb/current');

if (topicTitle.length && data.topic.title && topicTitle.html() !== data.topic.title) {
if (topicTitle.length && data.topic.title && data.topic.renamed) {
ajaxify.data.title = 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);
Expand All @@ -126,27 +126,29 @@ define('forum/topic/events', [
});
}

editedPostEl.fadeOut(250, function () {
editedPostEl.html(translator.unescape(data.post.content));
editedPostEl.find('img:not(.not-responsive)').addClass('img-responsive');
images.wrapImagesInLinks(editedPostEl.parent());
posts.addBlockquoteEllipses(editedPostEl.parent());
editedPostEl.fadeIn(250);

var editData = {
editor: data.editor,
editedISO: utils.toISOString(data.post.edited),
};

Benchpress.parse('partials/topic/post-editor', editData, function (html) {
translator.translate(html, function (translated) {
html = $(translated);
editorEl.replaceWith(html);
$('[data-pid="' + data.post.pid + '"] [component="post/editor"] .timeago').timeago();
$(window).trigger('action:posts.edited', data);
if (data.post.changed) {
editedPostEl.fadeOut(250, function () {
editedPostEl.html(translator.unescape(data.post.content));
editedPostEl.find('img:not(.not-responsive)').addClass('img-responsive');
images.wrapImagesInLinks(editedPostEl.parent());
posts.addBlockquoteEllipses(editedPostEl.parent());
editedPostEl.fadeIn(250);

var editData = {
editor: data.editor,
editedISO: utils.toISOString(data.post.edited),
};

Benchpress.parse('partials/topic/post-editor', editData, function (html) {
translator.translate(html, function (translated) {
html = $(translated);
editorEl.replaceWith(html);
$('[data-pid="' + data.post.pid + '"] [component="post/editor"] .timeago').timeago();
$(window).trigger('action:posts.edited', data);
});
});
});
});
}

if (data.topic.tags && tagsUpdated(data.topic.tags)) {
Benchpress.parse('partials/topic/tags', { tags: data.topic.tags }, function (html) {
Expand Down
1 change: 1 addition & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
users: require('./users'),
groups: require('./groups'),
topics: require('./topics'),
posts: require('./posts'),
categories: require('./categories'),
};
65 changes: 65 additions & 0 deletions src/api/posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

const validator = require('validator');
const _ = require('lodash');

const utils = require('../utils');
const posts = require('../posts');
const groups = require('../groups');
const meta = require('../meta');
const events = require('../events');
const apiHelpers = require('./helpers');
const websockets = require('../socket.io');

const postsAPI = module.exports;

postsAPI.edit = async function (caller, data) {
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
throw new Error('[[error:invalid-data]]');
}
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
const contentLen = utils.stripHTMLTags(data.content).trim().length;

if (data.title && data.title.length < meta.config.minimumTitleLength) {
throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]');
} else if (data.title && data.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 + ']]');
}

data.uid = caller.uid;
data.req = apiHelpers.buildReqObject(caller);

const editResult = await posts.edit(data);
if (editResult.topic.renamed) {
await events.log({
type: 'topic-rename',
uid: caller.uid,
ip: caller.ip,
tid: editResult.topic.tid,
oldTitle: validator.escape(String(editResult.topic.oldTitle)),
newTitle: validator.escape(String(editResult.topic.title)),
});
}
const postObj = await posts.getPostSummaryByPids([editResult.post.pid], caller.uid, {});
const returnData = { ...postObj[0], ...editResult.post };

if (!editResult.post.deleted) {
websockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult);
return returnData;
}

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(String(caller.uid)));
uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult));
return returnData;
};
68 changes: 6 additions & 62 deletions src/controllers/write/posts.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
'use strict';

const validator = require('validator');
const _ = require('lodash');

const meta = require('../../meta');
const privileges = require('../../privileges');
const groups = require('../../groups');
const posts = require('../../posts');
const topics = require('../../topics');
const events = require('../../events');
const utils = require('../../utils');

const api = require('../../api');
const helpers = require('../helpers');
const sockets = require('../../socket.io');
const apiHelpers = require('../../api/helpers');
Expand All @@ -19,65 +14,14 @@ const socketPostHelpers = require('../../socket.io/posts/helpers'); // eehhh...
const Posts = module.exports;

Posts.edit = async (req, res) => {
// 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,
const editResult = await api.posts.edit(req, {
...req.body,
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];
}
uid: req.uid,
req: apiHelpers.buildReqObject(req),
});

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));
helpers.formatApiResponse(200, res, editResult);
};

Posts.purge = async (req, res) => {
Expand Down
1 change: 1 addition & 0 deletions src/posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module.exports = function (Posts) {
returnPostData.cid = topic.cid;
returnPostData.topic = topic;
returnPostData.editedISO = utils.toISOString(now);
returnPostData.changed = oldContent !== data.content;

await topics.notifyFollowers(returnPostData, data.uid, {
type: 'post-edit',
Expand Down
56 changes: 2 additions & 54 deletions src/socket.io/posts/edit.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use strict';

const validator = require('validator');
const _ = require('lodash');

const posts = require('../../posts');
const groups = require('../../groups');
const events = require('../../events');
const meta = require('../../meta');
const utils = require('../../utils');
const apiHelpers = require('../../api/helpers');
const api = require('../../api');
const websockets = require('../index');

module.exports = function (SocketPosts) {
Expand All @@ -17,52 +9,8 @@ module.exports = function (SocketPosts) {

if (!socket.uid) {
throw new Error('[[error:not-logged-in]]');
} else if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
throw new Error('[[error:invalid-data]]');
}

// Trim and remove HTML (latter for composers that send in HTML, like redactor)
var contentLen = utils.stripHTMLTags(data.content).trim().length;

if (data.title && data.title.length < meta.config.minimumTitleLength) {
throw new Error('[[error:title-too-short, ' + meta.config.minimumTitleLength + ']]');
} else if (data.title && data.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 + ']]');
}

data.uid = socket.uid;
data.req = apiHelpers.buildReqObject(socket);

const editResult = await posts.edit(data);
if (editResult.topic.renamed) {
await events.log({
type: 'topic-rename',
uid: socket.uid,
ip: socket.ip,
tid: editResult.topic.tid,
oldTitle: validator.escape(String(editResult.topic.oldTitle)),
newTitle: validator.escape(String(editResult.topic.title)),
});
}

if (!editResult.post.deleted) {
websockets.in('topic_' + editResult.topic.tid).emit('event:post_edited', editResult);
return editResult.post;
}

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(socket.uid.toString()));
uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult));
return editResult.post;
return await api.posts.edit(socket, data);
};
};

0 comments on commit 9738e20

Please sign in to comment.