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
|
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Request, Response, Router } from "express";
import { Not } from "typeorm";
import { route } from "@spacebar/api/middlewares";
import { Channel, Emoji, Guild, InstanceBan, Member, Recipient, Sticker, User, UserSettingsProtos } from "@spacebar/database";
import { ChannelDeleteEvent, ChannelRecipientRemoveEvent, emitEvent, UserDeleteEvent } from "@spacebar/util";
import { ChannelType, InstanceUserDeleteSchema, PrivateUserProjection } from "@spacebar/schemas";
import { Stopwatch } from "@spacebar/extensions";
const router = Router({ mergeParams: true });
router.post(
"/",
route({
right: "MANAGE_USERS",
requestBody: "InstanceUserDeleteSchema",
responses: {
204: {},
403: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const sw = Stopwatch.startNew();
const body = req.body as InstanceUserDeleteSchema | undefined;
const user = await User.findOneOrFail({
where: { id: req.params.user_id as string },
select: Object.fromEntries([...PrivateUserProjection, "data"].map((i) => [i, true])), // TODO: clean up
});
if ((body?.persistInstanceBan ?? true) && !(await InstanceBan.findOne({ where: { user_id: user.id } })))
await InstanceBan.create({ user_id: user.id, reason: body?.reason ?? "<legacy instance ban API - no reason specified>" }).save();
// prevent bugginess with clients - delete all DMs, only having half of the conversation is quite useless anyhow
const dmChannels = await user.getDmChannels();
for (const channel of dmChannels) {
console.log(`[Instance ban] Deleting DM channel ${channel.id} for user ${user.id}`);
await emitEvent({
event: "CHANNEL_DELETE",
data: channel.toJSON(),
channel_id: channel.id,
} satisfies ChannelDeleteEvent);
await Recipient.delete({ channel_id: channel.id });
await Channel.deleteChannel(channel);
}
//leave all group channels
const groupChannels = await Channel.find({
where: { type: ChannelType.GROUP_DM },
relations: { recipients: true },
select: {
id: true,
owner_id: true,
recipients: {
id: true,
user_id: true,
},
},
});
await Promise.all(
groupChannels.map(async (channel) => {
const recipient = channel.recipients!.find((r) => r.user_id === user.id);
if (recipient) {
await Recipient.delete({ id: recipient.id });
await emitEvent({
event: "CHANNEL_RECIPIENT_REMOVE",
data: {
user: user.toPublicUser(),
channel_id: channel.id,
},
channel_id: channel.id,
} satisfies ChannelRecipientRemoveEvent);
console.log(`[Instance ban] Removed user ${user.id} from group channel ${channel.id}`);
}
// if no recipients remain, delete the channel
const remainingRecipients = await Recipient.find({ where: { channel_id: channel.id } });
if (remainingRecipients.length === 0) {
await emitEvent({
event: "CHANNEL_DELETE",
data: channel.toJSON(),
channel_id: channel.id,
} satisfies ChannelDeleteEvent);
await Channel.deleteChannel(channel);
console.log(`[Instance ban] Deleted empty group channel ${channel.id}`);
} else {
// otherwise, if the banned user was the owner, reassign ownership
if (channel.owner_id === user.id) {
channel.owner_id = remainingRecipients[0].user_id;
await channel.save();
console.log(`[Instance ban] Reassigned ownership of group channel ${channel.id} to user ${channel.owner_id}`);
}
}
}),
);
// change ownership on guilds
const guilds = await Guild.find({ where: { owner_id: req.params.user_id as string } });
await Promise.all(
guilds.map(async (guild) => {
const members = await Member.find({
where: { guild_id: guild.id, id: Not(req.params.user_id as string) },
relations: { roles: true },
select: { id: true, roles: { id: true, position: true } },
});
const sortedMembers = members
.filter((m) => m.id !== req.params.user_id)
.sort((a, b) => {
const aHighestRole = a.roles.reduce((prev, curr) => (curr.position > prev.position ? curr : prev), { position: -1 } as { position: number });
const bHighestRole = b.roles.reduce((prev, curr) => (curr.position > prev.position ? curr : prev), { position: -1 } as { position: number });
return bHighestRole.position - aHighestRole.position;
});
if (sortedMembers.length === 0) {
// no members left, delete guild
await guild.remove();
console.log(`[Instance ban] Deleted guild ${guild.id} as user ${user.id} was the last member`);
} else {
// assign new owner
guild.owner_id = sortedMembers[0].id;
await guild.save();
console.log(`[Instance ban] Transferred ownership of guild ${guild.id} to user ${guild.owner_id}`);
// safety - reassign emojis/stickers owned by the old owner
const stickers = await Sticker.find({ where: { guild_id: guild.id, user_id: req.params.user_id as string } });
await Promise.all(
stickers.map(async (sticker) => {
sticker.user_id = guild.owner_id;
await sticker.save();
console.log(`[Instance ban] Reassigned sticker ${sticker.id} ownership to user ${guild.owner_id}`);
}),
);
const emojis = await Emoji.find({ where: { guild_id: guild.id, user_id: req.params.user_id as string } });
await Promise.all(
emojis.map(async (emoji) => {
emoji.user_id = guild.owner_id!;
await emoji.save();
console.log(`[Instance ban] Reassigned emoji ${emoji.id} ownership to user ${guild.owner_id}`);
}),
);
}
}),
);
const members = await Member.find({ where: { id: req.params.user_id as string } });
await Promise.all([...members.map((member) => Member.removeFromGuild(member.id, member.guild_id))]);
await UserSettingsProtos.delete({ user_id: req.params.user_id as string });
await User.delete({ id: req.params.user_id as string });
// TODO: respect intents as USER_DELETE has potential to cause privacy issues
await emitEvent({
event: "USER_DELETE",
user_id: req.user_id,
data: { user_id: req.params.user_id as string },
} satisfies UserDeleteEvent);
console.log(`[Instance ban] Deleted user ${user.id} from instance in ${sw.elapsed().toString()}`);
res.sendStatus(204);
},
);
export default router;
|