Skip to content

Commit

Permalink
Ensure you can't send friends requests to current friends (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Advayp authored Feb 24, 2025
1 parent 72746a3 commit 7898a3b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
1 change: 1 addition & 0 deletions backend/app/api/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const GET = async () => {
include: {
likedSongs: true,
preferences: true,
friends: true,
},
});

Expand Down
25 changes: 10 additions & 15 deletions frontend/app/main/(tabs)/search.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import React, { useEffect, useState } from 'react';
import {
StyleSheet,
SafeAreaView,
ScrollView,
Text,
View,
Image,
TouchableOpacity,
} from 'react-native';
import { StyleSheet, SafeAreaView, ScrollView, View } from 'react-native';
import { InputField } from '@/components/InputField';
import CurveTextHeader from '@/components/CurveTextHeader';
import { Button } from '@/components/Button';
import { Song, User } from '@/types';
import { searchSongs } from '@/services/songs';
import TrackCard from '@/components/TrackCard';
import { searchUsers } from '@/services/user';
import { AntDesign } from '@expo/vector-icons';
import { useAuth } from '@/hooks/useAuth';
import { sendFriendRequest } from '@/services/friendRequests';
import Toast from 'react-native-toast-message';
Expand Down Expand Up @@ -93,7 +84,7 @@ export default function SearchScreen() {
return <TrackCard rawTrack={song} key={song.trackID} />;
})
) : (
<UserListView users={userSearchResults} currentUserId={user!.id} />
<UserListView users={userSearchResults} currentUser={user!} />
)}
</ScrollView>
</SafeAreaView>
Expand All @@ -102,8 +93,8 @@ export default function SearchScreen() {

const UserListView: React.FC<{
users: User[];
currentUserId: number;
}> = ({ users, currentUserId }) => {
currentUser: User;
}> = ({ users, currentUser }) => {
const handleFriendRequest = async (id: number) => {
const successValue = await sendFriendRequest(id);
if (successValue) {
Expand All @@ -114,16 +105,20 @@ const UserListView: React.FC<{
}
};

const userFriends = new Set(currentUser.friends.map((friend) => friend.id));

return (
<>
{users
.filter((value) => value.id !== currentUserId)
.filter((value) => value.id !== currentUser.id)
.map((user) => {
return (
<UserView
key={user.id}
user={user}
handleFriendRequest={handleFriendRequest}
handleFriendRequest={
userFriends.has(user.id) ? undefined : handleFriendRequest
}
/>
);
})}
Expand Down
1 change: 1 addition & 0 deletions frontend/services/auth/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const getCurrentSession = async (): Promise<
...(data.user as User),
createdAt: new Date(data.user.createdAt),
updatedAt: new Date(data.user.updatedAt),
friends: data.user.friends as User[],
},
null,
];
Expand Down
1 change: 1 addition & 0 deletions frontend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface User {
createdAt: Date;
updatedAt: Date;
profilePictureURL: string;
friends: User[];
}

export interface ErrorResponse {
Expand Down

0 comments on commit 7898a3b

Please sign in to comment.