@@ -7,8 +7,11 @@ import {
77 type Timestamp ,
88 where ,
99} from 'firebase/firestore' ;
10+ import { getDownloadURL , ref } from 'firebase/storage' ;
1011
11- import { db } from '../common/firebase' ;
12+ import { getCurrentUserId } from '@/core' ;
13+
14+ import { db , storage } from '../common/firebase' ;
1215import {
1316 type DbRelationshipT ,
1417 type DbUserT ,
@@ -68,13 +71,19 @@ const getRelationshipStatus = async (
6871 return convertDbRelationshipToRelationship ( relationship as DbRelationshipT ) ;
6972} ;
7073
71- export const getUserById = async (
74+ export const getUserById = async ( id : UserIdT ) : Promise < UserT | null > => {
75+ const userDoc = await getDoc ( doc ( db , 'users' , id ) ) ;
76+ if ( ! userDoc . exists ( ) ) return null ;
77+ return convertDbUserToUser ( id , userDoc . data ( ) as DbUserT ) ;
78+ } ;
79+
80+ export const getUserWithRelationshipById = async (
7281 id : UserIdT ,
7382) : Promise < UserWithRelationshipT | null > => {
7483 const userDoc = await getDoc ( doc ( db , 'users' , id ) ) ;
7584 if ( ! userDoc . exists ( ) ) return null ;
7685
77- const myId = '1' as UserIdT ; // TODO: Get from auth context
86+ const myId = getCurrentUserId ( ) ;
7887 const relationship = await getRelationshipStatus ( myId , id ) ;
7988
8089 return {
@@ -141,9 +150,15 @@ export const searchUsers = async (searchQuery: string): Promise<UserT[]> => {
141150} ;
142151
143152export const getUserPicture = async ( userId : UserIdT ) : Promise < string > => {
144- const pictureDoc = await getDoc ( doc ( db , 'pictures' , userId ) ) ;
145- if ( ! pictureDoc . exists ( ) ) throw new Error ( 'User picture not found' ) ;
146- return pictureDoc . data ( ) . url ;
153+ const defaultProfilePic = require ( '/assets/images/default_profile_pic.png' ) ;
154+ try {
155+ const pictureRef = ref ( storage , `profilePics/${ userId } ` ) ;
156+ const url = await getDownloadURL ( pictureRef ) ;
157+ return url ;
158+ } catch ( error ) {
159+ console . log ( `No profile image found for ${ userId } , using default.` ) ;
160+ return defaultProfilePic ;
161+ }
147162} ;
148163
149164export const getFriends = async ( userId : UserIdT ) : Promise < UserT [ ] > => {
@@ -177,7 +192,7 @@ export const getFriends = async (userId: UserIdT): Promise<UserT[]> => {
177192 // Fetch user data for each friend
178193 const friends = await Promise . all (
179194 uniqueFriendIds . map ( async ( friendId ) => {
180- const friend = await getUserById ( friendId as UserIdT ) ;
195+ const friend = await getUserWithRelationshipById ( friendId as UserIdT ) ;
181196 if ( ! friend ) throw new Error ( 'Friend not found' ) ;
182197 return friend ;
183198 } ) ,
0 commit comments