mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	refactor: topic creation to use api lib
This commit is contained in:
		| @@ -3,4 +3,5 @@ | ||||
| module.exports = { | ||||
| 	users: require('./users'), | ||||
| 	groups: require('./groups'), | ||||
| 	topics: require('./topics'), | ||||
| }; | ||||
|   | ||||
| @@ -14,6 +14,8 @@ const meta = require('../meta'); | ||||
| const middleware = require('../middleware'); | ||||
| const translator = require('../translator'); | ||||
|  | ||||
| const websockets = require('../socket.io'); | ||||
|  | ||||
| const isLanguageKey = /^\[\[[\w.\-_:]+]]$/; | ||||
| const helpers = module.exports; | ||||
|  | ||||
| @@ -341,8 +343,8 @@ helpers.getHomePageRoutes = async function (uid) { | ||||
| }; | ||||
|  | ||||
| helpers.formatApiResponse = async (statusCode, res, payload) => { | ||||
| 	if (statusCode === 200) { | ||||
| 		res.status(200).json({ | ||||
| 	if (String(statusCode).startsWith('2')) { | ||||
| 		res.status(statusCode).json({ | ||||
| 			status: { | ||||
| 				code: 'ok', | ||||
| 				message: 'OK', | ||||
| @@ -426,6 +428,11 @@ helpers.generateError = (statusCode, message) => { | ||||
| }; | ||||
|  | ||||
| helpers.buildReqObject = (req) => { | ||||
| 	// If a socket object is received instead, handle accordingly | ||||
| 	if (req.id) { | ||||
| 		return websockets.reqFromSocket(req); | ||||
| 	} | ||||
|  | ||||
| 	var headers = req.headers; | ||||
| 	var encrypted = !!req.connection.encrypted; | ||||
| 	var host = headers.host; | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const api = require('../../api'); | ||||
| const topics = require('../../topics'); | ||||
| const posts = require('../../posts'); | ||||
| const user = require('../../user'); | ||||
| @@ -13,29 +14,12 @@ const socketHelpers = require('../../socket.io/helpers'); | ||||
| const Topics = module.exports; | ||||
|  | ||||
| Topics.create = async (req, res) => { | ||||
| 	const payload = { ...req.body }; | ||||
| 	payload.tags = payload.tags || []; | ||||
| 	payload.uid = req.user.uid; | ||||
| 	payload.uid = req.user.uid; | ||||
| 	payload.req = req; | ||||
| 	payload.timestamp = Date.now(); | ||||
| 	payload.fromQueue = false; | ||||
|  | ||||
| 	// Blacklist & Post Queue | ||||
| 	await meta.blacklist.test(req.ip); | ||||
| 	const shouldQueue = await posts.shouldQueue(req.user.uid, payload); | ||||
| 	if (shouldQueue) { | ||||
| 		const queueObj = await posts.addToQueue(payload); | ||||
| 		return helpers.formatApiResponse(202, res, queueObj); | ||||
| 	const payload = await api.topics.create(req, req.body); | ||||
| 	if (payload.queued) { | ||||
| 		helpers.formatApiResponse(202, res, payload); | ||||
| 	} else { | ||||
| 		helpers.formatApiResponse(200, res, payload); | ||||
| 	} | ||||
|  | ||||
| 	const result = await topics.post(payload); | ||||
| 	helpers.formatApiResponse(200, res, result.topicData); | ||||
|  | ||||
| 	// TODO | ||||
| 	// socket.emit('event:new_post', { posts: [result.postData] }); | ||||
| 	// socket.emit('event:new_topic', result.topicData); | ||||
| 	socketHelpers.notifyNew(req.user.uid, 'newTopic', { posts: [result.postData], topic: result.topicData }); | ||||
| }; | ||||
|  | ||||
| Topics.reply = async (req, res) => { | ||||
| @@ -54,7 +38,8 @@ Topics.reply = async (req, res) => { | ||||
| 	await meta.blacklist.test(req.ip); | ||||
| 	const shouldQueue = await posts.shouldQueue(req.user.uid, payload); | ||||
| 	if (shouldQueue) { | ||||
| 		return await posts.addToQueue(payload); | ||||
| 		const queueObj = await posts.addToQueue(payload); | ||||
| 		return helpers.formatApiResponse(202, res, queueObj); | ||||
| 	} | ||||
|  | ||||
| 	const postData = await topics.reply(payload);	// postData seems to be a subset of postObj, refactor? | ||||
| @@ -163,7 +148,7 @@ async function doTopicAction(action, event, socket, { tids }) { | ||||
| 		const title = await topics.getTopicField(tid, 'title'); | ||||
| 		const data = await topics.tools[action](tid, socket.uid); | ||||
| 		const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids); | ||||
| 		socketHelpers.emitToTopicAndCategory(event, data, notifyUids); | ||||
| 		socketHelpers.emitToUids(event, data, notifyUids); | ||||
| 		await logTopicAction(action, socket, tid, title); | ||||
| 	})); | ||||
| } | ||||
|   | ||||
| @@ -191,7 +191,7 @@ SocketHelpers.rescindUpvoteNotification = async function (pid, fromuid) { | ||||
| 	websockets.in('uid_' + uid).emit('event:notifications.updateCount', count); | ||||
| }; | ||||
|  | ||||
| SocketHelpers.emitToTopicAndCategory = async function (event, data, uids) { | ||||
| SocketHelpers.emitToUids = async function (event, data, uids) { | ||||
| 	uids.forEach(toUid => websockets.in('uid_' + toUid).emit(event, data)); | ||||
| }; | ||||
|  | ||||
|   | ||||
| @@ -13,7 +13,6 @@ const logger = require('../logger'); | ||||
| const plugins = require('../plugins'); | ||||
| const ratelimit = require('../middleware/ratelimit'); | ||||
|  | ||||
|  | ||||
| const Namespaces = {}; | ||||
|  | ||||
| const Sockets = module.exports; | ||||
|   | ||||
| @@ -1,13 +1,12 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const api = require('../api'); | ||||
| const topics = require('../topics'); | ||||
| const posts = require('../posts'); | ||||
| const user = require('../user'); | ||||
| const meta = require('../meta'); | ||||
| const apiController = require('../controllers/api'); | ||||
| const privileges = require('../privileges'); | ||||
| const sockets = require('.'); | ||||
| const socketHelpers = require('./helpers'); | ||||
|  | ||||
| const SocketTopics = module.exports; | ||||
|  | ||||
| @@ -20,30 +19,9 @@ require('./topics/merge')(SocketTopics); | ||||
|  | ||||
| SocketTopics.post = async function (socket, data) { | ||||
| 	sockets.warnDeprecated(socket, 'POST /api/v3/topics'); | ||||
|  | ||||
| 	if (!data) { | ||||
| 		throw new Error('[[error:invalid-data]]'); | ||||
| 	} | ||||
|  | ||||
| 	socketHelpers.setDefaultPostData(data, socket); | ||||
| 	await meta.blacklist.test(data.req.ip); | ||||
| 	const shouldQueue = await posts.shouldQueue(socket.uid, data); | ||||
| 	if (shouldQueue) { | ||||
| 		return await posts.addToQueue(data); | ||||
| 	} | ||||
| 	return await postTopic(socket, data); | ||||
| 	return await api.topics.create(socket, data); | ||||
| }; | ||||
|  | ||||
| async function postTopic(socket, data) { | ||||
| 	const result = await topics.post(data); | ||||
|  | ||||
| 	socket.emit('event:new_post', { posts: [result.postData] }); | ||||
| 	socket.emit('event:new_topic', result.topicData); | ||||
|  | ||||
| 	socketHelpers.notifyNew(socket.uid, 'newTopic', { posts: [result.postData], topic: result.topicData }); | ||||
| 	return result.topicData; | ||||
| } | ||||
|  | ||||
| SocketTopics.postcount = async function (socket, tid) { | ||||
| 	const canRead = await privileges.topics.can('topics:read', tid, socket.uid); | ||||
| 	if (!canRead) { | ||||
|   | ||||
| @@ -30,7 +30,7 @@ module.exports = function (SocketTopics) { | ||||
| 			await topics.tools.move(tid, data); | ||||
|  | ||||
| 			const notifyUids = await privileges.categories.filterUids('topics:read', topicData.cid, uids); | ||||
| 			socketHelpers.emitToTopicAndCategory('event:topic_moved', topicData, notifyUids); | ||||
| 			socketHelpers.emitToUids('event:topic_moved', topicData, notifyUids); | ||||
| 			if (!topicData.deleted) { | ||||
| 				socketHelpers.sendNotificationToTopicOwner(tid, socket.uid, 'move', 'notifications:moved_your_topic'); | ||||
| 			} | ||||
|   | ||||
| @@ -88,7 +88,7 @@ module.exports = function (SocketTopics) { | ||||
| 			const title = await topics.getTopicField(tid, 'title'); | ||||
| 			const data = await topics.tools[action](tid, socket.uid); | ||||
| 			const notifyUids = await privileges.categories.filterUids('topics:read', data.cid, uids); | ||||
| 			socketHelpers.emitToTopicAndCategory(event, data, notifyUids); | ||||
| 			socketHelpers.emitToUids(event, data, notifyUids); | ||||
| 			await logTopicAction(action, socket, tid, title); | ||||
| 		})); | ||||
| 	}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user