diff options
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/discord.tsx | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/utils/discord.tsx b/src/utils/discord.tsx index 4f4326b..458509b 100644 --- a/src/utils/discord.tsx +++ b/src/utils/discord.tsx @@ -18,7 +18,7 @@ import { MessageObject } from "@api/MessageEvents"; import { findByCodeLazy, findByPropsLazy, findLazy } from "@webpack"; -import { ChannelStore, ComponentDispatch, GuildStore, MaskedLink, ModalImageClasses, PrivateChannelsStore, SelectedChannelStore, SelectedGuildStore, UserUtils } from "@webpack/common"; +import { ChannelStore, ComponentDispatch, FluxDispatcher, GuildStore, MaskedLink, ModalImageClasses, PrivateChannelsStore, RestAPI, SelectedChannelStore, SelectedGuildStore, UserProfileStore, UserUtils } from "@webpack/common"; import { Guild, Message, User } from "discord-types/general"; import { ImageModal, ModalRoot, ModalSize, openModal } from "./modal"; @@ -118,6 +118,41 @@ export async function openUserProfile(id: string) { }); } +interface FetchUserProfileOptions { + friend_token?: string; + connections_role_id?: string; + guild_id?: string; + with_mutual_guilds?: boolean; + with_mutual_friends_count?: boolean; +} + +/** + * Fetch a user's profile + */ +export async function fetchUserProfile(id: string, options?: FetchUserProfileOptions) { + const cached = UserProfileStore.getUserProfile(id); + if (cached) return cached; + + FluxDispatcher.dispatch({ type: "USER_PROFILE_FETCH_START", userId: id }); + + const { body } = await RestAPI.get({ + url: `/users/${id}/profile`, + query: { + with_mutual_guilds: false, + with_mutual_friends_count: false, + ...options + }, + oldFormErrors: true, + }); + + FluxDispatcher.dispatch({ type: "USER_UPDATE", user: body.user }); + await FluxDispatcher.dispatch({ type: "USER_PROFILE_FETCH_SUCCESS", ...body }); + if (options?.guild_id && body.guild_member) + FluxDispatcher.dispatch({ type: "GUILD_MEMBER_PROFILE_UPDATE", guildId: options.guild_id, guildMember: body.guild_member }); + + return UserProfileStore.getUserProfile(id); +} + /** * Get the unique username for a user. Returns user.username for pomelo people, user.tag otherwise */ |