summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/users/groups/leave.ts
blob: 0e52f2abdf6c129e0530c4be8b70b9e53bcdad3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import $ from 'cafy';
import { ID } from '@/misc/cafy-id';
import define from '../../../define';
import { ApiError } from '../../../error';
import { UserGroups, UserGroupJoinings } from '@/models/index';

export const meta = {
	tags: ['groups', 'users'],

	requireCredential: true as const,

	kind: 'write:user-groups',

	params: {
		groupId: {
			validator: $.type(ID),
		},
	},

	errors: {
		noSuchGroup: {
			message: 'No such group.',
			code: 'NO_SUCH_GROUP',
			id: '62780270-1f67-5dc0-daca-3eb510612e31'
		},

		youAreOwner: {
			message: 'Your are the owner.',
			code: 'YOU_ARE_OWNER',
			id: 'b6d6e0c2-ef8a-9bb8-653d-79f4a3107c69'
		},
	}
};

export default define(meta, async (ps, me) => {
	// Fetch the group
	const userGroup = await UserGroups.findOne({
		id: ps.groupId,
	});

	if (userGroup == null) {
		throw new ApiError(meta.errors.noSuchGroup);
	}

	if (me.id === userGroup.userId) {
		throw new ApiError(meta.errors.youAreOwner);
	}

	await UserGroupJoinings.delete({ userGroupId: userGroup.id, userId: me.id });
});