diff --git a/src/equicordplugins/lastActive/index.tsx b/src/equicordplugins/lastActive/index.tsx index 9ca7a035c..1e5fcc805 100644 --- a/src/equicordplugins/lastActive/index.tsx +++ b/src/equicordplugins/lastActive/index.tsx @@ -7,25 +7,30 @@ import { NavContextMenuPatchCallback } from "@api/ContextMenu"; import { EquicordDevs } from "@utils/constants"; import definePlugin from "@utils/types"; +import { Channel } from "@vencord/discord-types"; import { Menu, NavigationRouter, RestAPI, Toasts, UserStore } from "@webpack/common"; async function findLastMessageFromUser(guildId: string, channelId: string, userId: string) { try { + const isDM = guildId === "@me"; const res = await RestAPI.get({ - url: `/guilds/${guildId}/messages/search?author_id=${userId}&channel_id=${channelId}&sort_by=timestamp&sort_order=desc&offset=0` + url: isDM + ? `/channels/${channelId}/messages/search` + : `/guilds/${guildId}/messages/search`, + query: { + author_id: userId, + channel_id: channelId, + sort_by: "timestamp", + sort_order: "desc", + offset: 0 + } }); - const allMessages = res.body.messages?.flat() || []; - const newestMessage = allMessages.find(msg => msg && msg.id); + const messageId = res.body.messages + ?.flat() + .find(msg => msg?.author?.id === userId)?.id; - if (newestMessage) return newestMessage.id; - - Toasts.show({ - type: Toasts.Type.FAILURE, - message: "Couldn't find any recent messages from this user.", - id: Toasts.genId() - }); - return null; + if (messageId) return messageId; } catch (error) { console.error("Error finding last message:", error); Toasts.show({ @@ -35,32 +40,32 @@ async function findLastMessageFromUser(guildId: string, channelId: string, userI }); return null; } + + Toasts.show({ + type: Toasts.Type.FAILURE, + message: "Couldn't find any recent messages from this user.", + id: Toasts.genId() + }); + return null; } -async function jumpToLastActive(channel: any, targetUserId?: string) { - try { - if (!channel) { - Toasts.show({ - type: Toasts.Type.FAILURE, - message: "Channel information not available.", - id: Toasts.genId() - }); - return; - } - const guildId = channel.guild_id !== null ? channel.guild_id : "@me"; - const channelId = channel.id; - let userId: string; - if (targetUserId) { +async function jumpToLastActive(channel: Channel, targetUserId?: string) { + if (!channel) { + Toasts.show({ + type: Toasts.Type.FAILURE, + message: "Channel information not available.", + id: Toasts.genId() + }); + return; + } - userId = targetUserId; - } else { - const currentUser = UserStore.getCurrentUser(); - userId = currentUser.id; - } - const messageId = await findLastMessageFromUser(guildId, channelId, userId); + try { + const guildId = channel.guild_id ?? "@me"; + const userId = targetUserId ?? UserStore.getCurrentUser().id; + + const messageId = await findLastMessageFromUser(guildId, channel.id, userId); if (messageId) { - const url = `/channels/${guildId}/${channelId}/${messageId}`; - NavigationRouter.transitionTo(url); + NavigationRouter.transitionTo(`/channels/${guildId}/${channel.id}/${messageId}`); } } catch (error) { console.error("Error in jumpToLastActive:", error); @@ -71,6 +76,7 @@ async function jumpToLastActive(channel: any, targetUserId?: string) { }); } } + const ChannelContextMenuPatch: NavContextMenuPatchCallback = (children, { channel }) => { children.push(