mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	feat: DELETE /api/v3/chats/:roomId/users and DELETE /api/v3/chats/:roomId/users/:uid
				
					
				
			This commit is contained in:
		| @@ -142,6 +142,8 @@ paths: | |||||||
|     $ref: 'write/chats/roomId.yaml' |     $ref: 'write/chats/roomId.yaml' | ||||||
|   /chats/{roomId}/users: |   /chats/{roomId}/users: | ||||||
|     $ref: 'write/chats/roomId/users.yaml' |     $ref: 'write/chats/roomId/users.yaml' | ||||||
|  |   /chats/{roomId}/users/{uid}: | ||||||
|  |     $ref: 'write/chats/roomId/users/uid.yaml' | ||||||
|   /chats/{roomId}/{mid}: |   /chats/{roomId}/{mid}: | ||||||
|     $ref: 'write/chats/roomId/mid.yaml' |     $ref: 'write/chats/roomId/mid.yaml' | ||||||
|   /flags/: |   /flags/: | ||||||
|   | |||||||
| @@ -46,7 +46,7 @@ post: | |||||||
|             uids: |             uids: | ||||||
|               type: array |               type: array | ||||||
|               description: A list of valid uids |               description: A list of valid uids | ||||||
|               example: [2] |               example: [2, 4] | ||||||
|               items: |               items: | ||||||
|                 type: number |                 type: number | ||||||
|                 description: A valid uid |                 description: A valid uid | ||||||
| @@ -62,3 +62,42 @@ post: | |||||||
|                 $ref: ../../../components/schemas/Status.yaml#/Status |                 $ref: ../../../components/schemas/Status.yaml#/Status | ||||||
|               response: |               response: | ||||||
|                 $ref: ../../../components/schemas/Chats.yaml#/RoomUserList |                 $ref: ../../../components/schemas/Chats.yaml#/RoomUserList | ||||||
|  | delete: | ||||||
|  |   tags: | ||||||
|  |     - chats | ||||||
|  |   summary: remove users from chat room | ||||||
|  |   description: This operation removes (kicks) a user from a chat room | ||||||
|  |   parameters: | ||||||
|  |     - in: path | ||||||
|  |       name: roomId | ||||||
|  |       schema: | ||||||
|  |         type: number | ||||||
|  |       required: true | ||||||
|  |       description: a valid chat room id | ||||||
|  |       example: 1 | ||||||
|  |   requestBody: | ||||||
|  |     required: true | ||||||
|  |     content: | ||||||
|  |       application/json: | ||||||
|  |         schema: | ||||||
|  |           type: object | ||||||
|  |           properties: | ||||||
|  |             uids: | ||||||
|  |               type: array | ||||||
|  |               description: A list of valid uids | ||||||
|  |               example: [2] | ||||||
|  |               items: | ||||||
|  |                 type: number | ||||||
|  |                 description: A valid uid | ||||||
|  |   responses: | ||||||
|  |     '200': | ||||||
|  |       description: users successfully removed from chat room | ||||||
|  |       content: | ||||||
|  |         application/json: | ||||||
|  |           schema: | ||||||
|  |             type: object | ||||||
|  |             properties: | ||||||
|  |               status: | ||||||
|  |                 $ref: ../../../components/schemas/Status.yaml#/Status | ||||||
|  |               response: | ||||||
|  |                 $ref: ../../../components/schemas/Chats.yaml#/RoomUserList | ||||||
							
								
								
									
										32
									
								
								public/openapi/write/chats/roomId/users/uid.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								public/openapi/write/chats/roomId/users/uid.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | delete: | ||||||
