Add Instagram-style social features and Postman collections
هذا الالتزام موجود في:
@@ -1,8 +1,11 @@
|
||||
import { BadRequestException, ForbiddenException, Injectable, Logger, NotFoundException } from '@nestjs/common';
|
||||
import { Types } from 'mongoose';
|
||||
import { extname } from 'path';
|
||||
import { ReactionType } from '../../common/enums/reaction-type.enum';
|
||||
import { decodeOffsetCursor, encodeOffsetCursor } from '../../common/utils/cursor.util';
|
||||
import { buildPaginatedResponse } from '../../common/utils/pagination.util';
|
||||
import { resolveMongoSortDirection } from '../../common/utils/sort.util';
|
||||
import { ManagedStorageService } from '../../infrastructure/storage/managed-storage.service';
|
||||
import { NotificationsService } from '../notifications/notifications.service';
|
||||
import { UsersRepository } from '../users/users.repository';
|
||||
import { CreateConversationDto } from './dto/create-conversation.dto';
|
||||
@@ -18,6 +21,7 @@ export class ChatService {
|
||||
private readonly chatRepository: ChatRepository,
|
||||
private readonly usersRepository: UsersRepository,
|
||||
private readonly notificationsService: NotificationsService,
|
||||
private readonly storageService: ManagedStorageService,
|
||||
) {}
|
||||
|
||||
async createConversation(currentUserId: string, dto: CreateConversationDto) {
|
||||
@@ -108,8 +112,8 @@ export class ChatService {
|
||||
const sort = { createdAt: resolveMongoSortDirection(query.sortOrder) } as Record<string, 1 | -1>;
|
||||
|
||||
const [items, total] = await Promise.all([
|
||||
this.chatRepository.findMessages(conversation.id, skip, limit, sort),
|
||||
this.chatRepository.countMessages(conversation.id),
|
||||
this.chatRepository.findMessages(conversation.id, currentUserId, skip, limit, sort),
|
||||
this.chatRepository.countMessages(conversation.id, currentUserId),
|
||||
]);
|
||||
|
||||
await this.chatRepository.clearConversationUnreadForUser(conversation.id, currentUserId);
|
||||
@@ -142,12 +146,14 @@ export class ChatService {
|
||||
throw new BadRequestException('mediaUrl is required for non-text messages');
|
||||
}
|
||||
|
||||
const replyToMessageId = await this.resolveReplyToMessageId(dto.replyToMessageId, conversation.id);
|
||||
const message = await this.chatRepository.createMessage({
|
||||
conversationId: conversation.id,
|
||||
senderId: currentUserId,
|
||||
content,
|
||||
messageType,
|
||||
mediaUrl,
|
||||
replyToMessageId,
|
||||
});
|
||||
|
||||
const preview = messageType === 'text' ? content : `${messageType} message`;
|
||||
@@ -167,6 +173,36 @@ export class ChatService {
|
||||
return message;
|
||||
}
|
||||
|
||||
async sendMessageWithUpload(
|
||||
currentUserId: string,
|
||||
dto: SendMessageDto,
|
||||
file?: { mimetype?: string; size: number; buffer: Buffer; originalname?: string },
|
||||
) {
|
||||
if (!file) {
|
||||
throw new BadRequestException('mediaFile is required');
|
||||
}
|
||||
|
||||
const mediaType = this.resolveUploadedMessageType(file);
|
||||
const mediaUrl = await this.storageService.saveFile({
|
||||
folderSegments: ['chat', 'media'],
|
||||
extension: this.resolveMediaExtension(mediaType, file),
|
||||
buffer: file.buffer,
|
||||
contentType: file.mimetype,
|
||||
fileNamePrefix: 'message',
|
||||
});
|
||||
|
||||
try {
|
||||
return await this.sendMessage(currentUserId, {
|
||||
...dto,
|
||||
messageType: mediaType,
|
||||
mediaUrl,
|
||||
});
|
||||
} catch (error) {
|
||||
await this.storageService.deleteFile(mediaUrl);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async markMessageSeen(currentUserId: string, messageId: string) {
|
||||
const message = await this.chatRepository.findMessageById(messageId);
|
||||
if (!message) {
|
||||
@@ -196,6 +232,29 @@ export class ChatService {
|
||||
return updated;
|
||||
}
|
||||
|
||||
async reactToMessage(currentUserId: string, messageId: string, reactionType: ReactionType) {
|
||||
const message = await this.chatRepository.findMessageById(messageId);
|
||||
if (!message) {
|
||||
throw new NotFoundException('Message not found');
|
||||
}
|
||||
await this.assertConversationMember(currentUserId, message.conversationId.toString());
|
||||
const updated = await this.chatRepository.setMessageReaction(messageId, currentUserId, reactionType);
|
||||
if (!updated) {
|
||||
throw new NotFoundException('Message not found');
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
async deleteMessageForMe(currentUserId: string, messageId: string) {
|
||||
const message = await this.chatRepository.findMessageById(messageId);
|
||||
if (!message) {
|
||||
throw new NotFoundException('Message not found');
|
||||
}
|
||||
await this.assertConversationMember(currentUserId, message.conversationId.toString());
|
||||
await this.chatRepository.deleteMessageForUser(messageId, currentUserId);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async blockUser(currentUserId: string, targetUserId: string) {
|
||||
if (!Types.ObjectId.isValid(targetUserId)) {
|
||||
throw new BadRequestException('Invalid target user id');
|
||||
@@ -267,6 +326,65 @@ export class ChatService {
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveReplyToMessageId(replyToMessageId: string | undefined, conversationId: string) {
|
||||
if (!replyToMessageId) {
|
||||
return null;
|
||||
}
|
||||
if (!Types.ObjectId.isValid(replyToMessageId)) {
|
||||
throw new BadRequestException('Invalid replyToMessageId');
|
||||
}
|
||||
const replyMessage = await this.chatRepository.findMessageById(replyToMessageId);
|
||||
if (!replyMessage || replyMessage.conversationId.toString() !== conversationId) {
|
||||
throw new BadRequestException('Reply message must belong to the same conversation');
|
||||
}
|
||||
return replyMessage.id;
|
||||
}
|
||||
|
||||
private resolveUploadedMessageType(file: { mimetype?: string; originalname?: string }) {
|
||||
if (file.mimetype?.startsWith('image/')) {
|
||||
return 'image' as const;
|
||||
}
|
||||
if (file.mimetype?.startsWith('video/')) {
|
||||
return 'video' as const;
|
||||
}
|
||||
if (file.mimetype?.startsWith('audio/')) {
|
||||
return 'audio' as const;
|
||||
}
|
||||
const extension = extname(file.originalname ?? '').toLowerCase();
|
||||
if (['.jpg', '.jpeg', '.png', '.webp', '.gif'].includes(extension)) {
|
||||
return 'image' as const;
|
||||
}
|
||||
if (['.mp4', '.mov', '.webm', '.mkv', '.avi'].includes(extension)) {
|
||||
return 'video' as const;
|
||||
}
|
||||
if (['.mp3', '.wav', '.m4a', '.aac', '.ogg'].includes(extension)) {
|
||||
return 'audio' as const;
|
||||
}
|
||||
throw new BadRequestException('mediaFile must be image, video, or audio');
|
||||
}
|
||||
|
||||
private resolveMediaExtension(
|
||||
mediaType: 'image' | 'video' | 'audio',
|
||||
file: { mimetype?: string; originalname?: string },
|
||||
): string {
|
||||
const extension = extname(file.originalname ?? '').toLowerCase();
|
||||
const allowed = {
|
||||
image: new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif']),
|
||||
video: new Set(['.mp4', '.mov', '.webm', '.mkv', '.avi']),
|
||||
audio: new Set(['.mp3', '.wav', '.m4a', '.aac', '.ogg', '.webm']),
|
||||
}[mediaType];
|
||||
if (allowed.has(extension)) {
|
||||
return extension;
|
||||
}
|
||||
if (mediaType === 'image') {
|
||||
return file.mimetype === 'image/png' ? '.png' : file.mimetype === 'image/webp' ? '.webp' : '.jpg';
|
||||
}
|
||||
if (mediaType === 'video') {
|
||||
return file.mimetype === 'video/quicktime' ? '.mov' : file.mimetype === 'video/webm' ? '.webm' : '.mp4';
|
||||
}
|
||||
return file.mimetype === 'audio/webm' ? '.webm' : file.mimetype === 'audio/ogg' ? '.ogg' : '.mp3';
|
||||
}
|
||||
|
||||
private async dispatchMessageNotifications(
|
||||
actorId: string,
|
||||
participantIds: string[],
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم