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')
}
const user = (await this.userService.findUser(connect.UserId)) as User
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// 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 !== '' &&
if ( channel.password !== '' &&
!(await bcrypt.compare(channel.password, connect.pwd))
) {
throw new WsException('Wrong password')
@ -88,6 +80,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
@SubscribeMessage('addMessage')
async onAddMessage (socket: Socket, message: CreateMessageDto): Promise<void> {
console.log(JSON.stringify(message));
const channel = await this.chatService.getChannel(message.ChannelId)
if (
(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> {
const channel = await this.ChannelRepository.findOne({
where: { id },
relations: { muted: true }
})
if (channel === null) {
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 User from 'src/users/entity/user.entity'
@ -12,6 +12,6 @@ export default class ConnectedUser {
@JoinColumn()
channel: Channel
@PrimaryGeneratedColumn()
@PrimaryColumn()
socket: string
}

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

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

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

@ -35,13 +35,23 @@ import { join } from 'path'
export class UsersController {
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')
@UseGuards(AuthenticatedGuard)
async blockUser (
@Profile42() profile: Profile,
@Param('id') id: number
): 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)
if (user === null || target === null) {
throw new BadRequestException('User not found')
@ -56,7 +66,7 @@ export class UsersController {
@Profile42() profile: Profile,
@Param('id') id: number
): 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')
user.blocked = user.blocked.filter((usr: User) => {
return usr.id !== id

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

@ -58,6 +58,16 @@ export class UsersService {
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[]> {
const users = await this.usersRepository.find({
where: { status: 'online' }

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

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

Loading…
Cancel
Save