diff --git a/back/src/chat/chat.service.ts b/back/src/chat/chat.service.ts index 6078bda..39d5af5 100644 --- a/back/src/chat/chat.service.ts +++ b/back/src/chat/chat.service.ts @@ -26,12 +26,16 @@ export class ChatService { let newChannel: Channel if (channel.isDM) { + if (channel.otherDMedUsername === undefined || channel.otherDMedUsername === null) { + throw new BadRequestException('No other user specified') + } const otherUser: User | null = await this.usersService.findUserByName( channel.otherDMedUsername ) if (otherUser == null) throw new BadRequestException(`User #${channel.otherDMedUsername} not found`) - if (user.blocked.some((usr: User) => usr.ftId === otherUser.ftId)) + if (user.blocked.some((usr: User) => usr.ftId === otherUser.ftId)) { throw new BadRequestException(`User ${otherUser.username} is blocked`) + } if (otherUser.id === user.id) throw new BadRequestException('Cannot DM yourself') const channels = await this.getChannelsForUser(user.id) @@ -55,7 +59,7 @@ export class ChatService { newChannel.isPrivate = channel.isPrivate newChannel.password = await this.hash(channel.password) newChannel.isDM = false - console.log("New channel: ", JSON.stringify(newChannel)) + console.log('New channel: ', JSON.stringify(newChannel)) } return await this.ChannelRepository.save(newChannel) } @@ -74,10 +78,10 @@ export class ChatService { async hash(password: string): Promise { if (!password) return '' - password = await bcrypt.hash( - password, - Number(process.env.HASH_SALT) - ) + password = await bcrypt.hash( + password, + Number(process.env.HASH_SALT) + ) return password } @@ -159,7 +163,7 @@ export class ChatService { await this.ChannelRepository.save(channel) } - async removeChannel(channelId: number): Promise { + async removeChannel (channelId: number): Promise { await this.ChannelRepository.remove(await this.getFullChannel(channelId)) } diff --git a/back/src/users/users.service.ts b/back/src/users/users.service.ts index d8cab42..256f3e1 100644 --- a/back/src/users/users.service.ts +++ b/back/src/users/users.service.ts @@ -32,6 +32,7 @@ export class UsersService { // WARNING: socketKey isn't removed here. it must be done before // any return from it in a route. async findUserByName (username: string): Promise { + if (username === undefined || username === null) throw new BadRequestException('No username specified.') const user = await this.usersRepository.findOne({ where: { username }, relations: { results: true } @@ -132,8 +133,8 @@ export class UsersService { } }) let r = 1 - let ret: Array = [] - for (let usr of leaderboard.filter((user) => user.matchs !== 0)) { + const ret: User[] = [] + for (const usr of leaderboard.filter((user) => user.matchs !== 0)) { usr.rank = r++ await this.usersRepository.save(usr) ret.push(usr) diff --git a/front/src/App.svelte b/front/src/App.svelte index ff95b6f..5fe3a52 100644 --- a/front/src/App.svelte +++ b/front/src/App.svelte @@ -21,33 +21,81 @@ import MatchHistory from "./components/MatchHistory.svelte"; import Friends, { addFriend } from "./components/Friends.svelte"; import Chat from "./components/Chat.svelte"; - import Channels, {openDirectChat} from "./components/Channels.svelte"; + import Channels, { formatChannelNames, getDMs } from "./components/Channels.svelte"; import Leaderboard from "./components/Leaderboard.svelte"; - import { popup } from "./components/Alert/content"; + import { popup, show_popup } from "./components/Alert/content"; import Pong from "./components/Pong/Pong.svelte"; import type { ChannelsType } from "./components/Channels.svelte"; - import { store, getUser, login, verify } from "./Auth"; + import { store, getUser, login, verify, API_URL } from "./Auth"; + import { get } from "svelte/store"; + import type { CreateChannelDto } from "./components/dtos/create-channel.dto"; - // Single Page Application config - let appState: string = APPSTATE.HOME; - history.replaceState({ appState: "" }, "", "/"); - window.onpopstate = (e: PopStateEvent) => { - if (e.state) { - appState = e.state.appState; + async function openDirectChat(event: CustomEvent) { + const DMUsername = event.detail; + let DMChannel: Array = []; + const res = await getDMs(DMUsername) + if (res && res.ok) { + DMChannel = await res.json(); + if (DMChannel.length != 0) + await formatChannelNames(DMChannel) + setAppState(APPSTATE.CHANNELS + "#" + DMChannel[0].name) + } else { + console.log("Creating DMChannel: " + get(store).username + "&" + DMUsername) + const body: CreateChannelDto = { + name: "none", + owner: get(store).ftId, + password: "", + isPrivate: true, + isDM: true, + otherDMedUsername: DMUsername + } + fetch(API_URL + "/channels", { + credentials: "include", + method: "POST", + mode: "cors", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }).then(async () => { + const response = await getDMs(DMUsername) + if (response && response.ok) { + DMChannel = await response.json(); + if (DMChannel.length != 0) { + await formatChannelNames(DMChannel) + setAppState(APPSTATE.CHANNELS + "#" + DMChannel[0].name) + } else { + show_popup("Error: Couldn't create DM.", false) + } + } else { + show_popup("Error: Couldn't create DM.", false) + } + }).catch(() => { + show_popup("Error: Couldn't create DM.", false) + }) } - }; - - function resetAppState() { - setAppState(APPSTATE.HOME); } + // Single Page Application config + let appState: string = APPSTATE.HOME; + function setAppState(newState: APPSTATE | string) { - if (newState === appState) return; + if (newState === appState) return; history.pushState({ appState: newState, prevState: appState }, "", newState); appState = newState; } + function resetAppState() { + setAppState(APPSTATE.HOME); + } + onMount(() => { + history.replaceState({ appState: "" }, "", "/"); + window.onpopstate = (e: PopStateEvent) => { + if (e.state) { + appState = e.state.appState; + } + }; getUser(); }); setInterval(() => { @@ -94,7 +142,13 @@
- + t * (2 - t), + } + } + > {#if $store === null}