Browse Source

* DMs done!

master
vvandenb 2 years ago
parent
commit
e40a280d7c
  1. 3
      back/volume/src/chat/chat.controller.ts
  2. 14
      back/volume/src/chat/chat.service.ts
  3. 110
      front/volume/src/App.svelte

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

@ -228,7 +228,8 @@ export class ChatController {
} }
const dms = channels.filter((channel: Channel) => { const dms = channels.filter((channel: Channel) => {
return ( return (
channel.name === (user.username + '&' + other.username) && (channel.name === (user.username + '&' + other.username) ||
channel.name === (other.username + '&' + user.username)) &&
channel.isPrivate && channel.isPrivate &&
(channel.password === undefined || channel.password === '') (channel.password === undefined || channel.password === '')
) )

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

@ -36,6 +36,20 @@ export class ChatService {
if (otherUser.id === user.id) { if (otherUser.id === user.id) {
throw new BadRequestException('Cannot DM yourself') throw new BadRequestException('Cannot DM yourself')
} }
const channels = await this.getChannelsForUser(user.id)
const dmAlreadyExists = channels.find((channel: Channel) => {
return (
(channel.name === (user.username + '&' + otherUser.username) ||
channel.name === (otherUser.username + '&' + user.username)) &&
channel.isPrivate &&
(channel.password === undefined || channel.password === '')
)
})
if (dmAlreadyExists !== undefined) {
throw new BadRequestException('DM already exists')
}
newChannel = this.createDM(user, otherUser) newChannel = this.createDM(user, otherUser)
} else { } else {
newChannel = new Channel() newChannel = new Channel()

110
front/volume/src/App.svelte

@ -34,10 +34,36 @@
// Single Page Application config // Single Page Application config
let appState: string = APPSTATE.HOME; let appState: string = APPSTATE.HOME;
function updateChat() {
const urlSplit = appState.split("#", 2)
if (urlSplit && urlSplit.length > 1) {
console.log(urlSplit[1])
}
if (appState.includes(APPSTATE.CHANNELS) && urlSplit.length > 1) {
const currentChannelName = appState.split("#", 2)[1];
fetch(API_URL + "/channels", {
credentials: "include",
mode: "cors",
}).then((res) => {
res.json().then((channels) => {
const channel = channels.find((c: ChannelsType) => c.name === currentChannelName);
if (channel) {
chan.selectChat(channel.id);
} else {
alert("Failed loading channel");
}
});
}).catch(() => {
alert("Failed loading channel");
});
}
}
history.replaceState({ appState: "" }, "", "/"); history.replaceState({ appState: "" }, "", "/");
window.onpopstate = (e: PopStateEvent) => { window.onpopstate = (e: PopStateEvent) => {
if (e.state) { if (e.state) {
appState = e.state.appState; appState = e.state.appState;
updateChat();
} }
}; };
@ -49,6 +75,7 @@
if (newState === appState) return; if (newState === appState) return;
appState = newState; appState = newState;
history.pushState({ appState }, "", appState); history.pushState({ appState }, "", appState);
updateChat();
} }
onMount(() => { onMount(() => {
@ -68,21 +95,26 @@
setAppState(APPSTATE.PROFILE_ID); setAppState(APPSTATE.PROFILE_ID);
} }
let chan: Channels; async function getDMs(username: string): Promise<Response> {
async function openDirectChat(event: CustomEvent<string>) { const response = await fetch(API_URL + "/channels/dms/" + username, {
const DMUsername = "test";
let DMChannel: Array<ChannelsType> = [];
const res = await fetch(API_URL + "/channels/dms/" + DMUsername, {
credentials: "include", credentials: "include",
mode: "cors", mode: "cors",
}); });
return response;
}
let chan: Channels;
async function openDirectChat(event: CustomEvent<string>) {
const DMUsername = event.detail;
let DMChannel: Array<ChannelsType> = [];
const res = await getDMs($store.username)
if (res.ok) { if (res.ok) {
DMChannel = await res.json(); DMChannel = await res.json();
if (DMChannel.length != 0) { if (DMChannel.length != 0) {
chan.selectChat(DMChannel[0].id); chan.selectChat(DMChannel[0].id);
} else { } else {
console.log("Creating DMChannel: " + $store.username + "&" + DMUsername) console.log("Creating DMChannel: " + $store.username + "&" + DMUsername)
const response = await fetch(API_URL + "/channels", { fetch(API_URL + "/channels", {
credentials: "include", credentials: "include",
method: "POST", method: "POST",
mode: "cors", mode: "cors",
@ -97,25 +129,24 @@
isDM: true, isDM: true,
otherDMedUsername: DMUsername otherDMedUsername: DMUsername
}), }),
}); }).then(async () => {
if (response.ok) { const response = await getDMs($store.username)
const res = await fetch(API_URL + "/channels/dms/" + DMUsername, { if (response.ok) {
credentials: "include", DMChannel = await response.json();
mode: "cors",
});
if (res.ok) {
DMChannel = await res.json();
if (DMChannel.length != 0) { if (DMChannel.length != 0) {
console.log("Found DMChannel: ", DMChannel);
chan.selectChat(DMChannel[0].id); chan.selectChat(DMChannel[0].id);
} else { } else {
alert("Error creating 1 DM"); alert("Error creating DM");
} }
} else { } else {
alert("Error creating 2 DM"); alert("Error creating DM");
} }
} }).catch((error) => {
alert(error.message);
})
} }
} else {
alert("Error creating DM");
} }
} }
@ -182,29 +213,26 @@
{clickLeaderboard} {clickLeaderboard}
/> />
{#if appState.includes(APPSTATE.CHANNELS)} {#if appState.includes(APPSTATE.CHANNELS)}
<!-- {#if appState.replace(APPSTATE.CHANNELS, "") !== ""} --> <div
<div class="{appState.replace(APPSTATE.CHANNELS, "") === "" ? 'hidden' : ''}"
class="{appState.replace(APPSTATE.CHANNELS, "") === "" ? 'hidden' : ''}" on:click={() => setAppState(APPSTATE.CHANNELS)}
on:click={() => setAppState(APPSTATE.CHANNELS)} on:keydown={() => setAppState(APPSTATE.CHANNELS)}
on:keydown={() => setAppState(APPSTATE.CHANNELS)} >
> <Chat
<Chat channel={selectedChannel}
channel={selectedChannel} on:view-profile={openIdProfile}
on:view-profile={openIdProfile} on:add-friend={addFriend}
on:add-friend={addFriend} on:invite-to-game={pong.inviteToGame}
on:invite-to-game={pong.inviteToGame} on:send-message={openDirectChat}
on:send-message={openDirectChat} />
/> </div>
</div> <div
<!-- {:else} --> class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}"
<div on:click={resetAppState}
class="{appState.replace(APPSTATE.CHANNELS, "") !== "" ? 'hidden' : ''}" on:keydown={resetAppState}
on:click={resetAppState} >
on:keydown={resetAppState} <Channels bind:this={chan} onSelectChannel={handleSelectChannel} />
> </div>
<Channels bind:this={chan} onSelectChannel={handleSelectChannel} />
</div>
<!-- {/if} -->
{/if} {/if}
{#if appState === APPSTATE.LEADERBOARD} {#if appState === APPSTATE.LEADERBOARD}
<div on:click={resetAppState} on:keydown={resetAppState}> <div on:click={resetAppState} on:keydown={resetAppState}>

Loading…
Cancel
Save