Spaces:
Sleeping
Sleeping
File size: 10,915 Bytes
7aa8153 | 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | # Gửi tin nhắn DM đầu tiên (First DM Message)
> Tài liệu này hướng dẫn Frontend cách gửi tin nhắn DM khi **chưa có conversation** giữa 2 ngườidùng.
---
## Tóm tắt
Trước đây, để gửi tin nhắn DM đầu tiên cho một ngườimới, FE phải gọi **2 API**:
1. `POST /dms` → tạo conversation
2. `POST /dms/:conversationId/messages` → gửi tin nhắn
Giờ đây, bạn chỉ cần **1 API duy nhất** để gửi tin nhắn đầu tiên. Hệ thống sẽ tự động tạo conversation nếu chưa có.
---
## HTTP API
### Endpoint
```
POST /dms/messages
```
### Request Headers
```
Authorization: Bearer <access_token>
Content-Type: application/json
```
### Request Body
```typescript
{
// Chọn 1 trong 2:
"recipientId": "uuid-of-user-b", // Dùng khi bắt đầu conversation mới
"conversationId": "existing-uuid", // Dùng khi conversation đã tồn tại
// Bắt buộc:
"content": "Nội dung tin nhắn"
// Tùy chọn:
"replyToId": "uuid-of-replied-message" // Reply tin nhắn khác
}
```
### Scenarios
#### Scenario A: Gửi tin nhắn đầu tiên (chưa có conversation)
```json
{
"recipientId": "550e8400-e29b-41d4-a716-446655440000",
"content": "Chào bạn!"
}
```
#### Scenario B: Gửi tin nhắn vào conversation đã có
```json
{
"conversationId": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"content": "Bạn khỏe không?"
}
```
### Response
#### Success 201 — Conversation mới được tạo
```json
{
"success": true,
"data": {
"conversationId": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"isNewConversation": true,
"conversation": {
"id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"user1_id": "user-a-uuid",
"user2_id": "550e8400-e29b-41d4-a716-446655440000",
"created_at": "2024-01-15T10:00:00Z",
"other_user": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"username": "johndoe",
"display_name": "John Doe",
"avatar_url": "https://...",
"status": "online",
"last_seen": "2024-01-15T10:05:00Z"
},
"last_message": null,
"unread_count": 0,
"is_new": true
},
"message": {
"id": "message-uuid",
"conversation_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"sender_id": "user-a-uuid",
"sender_username": "alice",
"content": "Chào bạn!",
"is_read": false,
"created_at": "2024-01-15T10:10:00Z",
"sender": {
"id": "user-a-uuid",
"username": "alice",
"display_name": "Alice",
"avatar_url": null,
"status": "online"
}
}
}
}
```
#### Success 201 — Conversation đã tồn tại
```json
{
"success": true,
"data": {
"conversationId": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"isNewConversation": false,
"message": {
"id": "message-uuid",
"conversation_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"sender_id": "user-a-uuid",
"sender_username": "alice",
"content": "Bạn khỏe không?",
"is_read": false,
"created_at": "2024-01-15T10:15:00Z",
"sender": { ... }
}
}
}
```
> **Lưu ý:** Khi `isNewConversation: false`, field `conversation` **không có** trong response.
### Error Responses
| Status | Code | Message | Nguyên nhân |
|--------|------|---------|-------------|
| 400 | `BAD_REQUEST` | Either recipientId or conversationId is required | Không cung cấp cả 2 field |
| 404 | `NOT_FOUND` | User not found | `recipientId` không tồn tại |
| 404 | `NOT_FOUND` | Conversation not found | `conversationId` không tồn tại hoặc user không thuộc conversation |
| 403 | `FORBIDDEN` | You have blocked this user or they have blocked you | 2 user đã block nhau |
| 400 | `BAD_REQUEST` | Cannot create conversation with yourself | Tự nhắn tin với chính mình |
---
## WebSocket (Real-time)
### Gửi tin nhắn qua WebSocket
```typescript
socket.emit('sendDM', {
recipientId: 'uuid-of-user-b', // Dùng cho conversation mới
// conversationId: 'existing-uuid', // Dùng cho conversation đã có
content: 'Chào bạn!',
tempId: 'client-temp-id-123' // ID tạm của FE để match response
});
```
### Listen events
#### `dmSent` — ACK ngay lập tức
```typescript
socket.on('dmSent', (data) => {
// data = { success: true, tempId: 'client-temp-id-123' }
// → Cập nhật UI: tin nhắn đang được xử lý
});
```
#### `conversationCreated` — Khi conversation mới được tạo (chỉ sender nhận)
```typescript
socket.on('conversationCreated', (data) => {
// data = {
// conversation: { id, user1_id, user2_id, other_user, ... },
// tempId: 'client-temp-id-123'
// }
// → Thêm conversation mới vào danh sách
});
```
#### `newDM` — Tin nhắn mới (cả sender và receiver đều nhận)
```typescript
socket.on('newDM', (data) => {
// data = {
// id: 'message-uuid',
// conversationId: 'conv-uuid',
// sender_id: 'user-a-uuid',
// content: 'Chào bạn!',
// tempId: 'client-temp-id-123',
// timestamp: '2024-01-15T10:10:00Z',
// ...
// }
// → Hiển thị tin nhắn trong UI
});
```
#### `error` — Khi có lỗi
```typescript
socket.on('error', (error) => {
// error = { message: '...', code: 'ERROR', tempId: '...' }
// → Hiển thị lỗi, có thể retry
});
```
---
## Flow gợi ý cho Frontend
### Flow A: Gửi tin nhắn đầu tiên qua HTTP
```typescript
async function sendFirstMessage(recipientId: string, content: string) {
const response = await fetch('/dms/messages', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ recipientId, content }),
});
const { data } = await response.json();
if (data.isNewConversation) {
// Thêm conversation mới vào sidebar/list
addConversation(data.conversation);
}
// Hiển thị tin nhắn trong chat UI
addMessage(data.message);
// Join WebSocket room để nhận real-time updates
socket.emit('joinDM', { conversationId: data.conversationId });
return data;
}
```
### Flow B: Gửi tin nhắn đầu tiên qua WebSocket
```typescript
function sendFirstMessageWS(recipientId: string, content: string) {
const tempId = generateTempId();
// Gửi qua WebSocket
socket.emit('sendDM', {
recipientId,
content,
tempId,
});
// Lắng nghe các events
socket.once('conversationCreated', (data) => {
if (data.tempId === tempId) {
addConversation(data.conversation);
// Auto join room
socket.emit('joinDM', { conversationId: data.conversation.id });
}
});
socket.once('newDM', (data) => {
if (data.tempId === tempId) {
// Replace temp message với real message
replaceTempMessage(tempId, data);
}
});
socket.once('error', (error) => {
if (error.tempId === tempId) {
showError(error.message);
markMessageFailed(tempId);
}
});
}
```
### Flow C: Gửi tin nhắn vào conversation đã có
```typescript
// HTTP
async function sendMessage(conversationId: string, content: string) {
const response = await fetch('/dms/messages', {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ conversationId, content }),
});
const { data } = await response.json();
addMessage(data.message);
}
// HOẶC WebSocket
function sendMessageWS(conversationId: string, content: string) {
socket.emit('sendDM', {
conversationId,
content,
tempId: generateTempId(),
});
}
```
---
## Backward Compatibility
| Endpoint / Event | Trạng thái |
|------------------|------------|
| `POST /dms` | ✅ Giữ nguyên |
| `POST /dms/:conversationId/messages` | ✅ Giữ nguyên |
| `POST /dms/messages` | 🆕 Mới |
| WS `sendDM` với `conversationId` | ✅ Vẫn hoạt động |
| WS `sendDM` với `recipientId` | 🆕 Mới |
> Các endpoint và events cũ vẫn hoạt động bình thường. FE có thể migrate dần sang endpoint mới.
---
## Checklist cho FE Developer
- [ ] Cập nhật API call từ 2-step (`POST /dms` + `POST /dms/:id/messages`) thành 1-step (`POST /dms/messages`)
- [ ] Handle `isNewConversation` flag để thêm conversation mới vào UI
- [ ] Lắng nghe `conversationCreated` event qua WebSocket
- [ ] Cập nhật `sendDM` payload để hỗ trợ `recipientId`
- [ ] Test race condition: 2 ngườicùng gửi tin nhắn đầu tiên cho nhau
---
## Ví dụ đầy đủ: React Component
```tsx
import { useState } from 'react';
import { socket } from './socket';
function ChatInput({ recipientId, conversationId }: {
recipientId?: string;
conversationId?: string;
}) {
const [content, setContent] = useState('');
const handleSend = async () => {
if (!content.trim()) return;
if (conversationId) {
// Conversation đã có → dùng HTTP hoặc WS
socket.emit('sendDM', { conversationId, content, tempId: Date.now() });
} else if (recipientId) {
// Conversation mới → dùng endpoint mới
const res = await fetch('/dms/messages', {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ recipientId, content }),
});
const { data } = await res.json();
if (data.isNewConversation) {
// Thêm conversation mới vào state
dispatch({ type: 'ADD_CONVERSATION', payload: data.conversation });
// Join WS room
socket.emit('joinDM', { conversationId: data.conversationId });
}
dispatch({ type: 'ADD_MESSAGE', payload: data.message });
}
setContent('');
};
return (
<div>
<input
value={content}
onChange={(e) => setContent(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleSend()}
placeholder="Nhập tin nhắn..."
/>
<button onClick={handleSend}>Gửi</button>
</div>
);
}
```
---
## Q&A
**Q: Tôi có bắt buộc phải chuyển sang endpoint mới không?**
A: Không. Endpoint cũ vẫn hoạt động. Chuyển sang khi FE ready.
**Q: Nếu tôi gửi `recipientId` nhưng conversation đã tồn tại?**
A: Hệ thống sẽ dùng conversation hiện có, không tạo mới. `isNewConversation` sẽ là `false`.
**Q: Có thể gửi cả `recipientId` và `conversationId` không?**
A: Không nên. Nếu gửi cả 2, `conversationId` sẽ được ưu tiên.
**Q: WebSocket `sendDM` với `recipientId` có tự động join room không?**
A: Không. FE vẫn phải tự `emit('joinDM', { conversationId })` sau khi nhận `conversationCreated`.
|