Browse Source

* DM front + back WIP

master
vvandenb 2 years ago
committed by vvandenb
parent
commit
d50a8011eb
  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

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
}
@Get(':id/leave')

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 { 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 = ''
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<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
}
@ -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(

66
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);
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() {
@ -171,7 +200,8 @@
<div
class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}"
on:click={resetAppState}
on:keydown={resetAppState}>
on:keydown={resetAppState}
>
<Channels bind:this={chan} onSelectChannel={handleSelectChannel} />
</div>
<!-- {/if} -->

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

@ -40,13 +40,16 @@
export let onSelectChannel: (channel: ChannelsType) => void;
export const selectChat = (id: number) => {
console.log(id);
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");
}
});
};

Loading…
Cancel
Save