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 { Channel, ChannelDeleteEvent, ChannelPermissionOverwriteType, ChannelService, ChannelType, ChannelUpdateEvent, emitEvent, Recipient } from "@fosscord/util"; | ||||||
| import { Request, Response, Router } from "express"; | import { Request, Response, Router } from "express"; | ||||||
| import { route } from "@fosscord/api"; | import { handleFile, route } from "@fosscord/api"; | ||||||
| 
 | 
 | ||||||
| const router: Router = Router(); | const router: Router = Router(); | ||||||
| // TODO: delete channel
 | // TODO: delete channel
 | ||||||
| @ -44,9 +44,10 @@ export interface ChannelModifySchema { | |||||||
| 	/** | 	/** | ||||||
| 	 * @maxLength 100 | 	 * @maxLength 100 | ||||||
| 	 */ | 	 */ | ||||||
| 	name: string; | 	name?: string; | ||||||
| 	type: ChannelType; | 	type?: ChannelType; | ||||||
| 	topic?: string; | 	topic?: string; | ||||||
|  | 	icon?: string | null; | ||||||
| 	bitrate?: number; | 	bitrate?: number; | ||||||
| 	user_limit?: number; | 	user_limit?: number; | ||||||
| 	rate_limit_per_user?: 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) => { | router.patch("/", route({ body: "ChannelModifySchema", permission: "MANAGE_CHANNELS" }), async (req: Request, res: Response) => { | ||||||
| 	var payload = req.body as ChannelModifySchema; | 	var payload = req.body as ChannelModifySchema; | ||||||
| 	const { channel_id } = req.params; | 	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 }); | 	const channel = await Channel.findOneOrFail({ id: channel_id }); | ||||||
| 	channel.assign(payload); | 	channel.assign(payload); | ||||||
|  | |||||||
| @ -58,6 +58,9 @@ export class CDNServer extends Server { | |||||||
| 		this.app.use("/team-icons/", avatarsRoute); | 		this.app.use("/team-icons/", avatarsRoute); | ||||||
| 		this.log("verbose", "[Server] Route /team-icons registered"); | 		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(); | 		return super.start(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -30,8 +30,8 @@ export class Channel extends BaseClass { | |||||||
| 	@Column({ nullable: true }) | 	@Column({ nullable: true }) | ||||||
| 	name?: string; | 	name?: string; | ||||||
| 
 | 
 | ||||||
| 	@Column({ nullable: true }) | 	@Column({ type: 'text', nullable: true }) | ||||||
| 	icon?: string; | 	icon?: string | null; | ||||||
| 
 | 
 | ||||||
| 	@Column({ type: "simple-enum", enum: ChannelType }) | 	@Column({ type: "simple-enum", enum: ChannelType }) | ||||||
| 	type: 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 { HTTPError } from "lambert-server"; | ||||||
| import { emitEvent, trimSpecial } from "../util"; | import { emitEvent, trimSpecial } from "../util"; | ||||||
| import { DmChannelDTO } from "../dtos"; | import { DmChannelDTO } from "../dtos"; | ||||||
| @ -72,10 +72,36 @@ export class ChannelService { | |||||||
| 
 | 
 | ||||||
| 	public static async removeRecipientFromChannel(channel: Channel, user_id: string) { | 	public static async removeRecipientFromChannel(channel: Channel, user_id: string) { | ||||||
| 		await Recipient.delete({ channel_id: channel.id, 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 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({ | 		await emitEvent({ | ||||||
| 			event: "CHANNEL_DELETE", | 			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 | 			user_id: user_id | ||||||
| 		}); | 		}); | ||||||
| 
 | 
 | ||||||
| @ -86,4 +112,10 @@ export class ChannelService { | |||||||
| 			}, channel_id: channel.id | 			}, channel_id: channel.id | ||||||
| 		} as ChannelRecipientRemoveEvent); | 		} 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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 AlTech98
						AlTech98