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

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

@ -19,14 +19,19 @@ export class UsersService {
} }
async findUsers (): Promise<User[]> { 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({ const user = await this.usersRepository.findOne({
where: { username }, where: { username },
relations: { results: true } relations: { results: true }
}) })
if (user == null) throw new BadRequestException('User not found.')
return user 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 }) 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.lastAccess = Date.now()
user.status = 'online' if (user.status === 'offline') user.status = 'online'
await this.usersRepository.save(user) await this.usersRepository.save(user)
return user return user
} }
async findOnlineUsers (): Promise<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 { try {
const newUser = this.usersRepository.create(userData) const newUser = this.usersRepository.create(userData)
return await this.usersRepository.save(newUser) return await this.usersRepository.save(newUser)
@ -87,8 +98,8 @@ export class UsersService {
where: { ftId }, where: { ftId },
relations: { friends: true } relations: { friends: true }
}) })
if (user != null) return user.friends if (user == null) throw new BadRequestException('User not found.')
return [] return user.friends
} }
async getInvits (ftId: number): Promise<User[]> { async getInvits (ftId: number): Promise<User[]> {
@ -98,8 +109,8 @@ export class UsersService {
followers: true followers: true
} }
}) })
if (user != null) return user.followers if (user == null) throw new BadRequestException('User not found.')
return [] return user.followers
} }
async getResults (ftId: number): Promise<Result[]> { async getResults (ftId: number): Promise<Result[]> {
@ -111,8 +122,8 @@ export class UsersService {
} }
} }
}) })
if (user != null) return user.results if (user == null) throw new BadRequestException('User not found.')
return [] return user.results
} }
async getLeaderboard (): Promise<User[]> { async getLeaderboard (): Promise<User[]> {
@ -128,7 +139,7 @@ export class UsersService {
return leader.findIndex((user) => user.ftId === ftId) 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({ const user: User | null = await this.usersRepository.findOne({
where: { ftId }, where: { ftId },
relations: { relations: {
@ -138,7 +149,7 @@ export class UsersService {
}) })
if (user == null) throw new BadRequestException('User not found.') if (user == null) throw new BadRequestException('User not found.')
if (user.friends.findIndex((friend) => friend.ftId === targetFtId) !== -1) { 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({ const target: User | null = await this.usersRepository.findOne({
where: { ftId: targetFtId }, where: { ftId: targetFtId },

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

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

Loading…
Cancel
Save