Browse Source

added chat members fetch

master
nicolas-arnaud 2 years ago
parent
commit
ee267acda9
  1. 7
      back/volume/src/chat/chat.controller.ts
  2. 1
      back/volume/src/chat/chat.gateway.ts
  3. 4
      back/volume/src/chat/chat.service.ts
  4. 31
      front/volume/src/components/Chat.svelte
  5. 2
      front/volume/src/components/Friends.svelte
  6. 2
      front/volume/src/components/MatchHistory.svelte
  7. 2
      front/volume/src/components/Profile.svelte

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

@ -84,8 +84,10 @@ export class ChatController {
} }
@Get(':id/users') @Get(':id/users')
async getUsersOfChannel (@Param('id') id: number): Promise<User[]> { async getUsersOfChannel (@Param('id', ParseIntPipe) id: number): Promise<User[]> {
return (await this.channelService.getChannel(id)).users let users = (await this.channelService.getFullChannel(id)).users
users.forEach((u) => u.socketKey = '')
return users;
} }
@Post(':id/admin') @Post(':id/admin')
@ -252,7 +254,6 @@ export class ChatController {
await this.channelService.save(channel) await this.channelService.save(channel)
} }
@Post() @Post()
async createChannel (@Body() channel: CreateChannelDto): Promise<Channel> { async createChannel (@Body() channel: CreateChannelDto): Promise<Channel> {
return await this.channelService.createChannel(channel) return await this.channelService.createChannel(channel)

1
back/volume/src/chat/chat.gateway.ts

@ -89,7 +89,6 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
const createdMessage: Message = await this.messageService.createMessage( const createdMessage: Message = await this.messageService.createMessage(
message message
) )
console.log(createdMessage)
this.server.to(channel.id.toString()).emit('newMessage', createdMessage) this.server.to(channel.id.toString()).emit('newMessage', createdMessage)
} }

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

@ -129,12 +129,14 @@ export class ChatService {
return channel return channel
} }
// Warning: those channels users contains socketKey.
// they have to be hidden before returned from a route
// but not save them without the key.
async getFullChannel (id: number): Promise<Channel> { async getFullChannel (id: number): Promise<Channel> {
const channel = await this.ChannelRepository.findOne({ const channel = await this.ChannelRepository.findOne({
where: { id }, where: { id },
relations: ['users', 'admins', 'banned', 'owner'] relations: ['users', 'admins', 'banned', 'owner']
}) })
if (channel == null) { if (channel == null) {
throw new BadRequestException(`Channel #${id} not found`) throw new BadRequestException(`Channel #${id} not found`)
} }

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

@ -12,6 +12,7 @@
</script> </script>
<script lang="ts"> <script lang="ts">
let usersInterval: ReturnType<typeof setInterval>;
let blockedUsers: Array<User> = []; let blockedUsers: Array<User> = [];
let chatMembers: Array<User> = []; let chatMembers: Array<User> = [];
let chatMessages: Array<chatMessagesType> = []; let chatMessages: Array<chatMessagesType> = [];
@ -22,12 +23,29 @@
credentials: "include", credentials: "include",
mode: "cors", mode: "cors",
}); });
if (res.ok) blockedUsers = await res.json(); if (res.ok) {
blockedUsers = await res.json();
usersInterval = setInterval(async () => {
getMembers();
}, 1000);
}
}); });
socket.on("messages", (msgs: Array<chatMessagesType>) => { socket.on("messages", (msgs: Array<chatMessagesType>) => {
chatMessages = msgs; chatMessages = msgs;
}); });
async function getMembers() {
if (!channel) return;
const res = await fetch(`${API_URL}/channels/${channel.id}/users`, {
credentials: "include",
mode: "cors"
})
if (res.ok) {
chatMembers = await res.json();
} else alert(res.text())
}
socket.on("newMessage", (msg: chatMessagesType) => { socket.on("newMessage", (msg: chatMessagesType) => {
console.log(msg) console.log(msg)
chatMessages = [...chatMessages, msg]; chatMessages = [...chatMessages, msg];
@ -37,6 +55,7 @@
socket.emit("LeaveChanel", async (response) => { socket.emit("LeaveChanel", async (response) => {
console.log(response.status); console.log(response.status);
}); });
clearInterval(usersInterval)
socket.disconnect(); socket.disconnect();
}); });
@ -281,9 +300,7 @@
} }
} }
//--------------------------------------------------------------------------------/
</script> </script>
<div class="overlay"> <div class="overlay">
<div class="chat" on:click|stopPropagation on:keydown|stopPropagation> <div class="chat" on:click|stopPropagation on:keydown|stopPropagation>
<div class="messages"> <div class="messages">
@ -371,12 +388,8 @@
<button on:click={() => banUser(member.username)}> ban </button> <button on:click={() => banUser(member.username)}> ban </button>
<button on:click={() => kickUser(member.username)}> kick </button> <button on:click={() => kickUser(member.username)}> kick </button>
<button on:click={() => muteUser(member.username)}> mute </button> <button on:click={() => muteUser(member.username)}> mute </button>
<button on:click={() => adminUser(member.username)}> <button on:click={() => adminUser(member.username)}> promote </button>
promote <button on:click={() => removeAdminUser(member.username)}> demote </button>
</button>
<button on:click={() => removeAdminUser(member.username)}>
demote
</button>
</p> </p>
</li> </li>
{/each} {/each}

2
front/volume/src/components/Friends.svelte

@ -92,7 +92,7 @@
<div class="friends-controls"> <div class="friends-controls">
<h3>Add a friend</h3> <h3>Add a friend</h3>
<form on:submit={addFriend}> <form on:submit={addFriend}>
<input type="text" /> <input type="text" required/>
<button type="submit">Add</button> <button type="submit">Add</button>
</form> </form>
</div> </div>

2
front/volume/src/components/MatchHistory.svelte

@ -35,6 +35,7 @@
}); });
} }
} }
if (response.ok) {
let tmp = await response.json(); let tmp = await response.json();
newBatch = tmp.data.map((match: Match) => { newBatch = tmp.data.map((match: Match) => {
return { return {
@ -50,6 +51,7 @@
}); });
page++; page++;
} }
}
onMount(() => { onMount(() => {
fetchData(); fetchData();

2
front/volume/src/components/Profile.svelte

@ -111,7 +111,7 @@
class="username" class="username"
on:submit|preventDefault={handleSubmit} on:submit|preventDefault={handleSubmit}
> >
<input type="text" id="username" bind:value={user.username} /> <input type="text" id="username" bind:value={user.username} required/>
<button type="submit" class="username" form="username-form" <button type="submit" class="username" form="username-form"
>Change</button >Change</button
> >

Loading…
Cancel
Save