Spaces:
Running
Running
| import { | |
| Controller, | |
| Get, | |
| Post, | |
| Delete, | |
| Body, | |
| Param, | |
| Query, | |
| UseGuards, | |
| Request, | |
| Res, | |
| HttpCode, | |
| HttpStatus, | |
| } from '@nestjs/common'; | |
| import type { Response } from 'express'; | |
| import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; | |
| import { DMsService } from './dms.service'; | |
| import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard'; | |
| import { | |
| CreateDMConversationDto, | |
| SendDMDto, | |
| SendDMMessageDto, | |
| QueryDMMessagesDto, | |
| QueryDMConversationsDto, | |
| } from './dto'; | |
| interface RequestWithUser extends Request { | |
| user: { | |
| id: string; | |
| email: string; | |
| username?: string; | |
| }; | |
| } | |
| ('Direct Messages') | |
| (JwtAuthGuard) | |
| () | |
| ('dms') | |
| export class DMsController { | |
| constructor(private readonly dmsService: DMsService) {} | |
| /** | |
| * POST /dms | |
| * Get or create DM conversation | |
| */ | |
| () | |
| (HttpStatus.CREATED) | |
| async createOrGetConversation( | |
| () dto: CreateDMConversationDto, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const conversation = await this.dmsService.getOrCreateConversation( | |
| req.user.id, | |
| dto, | |
| ); | |
| return { | |
| success: true, | |
| data: conversation, | |
| }; | |
| } | |
| /** | |
| * GET /dms | |
| * Get user's DM conversations | |
| */ | |
| () | |
| (HttpStatus.OK) | |
| async getConversations( | |
| () query: QueryDMConversationsDto, | |
| () req: RequestWithUser, | |
| ({ passthrough: true }) res: Response, | |
| ): Promise<any> { | |
| const result = await this.dmsService.getConversations(req.user.id, query); | |
| // Cache 30 seconds on client side | |
| res.setHeader('Cache-Control', 'private, max-age=30'); | |
| return { | |
| success: true, | |
| data: result.conversations, | |
| meta: { | |
| total: result.total, | |
| hasMore: result.hasMore, | |
| page: query.page || 1, | |
| limit: query.limit || 20, | |
| }, | |
| }; | |
| } | |
| /** | |
| * GET /dms/:conversationId/messages | |
| * Get messages in a DM conversation | |
| */ | |
| (':conversationId/messages') | |
| (HttpStatus.OK) | |
| async getMessages( | |
| ('conversationId') conversationId: string, | |
| () query: QueryDMMessagesDto, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const result = await this.dmsService.getMessages( | |
| conversationId, | |
| req.user.id, | |
| req.user.username || '', | |
| query, | |
| ); | |
| return { | |
| success: true, | |
| data: result.messages, | |
| meta: { | |
| total: result.total, | |
| hasMore: result.hasMore, | |
| page: query.page || 1, | |
| limit: query.limit || 20, | |
| }, | |
| }; | |
| } | |
| /** | |
| * POST /dms/:conversationId/messages | |
| * Send a message in an existing DM conversation | |
| */ | |
| (':conversationId/messages') | |
| (HttpStatus.CREATED) | |
| async sendMessage( | |
| ('conversationId') conversationId: string, | |
| () dto: SendDMDto, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const message = await this.dmsService.sendMessage( | |
| conversationId, | |
| req.user.id, | |
| dto, | |
| ); | |
| return { | |
| success: true, | |
| data: message, | |
| }; | |
| } | |
| /** | |
| * POST /dms/messages | |
| * Send a DM message (auto-creates conversation if needed) | |
| * | |
| * Use this endpoint when: | |
| * - Starting a new conversation: provide recipientId | |
| * - Sending to existing conversation: provide conversationId | |
| */ | |
| ('messages') | |
| (HttpStatus.CREATED) | |
| async sendDMMessage( | |
| () dto: SendDMMessageDto, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| const result = await this.dmsService.sendDMMessage(req.user.id, dto); | |
| return { | |
| success: true, | |
| data: result, | |
| }; | |
| } | |
| /** | |
| * DELETE /dms/messages/:messageId | |
| * Delete a DM message | |
| */ | |
| ('messages/:messageId') | |
| (HttpStatus.OK) | |
| async deleteMessage( | |
| ('messageId') messageId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| await this.dmsService.deleteMessage(messageId, req.user.id); | |
| return { | |
| success: true, | |
| message: 'Message deleted successfully', | |
| }; | |
| } | |
| /** | |
| * POST /dms/:conversationId/read | |
| * Mark all messages in conversation as read | |
| */ | |
| (':conversationId/read') | |
| (HttpStatus.OK) | |
| async markAsRead( | |
| ('conversationId') conversationId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| await this.dmsService.markAsRead( | |
| conversationId, | |
| req.user.id, | |
| req.user.username || '', | |
| ); | |
| return { | |
| success: true, | |
| message: 'Messages marked as read', | |
| }; | |
| } | |
| /** | |
| * GET /dms/messages/:messageId/read-receipts | |
| * Get DM message read receipts with timestamps and latency | |
| */ | |
| ('messages/:messageId/read-receipts') | |
| (HttpStatus.OK) | |
| async getDMMessageReadReceipts( | |
| ('messageId') messageId: string, | |
| ): Promise<any> { | |
| const latency = await this.dmsService.getDMReadLatency(messageId); | |
| return { | |
| success: true, | |
| data: latency, | |
| }; | |
| } | |
| /** | |
| * POST /dms/block/:userId | |
| * Block a user | |
| */ | |
| ('block/:userId') | |
| (HttpStatus.OK) | |
| async blockUser( | |
| ('userId') userId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| await this.dmsService.blockUser(req.user.id, userId); | |
| return { | |
| success: true, | |
| message: 'User blocked successfully', | |
| }; | |
| } | |
| /** | |
| * DELETE /dms/block/:userId | |
| * Unblock a user | |
| */ | |
| ('block/:userId') | |
| (HttpStatus.OK) | |
| async unblockUser( | |
| ('userId') userId: string, | |
| () req: RequestWithUser, | |
| ): Promise<any> { | |
| await this.dmsService.unblockUser(req.user.id, userId); | |
| return { | |
| success: true, | |
| message: 'User unblocked successfully', | |
| }; | |
| } | |
| /** | |
| * GET /dms/blocked | |
| * Get blocked users list | |
| */ | |
| ('blocked/list') | |
| (HttpStatus.OK) | |
| async getBlockedUsers(() req: RequestWithUser): Promise<any> { | |
| const blocked = await this.dmsService.getBlockedUsers(req.user.id); | |
| return { | |
| success: true, | |
| data: blocked, | |
| }; | |
| } | |
| } | |