Browse Source

chat in progress

master
nicolas-arnaud 2 years ago
parent
commit
86fab0942f
  1. 11
      back/volume/src/chat/chat.gateway.ts
  2. 1
      back/volume/src/chat/chat.service.ts
  3. 4
      back/volume/src/chat/entity/connection.entity.ts
  4. 6
      back/volume/src/chat/message.service.ts
  5. 14
      back/volume/src/users/users.controller.ts
  6. 10
      back/volume/src/users/users.service.ts
  7. 29
      front/volume/src/components/Chat.svelte

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

@ -49,15 +49,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
throw new WsException('You are banned from entering this channel') throw new WsException('You are banned from entering this channel')
} }
const user = (await this.userService.findUser(connect.UserId)) as User const user = (await this.userService.findUser(connect.UserId)) as User
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// if ( channel.password !== '' &&
// We don't need to verify if the user is already in imo
//
// if (
// channel.users.find((usr) => usr.id === user.id) == null &&
// channel.password !== ''
// ) {
if (
channel.password !== '' &&
!(await bcrypt.compare(channel.password, connect.pwd)) !(await bcrypt.compare(channel.password, connect.pwd))
) { ) {
throw new WsException('Wrong password') throw new WsException('Wrong password')
@ -88,6 +80,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@SubscribeMessage('addMessage') @SubscribeMessage('addMessage')
async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> { async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> {
console.log(JSON.stringify(message));
const channel = await this.chatService.getChannel(message.ChannelId) const channel = await this.chatService.getChannel(message.ChannelId)
if ( if (
(await this.chatService.getMuteDuration(channel.id, message.UserId)) > 0 (await this.chatService.getMuteDuration(channel.id, message.UserId)) > 0

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

@ -180,7 +180,6 @@ export class ChatService {
async getMuteDuration (id: number, userId: number): Promise<number> { async getMuteDuration (id: number, userId: number): Promise<number> {
const channel = await this.ChannelRepository.findOne({ const channel = await this.ChannelRepository.findOne({
where: { id }, where: { id },
relations: { muted: true }
}) })
if (channel === null) { if (channel === null) {
throw new NotFoundException(`Channel #${id} not found`) throw new NotFoundException(`Channel #${id} not found`)

4
back/volume/src/chat/entity/connection.entity.ts

@ -1,4 +1,4 @@
import { Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from 'typeorm' import { Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm'
import Channel from './channel.entity' import Channel from './channel.entity'
import User from 'src/users/entity/user.entity' import User from 'src/users/entity/user.entity'
@ -12,6 +12,6 @@ export default class ConnectedUser {
@JoinColumn() @JoinColumn()
channel: Channel channel: Channel
@PrimaryGeneratedColumn() @PrimaryColumn()
socket: string socket: string
} }

6
back/volume/src/chat/message.service.ts

@ -23,10 +23,7 @@ export class MessageService {
msg.text = message.text msg.text = message.text
msg.channel = await this.channelService.getChannel(message.ChannelId) msg.channel = await this.channelService.getChannel(message.ChannelId)
msg.author = (await this.usersService.findUser(message.UserId)) as User msg.author = (await this.usersService.findUser(message.UserId)) as User
msg.channel.messages.push(msg) return await this.MessageRepository.save(msg)
return await this.MessageRepository.save(
this.MessageRepository.create(msg)
)
} }
async findMessagesInChannelForUser ( async findMessagesInChannelForUser (
@ -34,6 +31,7 @@ export class MessageService {
user: User user: User
): Promise<Message[]> { ): Promise<Message[]> {
return await this.MessageRepository.createQueryBuilder('message') return await this.MessageRepository.createQueryBuilder('message')
.innerJoin('message.channel', 'channel')
.where('message.channel = :chan', { chan: channel }) .where('message.channel = :chan', { chan: channel })
.andWhere('message.author NOT IN (:...blocked)', { .andWhere('message.author NOT IN (:...blocked)', {
blocked: user.blocked blocked: user.blocked

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

@ -35,13 +35,23 @@ import { join } from 'path'
export class UsersController { export class UsersController {
constructor (private readonly usersService: UsersService) {} constructor (private readonly usersService: UsersService) {}
@Get('blocked')
@UseGuards(AuthenticatedGuard)
async getBlockedUsers (
@Profile42() profile: Profile
): Promise<User[]> {
const user = await this.usersService.getFullUser(profile.id)
if (user === null) throw new BadRequestException('User not found')
return user.blocked
}
@Get('block/:id') @Get('block/:id')
@UseGuards(AuthenticatedGuard) @UseGuards(AuthenticatedGuard)
async blockUser ( async blockUser (
@Profile42() profile: Profile, @Profile42() profile: Profile,
@Param('id') id: number @Param('id') id: number
): Promise<void> { ): Promise<void> {
const user = await this.usersService.findUser(profile.id) const user = await this.usersService.getFullUser(profile.id)
const target = await this.usersService.findUser(id) const target = await this.usersService.findUser(id)
if (user === null || target === null) { if (user === null || target === null) {
throw new BadRequestException('User not found') throw new BadRequestException('User not found')
@ -56,7 +66,7 @@ export class UsersController {
@Profile42() profile: Profile, @Profile42() profile: Profile,
@Param('id') id: number @Param('id') id: number
): Promise<void> { ): Promise<void> {
const user = await this.usersService.findUser(profile.id) const user = await this.usersService.getFullUser(profile.id)
if (user === null) throw new BadRequestException('User not found') if (user === null) throw new BadRequestException('User not found')
user.blocked = user.blocked.filter((usr: User) => { user.blocked = user.blocked.filter((usr: User) => {
return usr.id !== id return usr.id !== id

10
back/volume/src/users/users.service.ts

@ -58,6 +58,16 @@ export class UsersService {
return user return user
} }
async getFullUser (ftId: number): Promise<User> {
let user = await this.usersRepository.findOne({
where: { ftId },
relations: ['results', 'blocked', 'friends']
})
if (user === null) throw new BadRequestException('User not found.')
return user
}
async findOnlineUsers (): Promise<User[]> { async findOnlineUsers (): Promise<User[]> {
const users = await this.usersRepository.find({ const users = await this.usersRepository.find({
where: { status: 'online' } where: { status: 'online' }

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

@ -1,7 +1,7 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
export interface chatMessagesType { export interface chatMessagesType {
id: number; id: number;
author: string; author: User;
text: string; text: string;
} }
import { createEventDispatcher, onDestroy, onMount } from "svelte"; import { createEventDispatcher, onDestroy, onMount } from "svelte";
@ -18,38 +18,35 @@
export let channel: ChannelsType; export let channel: ChannelsType;
let newText = ""; let newText = "";
onMount(async () => { onMount(async () => {
let res = await fetch(API_URL + "/users/block/" + $store.ftId, { let res = await fetch(API_URL + "/users/blocked/", {
credentials: "include", credentials: "include",
mode: "cors", mode: "cors",
}); });
if (res.ok) blockedUsers = await res.json(); if (res.ok) blockedUsers = await res.json();
});
socket.on("messages", (msgs: Array<chatMessagesType>) => { socket.on("messages", (msgs: Array<chatMessagesType>) => {
chatMessages = msgs; chatMessages = msgs;
}); });
socket.on("newMessage", (msg: chatMessagesType) => { socket.on("newMessage", (msg: chatMessagesType) => {
chatMessages = [...chatMessages.slice(-5 + 1), msg]; chatMessages = [...chatMessages, msg];
}); });
onDestroy(() => { onDestroy(() => {
socket.emit("leaveChannel", channel.id, $store.ftId); socket.emit("leaveChannel");
}); })
});
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/
const sendMessage = () => { const sendMessage = () => {
if (newText !== "") { if (newText !== "") {
/*
const newMessage = {
id: chatMessages.length + 1,
author: $store.username,
text: newText,
};
*/
chatMessages = [...chatMessages.slice(-5 + 1)]; chatMessages = [...chatMessages.slice(-5 + 1)];
socket.emit("addMessage", channel.id, $store.ftId, newText); socket.emit("addMessage", {
text: newText,
UserId: $store.ftId,
ChannelId: channel.id,
})
newText = ""; newText = "";
const messagesDiv = document.querySelector(".messages"); const messagesDiv = document.querySelector(".messages");
if (messagesDiv) { if (messagesDiv) {
@ -275,8 +272,8 @@
{#if !blockedUsers.filter((user) => user.username == message.author).length} {#if !blockedUsers.filter((user) => user.username == message.author).length}
<span <span
class="message-name" class="message-name"
on:click={() => openProfile(message.author)} on:click={() => openProfile(message.author.username)}
on:keydown={() => openProfile(message.author)} on:keydown={() => openProfile(message.author.username)}
style="cursor: pointer;" style="cursor: pointer;"
> >
{message.author} {message.author}

Loading…
Cancel
Save