From e7e16368361f91475d92fe21c9950f31b26083d2 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sun, 19 Mar 2023 00:41:05 +0100 Subject: [PATCH] *implemented password question to join a chat *fixed chats opening twice. ? --- back/volume/src/auth/mails/confirm.hbs | 2 +- back/volume/src/chat/chat.gateway.ts | 17 +----- back/volume/src/chat/chat.service.ts | 1 + back/volume/src/chat/entity/channel.entity.ts | 2 +- front/volume/src/App.svelte | 11 ++-- front/volume/src/components/Channels.svelte | 60 +++++++++++++------ front/volume/src/components/Chat.svelte | 17 ++---- 7 files changed, 59 insertions(+), 51 deletions(-) diff --git a/back/volume/src/auth/mails/confirm.hbs b/back/volume/src/auth/mails/confirm.hbs index 24c45c2..fbcb9c5 100644 --- a/back/volume/src/auth/mails/confirm.hbs +++ b/back/volume/src/auth/mails/confirm.hbs @@ -8,7 +8,7 @@

Hello {{username}}!

Once you clicked on the next verify button, you will have access to the app

-
+
diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index 31a025f..899c2a1 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -51,7 +51,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { console.log('here') const channel = await this.chatService.getFullChannel(connect.ChannelId) if (channel.banned.findIndex((ban) => ban[0] === connect.UserId) !== -1) { - throw new WsException('You are banned from entering this channel') + this.server.to(socket.id).emit('failedJoin', 'You are banned from this channel') } const user = await this.userService.getFullUser(connect.UserId) if (channel.password && channel.password !== '') { @@ -59,7 +59,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { !connect.pwd || !(await bcrypt.compare(connect.pwd, channel.password)) ) { - throw new WsException('Wrong password') + this.server.to(socket.id).emit('failedJoin', 'Wrong password') } } await this.chatService.addUserToChannel(channel, user) @@ -73,20 +73,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { conUser.socket = socket.id const test = await this.connectedUserRepository.save(conUser) console.log(test) - this.server.to(socket.id).emit('messages', messages) await socket.join(channel.id.toString()) - console.log(this.server.sockets.adapter.rooms.get(channel.id.toString())) - } - - @SubscribeMessage('getMessages') - async onGetMessages (socket: Socket, connect: ConnectionDto): Promise { - const user = await this.userService.getFullUser(connect.UserId) - const channel = await this.chatService.getFullChannel(connect.ChannelId) - const messages = await this.messageService.findMessagesInChannelForUser( - channel, - user - ) this.server.to(socket.id).emit('messages', messages) + console.log(this.server.sockets.adapter.rooms.get(channel.id.toString())) } @SubscribeMessage('leaveChannel') diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index 9603727..89e3bfb 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -59,6 +59,7 @@ export class ChatService { newChannel.name = channel.name newChannel.isPrivate = channel.isPrivate newChannel.password = channel.password + console.log("New channel: ", JSON.stringify(newChannel)) } return await this.ChannelRepository.save(newChannel) } diff --git a/back/volume/src/chat/entity/channel.entity.ts b/back/volume/src/chat/entity/channel.entity.ts index 13cf6ff..0eda1f6 100644 --- a/back/volume/src/chat/entity/channel.entity.ts +++ b/back/volume/src/chat/entity/channel.entity.ts @@ -24,7 +24,7 @@ export default class Channel { @Column({ default: false }) isPrivate: boolean - @Column({ select: false, default: '' }) + @Column({ default: '' }) password: string @BeforeInsert() diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 44c915d..db7a283 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -22,7 +22,7 @@ import MatchHistory from "./components/MatchHistory.svelte"; import Friends, { addFriend } from "./components/Friends.svelte"; import Chat from "./components/Chat.svelte"; - import Channels, { formatChannelNames } from "./components/Channels.svelte"; + import Channels, { formatChannelNames, type chatMessagesType } from "./components/Channels.svelte"; import Leaderboard from "./components/Leaderboard.svelte"; import { popup } from "./components/Alert/content"; import Pong from "./components/Pong/Pong.svelte"; @@ -47,7 +47,7 @@ await formatChannelNames(channels); const channel = channels.find((c: ChannelsType) => c.name === currentChannelName); if (channel) { - chan.selectChat(channel.id); + //chan.selectChat(channel.id); // disabled as it causes a bug where joining a channel happen twice } else { alert("Failed loading channel"); } @@ -62,7 +62,7 @@ window.onpopstate = (e: PopStateEvent) => { if (e.state) { appState = e.state.appState; - void updateChat(); + void updateChat(); // why this? } }; @@ -167,8 +167,10 @@ setAppState(APPSTATE.CHANNELS); } let selectedChannel: ChannelsType; - const handleSelectChannel = (channel: ChannelsType) => { + let selectedMessages: Array; + const handleSelectChannel = (channel: ChannelsType, messages: Array) => { selectedChannel = channel; + selectedMessages = messages; setAppState(APPSTATE.CHANNELS + "#" + channel.name); }; @@ -223,6 +225,7 @@ > { } - - export interface ChannelsType { id: number; @@ -12,6 +10,11 @@ password: string; owner: User; } + export interface chatMessagesType { + id: number; + author: User; + text: string; + } import { onMount } from "svelte"; import { API_URL, store } from "../Auth"; import { socket } from "../socket"; @@ -73,13 +76,23 @@ return true; } - const joinChannel = async (id: number) => { + const joinChannel = async (channel: ChannelsType) => { console.log(channels) - socket.emit("joinChannel", { - UserId: $store.ftId, - ChannelId: id, - - }); + socket.connect(); + if (!channel.password) { + socket.emit("joinChannel", { + UserId: $store.ftId, + ChannelId: channel.id, + }); + } else { + await show_popup("Channel is protected, enter password:") + socket.emit("joinChannel", { + UserId: $store.ftId, + ChannelId: channel.id, + pwd: $content, + }); + } + console.log("Try to join channel: ", $store.ftId, channel.id, $content) }; const getChannels = async () => { @@ -101,20 +114,31 @@ //--------------------------------------------------------------------------------/ - export let onSelectChannel: (channel: ChannelsType) => void; + + export let onSelectChannel: (channel: ChannelsType, messages: Array) => void; + let channel: ChannelsType; export const selectChat = (id: number) => { console.log("channel: ", id) - getChannels().then(() => { - const channel = channels.find((c) => c.id === id); - if (channel) { - joinChannel(id); - onSelectChannel(channel); - } else { - show_popup("Did not find channel", false) - } - }); + channel = channels.find((c) => c.id === id); + if (channel) { + joinChannel(channel); + } else { + show_popup("Did not find channel", false) + } }; + socket.on("messages", (msgs: Array) => { + console.log("You are joining channel: ", channel.name) + onSelectChannel(channel, msgs); + channel = undefined; + }); + + socket.on("failedJoin", (error: string) => { + show_popup(`Failed to join channel: ${error}`, false) + channel = undefined; + }); + + const createChannel = async () => { let name: string; let password = ""; diff --git a/front/volume/src/components/Chat.svelte b/front/volume/src/components/Chat.svelte index 34feef5..368afe7 100644 --- a/front/volume/src/components/Chat.svelte +++ b/front/volume/src/components/Chat.svelte @@ -1,14 +1,9 @@ @@ -16,21 +11,17 @@ let usersInterval: ReturnType; let blockedUsers: Array = []; let chatMembers: Array = []; - let chatMessages: Array = []; export let channel: ChannelsType; + export let messages: Array = []; let newText = ""; onMount(async () => { - socket.connect(); getMembers(); usersInterval = setInterval(async () => { getMembers(); }, 1000); }); - socket.on("messages", (msgs: Array) => { - chatMessages = msgs; - }); async function getMembers() { if (!channel) return; @@ -48,7 +39,7 @@ socket.on("newMessage", (msg: chatMessagesType) => { console.log(msg) - chatMessages = [...chatMessages, msg]; + messages = [...messages, msg]; }); onDestroy(() => { @@ -301,7 +292,7 @@
- {#each chatMessages as message} + {#each messages as message}

{#if !blockedUsers.filter((user) => user.username == message.author).length}