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-20 21:27:08 +0200
committerGitHub <noreply@github.com>2021-09-20 21:27:08 +0200
commit8af0094d8e8c46176cfbfac62e0b112f00c8e4be (patch)
tree0642670af9ad961d428d114ae9f24fa6a32626e6 /util/src/entities/Channel.ts
parentMerge pull request #386 from ChrisChrome/master (diff)
parent:bug: fix .delete -> add onDelete: "CASCADE" (diff)
downloadserver-8af0094d8e8c46176cfbfac62e0b112f00c8e4be.tar.xz
Merge pull request #382 from fosscord/auto-delete-relations
Auto delete relations
Diffstat (limited to 'util/src/entities/Channel.ts')
-rw-r--r--util/src/entities/Channel.ts116
1 files changed, 81 insertions, 35 deletions
diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts

index ea632778..ece82bd0 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts
@@ -6,8 +6,12 @@ import { HTTPError } from "lambert-server"; import { containsAll, emitEvent, getPermission, Snowflake, trimSpecial } from "../util"; import { ChannelCreateEvent, ChannelRecipientRemoveEvent } from "../interfaces"; import { Recipient } from "./Recipient"; -import { DmChannelDTO } from "../dtos"; import { Message } from "./Message"; +import { ReadState } from "./ReadState"; +import { Invite } from "./Invite"; +import { VoiceState } from "./VoiceState"; +import { Webhook } from "./Webhook"; +import { DmChannelDTO } from "../dtos"; export enum ChannelType { GUILD_TEXT = 0, // a text channel within a server @@ -32,13 +36,16 @@ export class Channel extends BaseClass { @Column({ nullable: true }) name?: string; - @Column({ type: 'text', nullable: true }) + @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", + }) recipients?: Recipient[]; @Column({ nullable: true }) @@ -49,7 +56,9 @@ export class Channel extends BaseClass { guild_id?: string; @JoinColumn({ name: "guild_id" }) - @ManyToOne(() => Guild) + @ManyToOne(() => Guild, { + onDelete: "CASCADE", + }) guild: Guild; @Column({ nullable: true }) @@ -99,6 +108,37 @@ export class Channel extends BaseClass { @Column({ nullable: true }) topic?: string; + @OneToMany(() => Invite, (invite: Invite) => invite.channel, { + cascade: true, + orphanedRowAction: "delete", + }) + invites?: Invite[]; + + @OneToMany(() => Message, (message: Message) => message.channel, { + cascade: true, + orphanedRowAction: "delete", + }) + messages?: Message[]; + + @OneToMany(() => VoiceState, (voice_state: VoiceState) => voice_state.channel, { + cascade: true, + orphanedRowAction: "delete", + }) + voice_states?: VoiceState[]; + + @OneToMany(() => ReadState, (read_state: ReadState) => read_state.channel, { + cascade: true, + orphanedRowAction: "delete", + }) + read_states?: ReadState[]; + + @OneToMany(() => Webhook, (webhook: Webhook) => webhook.channel, { + cascade: true, + orphanedRowAction: "delete", + }) + webhooks?: Webhook[]; + + // TODO: DM channel static async createChannel( channel: Partial<Channel>, user_id: string = "0", @@ -151,10 +191,10 @@ export class Channel extends BaseClass { new Channel(channel).save(), !opts?.skipEventEmit ? emitEvent({ - event: "CHANNEL_CREATE", - data: channel, - guild_id: channel.guild_id, - } as ChannelCreateEvent) + event: "CHANNEL_CREATE", + data: channel, + guild_id: channel.guild_id, + } as ChannelCreateEvent) : Promise.resolve(), ]); @@ -174,17 +214,20 @@ export class Channel extends BaseClass { let channel = null; - const channelRecipients = [...recipients, creator_user_id] + const channelRecipients = [...recipients, creator_user_id]; - const userRecipients = await Recipient.find({ where: { user_id: creator_user_id }, relations: ["channel", "channel.recipients"] }) + const userRecipients = await Recipient.find({ + where: { user_id: creator_user_id }, + relations: ["channel", "channel.recipients"], + }); for (let ur of userRecipients) { - let re = ur.channel.recipients!.map(r => r.user_id) + let re = ur.channel.recipients!.map((r) => r.user_id); if (re.length === channelRecipients.length) { if (containsAll(re, channelRecipients)) { if (channel == null) { - channel = ur.channel - await ur.assign({ closed: false }).save() + channel = ur.channel; + await ur.assign({ closed: false }).save(); } } } @@ -196,80 +239,83 @@ export class Channel extends BaseClass { channel = await new Channel({ name, type, - owner_id: (type === ChannelType.DM ? undefined : creator_user_id), + owner_id: type === ChannelType.DM ? undefined : creator_user_id, created_at: new Date(), last_message_id: null, - recipients: channelRecipients.map((x) => new Recipient({ user_id: x, closed: !(type === ChannelType.GROUP_DM || x === creator_user_id) })), + recipients: channelRecipients.map( + (x) => + new Recipient({ user_id: x, closed: !(type === ChannelType.GROUP_DM || x === creator_user_id) }) + ), }).save(); } - - const channel_dto = await DmChannelDTO.from(channel) + const channel_dto = await DmChannelDTO.from(channel); if (type === ChannelType.GROUP_DM) { - for (let recipient of channel.recipients!) { await emitEvent({ event: "CHANNEL_CREATE", data: channel_dto.excludedRecipients([recipient.user_id]), - user_id: recipient.user_id - }) + user_id: recipient.user_id, + }); } } else { await emitEvent({ event: "CHANNEL_CREATE", data: channel_dto, user_id: creator_user_id }); } - return channel_dto.excludedRecipients([creator_user_id]) + return channel_dto.excludedRecipients([creator_user_id]); } static async removeRecipientFromChannel(channel: Channel, user_id: string) { - await Recipient.delete({ channel_id: channel.id, user_id: user_id }) - channel.recipients = channel.recipients?.filter(r => r.user_id !== user_id) + await Recipient.delete({ channel_id: channel.id, user_id: user_id }); + channel.recipients = channel.recipients?.filter((r) => r.user_id !== user_id); if (channel.recipients?.length === 0) { await Channel.deleteChannel(channel); await emitEvent({ event: "CHANNEL_DELETE", data: await DmChannelDTO.from(channel, [user_id]), - user_id: user_id + user_id: user_id, }); - return + return; } await emitEvent({ event: "CHANNEL_DELETE", data: await DmChannelDTO.from(channel, [user_id]), - user_id: user_id + user_id: user_id, }); //If the owner leave we make the first recipient in the list the new owner if (channel.owner_id === user_id) { - channel.owner_id = channel.recipients!.find(r => r.user_id !== user_id)!.user_id //Is there a criteria to choose the new owner? + channel.owner_id = channel.recipients!.find((r) => r.user_id !== user_id)!.user_id; //Is there a criteria to choose the new owner? await emitEvent({ event: "CHANNEL_UPDATE", data: await DmChannelDTO.from(channel, [user_id]), - channel_id: channel.id + channel_id: channel.id, }); } - await channel.save() + await channel.save(); await emitEvent({ - event: "CHANNEL_RECIPIENT_REMOVE", data: { + event: "CHANNEL_RECIPIENT_REMOVE", + data: { channel_id: channel.id, - user: await User.findOneOrFail({ where: { id: user_id }, select: PublicUserProjection }) - }, channel_id: channel.id + user: await User.findOneOrFail({ where: { id: user_id }, select: PublicUserProjection }), + }, + channel_id: channel.id, } as ChannelRecipientRemoveEvent); } static async deleteChannel(channel: Channel) { - await Message.delete({ channel_id: channel.id }) //TODO we should also delete the attachments from the cdn but to do that we need to move cdn.ts in util + await Message.delete({ channel_id: channel.id }); //TODO we should also delete the attachments from the cdn but to do that we need to move cdn.ts in util //TODO before deleting the channel we should check and delete other relations - await Channel.delete({ id: channel.id }) + await Channel.delete({ id: channel.id }); } isDm() { - return this.type === ChannelType.DM || this.type === ChannelType.GROUP_DM + return this.type === ChannelType.DM || this.type === ChannelType.GROUP_DM; } }