> ## 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 Collection

`messages` holds one document per message: a line in the project chat, or a comment or reply in a review comment thread. Only the chat service writes it, in `services/chat/app/js/Features/Messages/MessageManager.js`. There is no Mongoose schema. Each message belongs to a room in the [rooms](https://ayakaleaf-pro.mintlify.site/dev/data-model/rooms-collection.md) collection.

#### Fields

| Field | Type | Meaning | Source |
| - | - | - | - |
| `_id` | ObjectId | Message id. | `MessageManager.js:13-14` |
| `room_id` | ObjectId | The room (`rooms._id`), not the project id or the thread id. | `MessageManager.js:8,106-108` |
| `user_id` | ObjectId | Author. | `MessageManager.js:9,103-105` |
| `content` | String | Text of the message, 1 to 10240 characters (`MAX_MESSAGE_LENGTH`). | `MessageManager.js:7`, `services/chat/app/js/Features/Messages/MessageHttpSchemas.js:3,12-15` |
| `timestamp` | Number | When it was sent, in milliseconds since 1970 (`Date.now()`), not a Date. | `MessageManager.js:10`, `services/chat/app/js/Features/Messages/MessageHttpController.js:288` |
| `edited_at` | Number | When it was last edited, in milliseconds (`Date.now()`). Missing if never edited. | `MessageManager.js:65-69`, `MessageHttpController.js:189,213` |

#### Who Writes It

| Code | What it does |
| - | - |
| `MessageManager.js:5-16` `createMessage` | Inserts a message (`MessageHttpController.js:275-294`). Web sends project chat messages from `services/web/app/src/Features/Chat/ChatController.mjs:32`, and comments from the track-changes module (`services/web/modules/track-changes/app/src/TrackChangesController.mjs:95`) and the mcp-server module (`services/web/modules/mcp-server/app/src/CommentTools.mjs:133`). |
| `MessageManager.js:51-72` `updateMessage` | Sets `content` and `edited_at`. When a user id is given, only that user's message matches (`MessageManager.js:62-64`). Called from `ChatController.mjs:135` and `TrackChangesController.mjs:110`. |
| `MessageManager.js:74-80` `deleteMessage` | Deletes one message. Called from `ChatController.mjs:105` and `TrackChangesController.mjs:129`. |
| `MessageManager.js:39-49` `deleteAllMessagesInRoom(s)` | Deletes the messages of a deleted thread (`MessageHttpController.js:172-173`) or of a whole project when a deleted project expires (`MessageHttpController.js:261-271`, called from `services/web/app/src/Features/Project/ProjectDeleter.mjs:391`). |
| `MessageManager.js:115-127` `duplicateRoomToOtherRoom` | Copies `content`, `timestamp` and `user_id` of every message into another room, without `edited_at`. Used when a thread is duplicated by a history restore (`MessageHttpController.js:316-342`) and when a project is copied with its ranges (`MessageHttpController.js:344-357`). |

#### Who Reads It

| Code | What it does |
| - | - |
| `MessageManager.js:18-29` `getMessages` | Newest first, at most `limit` (default 50) older than `before` (a `timestamp`). Used for the project chat (`ChatController.mjs:80`). |
| `MessageManager.js:31-37` `findAllMessagesInRooms` | All messages of some rooms, for the thread list of the review panel (`TrackChangesController.mjs:82`), the mcp-server module (`CommentTools.mjs:83`) and history restore (`services/web/app/src/Features/History/RestoreManager.mjs:259`). Web also checks the first message of a thread to let a reviewer change only threads they started (`services/web/app/src/Features/Authorization/AuthorizationManager.mjs:409-411`). |
| `MessageManager.js:90-100` `getMessage` | One message. Web checks its `user_id` before a user edits or deletes their own message (`services/web/app/src/Features/Authorization/AuthorizationMiddleware.mjs:253-257`, `TrackChangesController.mjs:124-125`). |

#### Indexes

| Name | Keys | Options | Migration |
| - | - | - | - |
| `room_id_1_timestamp_-1` | `{ room_id: 1, timestamp: -1 }` | | `tools/migrations/20190912145013_create_messages_indexes.mjs` |

#### Notes

* To find the messages of a comment, look up the room by `project_id` and `thread_id` first, then query `room_id`. The comment id in the doc is the `thread_id`, not the `room_id`.
* The text a comment points at is `op.c` in `docs.ranges.comments`; the discussion is here.


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