✨ Message edit
This commit is contained in:
		
							parent
							
								
									b5f8a2e4e1
								
							
						
					
					
						commit
						71c0050a54
					
				| @ -1,11 +1,47 @@ | ||||
| import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel } from "@fosscord/server-util"; | ||||
| import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/server-util"; | ||||
| import { Router } from "express"; | ||||
| import { HTTPError } from "lambert-server"; | ||||
| import { MessageCreateSchema } from "../../../../../schema/Message"; | ||||
| import { emitEvent } from "../../../../../util/Event"; | ||||
| import { check } from "../../../../../util/instanceOf"; | ||||
| import { handleMessage } from "../../../../../util/Message"; | ||||
| 
 | ||||
| const router = Router(); | ||||
| // TODO:
 | ||||
| 
 | ||||
| router.patch("/", check(MessageCreateSchema), async (req, res) => { | ||||
| 	const { message_id, channel_id } = req.params; | ||||
| 	var body = req.body as MessageCreateSchema; | ||||
| 
 | ||||
| 	var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true }).exec(); | ||||
| 	if (!message) throw new HTTPError("Message not found", 404); | ||||
| 
 | ||||
| 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||
| 
 | ||||
| 	if (req.user_id !== message.author_id) { | ||||
| 		permissions.hasThrow("MANAGE_MESSAGES"); | ||||
| 		body = { flags: body.flags }; | ||||
| 	} | ||||
| 
 | ||||
| 	const opts = await handleMessage({ | ||||
| 		...body, | ||||
| 		author_id: message.author_id, | ||||
| 		channel_id, | ||||
| 		id: message_id, | ||||
| 		edited_timestamp: new Date() | ||||
| 	}); | ||||
| 
 | ||||
| 	message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec(); | ||||
| 	if (!message) throw new HTTPError("Message not found", 404); | ||||
| 
 | ||||
| 	await emitEvent({ | ||||
| 		event: "MESSAGE_UPDATE", | ||||
| 		channel_id, | ||||
| 		guild_id: message.guild_id, | ||||
| 		data: { ...toObject(message), nonce: undefined } | ||||
| 	} as MessageUpdateEvent); | ||||
| 
 | ||||
| 	return res.json(toObject(message)); | ||||
| }); | ||||
| 
 | ||||
| router.delete("/", async (req, res) => { | ||||
| 	const { message_id, channel_id } = req.params; | ||||
|  | ||||
| @ -13,7 +13,7 @@ import { | ||||
| 	toObject, | ||||
| 	UserModel | ||||
| } from "@fosscord/server-util"; | ||||
| import { Request, Response, Router } from "express"; | ||||
| import { Router } from "express"; | ||||
| import { HTTPError } from "lambert-server"; | ||||
| import { emitEvent } from "../../../../../util/Event"; | ||||
| 
 | ||||
|  | ||||
| @ -5,6 +5,7 @@ export const MessageCreateSchema = { | ||||
| 	$content: new Length(String, 0, 2000), | ||||
| 	$nonce: String, | ||||
| 	$tts: Boolean, | ||||
| 	$flags: BigInt, | ||||
| 	$embed: { | ||||
| 		$title: new Length(String, 0, 256), //title of embed
 | ||||
| 		$type: String, // type of embed (always "rich" for webhook embeds)
 | ||||
| @ -15,48 +16,49 @@ export const MessageCreateSchema = { | ||||
| 		$footer: { | ||||
| 			text: new Length(String, 0, 2048), | ||||
| 			icon_url: String, | ||||
| 			proxy_icon_url: String, | ||||
| 			proxy_icon_url: String | ||||
| 		}, // footer object	footer information
 | ||||
| 		$image: EmbedImage, // image object	image information
 | ||||
| 		$thumbnail: EmbedImage, // thumbnail object	thumbnail information
 | ||||
| 		$video: EmbedImage, // video object	video information
 | ||||
| 		$provider: { | ||||
| 			name: String, | ||||
| 			url: String, | ||||
| 			url: String | ||||
| 		}, // provider object	provider information
 | ||||
| 		$author: { | ||||
| 			name: new Length(String, 0, 256), | ||||
| 			url: String, | ||||
| 			icon_url: String, | ||||
| 			proxy_icon_url: String, | ||||
| 			proxy_icon_url: String | ||||
| 		}, // author object	author information
 | ||||
| 		$fields: new Length( | ||||
| 			[ | ||||
| 				{ | ||||
| 					name: new Length(String, 0, 256), | ||||
| 					value: new Length(String, 0, 1024), | ||||
| 					$inline: Boolean, | ||||
| 				}, | ||||
| 					$inline: Boolean | ||||
| 				} | ||||
| 			], | ||||
| 			0, | ||||
| 			25 | ||||
| 		), | ||||
| 		) | ||||
| 	}, | ||||
| 	$allowed_mentions: [], | ||||
| 	$message_reference: { | ||||
| 		message_id: String, | ||||
| 		channel_id: String, | ||||
| 		$guild_id: String, | ||||
| 		$fail_if_not_exists: Boolean, | ||||
| 		$fail_if_not_exists: Boolean | ||||
| 	}, | ||||
| 	$payload_json: String, | ||||
| 	$file: Object, | ||||
| 	$file: Object | ||||
| }; | ||||
| 
 | ||||
| export interface MessageCreateSchema { | ||||
| 	content?: string; | ||||
| 	nonce?: string; | ||||
| 	tts?: boolean; | ||||
| 	flags?: bigint; | ||||
| 	embed?: Embed & { timestamp?: string }; | ||||
| 	allowed_mentions?: []; | ||||
| 	message_reference?: { | ||||
|  | ||||
| @ -9,7 +9,7 @@ import { HTTPError } from "lambert-server"; | ||||
| import { emitEvent } from "./Event"; | ||||
| // TODO: check webhook, application, system author
 | ||||
| 
 | ||||
| export async function sendMessage(opts: Partial<Message>) { | ||||
| export async function handleMessage(opts: Partial<Message>) { | ||||
| 	const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); | ||||
| 	if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); | ||||
| 	// TODO: are tts messages allowed in dm channels? should permission be checked?
 | ||||
| @ -28,10 +28,8 @@ export async function sendMessage(opts: Partial<Message>) { | ||||
| 	} | ||||
| 
 | ||||
| 	// TODO: check and put it all in the body
 | ||||
| 	const message: Message = { | ||||
| 	return { | ||||
| 		...opts, | ||||
| 		id: Snowflake.generate(), | ||||
| 		timestamp: new Date(), | ||||
| 		guild_id: channel.guild_id, | ||||
| 		channel_id: opts.channel_id, | ||||
| 		// TODO: generate mentions and check permissions
 | ||||
| @ -43,10 +41,14 @@ export async function sendMessage(opts: Partial<Message>) { | ||||
| 		reactions: opts.reactions || [], | ||||
| 		type: opts.type ?? 0 | ||||
| 	}; | ||||
| } | ||||
| 
 | ||||
| export async function sendMessage(opts: Partial<Message>) { | ||||
| 	const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); | ||||
| 
 | ||||
| 	const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); | ||||
| 
 | ||||
| 	await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); | ||||
| 	await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent); | ||||
| 
 | ||||
| 	return data; | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user