summary refs log tree commit diff
path: root/src/util/entities/Invite.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-25 18:24:21 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-25 23:35:18 +1000
commitf44f5d7ac2d24ff836c2e1d4b2fa58da04b13052 (patch)
treea6655c41bb3db79c30fd876b06ee60fe9cf70c9b /src/util/entities/Invite.ts
parentAllow edited_timestamp to passthrough in handleMessage (diff)
downloadserver-f44f5d7ac2d24ff836c2e1d4b2fa58da04b13052.tar.xz
Refactor to mono-repo + upgrade packages
Diffstat (limited to 'src/util/entities/Invite.ts')
-rw-r--r--src/util/entities/Invite.ts85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/util/entities/Invite.ts b/src/util/entities/Invite.ts
new file mode 100644
index 00000000..4f36f247
--- /dev/null
+++ b/src/util/entities/Invite.ts
@@ -0,0 +1,85 @@
+import { Column, Entity, JoinColumn, ManyToOne, RelationId, PrimaryColumn } from "typeorm";
+import { Member } from "./Member";
+import { BaseClassWithoutId } from "./BaseClass";
+import { Channel } from "./Channel";
+import { Guild } from "./Guild";
+import { User } from "./User";
+
+export const PublicInviteRelation = ["inviter", "guild", "channel"];
+
+@Entity("invites")
+export class Invite extends BaseClassWithoutId {
+	@PrimaryColumn()
+	code: string;
+
+	@Column()
+	temporary: boolean;
+
+	@Column()
+	uses: number;
+
+	@Column()
+	max_uses: number;
+
+	@Column()
+	max_age: number;
+
+	@Column()
+	created_at: Date;
+
+	@Column()
+	expires_at: Date;
+
+	@Column({ nullable: true })
+	@RelationId((invite: Invite) => invite.guild)
+	guild_id: string;
+
+	@JoinColumn({ name: "guild_id" })
+	@ManyToOne(() => Guild, {
+		onDelete: "CASCADE",
+	})
+	guild: Guild;
+
+	@Column({ nullable: true })
+	@RelationId((invite: Invite) => invite.channel)
+	channel_id: string;
+
+	@JoinColumn({ name: "channel_id" })
+	@ManyToOne(() => Channel, {
+		onDelete: "CASCADE",
+	})
+	channel: Channel;
+
+	@Column({ nullable: true })
+	@RelationId((invite: Invite) => invite.inviter)
+	inviter_id?: string;
+
+	@JoinColumn({ name: "inviter_id" })
+	@ManyToOne(() => User)
+	inviter: User;
+
+	@Column({ nullable: true })
+	@RelationId((invite: Invite) => invite.target_user)
+	target_user_id: string;
+
+	@JoinColumn({ name: "target_user_id" })
+	@ManyToOne(() => User, {
+		onDelete: "CASCADE",
+	})
+	target_user?: string; // could be used for "User specific invites" https://github.com/fosscord/fosscord/issues/62
+
+	@Column({ nullable: true })
+	target_user_type?: number;
+
+	@Column({ nullable: true })
+	vanity_url?: boolean;
+
+	static async joinGuild(user_id: string, code: string) {
+		const invite = await Invite.findOneOrFail({ where: { code } });
+		if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0) await Invite.delete({ code });
+		else await invite.save();
+
+		await Member.addToGuild(user_id, invite.guild_id);
+		return invite;
+	}
+}