mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-24 19:32:46 +01:00
Fix icon, owner_id change and channel deletion for group DMs
This commit is contained in:
parent
6d0f9abe0d
commit
50ab5e7d49
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
import { Channel, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelService, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { route } from "@fosscord/api";
|
||||
import { handleFile, route } from "@fosscord/api";
|
||||
|
||||
const router: Router = Router();
|
||||
// TODO: delete channel
|
||||
@ -44,9 +44,10 @@ export interface ChannelModifySchema {
|
||||
/**
|
||||
* @maxLength 100
|
||||
*/
|
||||
name: string;
|
||||
type: ChannelType;
|
||||
name?: string;
|
||||
type?: ChannelType;
|
||||
topic?: string;
|
||||
icon?: string | null;
|
||||
bitrate?: number;
|
||||
user_limit?: number;
|
||||
rate_limit_per_user?: number;
|
||||
@ -67,6 +68,7 @@ export interface ChannelModifySchema {
|
||||
router.patch("/", route({ body: "ChannelModifySchema", permission: "MANAGE_CHANNELS" }), async (req: Request, res: Response) => {
|
||||
var payload = req.body as ChannelModifySchema;
|
||||
const { channel_id } = req.params;
|
||||
if (payload.icon) payload.icon = await handleFile(`/channel-icons/${channel_id}`, payload.icon);
|
||||
|
||||
const channel = await Channel.findOneOrFail({ id: channel_id });
|
||||
channel.assign(payload);
|
||||
|
@ -58,6 +58,9 @@ export class CDNServer extends Server {
|
||||
this.app.use("/team-icons/", avatarsRoute);
|
||||
this.log("verbose", "[Server] Route /team-icons registered");
|
||||
|
||||
this.app.use("/channel-icons/", avatarsRoute);
|
||||
this.log("verbose", "[Server] Route /channel-icons registered");
|
||||
|
||||
return super.start();
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,12 +72,38 @@ 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: channel_dto !== null ? channel_dto : await DmChannelDTO.from(channel, [user_id]),
|
||||
user_id: user_id
|
||||
});
|
||||
|
||||
await emitEvent({
|
||||
event: "CHANNEL_RECIPIENT_REMOVE", data: {
|
||||
@ -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 })
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user