Browse Source

* DM front + back WIP

master
vvandenb 2 years ago
parent
commit
7c72a8b0ec
  1. 14
      back/volume/src/chat/chat.controller.ts
  2. 28
      back/volume/src/chat/chat.service.ts
  3. 66
      front/volume/src/App.svelte
  4. 5
      front/volume/src/components/Channels.svelte
  5. 6
      front/volume/src/components/Chat.svelte

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

@ -214,22 +214,26 @@ export class ChatController {
return await this.channelService.getChannelsForUser(+profile.id) return await this.channelService.getChannelsForUser(+profile.id)
} }
@Get('dms/:otherId') @Get('dms/:otherName')
async getDMsForUser ( async getDMsForUser (
@Profile42() profile: Profile, @Profile42() profile: Profile,
@Param('otherName') otherName: string @Param('otherName') otherName: string
): Promise<Channel[]> { ): Promise<Channel[]> {
const user = await this.usersService.findUser(profile.fdId)
const other = await this.usersService.findUserByName(otherName) const other = await this.usersService.findUserByName(otherName)
const otherId = other.ftId
const channels = await this.channelService.getChannelsForUser(+profile.id) 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 ( return (
channel.users?.some((ch) => ch.id === otherId) && channel.name === (user.username + '&' + other.username) &&
channel.isPrivate && channel.isPrivate &&
channel.password === '' (channel.password === undefined || channel.password === '')
) )
}) })
return dms
} }
@Post() @Post()

