From 835a5ab3a339bebd916d72487cddc4c344121e7b Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Thu, 29 Sep 2022 21:27:33 +1000 Subject: Fix fetching members for roles in POST message --- .../routes/channels/#channel_id/messages/index.ts | 27 +++++++++++++--------- src/api/util/handlers/Message.ts | 10 ++++---- 2 files changed, 21 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index 8598d1f8..747701db 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -255,17 +255,22 @@ router.post( ); } - const member = await Member.findOneOrFail({ - where: { id: req.user_id }, - relations: ["roles"], - }); - member.roles = member.roles - .filter((role: Role) => { - return role.id !== role.guild_id; - }) - .map((role: Role) => { - return role.id; - }) as any; + if (message.guild_id) { + // handleMessage will fetch the Member, but only if they are not guild owner. + // have to fetch ourselves otherwise. + if (!message.member) { + message.member = await Member.findOneOrFail({ + where: { id: req.user_id, guild_id: message.guild_id }, + relations: ["roles"] + }); + } + + //@ts-ignore + message.member.roles = + message.member.roles. + filter(x => x.id != x.guild_id) + .map(x => x.id); + } let read_state = await ReadState.findOne({ where: { user_id: req.user_id, channel_id } diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index 005d9d23..c0bdb6b0 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -64,7 +64,7 @@ export async function handleMessage(opts: MessageOptions): Promise { channel_id: opts.channel_id, attachments: opts.attachments || [], embeds: opts.embeds || [], - reactions: /*opts.reactions ||*/ [], + reactions: /*opts.reactions ||*/[], type: opts.type ?? 0, }); @@ -247,12 +247,12 @@ export async function postHandleMessage(message: Message) { const width = parseInt( $('meta[property="og:image:width"]').attr("content") || - "", + "", ) || undefined; const height = parseInt( $('meta[property="og:image:height"]').attr("content") || - "", + "", ) || undefined; const url = $('meta[property="og:url"]').attr("content"); @@ -317,7 +317,7 @@ export async function postHandleMessage(message: Message) { data.embeds.push(embed); } } - } catch (error) {} + } catch (error) { } } await Promise.all([ @@ -345,7 +345,7 @@ export async function sendMessage(opts: MessageOptions) { } as MessageCreateEvent), ]); - postHandleMessage(message).catch((e) => {}); // no await as it should catch error non-blockingly + postHandleMessage(message).catch((e) => { }); // no await as it should catch error non-blockingly return message; } -- cgit 1.4.1