Fix icon, owner_id change and channel deletion for group DMs

This commit is contained in:
AlTech98
2021-09-17 18:29:02 +02:00
parent 6d0f9abe0d
commit 50ab5e7d49
5 changed files with 631 additions and 606 deletions
+2 -2
View File
@@ -30,8 +30,8 @@ export class Channel extends BaseClass {
@Column({ nullable: true })
name?: string;
@Column({ nullable: true })
icon?: string;
@Column({ type: 'text', nullable: true })
icon?: string | null;
@Column({ type: "simple-enum", enum: ChannelType })
type: ChannelType;
+34 -2
View File
@@ -1,4 +1,4 @@
import { Channel, ChannelType, PublicUserProjection, Recipient, User } from "../entities";
import { Channel, ChannelType, Message, PublicUserProjection, Recipient, User } from "../entities";
import { HTTPError } from "lambert-server";
import { emitEvent, trimSpecial } from "../util";
import { DmChannelDTO } from "../dtos";
@@ -72,10 +72,36 @@ export class ChannelService {
public 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)
if (channel.recipients?.length === 0) {
await ChannelService.deleteChannel(channel);
await emitEvent({
event: "CHANNEL_DELETE",
data: await DmChannelDTO.from(channel, [user_id]),
user_id: user_id
});
return
}
let channel_dto = null;
//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_dto = await DmChannelDTO.from(channel, [user_id])
await emitEvent({
event: "CHANNEL_UPDATE",
data: channel_dto,
channel_id: channel.id
});
}
await channel.save()
await emitEvent({
event: "CHANNEL_DELETE",
data: await DmChannelDTO.from(channel, [user_id]),
data: channel_dto !== null ? channel_dto : await DmChannelDTO.from(channel, [user_id]),
user_id: user_id
});
@@ -86,4 +112,10 @@ export class ChannelService {
}, channel_id: channel.id
} as ChannelRecipientRemoveEvent);
}
public 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
//TODO before deleting the channel we should check and delete other relations
await Channel.delete({ id: channel.id })
}
}