28
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 { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm' import { Repository } from 'typeorm'
@ -20,7 +20,7 @@ export class ChatService {
async createChannel (channel: CreateChannelDto): Promise<Channel> { async createChannel (channel: CreateChannelDto): Promise<Channel> {
const user: User | null = await this.usersService.findUser(channel.owner) const user: User | null = await this.usersService.findUser(channel.owner)
if (user == null) { if (user == null) {
throw new NotFoundException(`User #${channel.owner} not found`) throw new BadRequestException(`User #${channel.owner} not found`)
} }
let newChannel: Channel let newChannel: Channel
@ -29,10 +29,13 @@ export class ChatService {
channel.otherDMedUsername channel.otherDMedUsername
) )
if (otherUser == null) { if (otherUser == null) {
throw new NotFoundException( throw new BadRequestException(
`User #${channel.otherDMedUsername} not found` `User #${channel.otherDMedUsername} not found`
) )
} }
if (otherUser.id === user.id) {
throw new BadRequestException('Cannot DM yourself')
}
newChannel = this.createDM(user, otherUser) newChannel = this.createDM(user, otherUser)
} else { } else {
newChannel = new Channel() newChannel = new Channel()
@ -50,13 +53,10 @@ export class ChatService {
const newDM = new Channel() const newDM = new Channel()
newDM.isPrivate = true newDM.isPrivate = true
newDM.password = '' newDM.password = ''
newDM.owner = user newDM.owner = user
newDM.users = [user, otherUser] newDM.users = [user, otherUser]
newDM.admins = [] newDM.admins = []
newDM.name = user.username + '&' + otherUser.username newDM.name = user.username + '&' + otherUser.username
newDM.isPrivate = true
newDM.password = ''
return newDM return newDM
} }
@ -65,7 +65,7 @@ export class ChatService {
id id
}) })
if (channel === null) { if (channel === null) {
throw new NotFoundException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
channel.password = password channel.password = password
await this.ChannelRepository.save(channel) await this.ChannelRepository.save(channel)
@ -109,7 +109,7 @@ export class ChatService {
async getChannel (id: number): Promise<Channel> { async getChannel (id: number): Promise<Channel> {
const channel = await this.ChannelRepository.findOneBy({ id }) const channel = await this.ChannelRepository.findOneBy({ id })
if (channel == null) { if (channel == null) {
throw new NotFoundException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
return channel return channel
} }
@ -120,7 +120,7 @@ export class ChatService {
relations: ['users', 'admins', 'banned', 'owner'] relations: ['users', 'admins', 'banned', 'owner']
}) })
if (channel == null) { if (channel == null) {
throw new NotFoundException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
return channel return channel
} }
@ -143,7 +143,7 @@ export class ChatService {
relations: { owner: true } relations: { owner: true }
}) })
if (channel === null) { if (channel === null) {
throw new NotFoundException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
return channel.owner.ftId === userId return channel.owner.ftId === userId
} }
@ -154,7 +154,7 @@ export class ChatService {
relations: { admins: true } relations: { admins: true }
}) })
if (channel === null) { 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 return channel.admins.findIndex((user) => user.ftId === userId) !== -1
} }
@ -165,7 +165,7 @@ export class ChatService {
relations: { users: true } relations: { users: true }
}) })
if (channel === null) { 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 return channel.users.findIndex((user) => user.ftId === userId) !== -1
} }
@ -176,7 +176,7 @@ export class ChatService {
relations: { banned: true } relations: { banned: true }
}) })
if (channel === null) { 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 return channel.banned.findIndex((user) => user.ftId === userId) !== -1
} }
@ -186,7 +186,7 @@ export class ChatService {
where: { id } where: { id }
}) })
if (channel === null) { if (channel === null) {
throw new NotFoundException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }
const mutation: number[] | undefined = channel.muted.find( const mutation: number[] | undefined = channel.muted.find(

66
front/volume/src/App.svelte

@ -30,7 +30,6 @@
import { store, getUser, login, verify } from "./Auth"; import { store, getUser, login, verify } from "./Auth";
import FakeLogin from "./FakeLogin.svelte"; import FakeLogin from "./FakeLogin.svelte";
// import { channel } from "diagnostics_channel";
// Single Page Application config // Single Page Application config
let appState: string = APPSTATE.HOME; let appState: string = APPSTATE.HOME;
@ -59,15 +58,6 @@
getUser(); getUser();
}, 15000); }, 15000);
let DMChannel: Array<ChannelsType> = [];
onMount(async () => {
const res = await fetch(API_URL + "/channels/dms/" + DMUsername, {
credentials: "include",
mode: "cors",
});
if (res.ok) DMChannel = await res.json();
});
function clickProfile() { function clickProfile() {
setAppState(APPSTATE.PROFILE); setAppState(APPSTATE.PROFILE);
} }
@ -79,15 +69,54 @@
} }
let chan: Channels; let chan: Channels;
let DMUsername: string = "";
async function openDirectChat(event: CustomEvent<string>) { async function openDirectChat(event: CustomEvent<string>) {
// pass profile to backend const DMUsername = "test";
// backend looks for a chat and if it doesn't exist it creates it let DMChannel: Array<ChannelsType> = [];
// backend send chat id to front end const res = await fetch(API_URL + "/channels/dms/" + DMUsername, {
// front opens chat with selectChat() credentials: "include",
DMUsername = event.detail; mode: "cors",
console.log(chan); });
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); chan.selectChat(DMChannel[0].id);
} else {
alert("Error creating 1 DM");
}
} else {
alert("Error creating 2 DM");
}
}
}
}
} }
async function clickHistory() { async function clickHistory() {
@ -171,7 +200,8 @@
<div <div
class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}" class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}"
on:click={resetAppState} on:click={resetAppState}
on:keydown={resetAppState}> on:keydown={resetAppState}
>
<Channels bind:this={chan} onSelectChannel={handleSelectChannel} /> <Channels bind:this={chan} onSelectChannel={handleSelectChannel} />
</div> </div>
<!-- {/if} --> <!-- {/if} -->

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

@ -40,12 +40,15 @@
export let onSelectChannel: (channel: ChannelsType) => void; export let onSelectChannel: (channel: ChannelsType) => void;
export const selectChat = (id: number) => { export const selectChat = (id: number) => {
console.log(id); getChannels().then(() => {
const channel = channels.find((c) => c.id === id); const channel = channels.find((c) => c.id === id);
if (channel) { if (channel) {
joinChannel(id); joinChannel(id);
onSelectChannel(channel); onSelectChannel(channel);
} else {
alert("Did not find channel");
} }
});
}; };
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/

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

@ -33,10 +33,10 @@
}); });
onDestroy(() => { onDestroy(() => {
io.emit("LeaveChanel", async (response) => { socket.emit("LeaveChanel", async (response) => {
console.log(response.status); console.log(response.status);
}); });
io.disconnect(); socket.disconnect();
}); });
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/
@ -380,7 +380,7 @@
} }
.chat { .chat {
background-color: #5f5e5e; background-color: #343a40;
border: 1px solid #dedede; border: 1px solid #dedede;
border-radius: 5px; border-radius: 5px;
padding: 1rem; padding: 1rem;

Loading…
Cancel
Save