From d50a8011eb10c57c66f781570ef5a61f99e29a95 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Thu, 16 Mar 2023 17:33:32 +0100 Subject: [PATCH] * DM front + back WIP --- back/volume/src/chat/chat.controller.ts | 14 ++-- back/volume/src/chat/chat.service.ts | 30 ++++---- front/volume/src/App.svelte | 76 ++++++++++++++------- front/volume/src/components/Channels.svelte | 19 +++--- 4 files changed, 88 insertions(+), 51 deletions(-) diff --git a/back/volume/src/chat/chat.controller.ts b/back/volume/src/chat/chat.controller.ts index f91b1b8..bfb71cd 100644 --- a/back/volume/src/chat/chat.controller.ts +++ b/back/volume/src/chat/chat.controller.ts @@ -214,22 +214,26 @@ export class ChatController { return await this.channelService.getChannelsForUser(+profile.id) } - @Get('dms/:otherId') + @Get('dms/:otherName') async getDMsForUser ( @Profile42() profile: Profile, @Param('otherName') otherName: string ): Promise { + const user = await this.usersService.findUser(profile.fdId) const other = await this.usersService.findUserByName(otherName) - const otherId = other.ftId const channels = await this.channelService.getChannelsForUser(+profile.id) - return channels.filter((channel: Channel) => { + if (user === null) { + throw new BadRequestException('User not found') + } + const dms = channels.filter((channel: Channel) => { return ( - channel.users?.some((ch) => ch.id === otherId) && + channel.name === (user.username + '&' + other.username) && channel.isPrivate && - channel.password === '' + (channel.password === undefined || channel.password === '') ) }) + return dms } @Get(':id/leave') diff --git a/back/volume/src/chat/chat.service.ts b/back/volume/src/chat/chat.service.ts index 3d52573..0f90e6a 100644 --- a/back/volume/src/chat/chat.service.ts +++ b/back/volume/src/chat/chat.service.ts @@ -1,4 +1,4 @@ -import { Injectable, NotFoundException } from '@nestjs/common' +import { BadRequestException, Injectable } from '@nestjs/common' import { InjectRepository } from '@nestjs/typeorm' import { Repository } from 'typeorm' @@ -20,7 +20,7 @@ export class ChatService { async createChannel (channel: CreateChannelDto): Promise { const user: User | null = await this.usersService.findUser(channel.owner) if (user == null) { - throw new NotFoundException(`User #${channel.owner} not found`) + throw new BadRequestException(`User #${channel.owner} not found`) } let newChannel: Channel @@ -29,10 +29,13 @@ export class ChatService { channel.otherDMedUsername ) if (otherUser == null) { - throw new NotFoundException( + throw new BadRequestException( `User #${channel.otherDMedUsername} not found` ) } + if (otherUser.id === user.id) { + throw new BadRequestException('Cannot DM yourself') + } newChannel = this.createDM(user, otherUser) } else { newChannel = new Channel() @@ -50,13 +53,10 @@ export class ChatService { const newDM = new Channel() newDM.isPrivate = true newDM.password = '' - newDM.owner = user newDM.users = [user, otherUser] newDM.admins = [] - newDM.name = user.username + ' & ' + otherUser.username - newDM.isPrivate = true - newDM.password = '' + newDM.name = user.username + '&' + otherUser.username return newDM } @@ -65,7 +65,7 @@ export class ChatService { id }) if (channel === null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } channel.password = password await this.ChannelRepository.save(channel) @@ -110,7 +110,7 @@ export class ChatService { async getChannel (id: number): Promise { const channel = await this.ChannelRepository.findOneBy({ id }) if (channel == null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } return channel } @@ -122,7 +122,7 @@ export class ChatService { }) if (channel == null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } return channel } @@ -145,7 +145,7 @@ export class ChatService { relations: { owner: true } }) if (channel === null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } return channel.owner.ftId === userId } @@ -156,7 +156,7 @@ export class ChatService { relations: { admins: true } }) if (channel === null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } return channel.admins.findIndex((user) => user.ftId === userId) !== -1 } @@ -167,7 +167,7 @@ export class ChatService { relations: { users: true } }) if (channel === null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } return channel.users.findIndex((user) => user.ftId === userId) !== -1 } @@ -178,7 +178,7 @@ export class ChatService { relations: { banned: true } }) if (channel === null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } return channel.banned.findIndex((user) => user.ftId === userId) !== -1 } @@ -188,7 +188,7 @@ export class ChatService { where: { id } }) if (channel === null) { - throw new NotFoundException(`Channel #${id} not found`) + throw new BadRequestException(`Channel #${id} not found`) } const mutation: number[] | undefined = channel.muted.find( diff --git a/front/volume/src/App.svelte b/front/volume/src/App.svelte index 987aef8..f79b317 100644 --- a/front/volume/src/App.svelte +++ b/front/volume/src/App.svelte @@ -30,7 +30,6 @@ import { store, getUser, login, verify } from "./Auth"; import FakeLogin from "./FakeLogin.svelte"; - // import { channel } from "diagnostics_channel"; // Single Page Application config let appState: string = APPSTATE.HOME; @@ -59,15 +58,6 @@ getUser(); }, 15000); - let DMChannel: Array = []; - onMount(async () => { - const res = await fetch(API_URL + "/channels/dms/" + DMUsername, { - credentials: "include", - mode: "cors", - }); - if (res.ok) DMChannel = await res.json(); - }); - function clickProfile() { setAppState(APPSTATE.PROFILE); } @@ -79,15 +69,54 @@ } let chan: Channels; - let DMUsername: string = ""; async function openDirectChat(event: CustomEvent) { - // pass profile to backend - // backend looks for a chat and if it doesn't exist it creates it - // backend send chat id to front end - // front opens chat with selectChat() - DMUsername = event.detail; - console.log(chan); - chan.selectChat(DMChannel[0].id); + const DMUsername = "test"; + let DMChannel: Array = []; + const res = await fetch(API_URL + "/channels/dms/" + DMUsername, { + credentials: "include", + mode: "cors", + }); + 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", { + credentials: "include", + method: "POST", + mode: "cors", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + name: $store.username + "&" + DMUsername, + owner: $store.ftId, + password: "", + isPrivate: true, + 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(); + if (DMChannel.length != 0) { + console.log("Found DMChannel: ", DMChannel); + chan.selectChat(DMChannel[0].id); + } else { + alert("Error creating 1 DM"); + } + } else { + alert("Error creating 2 DM"); + } + } + } + } } async function clickHistory() { @@ -155,7 +184,7 @@ {#if appState.includes(APPSTATE.CHANNELS)}
setAppState(APPSTATE.CHANNELS)} on:keydown={() => setAppState(APPSTATE.CHANNELS)} > @@ -164,14 +193,15 @@ on:view-profile={openIdProfile} on:add-friend={addFriend} on:invite-to-game={pong.inviteToGame} - on:send-message={openDirectChat} + on:send-message={openDirectChat} />
+ class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}" + on:click={resetAppState} + on:keydown={resetAppState} + >
diff --git a/front/volume/src/components/Channels.svelte b/front/volume/src/components/Channels.svelte index 2c38120..454785a 100644 --- a/front/volume/src/components/Channels.svelte +++ b/front/volume/src/components/Channels.svelte @@ -32,21 +32,24 @@ }; let channels: Array = []; - onMount(async () => { + onMount(async () => { getChannels() - }); + }); //--------------------------------------------------------------------------------/ export let onSelectChannel: (channel: ChannelsType) => void; export const selectChat = (id: number) => { - console.log(id); - const channel = channels.find((c) => c.id === id); - if (channel) { - joinChannel(id); - console.log("joined a channel") + getChannels().then(() => { + const channel = channels.find((c) => c.id === id); + if (channel) { + joinChannel(id); + console.log("joined a channel") onSelectChannel(channel); - } + } else { + alert("Did not find channel"); + } + }); };