From 7ae9e2ba788a815e8f20d2ec84a82b0abd85278e Mon Sep 17 00:00:00 2001 From: kinou-p Date: Sat, 17 Jun 2023 23:18:18 +0200 Subject: [PATCH] merge qrcode --- containers/api/src/chat/chat.service.ts | 177 ++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 containers/api/src/chat/chat.service.ts diff --git a/containers/api/src/chat/chat.service.ts b/containers/api/src/chat/chat.service.ts new file mode 100644 index 00000000..ef81132e --- /dev/null +++ b/containers/api/src/chat/chat.service.ts @@ -0,0 +1,177 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* chat.service.ts :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/17 01:00:25 by apommier #+# #+# */ +/* Updated: 2023/06/17 17:31:11 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Conv } from '../model/chat.entity'; +import { Message } from '../model/chat.entity'; + +import * as bcrypt from 'bcrypt'; + +import { ArrayContains } from "typeorm" +import { query } from 'express'; +import { InitializeOnPreviewAllowlist } from '@nestjs/core'; + + +@Injectable() +export class ChatService { + constructor(@InjectRepository(Conv) private chatRepository: Repository, + @InjectRepository(Message) private messageRepository: Repository, + ) {} + + async save(conv: Conv): Promise { + return await this.chatRepository.save(conv); +} + +async findAll(): Promise { + return await this.chatRepository.find(); +} + + async createConv(conv: Conv): Promise { + return await this.chatRepository.save(conv); + } + + async getConv(username: string): Promise{ + username = "sadjigui" + const convs = await this.chatRepository.query("SELECT * FROM \"conv\" WHERE $1 = ANY (ARRAY[members]);", [username]) + console.log(`convs= ${convs}`) + return convs; +} + +async findConv(number: number){ + // username = "apommier" + console.log(`fincConv; ${number}`) + const conv = await this.chatRepository.findOneBy({id: number}) + return conv; +} + + async createMessage(message: Message, username: string): Promise { + const conv = await this.findConv(message.convid); + if (conv.banned.find(item => item === username)) + return ; + if (conv.muted.find(item => item === username)) + return ; + return await this.messageRepository.save(message); + } + + async isAllowed(convId: number, username: string) { + const conv = await this.findConv(convId); + if (conv.banned.find(item => item === username)) + return (0); + if (conv.muted.find(item => item === username)) + return (0); + return (1); + } + + async getMessages(convId: number): Promise { + const convs = await this.chatRepository + .query("SELECT * FROM \"message\" WHERE $1 = message.convid;", [convId]) + + return (convs) +} + +async banUser(convId: number, username: string) { + const conv = await this.findConv(convId); + + conv.banned = conv.banned || []; + if (conv.banned.find(item => item === username)) + return (1); + conv.banned.push(username); + this.save(conv); +} + +async inviteUser(convId: number, username: string) { + // const conv = await this.findConv(convId); + // this.save(conv); + + //find user + //add in chanInvite chanID + //save user +} + + + +async setPassword(convId: number, password: string) { + //verify is user is admin ? + const conv = await this.findConv(convId); + const saltRounds = 10; + const hashedPassword = await bcrypt.hash(password, saltRounds); + // return hashedPassword; + conv.password = hashedPassword + this.save(conv); +} + +async verifyPassword(convId: number, password: string) { + //verify is user is admin ? + const conv = await this.findConv(convId); + return await bcrypt.compare(password, conv.password); + + // conv.password = password +} + +async muteUser(convId: number, username: string) { + const conv = await this.findConv(convId); + + conv.muted = conv.muted || []; + if (conv.muted.find(item => item === username)) + return (1); + conv.muted.push(username); + this.save(conv); +} + +async setAdmin(convId: number, username: string) { + const conv = await this.findConv(convId); + + conv.admin = conv.admin || []; + if (conv.admin.find(item => item === username)) + return (1); + conv.admin.push(username); + this.save(conv); +} + +async isAdmin(convId: number, username: string) { + const conv = await this.findConv(convId); + + conv.admin = conv.admin || []; + if (conv.admin.find(item => item === username)) + return (1); + console.log("nope"); + return (0); +} + +async setPrivate(convId: number) { + const conv = await this.findConv(convId); + if (conv.private === true) + conv.private = false; + else + conv.private = true; + this.save(conv); +} + +async setName(convId: number, name: string) { + const conv = await this.findConv(convId); + conv.name = name; + this.save(conv); +} + +async joinChannel(convId: number, username: string) { + const conv = await this.findConv(convId); + conv.members = conv.members || []; + if (conv.members.find(item => item === username)) + return ; + conv.members.push(username); + // conv.name = name; + this.save(conv); +} + +}