Browse Source

* DM front + back WIP

master
vvandenb 2 years ago
parent
commit
7c72a8b0ec
  1. 14
      back/volume/src/chat/chat.controller.ts
  2. 30
      back/volume/src/chat/chat.service.ts
  3. 76
      front/volume/src/App.svelte
  4. 19
      front/volume/src/components/Channels.svelte
  5. 8
      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)
}
@Get('dms/:otherId')
@Get('dms/:otherName')
async getDMsForUser (
@Profile42() profile: Profile,
@Param('otherName') otherName: string
): Promise<Channel[]> {
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
}
@Post()

30
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<Channel> {
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)
@ -109,7 +109,7 @@ export class ChatService {
async getChannel (id: number): Promise<Channel> {
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
}
@ -120,7 +120,7 @@ export class ChatService {
relations: ['users', 'admins', 'banned', 'owner']
})
if (channel == null) {
throw new NotFoundException(`Channel #${id} not found`)
throw new BadRequestException(`Channel #${id} not found`)
}
return channel
}
@ -143,7 +143,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
}
@ -154,7 +154,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
}
@ -165,7 +165,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
}
@ -176,7 +176,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
}
@ -186,7 +186,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(

76
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<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() {
setAppState(APPSTATE.PROFILE);
}
@ -79,15 +69,54 @@
}
let chan: Channels;
let DMUsername: string = "";
async function openDirectChat(event: CustomEvent<string>) {
// 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<ChannelsType> = [];
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)}
<!-- {#if appState.replace(APPSTATE.CHANNELS, "") !== ""} -->
<div
class="{appState.replace(APPSTATE.CHANNELS, "") === "" ? 'hidden' : ''}"
class="{appState.replace(APPSTATE.CHANNELS, "") === "" ? 'hidden' : ''}"
on:click={() => 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}
/>
</div>
<!-- {:else} -->
<div
class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}"
on:click={resetAppState}
on:keydown={resetAppState}>
class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}"
on:click={resetAppState}
on:keydown={resetAppState}
>
<Channels bind:this={chan} onSelectChannel={handleSelectChannel} />
</div>
<!-- {/if} -->

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

@ -32,20 +32,23 @@
};
let channels: Array<ChannelsType> = [];
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);
onSelectChannel(channel);
}
getChannels().then(() => {
const channel = channels.find((c) => c.id === id);
if (channel) {
joinChannel(id);
onSelectChannel(channel);
} else {
alert("Did not find channel");
}
});
};
//--------------------------------------------------------------------------------/

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

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

Loading…
Cancel
Save