Browse Source

fixed friends invitations

master
nicolas-arnaud 2 years ago
parent
commit
0fd395e6d3
  1. 12
      back/volume/src/users/users.controller.ts
  2. 18
      back/volume/src/users/users.service.ts
  3. 2
      front/volume/src/App.svelte
  4. 3
      front/volume/src/components/Friends.svelte
  5. 3
      front/volume/src/components/MatchHistory.svelte

12
back/volume/src/users/users.controller.ts

@ -138,14 +138,12 @@ export class UsersController {
@FtUser() profile: Profile, @FtUser() profile: Profile,
@Param('username') username: string @Param('username') username: string
): Promise<void> { ): Promise<void> {
const target: User | null = await this.usersService.findUserByName( const target: User | null = await this.usersService.findUserByName( username )
username if (!target) throw new BadRequestException(`User ${username} not found.`)
) if (+profile.id === target.ftId)
if (target == null) throw new BadRequestException('Target unknown.')
if (profile.id === target.ftId) {
throw new BadRequestException("You can't invit yourself.") throw new BadRequestException("You can't invit yourself.")
} const ret: string = await this.usersService.invit(profile.id, target.ftId);
await this.usersService.invit(profile.id, target.id) if (ret !== "OK") throw new BadRequestException(ret)
} }
@Get('avatar/:id') @Get('avatar/:id')

18
back/volume/src/users/users.service.ts

@ -142,7 +142,7 @@ export class UsersService {
return leaderboard.findIndex((user) => user.ftId === ftId); return leaderboard.findIndex((user) => user.ftId === ftId);
} }
async invit(ftId: number, targetFtId: number): Promise<void> { async invit(ftId: number, targetFtId: number): Promise<string> {
const user: User | null = await this.usersRepository.findOne({ const user: User | null = await this.usersRepository.findOne({
where: { ftId }, where: { ftId },
relations: { relations: {
@ -150,9 +150,9 @@ export class UsersService {
friends: true, friends: true,
}, },
}); });
if (user == null) throw new BadRequestException("User not found."); if (!user) throw new BadRequestException("User not found.");
if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) {
throw new BadRequestException("You are already friends."); return "You are already friends.";
} }
const target: User | null = await this.usersRepository.findOne({ const target: User | null = await this.usersRepository.findOne({
where: { ftId: targetFtId }, where: { ftId: targetFtId },
@ -161,16 +161,20 @@ export class UsersService {
friends: true, friends: true,
}, },
}); });
if (target == null) throw new BadRequestException("Target not found."); if (!target) return "Target not found.";
const id = user.followers.findIndex( const id = user.followers.findIndex(
(follower) => follower.ftId === targetFtId (follower) => follower.ftId === targetFtId
); );
if (id !== -1) { if (target.followers.findIndex((follower) => follower.ftId === user.ftId) !== -1) {
return "Invitation already sent.";
}else if (user.followers.findIndex((follower) => follower.ftId === targetFtId) !== -1) {
user.friends.push(target); user.friends.push(target);
if (user.ftId !== target.ftId) target.friends.push(user); target.friends.push(user);
user.followers.slice(id, 1); user.followers.slice(id, 1);
await this.usersRepository.save(user); await this.usersRepository.save(user);
} else target.followers.push(user); } else
target.followers.push(user);
await this.usersRepository.save(target); await this.usersRepository.save(target);
return "OK"
} }
} }

2
front/volume/src/App.svelte

@ -250,7 +250,7 @@
{/if} {/if}
{#if appState === APPSTATE.HISTORY_ID} {#if appState === APPSTATE.HISTORY_ID}
<div on:click={resetAppState} on:keydown={resetAppState}> <div on:click={resetAppState} on:keydown={resetAppState}>
<MatchHistory {matches} /> <MatchHistory username={$store.username} {matches} />
</div> </div>
{/if} {/if}
{#if appState === APPSTATE.PROFILE} {#if appState === APPSTATE.PROFILE}

3
front/volume/src/components/Friends.svelte

@ -19,7 +19,8 @@
if (response.ok) { if (response.ok) {
alert("Invitation send."); alert("Invitation send.");
} else { } else {
alert("Invitation failed."); const error = (await response.json()).message
alert("Invitation failed: " + error);;
} }
} }
</script> </script>

3
front/volume/src/components/MatchHistory.svelte

@ -9,6 +9,7 @@
</script> </script>
<script lang="ts"> <script lang="ts">
export let username: string = "Global";
export let matches: Array<Match> = []; export let matches: Array<Match> = [];
function displayDate(str: string) { function displayDate(str: string) {
const splitT = str.split("T"); const splitT = str.split("T");
@ -25,7 +26,7 @@
<table> <table>
<thead> <thead>
<tr> <tr>
<th colspan="3">Last 10 monkey games</th> <th colspan="3">{username}'s last matchs</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>

Loading…
Cancel
Save