✨ 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 { Router } from "express"; | ||||||
| import { HTTPError } from "lambert-server"; | import { HTTPError } from "lambert-server"; | ||||||
|  | import { MessageCreateSchema } from "../../../../../schema/Message"; | ||||||
| import { emitEvent } from "../../../../../util/Event"; | import { emitEvent } from "../../../../../util/Event"; | ||||||
| import { check } from "../../../../../util/instanceOf"; | import { check } from "../../../../../util/instanceOf"; | ||||||
|  | import { handleMessage } from "../../../../../util/Message"; | ||||||
| 
 | 
 | ||||||
| const router = Router(); | 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) => { | router.delete("/", async (req, res) => { | ||||||
| 	const { message_id, channel_id } = req.params; | 	const { message_id, channel_id } = req.params; | ||||||
|  | |||||||
| @ -13,7 +13,7 @@ import { | |||||||
| 	toObject, | 	toObject, | ||||||
| 	UserModel | 	UserModel | ||||||
| } from "@fosscord/server-util"; | } from "@fosscord/server-util"; | ||||||
| import { Request, Response, Router } from "express"; | import { Router } from "express"; | ||||||
| import { HTTPError } from "lambert-server"; | import { HTTPError } from "lambert-server"; | ||||||
| import { emitEvent } from "../../../../../util/Event"; | import { emitEvent } from "../../../../../util/Event"; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ export const MessageCreateSchema = { | |||||||
| 	$content: new Length(String, 0, 2000), | 	$content: new Length(String, 0, 2000), | ||||||
| 	$nonce: String, | 	$nonce: String, | ||||||
| 	$tts: Boolean, | 	$tts: Boolean, | ||||||
|  | 	$flags: BigInt, | ||||||
| 	$embed: { | 	$embed: { | ||||||
| 		$title: new Length(String, 0, 256), //title of embed
 | 		$title: new Length(String, 0, 256), //title of embed
 | ||||||
| 		$type: String, // type of embed (always "rich" for webhook embeds)
 | 		$type: String, // type of embed (always "rich" for webhook embeds)
 | ||||||
| @ -15,48 +16,49 @@ export const MessageCreateSchema = { | |||||||
| 		$footer: { | 		$footer: { | ||||||
| 			text: new Length(String, 0, 2048), | 			text: new Length(String, 0, 2048), | ||||||
| 			icon_url: String, | 			icon_url: String, | ||||||
| 			proxy_icon_url: String, | 			proxy_icon_url: String | ||||||
| 		}, // footer object	footer information
 | 		}, // footer object	footer information
 | ||||||
| 		$image: EmbedImage, // image object	image information
 | 		$image: EmbedImage, // image object	image information
 | ||||||
| 		$thumbnail: EmbedImage, // thumbnail object	thumbnail information
 | 		$thumbnail: EmbedImage, // thumbnail object	thumbnail information
 | ||||||
| 		$video: EmbedImage, // video object	video information
 | 		$video: EmbedImage, // video object	video information
 | ||||||
| 		$provider: { | 		$provider: { | ||||||
| 			name: String, | 			name: String, | ||||||
| 			url: String, | 			url: String | ||||||
| 		}, // provider object	provider information
 | 		}, // provider object	provider information
 | ||||||
| 		$author: { | 		$author: { | ||||||
| 			name: new Length(String, 0, 256), | 			name: new Length(String, 0, 256), | ||||||
| 			url: String, | 			url: String, | ||||||
| 			icon_url: String, | 			icon_url: String, | ||||||
| 			proxy_icon_url: String, | 			proxy_icon_url: String | ||||||
| 		}, // author object	author information
 | 		}, // author object	author information
 | ||||||
| 		$fields: new Length( | 		$fields: new Length( | ||||||
| 			[ | 			[ | ||||||
| 				{ | 				{ | ||||||
| 					name: new Length(String, 0, 256), | 					name: new Length(String, 0, 256), | ||||||
| 					value: new Length(String, 0, 1024), | 					value: new Length(String, 0, 1024), | ||||||
| 					$inline: Boolean, | 					$inline: Boolean | ||||||
| 				}, | 				} | ||||||
| 			], | 			], | ||||||
| 			0, | 			0, | ||||||
| 			25 | 			25 | ||||||
| 		), | 		) | ||||||
| 	}, | 	}, | ||||||
| 	$allowed_mentions: [], | 	$allowed_mentions: [], | ||||||
| 	$message_reference: { | 	$message_reference: { | ||||||
| 		message_id: String, | 		message_id: String, | ||||||
| 		channel_id: String, | 		channel_id: String, | ||||||
| 		$guild_id: String, | 		$guild_id: String, | ||||||
| 		$fail_if_not_exists: Boolean, | 		$fail_if_not_exists: Boolean | ||||||
| 	}, | 	}, | ||||||
| 	$payload_json: String, | 	$payload_json: String, | ||||||
| 	$file: Object, | 	$file: Object | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export interface MessageCreateSchema { | export interface MessageCreateSchema { | ||||||
| 	content?: string; | 	content?: string; | ||||||
| 	nonce?: string; | 	nonce?: string; | ||||||
| 	tts?: boolean; | 	tts?: boolean; | ||||||
|  | 	flags?: bigint; | ||||||
| 	embed?: Embed & { timestamp?: string }; | 	embed?: Embed & { timestamp?: string }; | ||||||
| 	allowed_mentions?: []; | 	allowed_mentions?: []; | ||||||
| 	message_reference?: { | 	message_reference?: { | ||||||
|  | |||||||
| @ -9,7 +9,7 @@ import { HTTPError } from "lambert-server"; | |||||||
| import { emitEvent } from "./Event"; | import { emitEvent } from "./Event"; | ||||||
| // TODO: check webhook, application, system author
 | // 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(); | 	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); | 	if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); | ||||||
| 	// TODO: are tts messages allowed in dm channels? should permission be checked?
 | 	// 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
 | 	// TODO: check and put it all in the body
 | ||||||
| 	const message: Message = { | 	return { | ||||||
| 		...opts, | 		...opts, | ||||||
| 		id: Snowflake.generate(), |  | ||||||
| 		timestamp: new Date(), |  | ||||||
| 		guild_id: channel.guild_id, | 		guild_id: channel.guild_id, | ||||||
| 		channel_id: opts.channel_id, | 		channel_id: opts.channel_id, | ||||||
| 		// TODO: generate mentions and check permissions
 | 		// TODO: generate mentions and check permissions
 | ||||||
| @ -43,10 +41,14 @@ export async function sendMessage(opts: Partial<Message>) { | |||||||
| 		reactions: opts.reactions || [], | 		reactions: opts.reactions || [], | ||||||
| 		type: opts.type ?? 0 | 		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()); | 	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; | 	return data; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Flam3rboy
						Flam3rboy