update invites endpoints to support latest api

This commit is contained in:
Madeline 2023-08-12 00:43:00 +10:00
parent 3eb2338c3e
commit 23e234a87b
No known key found for this signature in database
GPG Key ID: 1958E017C36F2E47
5 changed files with 20 additions and 8 deletions

Binary file not shown.

Binary file not shown.

View File

@ -22,6 +22,7 @@ import {
Guild, Guild,
Invite, Invite,
InviteCreateEvent, InviteCreateEvent,
InviteCreateSchema,
PublicInviteRelation, PublicInviteRelation,
User, User,
emitEvent, emitEvent,
@ -50,6 +51,7 @@ router.post(
}), }),
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { user_id } = req; const { user_id } = req;
const body = req.body as InviteCreateSchema;
const { channel_id } = req.params; const { channel_id } = req.params;
const channel = await Channel.findOneOrFail({ const channel = await Channel.findOneOrFail({
where: { id: channel_id }, where: { id: channel_id },
@ -62,22 +64,27 @@ router.post(
} }
const { guild_id } = channel; const { guild_id } = channel;
const expires_at = new Date(req.body.max_age * 1000 + Date.now()); const expires_at =
body.max_age == 0 || body.max_age == undefined
? undefined
: new Date(body.max_age * 1000 + Date.now());
const invite = await Invite.create({ const invite = await Invite.create({
code: random(), code: random(),
temporary: req.body.temporary || true, temporary: body.temporary || true,
uses: 0, uses: 0,
max_uses: req.body.max_uses, max_uses: body.max_uses ? Math.max(0, body.max_uses) : 0,
max_age: req.body.max_age, max_age: body.max_age ? Math.max(0, body.max_age) : 0,
expires_at, expires_at,
created_at: new Date(), created_at: new Date(),
guild_id, guild_id,
channel_id: channel_id, channel_id: channel_id,
inviter_id: user_id, inviter_id: user_id,
flags: body.flags ?? 0,
}).save(); }).save();
const data = invite.toJSON(); const data = invite.toJSON();
data.inviter = await User.getPublicUser(req.user_id); data.inviter = (await User.getPublicUser(req.user_id)).toPublicUser();
data.guild = await Guild.findOne({ where: { id: guild_id } }); data.guild = await Guild.findOne({ where: { id: guild_id } });
data.channel = channel; data.channel = channel;
@ -86,6 +93,7 @@ router.post(
data, data,
guild_id, guild_id,
} as InviteCreateEvent); } as InviteCreateEvent);
res.status(201).send(data); res.status(201).send(data);
}, },
); );

View File

@ -17,10 +17,10 @@
*/ */
import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
import { Member } from "./Member";
import { BaseClassWithoutId, PrimaryIdColumn } from "./BaseClass"; import { BaseClassWithoutId, PrimaryIdColumn } from "./BaseClass";
import { Channel } from "./Channel"; import { Channel } from "./Channel";
import { Guild } from "./Guild"; import { Guild } from "./Guild";
import { Member } from "./Member";
import { User } from "./User"; import { User } from "./User";
export const PublicInviteRelation = ["inviter", "guild", "channel"]; export const PublicInviteRelation = ["inviter", "guild", "channel"];
@ -45,8 +45,8 @@ export class Invite extends BaseClassWithoutId {
@Column() @Column()
created_at: Date; created_at: Date;
@Column() @Column({ nullable: true })
expires_at: Date; expires_at?: Date;
@Column({ nullable: true }) @Column({ nullable: true })
@RelationId((invite: Invite) => invite.guild) @RelationId((invite: Invite) => invite.guild)
@ -94,6 +94,9 @@ export class Invite extends BaseClassWithoutId {
@Column({ nullable: true }) @Column({ nullable: true })
vanity_url?: boolean; vanity_url?: boolean;
@Column()
flags: number;
static async joinGuild(user_id: string, code: string) { static async joinGuild(user_id: string, code: string) {
const invite = await Invite.findOneOrFail({ where: { code } }); const invite = await Invite.findOneOrFail({ where: { code } });
if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0) if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0)

View File

@ -26,4 +26,5 @@ export interface InviteCreateSchema {
unique?: boolean; unique?: boolean;
target_user?: string; target_user?: string;
target_user_type?: number; target_user_type?: number;
flags?: number;
} }