Browse Source

*fix avatar upload

*fix invid on ourself
*fix some profile.id types comp
master
nicolas-arnaud 2 years ago
parent
commit
1661c225ae
  1. 40
      back/volume/src/chat/chat.controller.ts
  2. 21
      back/volume/src/chat/chat.service.ts
  3. 11
      back/volume/src/users/users.controller.ts
  4. 2
      front/volume/src/components/Profile.svelte

40
back/volume/src/chat/chat.controller.ts

@ -29,6 +29,24 @@ export class ChatController {
private readonly usersService: UsersService
) {}
@Post(':id/invite')
async addUser (@Param('id') id: number, @Body() userId: number) {
const channel = await this.channelService.getChannel(id)
const user: User | null = await this.usersService.findUser(userId)
if (user == null) throw new NotFoundException(`User #${userId} not found`)
channel.users.push(user)
this.channelService.update(channel)
}
@Delete(':id/kick')
async removeUser (@Param('id') id: number, @Body() userId: number) {
const channel = await this.channelService.getChannel(id)
channel.users = channel.admins.filter((usr: User) => {
return usr.ftId !== userId
})
this.channelService.update(channel)
}
@Post(':id/admin')
async addAdmin (@Param('id') id: number, @Body() userId: number) {
const channel = await this.channelService.getChannel(id)
@ -76,7 +94,10 @@ export class ChatController {
@Delete(':id')
@UseGuards(AuthenticatedGuard)
async deleteChannel (@Profile42() profile: Profile, @Param('id') id: number) {
async deleteChannel (
@Profile42() profile: Profile,
@Param('id') id: number
) {
if (await this.channelService.isOwner(id, +profile.id)) {
await this.channelService.removeChannel(id)
return
@ -84,10 +105,25 @@ export class ChatController {
throw new BadRequestException('You are not the owner of this channel')
}
@Post(':id/password')
@UseGuards(AuthenticatedGuard)
async updatePassword (
@Profile42() profile: Profile,
@Param('id') id: number,
@Body() password: string
) {
if (await this.channelService.isOwner(id, +profile.id)) {
await this.channelService.updatePassword(id, password)
return
}
throw new BadRequestException('You are not the owner of this channel')
}
@Get()
@UseGuards(AuthenticatedGuard)
async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> {
return await this.channelService.getChannelsForUser(profile.id)
return await this.channelService.getChannelsForUser(+profile.id)
}
@Post()

21
back/volume/src/chat/chat.service.ts

@ -19,9 +19,8 @@ export class ChannelService {
async createChannel (channel: CreateChannelDto): Promise<Channel> {
const user: User | null = await this.usersService.findUser(channel.owner)
if (user == null) {
if (user == null)
throw new NotFoundException(`User #${channel.owner} not found`)
}
const newChannel = new Channel()
newChannel.owner = user
newChannel.users = [user]
@ -32,6 +31,13 @@ export class ChannelService {
return await this.ChannelRepository.save(newChannel)
}
async updatePassword (id: number, password: string) {
let channel: Channel | null = await this.ChannelRepository.findOneBy({id})
if (channel === null) { throw new NotFoundException(`Channel #${id} not found`) }
channel.password = password
await this.ChannelRepository.save(channel)
}
async getChannelsForUser (ftId: number): Promise<Channel[]> {
let rooms: Channel[] = []
rooms = [
@ -76,8 +82,7 @@ export class ChannelService {
relations: { owner: true }
})
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
console.log(channel.owner.ftId, userId)
return channel.owner.ftId == userId
return channel.owner.ftId === userId
}
async isAdmin (id: number, userId: number): Promise<boolean> {
@ -86,7 +91,7 @@ export class ChannelService {
relations: { admins: true }
})
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
return channel.admins.findIndex((user) => user.ftId == userId) != -1
return channel.admins.findIndex((user) => user.ftId === userId) != -1
}
async isUser (id: number, userId: number): Promise<boolean> {
@ -95,7 +100,7 @@ export class ChannelService {
relations: { users: true }
})
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
return channel.users.findIndex((user) => user.ftId == userId) != -1
return channel.users.findIndex((user) => user.ftId === userId) != -1
}
async isBanned (id: number, userId: number): Promise<boolean> {
@ -104,7 +109,7 @@ export class ChannelService {
relations: { banned: true }
})
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
return channel.banned.findIndex((user) => user.ftId == userId) != -1
return channel.banned.findIndex((user) => user.ftId === userId) != -1
}
async isMuted (id: number, userId: number): Promise<boolean> {
@ -113,6 +118,6 @@ export class ChannelService {
relations: { muted: true }
})
if (channel == null) { throw new NotFoundException(`Channel #${id} not found`) }
return channel.muted.findIndex((user) => user.ftId == userId) != -1
return channel.muted.findIndex((user) => user.ftId === userId) != -1
}
}

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

@ -31,9 +31,11 @@ import { type Request, Response } from 'express'
import { createReadStream } from 'fs'
import { join } from 'path'
@Controller('users')
@Controller("users")
export class UsersController {
constructor (private readonly usersService: UsersService) {}
constructor (
private readonly usersService: UsersService,
) {}
@Get('all')
async getAllUsers (): Promise<User[]> {
@ -68,7 +70,6 @@ export class UsersController {
async getRank (@Param('id', ParseIntPipe) id: number): Promise<number> {
return await this.usersService.getRank(id)
}
@Post('avatar')
@UseGuards(AuthenticatedGuard)
@Redirect('http://localhost')
@ -118,7 +119,7 @@ export class UsersController {
@Get('invit/:username')
@UseGuards(AuthenticatedGuard)
async invitUser (
@Profile42() profile: Profile,
@Profile42() profile: Profile,
@Param('username') username: string
): Promise<void> {
const target: User | null = await this.usersService.findUserByName(
@ -127,7 +128,7 @@ export class UsersController {
if (target === null) {
throw new BadRequestException(`User ${username} not found.`)
}
if (profile.id === target.ftId) {
if (+profile.id === +target.ftId) {
throw new BadRequestException("You can't invite yourself.")
}
const ret: string = await this.usersService.invit(profile.id, target.ftId)

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

@ -68,7 +68,7 @@
<div class="profile" on:click|stopPropagation on:keydown|stopPropagation>
<h3>===| <mark>{user.username}'s Profile</mark> |===</h3>
<div class="profile-header">
{#if edit}
{#if !edit}
<img src={API_URL + "/users/avatar"} alt="avatar" class="profile-img" />
{:else}
<form

Loading…
Cancel
Save