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

# Contacts 集合

`contacts` 记录某个用户曾与哪些用户合作，以便共享对话框推荐他们。每个用户一个文档。由 `web` 负责，没有 Mongoose schema；代码为 `services/web/app/src/Features/Contacts/ContactManager.mjs`。

#### 字段

| 字段 | 类型 | 含义 | 来源 |
| - | - | - | - |
| `_id` | ObjectId | 文档 id，由 upsert 创建。 | `ContactManager.mjs:5-12` |
| `user_id` | ObjectId | 这些联系人所属的用户。每个用户一个文档。 | `ContactManager.mjs:6` |
| `contacts` | Object | 以联系人的用户 id（字符串）为键的映射。 | `ContactManager.mjs:8-9` |
| `contacts.<userId>.n` | Number | 两位用户被关联在一起的次数。 | `ContactManager.mjs:8` |
| `contacts.<userId>.ts` | Date | 最近一次发生的时间。 | `ContactManager.mjs:9` |

#### 谁写入

| 代码 | 作用 |
| - | - |
| `ContactManager.mjs:4-13` (`touchContact`), `:23-28` (`addContact`) | 对两位用户的文档执行 upsert：对 `n` 执行 `$inc`，对 `ts` 执行 `$set`。 |
| `services/web/app/src/Features/Collaborators/CollaboratorsHandler.mjs:155-157` | `addUserIdToProject` 在获得添加者时调用 `addContact`：接受邀请时（邀请人和受邀人，`services/web/app/src/Features/Collaborators/CollaboratorsInviteHandler.mjs:199-205`）以及转移所有权时（新旧所有者，`services/web/app/src/Features/Collaborators/OwnershipTransferHandler.mjs:138-144`）。通过共享链接加入时不传入添加者（`services/web/app/src/Features/TokenAccess/TokenAccessController.mjs:386-388`、`:632-634`），因此不会添加联系人。 |

#### 谁读取

| 代码 | 作用 |
| - | - |
| `ContactManager.mjs:15-21`, `:30-42` | `getContactIds`：先按 `n` 排序，再按 `ts` 排序，均为从高到低。 |
| `services/web/app/src/Features/Contacts/ContactController.mjs:19-59` | `GET /user/contacts` (`services/web/app/src/Features/Contacts/ContactRouter.mjs:22-26`): 以用户形式返回前 50 位（不含占位账户），以及模块通过 `getContacts` 钩子添加的内容。 |

#### 索引

| 名称 | 键 | 选项 | 迁移 |
| - | - | - | - |
| `user_id_1` | `{ user_id: 1 }` | unique | `tools/migrations/20190912145001_create_contacts_indexes.mjs` |

#### 说明

* 联系人是双向的：把 B 加入 A 的项目后，B 成为 A 的联系人，A 也成为 B 的联系人。
* 没有任何操作会删除联系人或该文档，即使删除用户也不会。


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