Browse Source

fixed avatar possible 404 and get others users socketKey

master
nicolas-arnaud 2 years ago
parent
commit
68994e551e
  1. 20
      back/volume/src/users/users.controller.ts
  2. 41
      back/volume/src/users/users.service.ts
  3. 3
      front/volume/src/components/Profile.svelte

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

@ -121,13 +121,15 @@ export class UsersController {
async getAvatar (
@FtUser() profile: Profile,
@Res({ passthrough: true }) response: Response
): Promise<StreamableFile | null> {
): Promise<StreamableFile> {
return await this.getAvatarById(profile.id, response)
}
@Get('user/:name')
async getUserByName (@Param('name') username: string): Promise<User | null> {
return await this.usersService.findUserByName(username)
async getUserByName (@Param('name') username: string): Promise<User> {
const user = await this.usersService.findUserByName(username)
user.socketKey = ''
return user
}
@Get('invit/:username')
@ -135,7 +137,7 @@ export class UsersController {
async invitUser (
@FtUser() profile: Profile,
@Param('username') username: string
): Promise<BadRequestException | null> {
): Promise<void> {
const target: User | null = await this.usersService.findUserByName(
username
)
@ -143,16 +145,16 @@ export class UsersController {
if (profile.id === target.ftId) {
throw new BadRequestException("You can't invit yourself.")
}
return await this.usersService.invit(profile.id, target.id)
await this.usersService.invit(profile.id, target.id)
}
@Get('avatar/:id')
async getAvatarById (
@Param('id', ParseIntPipe) ftId: number,
@Res({ passthrough: true }) response: Response
): Promise<StreamableFile | null> {
): Promise<StreamableFile> {
const user = await this.usersService.findUser(ftId)
if (user == null) return null
if (!user) throw new BadRequestException('User unknown.')
const filename = user.avatar
const stream = createReadStream(join(process.cwd(), 'avatars/' + filename))
response.set({
@ -166,7 +168,9 @@ export class UsersController {
async getUserById (
@Param('id', ParseIntPipe) ftId: number
): Promise<User | null> {
return await this.usersService.findUser(ftId)
const user = await this.usersService.findUser(ftId)
user.socketKey = ''
return user
}
@Post(':id')

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

@ -19,14 +19,19 @@ export class UsersService {
}
async findUsers (): Promise<User[]> {
return await this.usersRepository.find({})
const users = await this.usersRepository.find({})
users.forEach((usr) => {
usr.socketKey = ''
})
return users
}
async findUserByName (username: string): Promise<User | null> {
async findUserByName (username: string): Promise<User> {
const user = await this.usersRepository.findOne({
where: { username },
relations: { results: true }
})
if (user == null) throw new BadRequestException('User not found.')
return user
}
@ -43,20 +48,26 @@ export class UsersService {
})
}
async findUser (ftId: number): Promise<User | null> {
async findUser (ftId: number): Promise<User> {
const user = await this.usersRepository.findOneBy({ ftId })
if (user == null) return null
if (user == null) throw new BadRequestException('User not found.')
user.lastAccess = Date.now()
user.status = 'online'
if (user.status === 'offline') user.status = 'online'
await this.usersRepository.save(user)
return user
}
async findOnlineUsers (): Promise<User[]> {
return await this.usersRepository.find({ where: { status: 'online' } })
const users = await this.usersRepository.find({
where: { status: 'online' }
})
users.forEach((usr) => {
usr.socketKey = ''
})
return users
}
async create (userData: UserDto): Promise<BadRequestException | User> {
async create (userData: UserDto): Promise<User | null> {
try {
const newUser = this.usersRepository.create(userData)
return await this.usersRepository.save(newUser)
@ -87,8 +98,8 @@ export class UsersService {
where: { ftId },
relations: { friends: true }
})
if (user != null) return user.friends
return []
if (user == null) throw new BadRequestException('User not found.')
return user.friends
}
async getInvits (ftId: number): Promise<User[]> {
@ -98,8 +109,8 @@ export class UsersService {
followers: true
}
})
if (user != null) return user.followers
return []
if (user == null) throw new BadRequestException('User not found.')
return user.followers
}
async getResults (ftId: number): Promise<Result[]> {
@ -111,8 +122,8 @@ export class UsersService {
}
}
})
if (user != null) return user.results
return []
if (user == null) throw new BadRequestException('User not found.')
return user.results
}
async getLeaderboard (): Promise<User[]> {
@ -128,7 +139,7 @@ export class UsersService {
return leader.findIndex((user) => user.ftId === ftId)
}
async invit (ftId: number, targetFtId: number): Promise<any> {
async invit (ftId: number, targetFtId: number): Promise<void> {
const user: User | null = await this.usersRepository.findOne({
where: { ftId },
relations: {
@ -138,7 +149,7 @@ export class UsersService {
})
if (user == null) throw new BadRequestException('User not found.')
if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) {
return new BadRequestException('You are already friends.')
throw new BadRequestException('You are already friends.')
}
const target: User | null = await this.usersRepository.findOne({
where: { ftId: targetFtId },

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

@ -50,7 +50,7 @@
<img src={API_URL + "/avatar"} alt="avatar" class="profile-img" />
{:else}
<form
action={`${API_URL}/avatar/${user.id}`}
action={`${API_URL}/avatar`}
method="post"
enctype="multipart/form-data"
id="upload_avatar"
@ -59,6 +59,7 @@
type="file"
id="avatar-input"
name="avatar"
accept="image/png, image/gif, image/jpeg"
on:change={submitAvatar}
/>
</form>

Loading…
Cancel
Save