summary refs log tree commit diff
path: root/util/src/entities/Channel.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-19 18:47:38 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-19 18:47:38 +0200
commitb5fb88ea8e63f66d6b6ba12519941ae92f0e776d (patch)
tree0159fbc2ba784e6fb6e3aac0468620a61352a72b /util/src/entities/Channel.ts
parent:art: remove deleteMessageAttachments and move to entity (diff)
downloadserver-b5fb88ea8e63f66d6b6ba12519941ae92f0e776d.tar.xz
:art: add orphanedRowAction and cascade onDelete to entities
Diffstat (limited to 'util/src/entities/Channel.ts')
-rw-r--r--util/src/entities/Channel.ts71
1 files changed, 63 insertions, 8 deletions
diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts
index fc954f63..0196fb3e 100644
--- a/util/src/entities/Channel.ts
+++ b/util/src/entities/Channel.ts
@@ -1,12 +1,26 @@
-import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm";
+import {
+	Column,
+	Entity,
+	FindConditions,
+	JoinColumn,
+	ManyToOne,
+	ObjectID,
+	OneToMany,
+	RelationId,
+	RemoveOptions,
+} from "typeorm";
 import { BaseClass } from "./BaseClass";
 import { Guild } from "./Guild";
-import { Message } from "./Message";
 import { User } from "./User";
 import { HTTPError } from "lambert-server";
 import { emitEvent, getPermission, Snowflake } from "../util";
 import { ChannelCreateEvent } from "../interfaces";
 import { Recipient } from "./Recipient";
+import { Message } from "./Message";
+import { ReadState } from "./ReadState";
+import { Invite } from "./Invite";
+import { VoiceState } from "./VoiceState";
+import { Webhook } from "./Webhook";
 
 export enum ChannelType {
 	GUILD_TEXT = 0, // a text channel within a server
@@ -31,20 +45,22 @@ export class Channel extends BaseClass {
 	@Column({ nullable: true })
 	name?: string;
 
+	@Column({ type: "text", nullable: true })
+	icon?: string | null;
+
 	@Column({ type: "simple-enum", enum: ChannelType })
 	type: ChannelType;
 
-	@OneToMany(() => Recipient, (recipient: Recipient) => recipient.channel, { cascade: true })
+	@OneToMany(() => Recipient, (recipient: Recipient) => recipient.channel, {
+		cascade: true,
+		orphanedRowAction: "delete",
+		onDelete: "CASCADE",
+	})
 	recipients?: Recipient[];
 
 	@Column({ nullable: true })
-	@RelationId((channel: Channel) => channel.last_message)
 	last_message_id: string;
 
-	@JoinColumn({ name: "last_message_id" })
-	@ManyToOne(() => Message)
-	last_message?: Message;
-
 	@Column({ nullable: true })
 	@RelationId((channel: Channel) => channel.guild)
 	guild_id?: string;
@@ -100,6 +116,41 @@ export class Channel extends BaseClass {
 	@Column({ nullable: true })
 	topic?: string;
 
+	@OneToMany(() => Invite, (invite: Invite) => invite.channel, {
+		cascade: true,
+		orphanedRowAction: "delete",
+		onDelete: "CASCADE",
+	})
+	invites?: Invite[];
+
+	@OneToMany(() => Message, (message: Message) => message.channel, {
+		cascade: true,
+		orphanedRowAction: "delete",
+		onDelete: "CASCADE",
+	})
+	messages?: Message[];
+
+	@OneToMany(() => VoiceState, (voice_state: VoiceState) => voice_state.channel, {
+		cascade: true,
+		orphanedRowAction: "delete",
+		onDelete: "CASCADE",
+	})
+	voice_states?: VoiceState[];
+
+	@OneToMany(() => ReadState, (read_state: ReadState) => read_state.channel, {
+		cascade: true,
+		orphanedRowAction: "delete",
+		onDelete: "CASCADE",
+	})
+	read_states?: ReadState[];
+
+	@OneToMany(() => Webhook, (webhook: Webhook) => webhook.channel, {
+		cascade: true,
+		orphanedRowAction: "delete",
+		onDelete: "CASCADE",
+	})
+	webhooks?: Webhook[];
+
 	// TODO: DM channel
 	static async createChannel(
 		channel: Partial<Channel>,
@@ -162,6 +213,10 @@ export class Channel extends BaseClass {
 
 		return channel;
 	}
+
+	isDm() {
+		return this.type === ChannelType.DM || this.type === ChannelType.GROUP_DM;
+	}
 }
 
 export interface ChannelPermissionOverwrite {