> ## 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 結構描述；程式碼為 `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.