summary refs log tree commit diff
path: root/api/src/util/Member.ts
blob: 7b06720bed51b053e2000e031d8792a239587dfa (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import {
	Guild,
	GuildCreateEvent,
	GuildDeleteEvent,
	GuildMemberAddEvent,
	GuildMemberRemoveEvent,
	GuildMemberUpdateEvent,
	GuildModel,
	MemberModel,
	RoleModel,
	toObject,
	UserModel,
	GuildDocument,
	Config
} from "@fosscord/server-util";

import { HTTPError } from "lambert-server";
import { emitEvent } from "./Event";
import { getPublicUser } from "./User";

export const PublicMemberProjection = {
	id: true,
	guild_id: true,
	nick: true,
	roles: true,
	joined_at: true,
	pending: true,
	deaf: true,
	mute: true,
	premium_since: true
};

export async function isMember(user_id: string, guild_id: string) {
	const exists = await MemberModel.exists({ id: user_id, guild_id });
	if (!exists) throw new HTTPError("You are not a member of this guild", 403);
	return exists;
}

export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) {
	const user = await getPublicUser(user_id, { guilds: true });

	const { maxGuilds } = Config.get().limits.user;
	if (user.guilds.length >= maxGuilds) {
		throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403);
	}

	const guild = cache?.guild || (await GuildModel.findOne({ id: guild_id }).exec());

	if (!guild) throw new HTTPError("Guild not found", 404);

	if (await MemberModel.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400);

	const member = {
		id: user_id,
		guild_id: guild_id,
		nick: undefined,
		roles: [guild_id], // @everyone role
		joined_at: new Date(),
		premium_since: undefined,
		deaf: false,
		mute: false,
		pending: false
	};

	await Promise.all([
		new MemberModel({
			...member,
			read_state: {},
			settings: {
				channel_overrides: [],
				message_notifications: 0,
				mobile_push: true,
				mute_config: null,
				muted: false,
				suppress_everyone: false,
				suppress_roles: false,
				version: 0
			}
		}).save(),

		UserModel.updateOne({ id: user_id }, { $push: { guilds: guild_id } }).exec(),
		GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: 1 } }).exec(),

		emitEvent({
			event: "GUILD_MEMBER_ADD",
			data: {
				...member,
				user,
				guild_id: guild_id
			},
			guild_id: guild_id
		} as GuildMemberAddEvent)
	]);

	await emitEvent({
		event: "GUILD_CREATE",
		data: toObject(
			await guild
				.populate({ path: "members", match: { guild_id } })
				.populate({ path: "joined_at", match: { id: user.id } })
				.execPopulate()
		),
		user_id
	} as GuildCreateEvent);
}

export async function removeMember(user_id: string, guild_id: string) {
	const user = await getPublicUser(user_id);

	const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec();
	if (!guild) throw new HTTPError("Guild not found", 404);
	if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild");
	if (!(await MemberModel.exists({ id: user.id, guild_id }))) throw new HTTPError("Is not member of this guild", 404);

	// use promise all to execute all promises at the same time -> save time
	return Promise.all([
		MemberModel.deleteOne({
			id: user_id,
			guild_id: guild_id
		}).exec(),
		UserModel.updateOne({ id: user.id }, { $pull: { guilds: guild_id } }).exec(),
		GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: -1 } }).exec(),

		emitEvent({
			event: "GUILD_DELETE",
			data: {
				id: guild_id
			},
			user_id: user_id
		} as GuildDeleteEvent),
		emitEvent({
			event: "GUILD_MEMBER_REMOVE",
			data: {
				guild_id: guild_id,
				user: user
			},
			guild_id: guild_id
		} as GuildMemberRemoveEvent)
	]);
}

export async function addRole(user_id: string, guild_id: string, role_id: string) {
	const user = await getPublicUser(user_id);

	const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
	if (!role) throw new HTTPError("role not found", 404);

	var memberObj = await MemberModel.findOneAndUpdate(
		{
			id: user_id,
			guild_id: guild_id
		},
		{ $push: { roles: role_id } }
	).exec();

	if (!memberObj) throw new HTTPError("Member not found", 404);

	await emitEvent({
		event: "GUILD_MEMBER_UPDATE",
		data: {
			guild_id: guild_id,
			user: user,
			roles: memberObj.roles
		},
		guild_id: guild_id
	} as GuildMemberUpdateEvent);
}

export async function removeRole(user_id: string, guild_id: string, role_id: string) {
	const user = await getPublicUser(user_id);

	const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
	if (!role) throw new HTTPError("role not found", 404);

	var memberObj = await MemberModel.findOneAndUpdate(
		{
			id: user_id,
			guild_id: guild_id
		},
		{ $pull: { roles: role_id } }
	).exec();

	if (!memberObj) throw new HTTPError("Member not found", 404);

	await emitEvent({
		event: "GUILD_MEMBER_UPDATE",
		data: {
			guild_id: guild_id,
			user: user,
			roles: memberObj.roles
		},
		guild_id: guild_id
	} as GuildMemberUpdateEvent);
}

export async function changeNickname(user_id: string, guild_id: string, nickname: string) {
	const user = await getPublicUser(user_id);

	var memberObj = await MemberModel.findOneAndUpdate(
		{
			id: user_id,
			guild_id: guild_id
		},
		{ nick: nickname }
	).exec();

	if (!memberObj) throw new HTTPError("Member not found", 404);

	await emitEvent({
		event: "GUILD_MEMBER_UPDATE",
		data: {
			guild_id: guild_id,
			user: user,
			nick: nickname
		},
		guild_id: guild_id
	} as GuildMemberUpdateEvent);
}