mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	feat(writeapi): added DELETE /groups/:slug/membership/:uid route
This commit is contained in:
		| @@ -1,9 +1,13 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const validator = require('validator'); | ||||
|  | ||||
| const user = require('../../user'); | ||||
| const groups = require('../../groups'); | ||||
| const events = require('../../events'); | ||||
| const meta = require('../../meta'); | ||||
| const utils = require('../../utils'); | ||||
| const notifications = require('../../notifications'); | ||||
|  | ||||
| const helpers = require('../helpers'); | ||||
|  | ||||
| @@ -85,6 +89,42 @@ Groups.join = async (req, res) => { | ||||
| 	}); | ||||
| }; | ||||
|  | ||||
| Groups.leave = async (req, res) => { | ||||
| 	const [group, userExists] = await Promise.all([ | ||||
| 		groups.getByGroupslug(req.params.slug, { | ||||
| 			uid: req.params.uid, | ||||
| 		}), | ||||
| 		user.exists(req.params.uid), | ||||
| 	]); | ||||
|  | ||||
| 	if (!userExists) { | ||||
| 		throw new Error('[[error:invalid-uid]]'); | ||||
| 	} else if (group.disableLeave) { | ||||
| 		throw new Error('[[error:group-leave-disabled]]'); | ||||
| 	} else if (!group.isMember) { | ||||
| 		// No change | ||||
| 		return helpers.formatApiResponse(200, res); | ||||
| 	} | ||||
|  | ||||
| 	await groups.leave(group.name, req.params.uid); | ||||
|  | ||||
| 	// Notify owners of user having left | ||||
| 	const username = await user.getUserField(req.params.uid, 'username'); | ||||
| 	const notification = await notifications.create({ | ||||
| 		type: 'group-leave', | ||||
| 		bodyShort: '[[groups:membership.leave.notification-title, ' + username + ', ' + group.name + ']]', | ||||
| 		nid: 'group:' + validator.escape(group.name) + ':uid:' + req.params.uid + ':group-leave', | ||||
| 		path: '/groups/' + utils.slugify(group.name), | ||||
| 	}); | ||||
| 	const uids = await groups.getOwners(group.name); | ||||
| 	await notifications.push(notification, uids); | ||||
|  | ||||
| 	helpers.formatApiResponse(200, res); | ||||
| 	logGroupEvent(req, 'group-leave', { | ||||
| 		groupName: group.name, | ||||
| 	}); | ||||
| }; | ||||
|  | ||||
| function logGroupEvent(req, event, additional) { | ||||
| 	events.log({ | ||||
| 		type: event, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user