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

# Project Invites 集合

`projectInvites` 保存项目所有者从共享对话框发出的邀请。一个文档对应发往一个邮箱地址的一份邀请。邀请被接受或撤销时，文档会被删除；邀请过期时，Mongo 也会将其删除。Schema 位于 `services/web/app/src/models/ProjectInvite.mjs`，由 `services/web/app/src/Features/Collaborators/CollaboratorsInviteHandler.mjs` 写入。

邀请不会让任何人成为成员。成员保存在项目的 `*_refs` 数组中，见 [Projects 集合](https://ayakaleaf-pro.mintlify.site/zh-CN/dev/data-model/projects-collection.md)。

#### 字段

| 字段 | 类型 | 含义 | 来源 |
| - | - | - | - |
| `_id` | ObjectId | 邀请 id。已有用户的邀请通知使用键 `project-invite-<_id>`（`services/web/app/src/Features/Notifications/NotificationsBuilder.mjs:102`）。 | `ProjectInvite.mjs:14` |
| `email` | String | 邀请发往的地址。为某个用户撤销邀请时，会将它与该用户已确认的邮箱比对（`CollaboratorsInviteHandler.mjs:103-116`）。共享链接邀请不设置此字段。 | `ProjectInvite.mjs:16`, `CollaboratorsInviteHandler.mjs:84` |
| `tokenHmac` | String | 邀请链接中令牌的 HMAC-SHA256（密钥 `overleaf-token-invite`）。明文令牌只出现在邮件和通知中；查询时会对 URL 中的令牌做哈希，再与此字段比对。 | `ProjectInvite.mjs:18`, `services/web/app/src/Features/Collaborators/CollaboratorsInviteHelper.mjs:11-15`, `CollaboratorsInviteHandler.mjs:82,256` |
| `encryptedToken` | String | 仅限共享链接邀请：用 `Settings.projectInviteEncryptorOptions` 加密的令牌，以便所有者再次查看链接（`services/web/app/src/Features/Collaborators/CollaboratorsInviteController.mjs:606-611`）。 | `ProjectInvite.mjs:17`, `CollaboratorsInviteHelper.mjs:30-47`, `CollaboratorsInviteHandler.mjs:255-258` |
| `sendingUserId` | ObjectId | 发出邀请的用户。接受邀请时，它会作为添加新成员的用户传递下去（`CollaboratorsInviteHandler.mjs:199-205`）。共享链接邀请不设置此字段。 | `ProjectInvite.mjs:19`, `CollaboratorsInviteHandler.mjs:86` |
| `projectId` | ObjectId | 邀请所属的项目。 | `ProjectInvite.mjs:20`, `CollaboratorsInviteHandler.mjs:87` |
| `privileges` | String or Boolean | 被邀请者获得的权限：`readOnly`、`readAndWrite` 或 `review`（`services/web/app/src/Features/Authorization/PrivilegeLevels.mjs:6-12`）。共享链接邀请也可能为 `false`，表示没有访问权限。 | `ProjectInvite.mjs:21-25`, `CollaboratorsInviteHandler.mjs:88` |
| `createdAt` | Date | 保存邀请时由 schema 默认值设置。 | `ProjectInvite.mjs:26` |
| `expires` | Date | Schema 默认值：创建后 30 天。TTL 索引会在此时间之后约 10 秒删除邀请。共享链接邀请为 `null`，永不过期。 | `ProjectInvite.mjs:6-12,27-31`, `CollaboratorsInviteHandler.mjs:264` |
| `reusable` | Boolean | 邮件邀请为 `false`，接受后即被删除（`CollaboratorsInviteHandler.mjs:207-220`）。共享链接邀请为 `true`，会一直保留。 | `ProjectInvite.mjs:32`, `CollaboratorsInviteHandler.mjs:89,263` |
| `subscriptionId` | ObjectId | 仅限共享链接邀请：其成员可以接受该链接的团体订阅（`CollaboratorsInviteController.mjs:490-501`）。 | `ProjectInvite.mjs:33`, `CollaboratorsInviteHandler.mjs:262` |

#### 写入方

| 代码 | 作用 |
| - | - |
| `CollaboratorsInviteHandler.mjs:76-101` (`inviteToProject`) | 创建邮件邀请，然后发送邮件，并为已有用户发送通知。由 `POST /project/:Project_id/invite` 调用（`services/web/app/src/Features/Collaborators/CollaboratorsRouter.mjs:114-125`）。 |
| `CollaboratorsInviteHandler.mjs:118-133` (`revokeInvite`) | 删除一份邀请，并将其通知标记为已读。由 `DELETE /project/:Project_id/invite/:invite_id` 调用（`CollaboratorsRouter.mjs:154-159`）。 |
| `CollaboratorsInviteHandler.mjs:135-153` (`generateNewInvite`) | 重新发送：删除邀请并用新令牌创建一份新邀请（`CollaboratorsRouter.mjs:161-169`）。 |
| `CollaboratorsInviteHandler.mjs:103-116` (`revokeInviteForUser`) | 当用户通过链接共享令牌加入（`services/web/app/src/Features/TokenAccess/TokenAccessController.mjs:396-401`），或在访问请求后获得访问权限时（`services/web/app/src/Features/Collaborators/CollaboratorsController.mjs:503-513`），删除发往该用户某个已确认邮箱的邀请。 |
| `CollaboratorsInviteHandler.mjs:155-221` (`acceptInvite`) | 将用户加入项目，然后删除邀请，除非邀请为 `reusable`。 |
| `CollaboratorsInviteHandler.mjs:249-269` (`createSharingLinkInvite`), `CollaboratorsInviteController.mjs:689-724` (`updateSharingLink`) | 创建或更新项目唯一的可复用共享链接邀请。这些路由需要 split test `sharing-updates-new-link`（`CollaboratorsRouter.mjs:134-152`），而它不在 `splitTestOverrides` 中（`services/web/config/settings.defaults.js:1371-1383`）。因此在配置该 split test 之前，它们会返回 403。 |

#### 读取方

| 代码 | 作用 |
| - | - |
| `services/web/app/src/Features/Collaborators/CollaboratorsInviteGetter.mjs:6-19` (`getAllInvites`) | 为共享对话框列出待处理的邮件邀请（`GET /project/:Project_id/invites`，`CollaboratorsRouter.mjs:127-132`）。跳过可复用的邀请。 |
| `CollaboratorsInviteGetter.mjs:21-29` (`getEditInviteCount`) | 统计不是 `readOnly` 的待处理邮件邀请。`services/web/app/src/Features/Subscription/LimitationsManager.mjs:61-70` 在检查协作者上限时会把它们计入编辑者。 |
| `CollaboratorsInviteGetter.mjs:31-44` (`getInviteByToken`) | 按 URL 中令牌的哈希查找邀请，用于邀请页面和接受邀请（`CollaboratorsInviteController.mjs:353,481`）。 |
| `CollaboratorsInviteGetter.mjs:46-70` | 查找项目的共享链接邀请。 |

#### 索引

| 名称 | 键 | 选项 | 迁移 |
| - | - | - | - |
| `expires_1` | `{ expires: 1 }` | TTL，`expireAfterSeconds: 10` | `20190912145023_create_projectInvites_indexes.mjs` |
| `projectId_1` | `{ projectId: 1 }` | | `20190912145023_create_projectInvites_indexes.mjs` |
| `tokenHmac_1` | `{ tokenHmac: 1 }` | | `20240524135408_add_token_hmac_project_invite_tokens.mjs` |

迁移位于 `tools/migrations/`。Schema 也声明了 `expires` 上的 TTL 索引，但 Mongoose 的 `autoIndex` 已关闭（`services/web/app/src/infrastructure/Mongoose.mjs:7`），因此由迁移创建该索引。

#### 说明

* 如果接受 `readAndWrite` 或 `review` 邀请时项目已没有空余的编辑者名额，用户会以只读身份加入，并列在项目的 `pendingEditor_refs` 或 `pendingReviewer_refs` 中（`CollaboratorsInviteHandler.mjs:162-205`）。
* 旧版邀请把明文令牌存放在 `token` 字段中。迁移 `20240524135408_add_token_hmac_project_invite_tokens.mjs` 添加了 `tokenHmac`，`20240531082910_remove_project_invite_tokens.mjs` 删除了 `token`。
* 没有任何代码会删除已删除项目的邀请；它们会一直保留，直到被 TTL 索引移除。


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