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,
@Param('username') username: string
): Promise<void> {
const target: User | null = await this.usersService.findUserByName(
username
)
if (target == null) throw new BadRequestException('Target unknown.')
if (profile.id === target.ftId) {
const target: User | null = await this.usersService.findUserByName( username )
if (!target) throw new BadRequestException(`User ${username} not found.`)
if (+profile.id === target.ftId)
throw new BadRequestException("You can't invit yourself.")
}
await this.usersService.invit(profile.id, target.id)
const ret: string = await this.usersService.invit(profile.id, target.ftId);
if (ret !== "OK") throw new BadRequestException(ret)
}
@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);
}
async invit(ftId: number, targetFtId: number): Promise<void> {
async invit(ftId: number, targetFtId: number): Promise<string> {
const user: User | null = await this.usersRepository.findOne({
where: { ftId },
relations: {
@ -150,9 +150,9 @@ export class UsersService {
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) {
throw new BadRequestException("You are already friends.");
return "You are already friends.";
}
const target: User | null = await this.usersRepository.findOne({
where: { ftId: targetFtId },
@ -161,16 +161,20 @@ export class UsersService {
friends: true,
},
});
if (target == null) throw new BadRequestException("Target not found.");
if (!target) return "Target not found.";
const id = user.followers.findIndex(
(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);
if (user.ftId !== target.ftId) target.friends.push(user);
target.friends.push(user);
user.followers.slice(id, 1);
await this.usersRepository.save(user);
} else target.followers.push(user);
} else
target.followers.push(user);
await this.usersRepository.save(target);
return "OK"
}
}

2
front/volume/src/App.svelte

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

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

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

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

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

Loading…
Cancel
Save