1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-13 06:02:39 +01:00

use insert instead of save

This commit is contained in:
Flam3rboy 2021-08-31 17:56:33 +02:00
parent f59fae674d
commit 42a50c0674
2 changed files with 25 additions and 25 deletions

View File

@ -181,10 +181,11 @@ router.post(
// appearently discord doesn't save the date of birth and just calculate if nsfw is allowed // 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 // if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
const user = await new User({ const user = {
created_at: new Date(), created_at: new Date(),
username: adjusted_username, username: adjusted_username,
discriminator, discriminator,
id: Snowflake.generate(),
bot: false, bot: false,
system: false, system: false,
desktop: false, desktop: false,
@ -204,8 +205,10 @@ router.post(
hash: adjusted_password, hash: adjusted_password,
valid_tokens_since: new Date() valid_tokens_since: new Date()
}, },
settings: defaultSettings settings: defaultSettings,
}).save(); fingerprints: []
};
await User.insert(user);
return res.json({ token: await generateToken(user.id) }); return res.json({ token: await generateToken(user.id) });
} }

View File

@ -1,9 +1,8 @@
import { Router, Request, Response } from "express"; import { Router, Request, Response } from "express";
import { Role, Guild, Snowflake, Config, User, Member } from "@fosscord/util"; import { Role, Guild, Snowflake, Config, User, Member, Channel } from "@fosscord/util";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { check } from "./../../util/instanceOf"; import { check } from "./../../util/instanceOf";
import { GuildCreateSchema } from "../../schema/Guild"; import { GuildCreateSchema } from "../../schema/Guild";
import { createChannel } from "../../util/Channel";
const router: Router = Router(); const router: Router = Router();
@ -13,14 +12,15 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
const body = req.body as GuildCreateSchema; const body = req.body as GuildCreateSchema;
const { maxGuilds } = Config.get().limits.user; const { maxGuilds } = Config.get().limits.user;
const guild_count = await Member.count({ where: { id: req.user_id } }); const guild_count = await Member.count({ id: req.user_id });
if (guild_count >= maxGuilds) { if (guild_count >= maxGuilds) {
throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403);
} }
const guild_id = Snowflake.generate(); const guild_id = Snowflake.generate();
const guild = new Guild(
{ const [guild, role] = await Promise.all([
Guild.insert({
name: body.name, name: body.name,
region: Config.get().regions.default, region: Config.get().regions.default,
owner_id: req.user_id, owner_id: req.user_id,
@ -38,7 +38,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
preferred_locale: "en-US", preferred_locale: "en-US",
premium_subscription_count: 0, premium_subscription_count: 0,
premium_tier: 0, premium_tier: 0,
system_channel_flags: "0", system_channel_flags: 0,
unavailable: false, unavailable: false,
verification_level: 0, verification_level: 0,
welcome_screen: { welcome_screen: {
@ -47,11 +47,9 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
welcome_channels: [] welcome_channels: []
}, },
widget_enabled: false widget_enabled: false
}, }),
{ id: guild_id } Role.insert({
); id: guild_id,
const role = new Role(
{
guild_id: guild_id, guild_id: guild_id,
color: 0, color: 0,
hoist: false, hoist: false,
@ -59,15 +57,9 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
mentionable: false, mentionable: false,
name: "@everyone", name: "@everyone",
permissions: String("2251804225"), permissions: String("2251804225"),
position: 0, position: 0
tags: null })
}, ]);
{
id: guild_id
}
);
await Promise.all([guild.save(), role.save()]);
if (!body.channels || !body.channels.length) body.channels = [{ id: "01", type: 0, name: "general" }]; if (!body.channels || !body.channels.length) body.channels = [{ id: "01", type: 0, name: "general" }];
@ -86,13 +78,18 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
// TODO: should we abort if parent_id is a category? (to disallow sub category channels) // TODO: should we abort if parent_id is a category? (to disallow sub category channels)
var parent_id = ids.get(x.parent_id); var parent_id = ids.get(x.parent_id);
return createChannel({ ...x, guild_id, id, parent_id }, req.user_id, { keepId: true, skipExistsCheck: true }); return Channel.createChannel({ ...x, guild_id, id, parent_id }, req.user_id, {
keepId: true,
skipExistsCheck: true,
skipPermissionCheck: true,
skipEventEmit: true
});
}) })
); );
await Member.addToGuild(req.user_id, guild_id); await Member.addToGuild(req.user_id, guild_id);
res.status(201).json({ id: guild.id }); res.status(201).json({ id: guild_id });
}); });
export default router; export default router;