|  |   tags: | ||||||
|  |     - chats | ||||||
|  |   summary: remove one user from chat room | ||||||
|  |   description: This operation removes (kicks) a single user from a chat room | ||||||
|  |   parameters: | ||||||
|  |     - in: path | ||||||
|  |       name: roomId | ||||||
|  |       schema: | ||||||
|  |         type: number | ||||||
|  |       required: true | ||||||
|  |       description: a valid chat room id | ||||||
|  |       example: 1 | ||||||
|  |     - in: path | ||||||
|  |       name: uid | ||||||
|  |       schema: | ||||||
|  |         type: number | ||||||
|  |       required: true | ||||||
|  |       description: a valid user id | ||||||
|  |       example: 4 | ||||||
|  |   responses: | ||||||
|  |     '200': | ||||||
|  |       description: user successfully removed from chat room | ||||||
|  |       content: | ||||||
|  |         application/json: | ||||||
|  |           schema: | ||||||
|  |             type: object | ||||||
|  |             properties: | ||||||
|  |               status: | ||||||
|  |                 $ref: ../../../../components/schemas/Status.yaml#/Status | ||||||
|  |               response: | ||||||
|  |                 $ref: ../../../../components/schemas/Chats.yaml#/RoomUserList | ||||||
| @@ -264,16 +264,9 @@ define('forum/chats', [ | |||||||
| 		modal.on('click', '[data-action="kick"]', function () { | 		modal.on('click', '[data-action="kick"]', function () { | ||||||
| 			const uid = parseInt(this.getAttribute('data-uid'), 10); | 			const uid = parseInt(this.getAttribute('data-uid'), 10); | ||||||
|  |  | ||||||
| 			socket.emit('modules.chats.removeUserFromRoom', { | 			api.delete(`/chats/${roomId}/users/${uid}`, {}).then((body) => { | ||||||
| 				roomId: roomId, | 				Chats.refreshParticipantsList(roomId, modal, body); | ||||||
| 				uid: uid, | 			}).catch(alerts.error); | ||||||
| 			}, function (err) { |  | ||||||
| 				if (err) { |  | ||||||
| 					return alerts.error(err); |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				Chats.refreshParticipantsList(roomId, modal); |  | ||||||
| 			}); |  | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -101,3 +101,15 @@ chatsAPI.invite = async (caller, data) => { | |||||||
| 	delete data.uids; | 	delete data.uids; | ||||||
| 	return chatsAPI.users(caller, data); | 	return chatsAPI.users(caller, data); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | chatsAPI.kick = async (caller, data) => { | ||||||
|  | 	const uidsExist = await user.exists(data.uids); | ||||||
|  | 	if (!uidsExist.every(Boolean)) { | ||||||
|  | 		throw new Error('[[error:no-user]]'); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	await messaging.removeUsersFromRoom(caller.uid, data.uids, data.roomId); | ||||||
|  |  | ||||||
|  | 	delete data.uids; | ||||||
|  | 	return chatsAPI.users(caller, data); | ||||||
|  | }; | ||||||
|   | |||||||
| @@ -70,7 +70,22 @@ Chats.invite = async (req, res) => { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| Chats.kick = async (req, res) => { | Chats.kick = async (req, res) => { | ||||||
| 	// ... | 	const users = await api.chats.kick(req, { | ||||||
|  | 		...req.body, | ||||||
|  | 		roomId: req.params.roomId, | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	helpers.formatApiResponse(200, res, users); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | Chats.kickUser = async (req, res) => { | ||||||
|  | 	req.body.uids = [req.params.uid]; | ||||||
|  | 	const users = await api.chats.kick(req, { | ||||||
|  | 		...req.body, | ||||||
|  | 		roomId: req.params.roomId, | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	helpers.formatApiResponse(200, res, users); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| Chats.messages = {}; | Chats.messages = {}; | ||||||
|   | |||||||
| @@ -21,7 +21,8 @@ module.exports = function () { | |||||||
|  |  | ||||||
| 	setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users); | 	setupApiRoute(router, 'get', '/:roomId/users', [...middlewares, middleware.assert.room], controllers.write.chats.users); | ||||||
| 	setupApiRoute(router, 'post', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.invite); | 	setupApiRoute(router, 'post', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.invite); | ||||||
| 	// setupApiRoute(router, 'delete', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.kick); | 	setupApiRoute(router, 'delete', '/:roomId/users', [...middlewares, middleware.assert.room, middleware.checkRequired.bind(null, ['uids'])], controllers.write.chats.kick); | ||||||
|  | 	setupApiRoute(router, 'delete', '/:roomId/users/:uid', [...middlewares, middleware.assert.room, middleware.assert.user], controllers.write.chats.kickUser); | ||||||
|  |  | ||||||
| 	setupApiRoute(router, 'get', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.get); | 	setupApiRoute(router, 'get', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.get); | ||||||
| 	setupApiRoute(router, 'put', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.edit); | 	setupApiRoute(router, 'put', '/:roomId/:mid', [...middlewares, middleware.assert.room, middleware.assert.message], controllers.write.chats.messages.edit); | ||||||
|   | |||||||
| @@ -117,15 +117,17 @@ SocketModules.chats.addUserToRoom = async function (socket, data) { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketModules.chats.removeUserFromRoom = async function (socket, data) { | SocketModules.chats.removeUserFromRoom = async function (socket, data) { | ||||||
|  | 	sockets.warnDeprecated(socket, 'DELETE /api/v3/chats/:roomId/users OR DELETE /api/v3/chats/:roomId/users/:uid'); | ||||||
|  |  | ||||||
| 	if (!data || !data.roomId) { | 	if (!data || !data.roomId) { | ||||||
| 		throw new Error('[[error:invalid-data]]'); | 		throw new Error('[[error:invalid-data]]'); | ||||||
| 	} | 	} | ||||||
| 	const exists = await user.exists(data.uid); |  | ||||||
| 	if (!exists) { |  | ||||||
| 		throw new Error('[[error:no-user]]'); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	await Messaging.removeUsersFromRoom(socket.uid, [data.uid], data.roomId); | 	// Revised API can accept multiple uids now | ||||||
|  | 	data.uids = [data.uid]; | ||||||
|  | 	delete data.uid; | ||||||
|  |  | ||||||
|  | 	await api.chats.kick(socket, data); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketModules.chats.leave = async function (socket, roomid) { | SocketModules.chats.leave = async function (socket, roomid) { | ||||||
|   | |||||||
| @@ -390,6 +390,7 @@ describe('API', async () => { | |||||||
|  |  | ||||||
| 					try { | 					try { | ||||||
| 						if (type === 'json') { | 						if (type === 'json') { | ||||||
|  | 							console.log(`calling ${url}`); | ||||||
| 							response = await request(url, { | 							response = await request(url, { | ||||||
| 								method: method, | 								method: method, | ||||||
| 								jar: !unauthenticatedRoutes.includes(path) ? jar : undefined, | 								jar: !unauthenticatedRoutes.includes(path) ? jar : undefined, | ||||||
| @@ -418,7 +419,12 @@ describe('API', async () => { | |||||||
|  |  | ||||||
| 				it('response status code should match one of the schema defined responses', () => { | 				it('response status code should match one of the schema defined responses', () => { | ||||||
| 					// HACK: allow HTTP 418 I am a teapot, for now   👇 | 					// HACK: allow HTTP 418 I am a teapot, for now   👇 | ||||||
| 					assert(context[method].responses.hasOwnProperty('418') || Object.keys(context[method].responses).includes(String(response.statusCode)), `${method.toUpperCase()} ${path} sent back unexpected HTTP status code: ${response.statusCode}`); | 					try { | ||||||
|  | 						assert(context[method].responses.hasOwnProperty('418') || Object.keys(context[method].responses).includes(String(response.statusCode)), `${method.toUpperCase()} ${path} sent back unexpected HTTP status code: ${response.statusCode}`); | ||||||
|  | 					} catch (e) { | ||||||
|  | 						console.log(response.body); | ||||||
|  | 						throw e; | ||||||
|  | 					} | ||||||
| 				}); | 				}); | ||||||
|  |  | ||||||
| 				// Recursively iterate through schema properties, comparing type | 				// Recursively iterate through schema properties, comparing type | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user