Initial commit: Prism messenger with Expo + NestJS + GraphQL + PostgreSQL
This commit is contained in:
84
backend/src/modules/conversations/conversations.service.ts
Normal file
84
backend/src/modules/conversations/conversations.service.ts
Normal file
@ -0,0 +1,84 @@
|
||||
import { Injectable, NotFoundException, ForbiddenException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Conversation } from './entities/conversation.entity';
|
||||
import { User } from '../users/entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ConversationsService {
|
||||
constructor(
|
||||
@InjectRepository(Conversation)
|
||||
private conversationsRepository: Repository<Conversation>,
|
||||
) {}
|
||||
|
||||
async create(participants: User[], name?: string, isGroup = false): Promise<Conversation> {
|
||||
const conversation = this.conversationsRepository.create({
|
||||
participants,
|
||||
name,
|
||||
isGroup,
|
||||
});
|
||||
return this.conversationsRepository.save(conversation);
|
||||
}
|
||||
|
||||
async findAllForUser(userId: string): Promise<Conversation[]> {
|
||||
return this.conversationsRepository
|
||||
.createQueryBuilder('conversation')
|
||||
.leftJoinAndSelect('conversation.participants', 'participants')
|
||||
.leftJoinAndSelect('conversation.messages', 'messages')
|
||||
.leftJoin('conversation.participants', 'user')
|
||||
.where('user.id = :userId', { userId })
|
||||
.orderBy('messages.createdAt', 'DESC')
|
||||
.getMany();
|
||||
}
|
||||
|
||||
async findOne(id: string, userId: string): Promise<Conversation> {
|
||||
const conversation = await this.conversationsRepository
|
||||
.createQueryBuilder('conversation')
|
||||
.leftJoinAndSelect('conversation.participants', 'participants')
|
||||
.leftJoinAndSelect('conversation.messages', 'messages')
|
||||
.leftJoinAndSelect('messages.sender', 'sender')
|
||||
.where('conversation.id = :id', { id })
|
||||
.orderBy('messages.createdAt', 'ASC')
|
||||
.getOne();
|
||||
|
||||
if (!conversation) {
|
||||
throw new NotFoundException('Беседа не найдена');
|
||||
}
|
||||
|
||||
const isParticipant = conversation.participants.some(p => p.id === userId);
|
||||
if (!isParticipant) {
|
||||
throw new ForbiddenException('Вы не являетесь участником этой беседы');
|
||||
}
|
||||
|
||||
return conversation;
|
||||
}
|
||||
|
||||
async findOrCreatePrivate(user1Id: string, user2Id: string): Promise<Conversation> {
|
||||
const existingConversation = await this.conversationsRepository
|
||||
.createQueryBuilder('conversation')
|
||||
.leftJoin('conversation.participants', 'p1')
|
||||
.leftJoin('conversation.participants', 'p2')
|
||||
.where('conversation.isGroup = false')
|
||||
.andWhere('p1.id = :user1Id', { user1Id })
|
||||
.andWhere('p2.id = :user2Id', { user2Id })
|
||||
.getOne();
|
||||
|
||||
if (existingConversation) {
|
||||
return existingConversation;
|
||||
}
|
||||
|
||||
// Создаем новую приватную беседу
|
||||
return this.create([], undefined, false);
|
||||
}
|
||||
|
||||
async addParticipant(conversationId: string, userId: string, participantId: string): Promise<Conversation> {
|
||||
const conversation = await this.findOne(conversationId, userId);
|
||||
|
||||
if (!conversation.isGroup) {
|
||||
throw new ForbiddenException('Нельзя добавлять участников в приватные беседы');
|
||||
}
|
||||
|
||||
// Добавляем участника
|
||||
return conversation;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user