nicolas-arnaud
2 years ago
10 changed files with 171 additions and 105 deletions
@ -1,23 +1,26 @@ |
|||
import { Module } from '@nestjs/common' |
|||
import { forwardRef, Module } from '@nestjs/common' |
|||
import { TypeOrmModule } from '@nestjs/typeorm' |
|||
|
|||
import { AuthModule } from 'src/auth/auth.module' |
|||
import { UsersModule } from 'src/users/users.module' |
|||
// import { ChatGateway } from './chat.gateway'
|
|||
import { ChatGateway } from './chat.gateway' |
|||
import { ChatController } from './chat.controller' |
|||
import { ChannelService } from './chat.service' |
|||
import { ChatService } from './chat.service' |
|||
import { MessageService } from './message.service' |
|||
|
|||
import Channel from './entity/channel.entity' |
|||
import Message from './entity/message.entity' |
|||
import ConnectedUser from './entity/connection.entity' |
|||
|
|||
@Module({ |
|||
imports: [ |
|||
AuthModule, |
|||
UsersModule, |
|||
TypeOrmModule.forFeature([Channel]), |
|||
TypeOrmModule.forFeature([Message]) |
|||
AuthModule, |
|||
TypeOrmModule.forFeature([Channel, Message, ConnectedUser]), |
|||
], |
|||
controllers: [ChatController], |
|||
providers: [ChannelService] |
|||
providers: [ChatService, ChatGateway, MessageService], |
|||
exports: [ChatService] |
|||
|
|||
}) |
|||
export class ChatModule {} |
|||
|
@ -1,16 +1,17 @@ |
|||
import { Column, Entity, OneToOne } from 'typeorm' |
|||
import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from 'typeorm' |
|||
|
|||
import Channel from './channel.entity' |
|||
import User from 'src/users/entity/user.entity' |
|||
|
|||
@Entity() |
|||
export class connectedUser { |
|||
export default class ConnectedUser { |
|||
@OneToOne(() => User) |
|||
user: User |
|||
|
|||
@OneToOne(() => Channel, (channel) => channel.id) |
|||
@OneToOne(() => Channel) |
|||
@JoinColumn() |
|||
channel: Channel |
|||
|
|||
@Column() |
|||
@PrimaryGeneratedColumn() |
|||
socket: string |
|||
} |
|||
|
@ -0,0 +1,43 @@ |
|||
import { Injectable } from '@nestjs/common'; |
|||
import { InjectRepository } from '@nestjs/typeorm'; |
|||
import { Repository } from 'typeorm'; |
|||
import { ChatService } from './chat.service'; |
|||
import { UsersService } from 'src/users/users.service'; |
|||
|
|||
import { type CreateMessageDto } from './dto/create-message.dto'; |
|||
import User from 'src/users/entity/user.entity'; |
|||
import Channel from './entity/channel.entity'; |
|||
import Message from './entity/message.entity'; |
|||
|
|||
@Injectable() |
|||
export class MessageService { |
|||
constructor( |
|||
@InjectRepository(Message) |
|||
private readonly MessageRepository: Repository<Message>, |
|||
private readonly channelService: ChatService, |
|||
private readonly usersService: UsersService, |
|||
) {} |
|||
|
|||
async createMessage(message: CreateMessageDto): Promise<Message> { |
|||
const msg = new Message(); |
|||
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), |
|||
); |
|||
} |
|||
|
|||
async findMessagesInChannelForUser( |
|||
channel: Channel, |
|||
user: User, |
|||
): Promise<Message[]> { |
|||
return await this.MessageRepository.createQueryBuilder('message') |
|||
.where('message.channel = :chan', { chan: channel }) |
|||
.andWhere('message.author NOT IN (:...blocked)', { |
|||
blocked: user.blocked, |
|||
}) |
|||
.getMany(); |
|||
} |
|||
} |
Loading…
Reference in new issue