File size: 6,078 Bytes
639bb77
 
 
 
 
 
 
 
 
 
4c815e5
639bb77
 
 
4c815e5
639bb77
 
 
 
 
 
7aa8153
639bb77
 
 
 
 
 
 
 
7aa8153
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c815e5
639bb77
 
4c815e5
 
 
 
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa8153
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa8153
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7aa8153
 
 
 
 
639bb77
 
 
 
 
 
7aa8153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639bb77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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;
  };
}

@ApiTags('Direct Messages')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Controller('dms')
export class DMsController {
  constructor(private readonly dmsService: DMsService) {}

  /**
   * POST /dms
   * Get or create DM conversation
   */
  @Post()
  @HttpCode(HttpStatus.CREATED)
  async createOrGetConversation(
    @Body() dto: CreateDMConversationDto,
    @Request() 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
   */
  @Get()
  @HttpCode(HttpStatus.OK)
  async getConversations(
    @Query() query: QueryDMConversationsDto,
    @Request() req: RequestWithUser,
    @Res({ 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
   */
  @Get(':conversationId/messages')
  @HttpCode(HttpStatus.OK)
  async getMessages(
    @Param('conversationId') conversationId: string,
    @Query() query: QueryDMMessagesDto,
    @Request() 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
   */
  @Post(':conversationId/messages')
  @HttpCode(HttpStatus.CREATED)
  async sendMessage(
    @Param('conversationId') conversationId: string,
    @Body() dto: SendDMDto,
    @Request() 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
   */
  @Post('messages')
  @HttpCode(HttpStatus.CREATED)
  async sendDMMessage(
    @Body() dto: SendDMMessageDto,
    @Request() 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
   */
  @Delete('messages/:messageId')
  @HttpCode(HttpStatus.OK)
  async deleteMessage(
    @Param('messageId') messageId: string,
    @Request() 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
   */
  @Post(':conversationId/read')
  @HttpCode(HttpStatus.OK)
  async markAsRead(
    @Param('conversationId') conversationId: string,
    @Request() 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
   */
  @Get('messages/:messageId/read-receipts')
  @HttpCode(HttpStatus.OK)
  async getDMMessageReadReceipts(
    @Param('messageId') messageId: string,
  ): Promise<any> {
    const latency = await this.dmsService.getDMReadLatency(messageId);
    return {
      success: true,
      data: latency,
    };
  }

  /**
   * POST /dms/block/:userId
   * Block a user
   */
  @Post('block/:userId')
  @HttpCode(HttpStatus.OK)
  async blockUser(
    @Param('userId') userId: string,
    @Request() 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
   */
  @Delete('block/:userId')
  @HttpCode(HttpStatus.OK)
  async unblockUser(
    @Param('userId') userId: string,
    @Request() 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
   */
  @Get('blocked/list')
  @HttpCode(HttpStatus.OK)
  async getBlockedUsers(@Request() req: RequestWithUser): Promise<any> {
    const blocked = await this.dmsService.getBlockedUsers(req.user.id);
    return {
      success: true,
      data: blocked,
    };
  }
}