feat: get friends event

This commit is contained in:
Zamitto 2024-07-21 16:35:02 -03:00
parent 909e288bec
commit d350aa950d
5 changed files with 44 additions and 9 deletions

View file

@ -0,0 +1,26 @@
import { registerEvent } from "../register-event";
import { HydraApi } from "@main/services";
import { UserFriends } from "@types";
export const getUserFriends = async (
userId: string,
take: number,
skip: number
): Promise<UserFriends> => {
return HydraApi.get(`/user/${userId}/friends`, { take, skip }).catch(
(_err) => {
return { totalFriends: 0, friends: [] } as UserFriends;
}
);
};
const getUserFriendsEvent = async (
_event: Electron.IpcMainInvokeEvent,
userId: string,
take: number,
skip: number
) => {
return getUserFriends(userId, take, skip);
};
registerEvent("getUserFriends", getUserFriendsEvent);

View file

@ -4,13 +4,17 @@ import { steamGamesWorker } from "@main/workers";
import { UserProfile } from "@types"; import { UserProfile } from "@types";
import { convertSteamGameToCatalogueEntry } from "../helpers/search-games"; import { convertSteamGameToCatalogueEntry } from "../helpers/search-games";
import { getSteamAppAsset } from "@main/helpers"; import { getSteamAppAsset } from "@main/helpers";
import { getUserFriends } from "./get-user-friends";
const getUser = async ( const getUser = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
userId: string userId: string
): Promise<UserProfile | null> => { ): Promise<UserProfile | null> => {
try { try {
const profile = await HydraApi.get(`/user/${userId}`); const [profile, friends] = await Promise.all([
HydraApi.get(`/user/${userId}`),
getUserFriends(userId, 12, 0),
]);
const recentGames = await Promise.all( const recentGames = await Promise.all(
profile.recentGames.map(async (game) => { profile.recentGames.map(async (game) => {
@ -46,7 +50,7 @@ const getUser = async (
}) })
); );
return { ...profile, libraryGames, recentGames }; return { ...profile, libraryGames, recentGames, friends };
} catch (err) { } catch (err) {
return null; return null;
} }

View file

@ -72,7 +72,7 @@ export class HydraApi {
this.instance.interceptors.request.use( this.instance.interceptors.request.use(
(request) => { (request) => {
logger.log(" ---- REQUEST -----"); logger.log(" ---- REQUEST -----");
logger.log(request.method, request.url, request.data); logger.log(request.method, request.url, request.params, request.data);
return request; return request;
}, },
(error) => { (error) => {
@ -196,12 +196,12 @@ export class HydraApi {
throw err; throw err;
}; };
static async get(url: string) { static async get(url: string, params?: any) {
if (!this.isLoggedIn()) throw new UserNotLoggedInError(); if (!this.isLoggedIn()) throw new UserNotLoggedInError();
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance return this.instance
.get(url, this.getAxiosConfig()) .get(url, { params, ...this.getAxiosConfig() })
.then((response) => response.data) .then((response) => response.data)
.catch(this.handleUnauthorizedError); .catch(this.handleUnauthorizedError);
} }

View file

@ -342,7 +342,7 @@ export function UserContent({
</div> </div>
{(isMe || {(isMe ||
(userProfile.friends && userProfile.friends.length > 0)) && ( (userProfile.friends && userProfile.friends.totalFriends > 0)) && (
<div className={styles.friendsSection}> <div className={styles.friendsSection}>
<button <button
className={styles.friendsSectionHeader} className={styles.friendsSectionHeader}
@ -358,7 +358,7 @@ export function UserContent({
}} }}
/> />
<h3 style={{ fontWeight: "400" }}> <h3 style={{ fontWeight: "400" }}>
{userProfile.friends.length} {userProfile.friends.totalFriends}
</h3> </h3>
</button> </button>
@ -369,7 +369,7 @@ export function UserContent({
gap: `${SPACING_UNIT}px`, gap: `${SPACING_UNIT}px`,
}} }}
> >
{userProfile.friends.map((friend) => { {userProfile.friends.friends.map((friend) => {
return ( return (
<button <button
key={friend.id} key={friend.id}

View file

@ -277,6 +277,11 @@ export interface UserFriend {
profileImageUrl: string | null; profileImageUrl: string | null;
} }
export interface UserFriends {
totalFriends: number;
friends: UserFriend[];
}
export interface FriendRequest { export interface FriendRequest {
id: string; id: string;
displayName: string; displayName: string;
@ -291,7 +296,7 @@ export interface UserProfile {
totalPlayTimeInSeconds: number; totalPlayTimeInSeconds: number;
libraryGames: UserGame[]; libraryGames: UserGame[];
recentGames: UserGame[]; recentGames: UserGame[];
friends: UserFriend[]; friends: UserFriends;
} }
export interface DownloadSource { export interface DownloadSource {