Browse Source

brought back connect Users to improve kick OwnerLeave and Join

master
Gabriel Mehdevi 2 years ago
parent
commit
6a85b418a8
  1. 15
      back/volume/src/chat/chat.controller.ts
  2. 40
      back/volume/src/chat/chat.gateway.ts
  3. 3
      back/volume/src/chat/chat.module.ts
  4. 16
      back/volume/src/chat/entity/connection.entity.ts
  5. 5
      front/volume/src/components/Chat.svelte

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

@ -217,21 +217,6 @@ export class ChatController {
await this.channelService.updatePassword(id, data.password) await this.channelService.updatePassword(id, data.password)
} }
@Get(':id/leave')
async leaveChannel (
@Profile42() profile: Profile,
@Param('id', ParseIntPipe) id: number
): Promise<void> {
if (await this.channelService.isOwner(id, +profile.id)) {
await this.channelService.removeChannel(id) // -> verify that the deletion the break others users behaviors.
}
const channel = await this.channelService.getFullChannel(id)
channel.users = channel.users.filter((usr: User) => {
return usr.ftId !== profile.id
})
await this.channelService.save(channel)
}
@Get() @Get()
async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> { async getChannelsForUser (@Profile42() profile: Profile): Promise<Channel[]> {
const chan = await this.channelService.getChannelsForUser(+profile.id) const chan = await this.channelService.getChannelsForUser(+profile.id)

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

@ -16,6 +16,11 @@ import { MessageService } from './message.service'
import { CreateMessageDto } from './dto/create-message.dto' import { CreateMessageDto } from './dto/create-message.dto'
import { ConnectionDto } from './dto/connection.dto' import { ConnectionDto } from './dto/connection.dto'
import { kickUserDto } from './dto/kickUser.dto' import { kickUserDto } from './dto/kickUser.dto'
import ConnectedUser from './entity/connection.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { connect } from 'http2'
import User from 'src/users/entity/user.entity'
@WebSocketGateway({ @WebSocketGateway({
cors: { cors: {
@ -31,7 +36,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
constructor ( constructor (
private readonly userService: UsersService, private readonly userService: UsersService,
private readonly messageService: MessageService, private readonly messageService: MessageService,
private readonly chatService: ChatService private readonly chatService: ChatService,
@InjectRepository(ConnectedUser)
private readonly connectedUserRepository: Repository<ConnectedUser>
) {} ) {}
async handleConnection (socket: Socket): Promise<void> {} async handleConnection (socket: Socket): Promise<void> {}
@ -66,6 +73,12 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
channel, channel,
user user
) )
const conUser = {
user : user.id,
channel : channel.id,
socket: socket.id
}
this.connectedUserRepository.create(conUser)
this.server.to(socket.id).emit('messages', messages) this.server.to(socket.id).emit('messages', messages)
await socket.join(channel.id.toString()) await socket.join(channel.id.toString())
} }
@ -83,7 +96,18 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@SubscribeMessage('leaveChannel') @SubscribeMessage('leaveChannel')
async onLeaveChannel (socket: Socket): Promise<void> { async onLeaveChannel (socket: Socket): Promise<void> {
const connect = await this.connectedUserRepository.findOneBy({socket : socket.id})
if (connect == null)
return
const channel = await this.chatService.getFullChannel(connect.channel)
socket.disconnect() socket.disconnect()
if (connect.user == channel.owner.id) {
this.server.in(channel.id.toString()).disconnectSockets()
this.chatService.removeChannel(channel.id)
} else {
channel.users = channel.users.filter((e) => e.id !== connect.user)
}
await this.connectedUserRepository.delete({ socket : socket.id })
} }
@SubscribeMessage('addMessage') @SubscribeMessage('addMessage')
@ -101,15 +125,19 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
} }
@SubscribeMessage('kickUser') @SubscribeMessage('kickUser')
async onKickUser (socket: Socket, msg: kickUserDto): Promise<void> { async onKickUser (socket: Socket, kick: kickUserDto): Promise<void> {
console.log('kick called') const channel = await this.chatService.getFullChannel(kick.chan);
const channel = await this.chatService.getFullChannel(msg.chan) if (channel.owner.id == kick.to)
throw new WsException('You cannot kick the owner of a channel')
if ( if (
channel.owner.id !== msg.from && channel.owner.id !== kick.from &&
channel.admins.findIndex((usr) => usr.id === msg.from) === -1 channel.admins.findIndex((usr) => usr.id === kick.from) === -1
) { ) {
throw new WsException('You do not have the required privileges') throw new WsException('You do not have the required privileges')
} }
const user = await this.userService.findUser(kick.to) as User
const connect = await this.connectedUserRepository.findOneBy({user : user.id}) as ConnectedUser
await this.onLeaveChannel(socket) await this.onLeaveChannel(socket)
this.server.sockets.sockets.get(connect.socket)?.disconnect()
} }
} }

3
back/volume/src/chat/chat.module.ts

@ -10,12 +10,13 @@ import { MessageService } from './message.service'
import Channel from './entity/channel.entity' import Channel from './entity/channel.entity'
import Message from './entity/message.entity' import Message from './entity/message.entity'
import ConnectedUser from './entity/connection.entity'
@Module({ @Module({
imports: [ imports: [
UsersModule, UsersModule,
AuthModule, AuthModule,
TypeOrmModule.forFeature([Channel, Message]) TypeOrmModule.forFeature([Channel, Message, ConnectedUser])
], ],
controllers: [ChatController], controllers: [ChatController],
providers: [ChatService, ChatGateway, MessageService], providers: [ChatService, ChatGateway, MessageService],

16
back/volume/src/chat/entity/connection.entity.ts

@ -0,0 +1,16 @@
import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm'
import Channel from './channel.entity'
import User from 'src/users/entity/user.entity'
@Entity()
export default class ConnectedUser {
@Column()
user: number
@Column()
channel: number
@PrimaryColumn()
socket: string
}

5
front/volume/src/components/Chat.svelte

@ -295,7 +295,10 @@
mode: "cors", mode: "cors",
}); });
if (response.ok) { if (response.ok) {
window.location.href = "/channels"; return {
status: 200,
redirect: "/channels/"
};
} else { } else {
await show_popup("Failed to leave channel",false); await show_popup("Failed to leave channel",false);
} }

Loading…
Cancel
Save