> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Messages 集合

`messages` 中每条消息对应一个文档：项目聊天中的一行，或审阅评论线程中的一条评论或回复。只有 chat 服务写入它，位置在 `services/chat/app/js/Features/Messages/MessageManager.js`。没有 Mongoose schema。每条消息都属于 [rooms](https://ayakaleaf-pro.mintlify.site/zh-CN/dev/data-model/rooms-collection.md) 集合中的一个房间。

#### 字段

| 字段 | 类型 | 含义 | 来源 |
| - | - | - | - |
| `_id` | ObjectId | 消息 ID。 | `MessageManager.js:13-14` |
| `room_id` | ObjectId | 房间（`rooms._id`），不是项目 ID，也不是线程 ID。 | `MessageManager.js:8,106-108` |
| `user_id` | ObjectId | 作者。 | `MessageManager.js:9,103-105` |
| `content` | String | 消息文本，1 到 10240 个字符（`MAX_MESSAGE_LENGTH`）。 | `MessageManager.js:7`, `services/chat/app/js/Features/Messages/MessageHttpSchemas.js:3,12-15` |
| `timestamp` | Number | 发送时间，以自 1970 年起的毫秒数表示（`Date.now()`），不是 Date。 | `MessageManager.js:10`, `services/chat/app/js/Features/Messages/MessageHttpController.js:288` |
| `edited_at` | Number | 最后编辑时间，以毫秒表示（`Date.now()`）。从未编辑过则不存在。 | `MessageManager.js:65-69`, `MessageHttpController.js:189,213` |

#### 写入方

| 代码 | 作用 |
| - | - |
| `MessageManager.js:5-16` `createMessage` | 插入一条消息（`MessageHttpController.js:275-294`）。Web 从 `services/web/app/src/Features/Chat/ChatController.mjs:32` 发送项目聊天消息，并从 track-changes 模块（`services/web/modules/track-changes/app/src/TrackChangesController.mjs:95`）和 mcp-server 模块（`services/web/modules/mcp-server/app/src/CommentTools.mjs:133`）发送评论。 |
| `MessageManager.js:51-72` `updateMessage` | 设置 `content` 和 `edited_at`。如果提供了用户 ID，则只匹配该用户的消息（`MessageManager.js:62-64`）。由 `ChatController.mjs:135` 和 `TrackChangesController.mjs:110` 调用。 |
| `MessageManager.js:74-80` `deleteMessage` | 删除一条消息。由 `ChatController.mjs:105` 和 `TrackChangesController.mjs:129` 调用。 |
| `MessageManager.js:39-49` `deleteAllMessagesInRoom(s)` | 删除已删除线程的消息（`MessageHttpController.js:172-173`），或在已删除项目过期时删除整个项目的消息（`MessageHttpController.js:261-271`，由 `services/web/app/src/Features/Project/ProjectDeleter.mjs:391` 调用）。 |
| `MessageManager.js:115-127` `duplicateRoomToOtherRoom` | 把每条消息的 `content`、`timestamp` 和 `user_id` 复制到另一个房间，不包括 `edited_at`。在历史恢复复制线程时使用（`MessageHttpController.js:316-342`），以及在连同范围一起复制项目时使用（`MessageHttpController.js:344-357`）。 |

#### 读取方

| 代码 | 作用 |
| - | - |
| `MessageManager.js:18-29` `getMessages` | 最新的在前，最多 `limit` 条（默认 50），且早于 `before`（一个 `timestamp`）。用于项目聊天（`ChatController.mjs:80`）。 |
| `MessageManager.js:31-37` `findAllMessagesInRooms` | 若干房间的全部消息，用于审阅面板的线程列表（`TrackChangesController.mjs:82`）、mcp-server 模块（`CommentTools.mjs:83`）和历史恢复（`services/web/app/src/Features/History/RestoreManager.mjs:259`）。Web 还会检查线程的第一条消息，使审阅者只能修改自己发起的线程（`services/web/app/src/Features/Authorization/AuthorizationManager.mjs:409-411`）。 |
| `MessageManager.js:90-100` `getMessage` | 一条消息。用户编辑或删除自己的消息之前，Web 会检查其 `user_id`（`services/web/app/src/Features/Authorization/AuthorizationMiddleware.mjs:253-257`、`TrackChangesController.mjs:124-125`）。 |

#### 索引

| 名称 | 键 | 选项 | 迁移 |
| - | - | - | - |
| `room_id_1_timestamp_-1` | `{ room_id: 1, timestamp: -1 }` | | `tools/migrations/20190912145013_create_messages_indexes.mjs` |

#### 说明

* 要查找某条评论的消息，先按 `project_id` 和 `thread_id` 查找房间，再查询 `room_id`。文档中的评论 ID 是 `thread_id`，不是 `room_id`。
* 评论指向的文本是 `docs.ranges.comments` 中的 `op.c`；讨论内容保存在这里。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.