summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-02 21:31:44 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-02 21:31:44 +0200
commit4c4bc248a5456e95b11102ee293fc5e7b37a83fa (patch)
tree984b7b8c6528d3a1a4bddeb81a2101e6258906c3
parentMerge branch 'master' of https://github.com/fosscord/fosscord-api (diff)
downloadserver-4c4bc248a5456e95b11102ee293fc5e7b37a83fa.tar.xz
:bug: fix entity missing an id (replace .insert with new Entity().save)
-rw-r--r--api/src/routes/auth/register.ts5
-rw-r--r--api/src/routes/guilds/#guild_id/delete.ts25
-rw-r--r--api/src/routes/guilds/#guild_id/roles.ts2
-rw-r--r--api/src/routes/guilds/index.ts8
-rw-r--r--util/src/entities/Channel.ts2
-rw-r--r--util/src/entities/Member.ts6
6 files changed, 25 insertions, 23 deletions
diff --git a/api/src/routes/auth/register.ts b/api/src/routes/auth/register.ts
index 8bcecda1..19c9c70f 100644
--- a/api/src/routes/auth/register.ts
+++ b/api/src/routes/auth/register.ts
@@ -181,7 +181,7 @@ router.post(
 		// appearently discord doesn't save the date of birth and just calculate if nsfw is allowed
 		// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
 
-		const user = {
+		const user = await new User({
 			created_at: new Date(),
 			username: adjusted_username,
 			discriminator,
@@ -207,8 +207,7 @@ router.post(
 			},
 			settings: defaultSettings,
 			fingerprints: []
-		};
-		await User.insert(user);
+		}).save();
 
 		return res.json({ token: await generateToken(user.id) });
 	}
diff --git a/api/src/routes/guilds/#guild_id/delete.ts b/api/src/routes/guilds/#guild_id/delete.ts
index 043260e9..bbbd1fa4 100644
--- a/api/src/routes/guilds/#guild_id/delete.ts
+++ b/api/src/routes/guilds/#guild_id/delete.ts
@@ -12,24 +12,27 @@ router.post("/", async (req: Request, res: Response) => {
 	const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["owner_id"] });
 	if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
 
-	await emitEvent({
-		event: "GUILD_DELETE",
-		data: {
-			id: guild_id
-		},
-		guild_id: guild_id
-	} as GuildDeleteEvent);
+	// do not put everything into promise all, because of "QueryFailedError: SQLITE_CONSTRAINT: FOREIGN KEY constraint failed"
+
+	await Message.delete({ guild_id }); // messages must be deleted before channel
 
 	await Promise.all([
-		Guild.delete({ id: guild_id }),
 		Role.delete({ guild_id }),
 		Channel.delete({ guild_id }),
 		Emoji.delete({ guild_id }),
-		Invite.delete({ guild_id }),
-		Message.delete({ guild_id }),
-		Member.delete({ guild_id })
+		Member.delete({ guild_id }),
+		emitEvent({
+			event: "GUILD_DELETE",
+			data: {
+				id: guild_id
+			},
+			guild_id: guild_id
+		} as GuildDeleteEvent)
 	]);
 
+	await Invite.delete({ guild_id }); // invite must be deleted after channel
+	await Guild.delete({ id: guild_id }); // guild must be deleted after everything else
+
 	return res.sendStatus(204);
 });
 
diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts
index c3dd92dc..20ab0779 100644
--- a/api/src/routes/guilds/#guild_id/roles.ts
+++ b/api/src/routes/guilds/#guild_id/roles.ts
@@ -53,7 +53,7 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) =>
 	};
 
 	await Promise.all([
-		Role.insert(role),
+		new Role(role).save(),
 		emitEvent({
 			event: "GUILD_ROLE_CREATE",
 			guild_id,
diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts
index 92525317..a87f926c 100644
--- a/api/src/routes/guilds/index.ts
+++ b/api/src/routes/guilds/index.ts
@@ -21,7 +21,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
 	const guild_id = Snowflake.generate();
 
 	const [guild, role] = await Promise.all([
-		Guild.insert({
+		new Guild({
 			name: body.name,
 			region: Config.get().regions.default,
 			owner_id: req.user_id,
@@ -48,8 +48,8 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
 				welcome_channels: []
 			},
 			widget_enabled: false
-		}),
-		Role.insert({
+		}).save(),
+		new Role({
 			id: guild_id,
 			guild_id: guild_id,
 			color: 0,
@@ -59,7 +59,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
 			name: "@everyone",
 			permissions: String("2251804225"),
 			position: 0
-		})
+		}).save()
 	]);
 
 	if (!body.channels || !body.channels.length) body.channels = [{ id: "01", type: 0, name: "general" }];
diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts
index e3586dfc..0be1c5ec 100644
--- a/util/src/entities/Channel.ts
+++ b/util/src/entities/Channel.ts
@@ -144,7 +144,7 @@ export class Channel extends BaseClass {
 		};
 
 		await Promise.all([
-			Channel.insert(channel),
+			new Channel(channel).save(),
 			!opts?.skipEventEmit
 				? emitEvent({
 						event: "CHANNEL_CREATE",
diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts
index e0dd411a..502f4813 100644
--- a/util/src/entities/Member.ts
+++ b/util/src/entities/Member.ts
@@ -205,7 +205,7 @@ export class Member extends BaseClass {
 		guild.joined_at = member.joined_at.toISOString();
 
 		await Promise.all([
-			Member.insert({
+			new Member({
 				...member,
 				roles: undefined,
 				// read_state: {},
@@ -218,7 +218,7 @@ export class Member extends BaseClass {
 					suppress_roles: false,
 					version: 0,
 				},
-			}),
+			}).save(),
 			Guild.increment({ id: guild_id }, "member_count", 1),
 			emitEvent({
 				event: "GUILD_MEMBER_ADD",
@@ -231,7 +231,7 @@ export class Member extends BaseClass {
 			} as GuildMemberAddEvent),
 			emitEvent({
 				event: "GUILD_CREATE",
-				data: { ...guild, members: [...guild.members, member] },
+				data: { ...guild, members: [...guild.members, { ...member, user }] },
 				user_id,
 			} as GuildCreateEvent),
 		]);