mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	refactor: merge post.edit
fix: dont fadeout/fadeint if title/post didnt change
This commit is contained in:
		| @@ -110,7 +110,7 @@ define('forum/topic/events', [ | |||||||
| 		var navbarTitle = components.get('navbar/title').find('span'); | 		var navbarTitle = components.get('navbar/title').find('span'); | ||||||
| 		var breadCrumb = components.get('breadcrumb/current'); | 		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; | 			ajaxify.data.title = data.topic.title; | ||||||
| 			var newUrl = 'topic/' + data.topic.slug + (window.location.search ? window.location.search : ''); | 			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); | 			history.replaceState({ url: newUrl }, null, window.location.protocol + '//' + window.location.host + config.relative_path + '/' + newUrl); | ||||||
| @@ -126,6 +126,7 @@ define('forum/topic/events', [ | |||||||
| 			}); | 			}); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		if (data.post.changed) { | ||||||
| 			editedPostEl.fadeOut(250, function () { | 			editedPostEl.fadeOut(250, function () { | ||||||
| 				editedPostEl.html(translator.unescape(data.post.content)); | 				editedPostEl.html(translator.unescape(data.post.content)); | ||||||
| 				editedPostEl.find('img:not(.not-responsive)').addClass('img-responsive'); | 				editedPostEl.find('img:not(.not-responsive)').addClass('img-responsive'); | ||||||
| @@ -147,6 +148,7 @@ define('forum/topic/events', [ | |||||||
| 					}); | 					}); | ||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		if (data.topic.tags && tagsUpdated(data.topic.tags)) { | 		if (data.topic.tags && tagsUpdated(data.topic.tags)) { | ||||||
| 			Benchpress.parse('partials/topic/tags', { tags: data.topic.tags }, function (html) { | 			Benchpress.parse('partials/topic/tags', { tags: data.topic.tags }, function (html) { | ||||||
|   | |||||||
| @@ -4,5 +4,6 @@ module.exports = { | |||||||
| 	users: require('./users'), | 	users: require('./users'), | ||||||
| 	groups: require('./groups'), | 	groups: require('./groups'), | ||||||
| 	topics: require('./topics'), | 	topics: require('./topics'), | ||||||
|  | 	posts: require('./posts'), | ||||||
| 	categories: require('./categories'), | 	categories: require('./categories'), | ||||||
| }; | }; | ||||||
|   | |||||||
							
								
								
									
										65
									
								
								src/api/posts.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								src/api/posts.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  | }; | ||||||
| @@ -1,16 +1,11 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| const validator = require('validator'); |  | ||||||
| const _ = require('lodash'); |  | ||||||
|  |  | ||||||
| const meta = require('../../meta'); |  | ||||||
| const privileges = require('../../privileges'); | const privileges = require('../../privileges'); | ||||||
| const groups = require('../../groups'); |  | ||||||
| const posts = require('../../posts'); | const posts = require('../../posts'); | ||||||
| const topics = require('../../topics'); | const topics = require('../../topics'); | ||||||
| const events = require('../../events'); | const events = require('../../events'); | ||||||
| const utils = require('../../utils'); |  | ||||||
|  |  | ||||||
|  | const api = require('../../api'); | ||||||
| const helpers = require('../helpers'); | const helpers = require('../helpers'); | ||||||
| const sockets = require('../../socket.io'); | const sockets = require('../../socket.io'); | ||||||
| const apiHelpers = require('../../api/helpers'); | const apiHelpers = require('../../api/helpers'); | ||||||
| @@ -19,65 +14,14 @@ const socketPostHelpers = require('../../socket.io/posts/helpers');	// eehhh... | |||||||
| const Posts = module.exports; | const Posts = module.exports; | ||||||
|  |  | ||||||
| Posts.edit = async (req, res) => { | Posts.edit = async (req, res) => { | ||||||
| 	// Trim and remove HTML (latter for composers that send in HTML, like redactor) | 	const editResult = await api.posts.edit(req, { | ||||||
| 	var contentLen = utils.stripHTMLTags(req.body.content).trim().length; | 		...req.body, | ||||||
|  |  | ||||||
| 	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, | 		pid: req.params.pid, | ||||||
| 		content: req.body.content, | 		uid: req.uid, | ||||||
| 		options: {}, | 		req: apiHelpers.buildReqObject(req), | ||||||
| 	}; |  | ||||||
| 	['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, editResult); | ||||||
| 	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)); |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
| Posts.purge = async (req, res) => { | Posts.purge = async (req, res) => { | ||||||
|   | |||||||
| @@ -67,6 +67,7 @@ module.exports = function (Posts) { | |||||||
| 		returnPostData.cid = topic.cid; | 		returnPostData.cid = topic.cid; | ||||||
| 		returnPostData.topic = topic; | 		returnPostData.topic = topic; | ||||||
| 		returnPostData.editedISO = utils.toISOString(now); | 		returnPostData.editedISO = utils.toISOString(now); | ||||||
|  | 		returnPostData.changed = oldContent !== data.content; | ||||||
|  |  | ||||||
| 		await topics.notifyFollowers(returnPostData, data.uid, { | 		await topics.notifyFollowers(returnPostData, data.uid, { | ||||||
| 			type: 'post-edit', | 			type: 'post-edit', | ||||||
|   | |||||||
| @@ -1,14 +1,6 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| const validator = require('validator'); | const api = require('../../api'); | ||||||
| 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 websockets = require('../index'); | const websockets = require('../index'); | ||||||
|  |  | ||||||
| module.exports = function (SocketPosts) { | module.exports = function (SocketPosts) { | ||||||
| @@ -17,52 +9,8 @@ module.exports = function (SocketPosts) { | |||||||
|  |  | ||||||
| 		if (!socket.uid) { | 		if (!socket.uid) { | ||||||
| 			throw new Error('[[error:not-logged-in]]'); | 			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) | 		return await api.posts.edit(socket, data); | ||||||
| 		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; |  | ||||||
| 	}; | 	}; | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user