ok after talking with cherif we decided not to send things like the user.id so i need to recreate more or less what i had before, but i'm gonna keep using the user.id everywhere in the back and keep my better simpler friendship.entity, working on all that now, got a thing that compiles in the front and back, a few more things to fix and then test everything

This commit is contained in:
Me
2023-01-03 19:42:14 +01:00
parent b7b9b3d645
commit 000987fbde
7 changed files with 75 additions and 66 deletions

View File

@@ -2,12 +2,12 @@ import { IsEnum, IsNotEmpty, IsString, IsPositive } from 'class-validator';
import { FriendshipStatus } from '../entities/friendship.entity';
export class CreateFriendshipDto {
@IsPositive()
// @IsPositive()
// @Max(1000) ?
readonly receiverId: number;
// @IsNotEmpty()
// @IsString()
// readonly receiverUsername: string;
// readonly receiverId: number;
@IsNotEmpty()
@IsString()
readonly receiverUsername: string;
@IsEnum(FriendshipStatus)
readonly status: FriendshipStatus;
}

View File

@@ -25,12 +25,10 @@ export class FriendshipController {
@Get('myfriends')
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findOne(@Query('username') otherUsername: string, @Query('id') id: number, @Req() req) {
findOne(@Query('username') otherUsername: string, @Req() req) {
console.log('GET myfriend')
const user = req.user;
if (id !== undefined) {
return this.friendshipService.findOneRelationshipById(id, user.id)
} else if (otherUsername !== undefined) {
if (otherUsername !== undefined) {
return this.friendshipService.findOneRelationshipByUsername(otherUsername, user.username);
}
// might change this
@@ -59,7 +57,7 @@ export class FriendshipController {
@UseGuards(TwoFactorGuard)
create(@Body() createFriendshipDto: CreateFriendshipDto, @Req() req) {
const user = req.user;
if (user.id !== createFriendshipDto.receiverId)
if (user.username !== createFriendshipDto.receiverUsername)
return this.friendshipService.create(createFriendshipDto, user);
return new HttpException('You can\'t request a frienship to yourself', HttpStatus.BAD_REQUEST);
}
@@ -124,8 +122,11 @@ export class FriendshipController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
findBlocked(@Query('relationshipId') relationshipId: number, @Req() req) {
console.log('friendship.controller fetching blocked users')
console.log(relationshipId)
const user = req.user;
if (relationshipId === undefined)
// if (relationshipId === undefined)
if (Number.isNaN(relationshipId))
return this.friendshipService.findAllBlockedFriends(user.id);
else
return this.friendshipService.findOneBlocked(relationshipId, user.id);

View File

@@ -103,7 +103,7 @@ export class FriendshipService {
// lets see what happens here, doing directly receiver.id not LeftJoinAndSelect ...
async findAllFriends(userId: number) {
const friendship = await this.friendshipRepository
const friendships = await this.friendshipRepository
.createQueryBuilder('friendship')
.leftJoinAndSelect('friendship.sender', 'sender')
.leftJoinAndSelect('friendship.receiver', 'receiver')
@@ -115,7 +115,13 @@ export class FriendshipService {
// for (const friend of friendship)
// console.log("FRIENDSHIP : " + friend.status);
// return friendship;
return new SendableFriendship(friendship);
// return new SendableFriendship(friendship);
let sendFrienships: SendableFriendship[] = []
for (const friendship of friendships) {
sendFrienships.push(new SendableFriendship(friendship));
}
// return new SendableFriendship(friendship);
return sendFrienships;
}
async findOneBlocked(friendshipId: number, userId: number) {
@@ -225,7 +231,7 @@ export class FriendshipService {
async create(createFriendshipDto: CreateFriendshipDto, creator : User) {
console.log("DTO : \n")
console.log({...createFriendshipDto})
const receiver = await this.userRepository.findOneBy({id: createFriendshipDto.receiverId});
const receiver = await this.userRepository.findOneBy({username: createFriendshipDto.receiverUsername});
if (!receiver)
throw new HttpException(`The addressee does not exist.`, HttpStatus.NOT_FOUND);
if (createFriendshipDto.status !== FriendshipStatus.REQUESTED && createFriendshipDto.status !== FriendshipStatus.BLOCKED)
@@ -278,7 +284,7 @@ export class FriendshipService {
// console.log({...relation})
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.senderId === user.id) {
if (relation.sender.id === user.id) {
throw new HttpException(`You can't accept your own request.`, HttpStatus.NOT_FOUND);
}
relation.status = FriendshipStatus.ACCEPTED;
@@ -301,7 +307,7 @@ export class FriendshipService {
const relation = await this.friendshipRepository.findOneBy({id: relationshipId });
if (!relation)
throw new HttpException(`The requested relationship not found.`, HttpStatus.NOT_FOUND);
if (relation.senderId === user.id) {
if (relation.sender.id === user.id) {
throw new HttpException(`You can't decline your own request.`, HttpStatus.NOT_FOUND);
}
relation.status = FriendshipStatus.DECLINED;
@@ -325,14 +331,14 @@ export class FriendshipService {
// do i need to check if they've already been blocked?
if (relation.receiverId === user.id) {
if (relation.receiver.id === user.id) {
// throw new HttpException(`You can't block yourself.`, HttpStatus.NOT_FOUND);
console.log('friendship.service blockFriendship trying to delete and recreate a friendship with block')
console.log({...relation})
// const newFriendshipDto = new CreateFriendshipDto(relation[0].receiver, FriendshipStatus.BLOCKED)
// const newFriendshipDto = new CreateFriendshipDto({"receiverUsername": relation[0].receiver, "status": "R"})
// we create a new one where you are the sender
const newFriendshipDto = {"receiverUsername": relation.senderUsername, "receiverId": relation.senderId, "status": FriendshipStatus.BLOCKED};
const newFriendshipDto = {"receiverUsername": relation.sender.username, "receiverId": relation.sender.id, "status": FriendshipStatus.BLOCKED};
// can't do it this way cuz READONLY
// const newFriendshipDto = new CreateFriendshipDto();
// newFriendshipDto.receiverUsername = relation[0].sender.username;
@@ -366,7 +372,7 @@ export class FriendshipService {
console.log({...user})
if (!friendship)
throw new HttpException(`Your friend could not be deleted.`, HttpStatus.NOT_FOUND);
if (friendship.senderId !== user.id && friendship.receiverId !== user.id) {
if (friendship.sender.id !== user.id && friendship.receiver.id !== user.id) {
throw new HttpException(`You can't do that.`, HttpStatus.FORBIDDEN);
}
console.log('.service deleted a friendship')

View File

@@ -62,15 +62,15 @@ export class UsersController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get()
findOne(@Query('id') toFindId: number, @Req() req) {
console.log('users service findOne toFindId:')
console.log(toFindId)
console.log('users service findOne my Id:')
console.log(req.user.id)
if (toFindId === undefined)
return this.usersService.findOne(req.user.id);
findOne(@Query('username') usernameToFind: string, @Req() req) {
console.log('users service findOne usernameToFind:')
console.log(usernameToFind)
console.log('users service findOne my Username:')
console.log(req.user.username)
if (usernameToFind === undefined)
return this.usersService.findOne(req.user.username);
else
return this.usersService.findOne(toFindId);
return this.usersService.findOne(usernameToFind);
// i would rather just use numbers but i'm guessing Cherif uses this all over
}

View File

@@ -32,11 +32,11 @@ export class UsersService {
return user;
}
async findOne(id: number) {
console.log(`FIND ONE USER SERVICE Find user ${id}`);
async findOne(username: string) {
console.log(`FIND ONE USER SERVICE Find user ${username}`);
const user = await this.userRepository.createQueryBuilder('user')
.leftJoinAndSelect('user.stats', 'stats')
.where('user.id = :id', { id: id })
.where('user.username = :username', { username: username })
.getOne();
if (!user)
throw new NotFoundException(`The requested user not found.`);
@@ -53,6 +53,9 @@ export class UsersService {
return partialUser;
}
/***** THIS IS THE THING I REALLY NEED TO FIX!!!!!!! *****/
// Ok this gets called in the Authenitcation Service, but like i was still able to make a username === someone else's
async isUsernameExists(usernameToSearch: string): Promise<boolean> {
const user = await this.userRepository.findOneBy({username : usernameToSearch});
@@ -123,28 +126,28 @@ export class UsersService {
return this.userRepository.save(user);
}
async update(id: string, updateUserDto: UpdateUsersDto) {
console.log(`Update user ${id} with ${updateUserDto.isEnabledTwoFactorAuth}`);
async update(id: number, updateUserDto: UpdateUsersDto) {
// console.log(`Update user ${id} with ${updateUserDto.isEnabledTwoFactorAuth}`);
const user = await this.userRepository.preload(
{id: +id,
{id: id,
...updateUserDto});
if (!user)
throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND);
return this.userRepository.save(user);
}
async remove(id: string) {
const user = await this.userRepository.findOneBy({id: +id});
async remove(id: number) {
const user = await this.userRepository.findOneBy({id: id});
if (!user)
throw new HttpException(`The user could not be deleted.`,HttpStatus.NOT_FOUND);
return this.userRepository.remove(user);
}
async enableTwoFactorAuth(id: string) {
async enableTwoFactorAuth(id: number) {
return this.userRepository.update(id, {isEnabledTwoFactorAuth: true});
}
async authenticateUserWith2FA(id: string) {
async authenticateUserWith2FA(id: number) {
return this.userRepository.update(id, { isTwoFactorAuthenticated: true})
}

View File

@@ -14,7 +14,7 @@
let myFriends;
let requestsMade, requestsRecieved;
let blockedUsers;
let userIdBeingViewed;
let usernameBeingViewed;
let friendshipStatusFull; // id, date, senderUsername, reveiverUsername, status
/**** Layout variables ****/
@@ -60,7 +60,7 @@
};
const fetchMyFriends = async() => {
myFriends = await fetch('http://transcendance:8080/api/v2/network/relations')
myFriends = await fetch('http://transcendance:8080/api/v2/network/myfriends')
.then( x => x.json() );
console.log('got my friends ')
console.log({...myFriends})
@@ -90,10 +90,10 @@
/**** END OF MAIN FETCH ****/
// returns everything but BLOCKED
const fetchFriendshipFull = async(aUserId) => {
const fetchFriendshipFull = async(aUsername) => {
console.log('fetch friendship from a username')
console.log(aUserId)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/relations/${aUserId}`)
console.log(aUsername)
friendshipStatusFull = await fetch(`http://transcendance:8080/api/v2/network/myfriends/${aUsername}`)
.then( x => x.json());
console.log({...friendshipStatusFull})
};
@@ -105,21 +105,20 @@
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
"receiverUsername": aUser.username,
"receiverId": aUser.id,
"status": "R"
})
})
.then( x => x.json())
fetchFriendshipFull(aUser.id);
fetchFriendshipFull(aUser.username);
};
const viewAUser = async(aUserId) => {
const viewAUser = async(aUsername) => {
console.log('Profile Friend updating userBeingViewed')
userIdBeingViewed = aUserId;
usernameBeingViewed = aUsername;
// friendshipStatusFull = undefined;
// id, date, senderUsername, reveiverUsername, status
await fetchFriendshipFull(aUserId);
await fetchFriendshipFull(aUsername);
console.log('Friendship Status Full')
console.log({...friendshipStatusFull})
@@ -135,7 +134,7 @@
// maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now...
console.log('accepted friend request, now response')
console.log({...resp})
await fetchFriendshipFull(userIdBeingViewed);
await fetchFriendshipFull(usernameBeingViewed);
// will this make it reload? C'est un peu bourain... do i even need it?
activeTabItem = activeTabItem;
@@ -150,7 +149,7 @@
// maybe not the most robust things, not super reusable cuz it depends on outside vars but works for now...
console.log('declined friend request, now response')
console.log({...resp})
await fetchFriendshipFull(userIdBeingViewed);
await fetchFriendshipFull(usernameBeingViewed);
};
const unfriend = async(relationshipId) => {
@@ -161,7 +160,7 @@
// friendshipStatusFull = undefined;
// OR
await fetchFriendshipFull(userIdBeingViewed);
await fetchFriendshipFull(usernameBeingViewed);
if (Object.keys(friendshipStatusFull).length === 0)
friendshipStatusFull = undefined;
@@ -178,14 +177,14 @@
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
"receiverUsername": aUser.username,
"receiverId": aUser.id,
"receiverId": aUser.username,
"status": "B"
})
})
.then( x => x.json())
await fetchBlockedUsers();
await fetchAllUsers();
userIdBeingViewed = undefined;
usernameBeingViewed = undefined;
friendshipStatusFull = undefined;
// will this make it reload?
@@ -201,7 +200,7 @@
console.log({...resp})
await fetchBlockedUsers();
await fetchAllUsers();
userIdBeingViewed = undefined;
usernameBeingViewed = undefined;
friendshipStatusFull = undefined;
// will this make it reload?
@@ -252,9 +251,9 @@
{/if}
<!-- does this work? -->
<!-- {#each allUsers as aUser (aUser.username)} -->
<!-- {#each allUsers as aUser} -->
{#each allUsers as aUser (aUser.id)}
<div class="sidebar-item" on:click={() => viewAUser(aUser.id)}>{aUser.username}</div>
<!-- {#each allUsers as aUser (aUser.id)} -->
{#each allUsers as aUser}
<div class="sidebar-item" on:click={() => viewAUser(aUser.username)}>{aUser.username}</div>
<!-- i could make an indicator component? like green for connected or something?
i could use words but color them?
i could make it so if they're in a game -->
@@ -268,7 +267,7 @@
{/if}
<!-- {#each myFriends as aUser (aUser.id)} -->
{#each myFriends as aUser}
<div class="sidebar-item" on:click={() => viewAUser(aUser.id)}>{aUser.username}</div>
<div class="sidebar-item" on:click={() => viewAUser(aUser.username)}>{aUser.username}</div>
<div class="status sidebar-item">{aUser.status}</div>
<br>
{/each}
@@ -300,9 +299,9 @@
<div class="main-display">
{#if userIdBeingViewed !== undefined}
{#if usernameBeingViewed !== undefined}
<!-- <DisplayAUser aUsername={usernameBeingViewed}/> -->
<DisplayAUser aUserId={userIdBeingViewed}/>
<DisplayAUser aUsername={usernameBeingViewed}/>
<div class="buttons-area">
{#if friendshipStatusFull && friendshipStatusFull.id}
@@ -333,8 +332,8 @@
{/if}
{/if}
{:else}
<Button type="secondary" on:click={() => sendFriendRequest(userIdBeingViewed)}>Add Friend</Button>
<Button on:click={() => blockANonFriendUser(userIdBeingViewed)}>Block User</Button>
<Button type="secondary" on:click={() => sendFriendRequest(usernameBeingViewed)}>Add Friend</Button>
<Button on:click={() => blockANonFriendUser(usernameBeingViewed)}>Block User</Button>
{/if}
</div>
{:else}

View File

@@ -4,14 +4,14 @@
import GenerateUserDisplay from './GenerateUserDisplay.svelte';
// export let aUsername;
export let aUserId;
export let aUsername;
let user;
onMount( async() => {
// console.log('Display aUser username: '+ aUsername)
// http://transcendance:8080/api/v2/user?username=NomDuUserATrouver
// user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`)
user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUserId}`)
user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUsername}`)
.then( (x) => x.json() );
// console.log('Display a user: ')
@@ -32,12 +32,12 @@
// console.log('Display Update aUser username: '+ aUsername)
// http://transcendance:8080/api/v2/user?username=NomDuUserATrouver
// user = await fetch(`http://transcendance:8080/api/v2/user?username=${aUsername}`)
user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUserId}`)
user = await fetch(`http://transcendance:8080/api/v2/user?id=${aUsername}`)
.then( (x) => x.json() );
};
// $: aUsername, updateUser();
$: aUserId, updateUser();
$: aUsername, updateUser();
</script>
@@ -49,7 +49,7 @@
<!-- <GenerateUserDisplay user={user} primary={true}/> -->
{:else}
<h2>Sorry</h2>
<div>Failed to load user {aUserId}</div>
<div>Failed to load user {aUsername}</div>
{/if}