Spaces:
Running
Running
File size: 16,187 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | # API Documentation — Spaces & Rooms
> Tài liệu dành cho Frontend để tích hợp API Spaces và Rooms.
---
## Base URL
```
/api
```
## Authentication
Tất cả endpoints yêu cầu header:
```http
Authorization: Bearer <access_token>
Content-Type: application/json
```
---
## 1. Spaces API (`/spaces`)
### 1.1 Lấy danh sách spaces của user
```http
GET /spaces
```
**Response:**
```json
{
"success": true,
"data": [
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Lớp Toán 12A",
"description": "Không gian học tập lớp 12A",
"icon_url": "https://...",
"owner_id": "user-uuid",
"is_private": true,
"invite_code": "abc123",
"created_at": "2026-05-01T10:00:00Z",
"updated_at": "2026-05-01T10:00:00Z"
}
]
}
```
---
### 1.2 Tạo space mới
```http
POST /spaces
```
**Request Body:**
```json
{
"name": "Lớp Toán 12A",
"description": "Không gian học tập",
"icon": "https://...",
"isPrivate": true
}
```
| Field | Type | Required | Constraints |
|-------|------|----------|-------------|
| `name` | string | ✅ | Min 2, Max 100 chars |
| `description` | string | ❌ | Max 500 chars |
| `icon` | string (URL) | ❌ | — |
| `isPrivate` | boolean | ❌ | Default: `false` |
**Response:**
```json
{
"success": true,
"data": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Lớp Toán 12A",
"description": "Không gian học tập",
"icon_url": "https://...",
"owner_id": "user-uuid",
"is_private": true,
"invite_code": "abc123",
"created_at": "2026-05-01T10:00:00Z"
}
}
```
---
### 1.3 Lấy chi tiết space
```http
GET /spaces/:spaceId
```
**Params:**
- `spaceId` (UUID) — ID của space
**Response:**
```json
{
"success": true,
"data": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Lớp Toán 12A",
"description": "Không gian học tập",
"icon_url": "https://...",
"owner_id": "user-uuid",
"is_private": true,
"invite_code": "abc123",
"created_at": "2026-05-01T10:00:00Z"
}
}
```
**Lỗi:**
- `403 Forbidden` — User không phải member của space private
- `404 Not Found` — Space không tồn tại
---
### 1.4 Cập nhật space
```http
PATCH /spaces/:spaceId
```
**Request Body:**
```json
{
"name": "Lớp Toán 12A (Updated)",
"description": "Mô tả mới",
"icon": "https://...",
"isPrivate": false
}
```
> **Lưu ý:** Chỉ owner hoặc admin mới có quyền cập nhật. Tất cả fields optional.
**Response:**
```json
{
"success": true,
"data": {
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Lớp Toán 12A (Updated)",
...
}
}
```
---
### 1.5 Xóa space
```http
DELETE /spaces/:spaceId
```
> **Lưu ý:** Chỉ owner mới có quyền xóa. Trả về `204 No Content`.
---
### 1.6 Tìm kiếm spaces công khai
```http
GET /spaces/search?q=toán
```
**Query Params:**
- `q` (string) — Từ khóa tìm kiếm (tối đa 100 ký tự)
**Response:**
```json
{
"success": true,
"data": [
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "Lớp Toán 12A",
"description": "...",
"is_private": false
}
]
}
```
---
### 1.7 Lấy danh sách rooms trong space
```http
GET /spaces/:spaceId/rooms
```
**Response:**
```json
{
"success": true,
"data": [
{
"id": "room-uuid-1",
"space_id": "space-uuid",
"name": "Thông báo",
"description": "Kênh thông báo chính",
"type": "text",
"is_private": false,
"created_at": "2026-05-01T10:00:00Z"
},
{
"id": "room-uuid-2",
"space_id": "space-uuid",
"name": "Tài liệu",
"description": "Chia sẻ tài liệu",
"type": "text",
"is_private": false,
"created_at": "2026-05-01T10:00:00Z"
}
]
}
```
---
### 1.8 Thêm member vào space
```http
POST /spaces/:spaceId/members
```
**Request Body:**
```json
{
"userId": "550e8400-e29b-41d4-a716-446655440000",
"role": "member"
}
```
| Field | Type | Required | Values |
|-------|------|----------|--------|
| `userId` | UUID | ✅ | — |
| `role` | string | ❌ | `"member"` (default), `"admin"` |
> **Lưu ý:** Chỉ owner hoặc admin mới có quyền thêm member.
**Response:**
```json
{
"success": true,
"data": {
"id": "member-id",
"space_id": "space-uuid",
"user_id": "user-uuid",
"role": "member",
"joined_at": "2026-05-01T10:00:00Z"
}
}
```
---
### 1.9 Tạo mã mờispace
```http
POST /spaces/:spaceId/invite
```
> **Lưu ý:** Chỉ owner hoặc admin mới có quyền tạo mã mờ.
**Response:**
```json
{
"success": true,
"data": {
"inviteCode": "abc123"
}
}
```
---
### 1.10 Tham gia space bằng mã mờ
```http
POST /spaces/join/:code
```
**Params:**
- `code` (string) — Mã mờ
**Response:**
```json
{
"success": true,
"data": {
"id": "space-uuid",
"name": "Lớp Toán 12A",
...
}
}
```
---
### 1.11 Rờikhỏi space
```http
POST /spaces/:spaceId/leave
```
> **Lưu ý:** Owner không thể rờikhỏi space. Trả về `204 No Content`.
---
## 2. Rooms API
### 2.1 Tạo room trong space
```http
POST /spaces/:spaceId/rooms
```
**Request Body:**
```json
{
"name": "Thảo luận",
"description": "Phòng thảo luận bài tập",
"type": "text",
"isPrivate": false
}
```
| Field | Type | Required | Values | Default |
|-------|------|----------|--------|---------|
| `name` | string | ✅ | Min 2, Max 100 chars | — |
| `description` | string | ❌ | Max 500 chars | — |
| `type` | string | ❌ | `"text"`, `"voice"` | `"text"` |
| `isPrivate` | boolean | ❌ | — | `false` |
> **Lưu ý:** Chỉ space member mới có quyền tạo room.
**Response:**
```json
{
"success": true,
"data": {
"id": "room-uuid",
"space_id": "space-uuid",
"name": "Thảo luận",
"description": "Phòng thảo luận bài tập",
"type": "text",
"is_private": false,
"created_at": "2026-05-01T10:00:00Z"
}
}
```
---
### 2.2 Lấy chi tiết room
```http
GET /rooms/:roomId
```
**Response:**
```json
{
"success": true,
"data": {
"id": "room-uuid",
"space_id": "space-uuid",
"name": "Thảo luận",
"description": "Phòng thảo luận bài tập",
"type": "text",
"is_private": false,
"created_at": "2026-05-01T10:00:00Z"
}
}
```
**Lỗi:**
- `403 Forbidden` — User không phải member của room
---
### 2.3 Cập nhật room
```http
PATCH /rooms/:roomId
```
**Request Body:**
```json
{
"name": "Thảo luận (Updated)",
"description": "Mô tả mới",
"type": "text",
"isPrivate": true
}
```
> **Lưu ý:** Chỉ room creator hoặc space admin mới có quyền cập nhật. Tất cả fields optional.
**Response:**
```json
{
"success": true,
"data": {
"id": "room-uuid",
"name": "Thảo luận (Updated)",
...
}
}
```
---
### 2.4 Xóa room
```http
DELETE /rooms/:roomId
```
> **Lưu ý:** Chỉ room creator hoặc space admin mới có quyền xóa.
**Response:**
```json
{
"success": true,
"message": "Room deleted successfully"
}
```
---
### 2.5 Lấy danh sách rooms trong space
```http
GET /spaces/:spaceId/rooms
```
> Cùng chức năng với **1.7**, trả về danh sách rooms trong space.
---
### 2.6 Lấy danh sách members trong room
```http
GET /rooms/:roomId/members
```
**Response:**
```json
{
"success": true,
"data": [
"user-id-1",
"user-id-2",
"user-id-3"
]
}
```
---
### 2.7 Thêm member vào room
```http
POST /rooms/:roomId/members
```
**Request Body:**
```json
{
"userId": "550e8400-e29b-41d4-a716-446655440000"
}
```
| Field | Type | Required |
|-------|------|----------|
| `userId` | UUID | ✅ |
> **Lưu ý:**
> - User phải là member của space trước
> - Chỉ room creator hoặc space admin mới có quyền thêm
**Response:**
```json
{
"success": true,
"message": "Member added successfully"
}
```
---
### 2.8 Xóa member khỏi room
```http
DELETE /rooms/:roomId/members/:userId
```
> **Lưu ý:** Chỉ room creator hoặc space admin mới có quyền xóa.
**Response:**
```json
{
"success": true,
"message": "Member removed successfully"
}
```
---
### 2.9 Lấy thống kê room
```http
GET /rooms/:roomId/stats
```
**Response:**
```json
{
"success": true,
"data": {
"memberCount": 25,
"messageCount": 1200,
"lastActivity": "2026-05-04T10:00:00Z"
}
}
```
---
## 3. Members API (`/spaces/:spaceId/members`)
### 3.1 Lấy danh sách members trong space
```http
GET /spaces/:spaceId/members
```
**Response:**
```json
{
"success": true,
"data": [
{
"id": "user-id-1",
"email": "alice@example.com",
"username": "alice",
"displayName": "Alice",
"avatar": "https://...",
"status": "online",
"role": "owner",
"joinedAt": "2026-05-01T10:00:00Z"
},
{
"id": "user-id-2",
"email": "bob@example.com",
"username": "bob",
"displayName": "Bob",
"avatar": "https://...",
"status": "offline",
"role": "member",
"joinedAt": "2026-05-02T10:00:00Z"
}
]
}
```
---
### 3.2 Tìm kiếm members trong space
```http
GET /spaces/:spaceId/members/search?q=alice
```
**Query Params:**
- `q` (string) — Từ khóa tìm kiếm username/display_name/email
**Response:**
```json
{
"success": true,
"data": [
{
"id": "user-id-1",
"email": "alice@example.com",
"username": "alice",
"displayName": "Alice",
"avatar": "https://...",
"status": "online",
"role": "owner",
"joinedAt": "2026-05-01T10:00:00Z"
}
]
}
```
---
### 3.3 Lấy role của member
```http
GET /spaces/:spaceId/members/:userId/role
```
**Response:**
```json
{
"success": true,
"data": {
"role": "admin"
}
}
```
---
### 3.4 Cập nhật role member
```http
PATCH /spaces/:spaceId/members/:userId/role
```
**Request Body:**
```json
{
"role": "admin"
}
```
| Field | Type | Required | Values |
|-------|------|----------|--------|
| `role` | string | ✅ | `"member"`, `"admin"` |
> **Lưu ý:**
> - Chỉ owner mới có quyền promote lên admin
> - Không thể thay đổi role của owner
**Response:**
```json
{
"success": true,
"data": {
"id": "member-id",
"space_id": "space-uuid",
"user_id": "user-uuid",
"role": "admin",
"joined_at": "2026-05-01T10:00:00Z"
}
}
```
---
### 3.5 Lấy activity của member
```http
GET /spaces/:spaceId/members/:userId/activity
```
**Response:**
```json
{
"success": true,
"data": {
"lastActive": "2026-05-04T10:00:00Z",
"messageCount": 150,
"reactionCount": 45
}
}
```
---
### 3.6 Xóa member khỏi space
```http
DELETE /spaces/:spaceId/members/:userId
```
> **Lưu ý:**
> - Chỉ owner hoặc admin mới có quyền xóa
> - Admin không thể xóa owner
> - Owner không thể bị xóa
> - Member có thể tự xóa chính mình (leave)
**Response:**
```json
{
"success": true,
"message": "Member removed successfully"
}
```
---
## 4. Error Responses
### Format lỗi chuẩn
```json
{
"success": false,
"message": "Error description",
"error": "ERROR_CODE"
}
```
### Các mã lỗi phổ biến
| Status | Code | Mô tả |
|--------|------|-------|
| `400` | `BAD_REQUEST` | Request không hợp lệ (validation error) |
| `401` | `UNAUTHORIZED` | Token hết hạn hoặc không hợp lệ |
| `403` | `FORBIDDEN` | Không có quyền thực hiện |
| `404` | `NOT_FOUND` | Resource không tồn tại |
| `409` | `CONFLICT` | Conflict (đã tồn tại, v.v.) |
| `429` | `RATE_LIMIT` | Quá nhiều requests |
### Ví dụ lỗi 403
```json
{
"statusCode": 403,
"message": "You are not a member of this space",
"error": "Forbidden"
}
```
### Ví dụ lỗi 400 (Validation)
```json
{
"statusCode": 400,
"message": ["name must be longer than or equal to 2 characters"],
"error": "Bad Request"
}
```
---
## 5. DTO Reference
### CreateSpaceDto
```typescript
{
name: string; // required, min 2, max 100 chars
description?: string; // optional, max 500 chars
icon?: string; // optional, URL
isPrivate?: boolean; // optional, default: false
}
```
### UpdateSpaceDto
```typescript
{
name?: string; // optional, min 2, max 100 chars
description?: string; // optional, max 500 chars
icon?: string; // optional, URL
isPrivate?: boolean; // optional
}
```
### CreateRoomDto
```typescript
{
name: string; // required, min 2, max 100 chars
description?: string; // optional, max 500 chars
type?: string; // optional, default: "text" ("text" | "voice")
isPrivate?: boolean; // optional, default: false
}
```
### UpdateRoomDto
```typescript
{
name?: string;
description?: string;
type?: string; // "text" | "voice"
isPrivate?: boolean;
}
```
### AddMemberDto
```typescript
{
userId: string; // required, UUID
role?: string; // optional, default: "member" ("member" | "admin")
}
```
### AddRoomMemberDto
```typescript
{
userId: string; // required, UUID
}
```
### UpdateRoleDto
```typescript
{
role: string; // required, "member" | "admin"
}
```
---
## 6. Flow gợi ý cho Frontend
### Tạo space mới
```typescript
async function createSpace(name: string, description?: string) {
const response = await fetch('/api/spaces', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, description, isPrivate: false }),
});
return response.json();
}
```
### Lấy danh sách spaces và rooms
```typescript
async function loadSpacesAndRooms() {
// 1. Lấy danh sách spaces
const spacesRes = await fetch('/api/spaces', {
headers: { 'Authorization': `Bearer ${token}` },
});
const { data: spaces } = await spacesRes.json();
// 2. Với mỗi space, lấy rooms
for (const space of spaces) {
const roomsRes = await fetch(`/api/spaces/${space.id}/rooms`, {
headers: { 'Authorization': `Bearer ${token}` },
});
const { data: rooms } = await roomsRes.json();
space.rooms = rooms;
}
return spaces;
}
```
### Tham gia room qua WebSocket
```typescript
import { io } from 'socket.io-client';
const socket = io('wss://api.example.com/chat', {
auth: { token: accessToken },
});
// Join room
socket.emit('joinRoom', { roomId: 'room-uuid' });
// Listen events
socket.on('joinedRoom', (data) => {
console.log('Joined room:', data);
});
socket.on('newMessage', (message) => {
console.log('New message:', message);
});
```
### Thêm member vào space
```typescript
async function addMember(spaceId: string, userId: string, role: string = 'member') {
const response = await fetch(`/api/spaces/${spaceId}/members`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ userId, role }),
});
return response.json();
}
```
---
## 7. Permissions Summary
| Action | Who can do it |
|--------|---------------|
| Create space | Any authenticated user |
| Update space | Owner, Admin |
| Delete space | Owner only |
| Add member | Owner, Admin |
| Remove member | Owner, Admin (except owner) |
| Update member role | Owner only |
| Create room | Space member |
| Update room | Room creator, Space admin |
| Delete room | Room creator, Space admin |
| Add room member | Room creator, Space admin |
| Remove room member | Room creator, Space admin |
| Generate invite code | Owner, Admin |
| Join by invite code | Any authenticated user |
| Leave space | Member (not owner) |
---
## 8. Cache Notes
| Endpoint | Cache |
|----------|-------|
| `GET /spaces/:spaceId/rooms` | `private, max-age=30` |
| `GET /rooms/:roomId/members` | Redis cache 5 phút |
| `GET /spaces/:spaceId/members` | Redis cache 5 phút |
Frontend nên implement stale-while-revalidate cho danh sách spaces/rooms.
|