Implement beautiful contacts screen with user search and instant messaging
This commit is contained in:
@ -54,21 +54,33 @@ export class ConversationsService {
|
||||
}
|
||||
|
||||
async findOrCreatePrivate(user1Id: string, user2Id: string): Promise<Conversation> {
|
||||
// Проверяем существующую приватную беседу между двумя пользователями
|
||||
const existingConversation = await this.conversationsRepository
|
||||
.createQueryBuilder('conversation')
|
||||
.leftJoin('conversation.participants', 'p1')
|
||||
.leftJoin('conversation.participants', 'p2')
|
||||
.leftJoinAndSelect('conversation.participants', 'participants')
|
||||
.where('conversation.isGroup = false')
|
||||
.andWhere('p1.id = :user1Id', { user1Id })
|
||||
.andWhere('p2.id = :user2Id', { user2Id })
|
||||
.andWhere((qb) => {
|
||||
const subQuery = qb.subQuery()
|
||||
.select('c.id')
|
||||
.from('conversations', 'c')
|
||||
.leftJoin('conversation_participants', 'cp1', 'cp1.conversationId = c.id')
|
||||
.leftJoin('conversation_participants', 'cp2', 'cp2.conversationId = c.id')
|
||||
.where('c.isGroup = false')
|
||||
.andWhere('cp1.userId = :user1Id', { user1Id })
|
||||
.andWhere('cp2.userId = :user2Id', { user2Id })
|
||||
.andWhere('(SELECT COUNT(*) FROM conversation_participants WHERE conversationId = c.id) = 2')
|
||||
.getQuery();
|
||||
return 'conversation.id IN ' + subQuery;
|
||||
})
|
||||
.getOne();
|
||||
|
||||
if (existingConversation) {
|
||||
return existingConversation;
|
||||
}
|
||||
|
||||
// Создаем новую приватную беседу
|
||||
return this.create([], undefined, false);
|
||||
// Создаем новую приватную беседу с двумя участниками
|
||||
const participants = [{ id: user1Id }, { id: user2Id }] as any;
|
||||
return this.create(participants, undefined, false);
|
||||
}
|
||||
|
||||
async addParticipant(conversationId: string, userId: string, participantId: string): Promise<Conversation> {
|
||||
|
Reference in New Issue
Block a user