From e40a280d7c351256db5fbd54895386b478a2a463 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Thu, 16 Mar 2023 20:52:26 +0100 Subject: [PATCH] * DMs done! --- back/volume/src/chat/chat.controller.ts | 3 +- back/volume/src/chat/chat.service.ts | 14 +++ front/volume/src/App.svelte | 110 +++++++++++++++--------- 3 files changed, 85 insertions(+), 42 deletions(-) diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index e0773ff..a452587 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -228,7 +228,8 @@ export class ChatController { } const dms = channels.filter((channel: Channel) => { return ( - channel.name === (user.username + '&' + other.username) && + (channel.name === (user.username + '&' + other.username) || + channel.name === (other.username + '&' + user.username)) && channel.isPrivate && (channel.password === undefined || channel.password === '') ) diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index ffd1cbf..af223a0 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -36,6 +36,20 @@ export class ChatService { if (otherUser.id === user.id) { throw new BadRequestException('Cannot DM yourself') } + + const channels = await this.getChannelsForUser(user.id) + const dmAlreadyExists = channels.find((channel: Channel) => { + return ( + (channel.name === (user.username + '&' + otherUser.username) || + channel.name === (otherUser.username + '&' + user.username)) && + channel.isPrivate && + (channel.password === undefined || channel.password === '') + ) + }) + if (dmAlreadyExists !== undefined) { + throw new BadRequestException('DM already exists') + } + newChannel = this.createDM(user, otherUser) } else { newChannel = new Channel() diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index f79b317..2f2ad41 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -34,10 +34,36 @@ // Single Page Application config let appState: string = APPSTATE.HOME; + function updateChat() { + const urlSplit = appState.split("#", 2) + if (urlSplit && urlSplit.length > 1) { + console.log(urlSplit[1]) + } + if (appState.includes(APPSTATE.CHANNELS) && urlSplit.length > 1) { + const currentChannelName = appState.split("#", 2)[1]; + fetch(API_URL + "/channels", { + credentials: "include", + mode: "cors", + }).then((res) => { + res.json().then((channels) => { + const channel = channels.find((c: ChannelsType) => c.name === currentChannelName); + if (channel) { + chan.selectChat(channel.id); + } else { + alert("Failed loading channel"); + } + }); + }).catch(() => { + alert("Failed loading channel"); + }); + } + } + history.replaceState({ appState: "" }, "", "/"); window.onpopstate = (e: PopStateEvent) => { if (e.state) { appState = e.state.appState; + updateChat(); } }; @@ -49,6 +75,7 @@ if (newState === appState) return; appState = newState; history.pushState({ appState }, "", appState); + updateChat(); } onMount(() => { @@ -68,21 +95,26 @@ setAppState(APPSTATE.PROFILE_ID); } - let chan: Channels; - async function openDirectChat(event: CustomEvent) { - const DMUsername = "test"; - let DMChannel: Array = []; - const res = await fetch(API_URL + "/channels/dms/" + DMUsername, { + async function getDMs(username: string): Promise { + const response = await fetch(API_URL + "/channels/dms/" + username, { credentials: "include", mode: "cors", }); + return response; + } + + let chan: Channels; + async function openDirectChat(event: CustomEvent) { + const DMUsername = event.detail; + let DMChannel: Array = []; + const res = await getDMs($store.username) if (res.ok) { DMChannel = await res.json(); if (DMChannel.length != 0) { chan.selectChat(DMChannel[0].id); } else { console.log("Creating DMChannel: " + $store.username + "&" + DMUsername) - const response = await fetch(API_URL + "/channels", { + fetch(API_URL + "/channels", { credentials: "include", method: "POST", mode: "cors", @@ -97,25 +129,24 @@ isDM: true, otherDMedUsername: DMUsername }), - }); - if (response.ok) { - const res = await fetch(API_URL + "/channels/dms/" + DMUsername, { - credentials: "include", - mode: "cors", - }); - if (res.ok) { - DMChannel = await res.json(); + }).then(async () => { + const response = await getDMs($store.username) + if (response.ok) { + DMChannel = await response.json(); if (DMChannel.length != 0) { - console.log("Found DMChannel: ", DMChannel); chan.selectChat(DMChannel[0].id); } else { - alert("Error creating 1 DM"); + alert("Error creating DM"); } } else { - alert("Error creating 2 DM"); + alert("Error creating DM"); } - } + }).catch((error) => { + alert(error.message); + }) } + } else { + alert("Error creating DM"); } } @@ -182,29 +213,26 @@ {clickLeaderboard} /> {#if appState.includes(APPSTATE.CHANNELS)} - -
setAppState(APPSTATE.CHANNELS)} - on:keydown={() => setAppState(APPSTATE.CHANNELS)} - > - -
- -
- -
- +
setAppState(APPSTATE.CHANNELS)} + on:keydown={() => setAppState(APPSTATE.CHANNELS)} + > + +
+
+ +
{/if} {#if appState === APPSTATE.LEADERBOARD}