> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nodeloc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 私信与聊天

> 列出聊天会话，读写私信、群聊和频道消息，处理分页与已读位置

所有接口均需 [NodeLoc 登录会话](https://docs.nodeloc.com/api-reference/v1/accounts.md)。发送 JSON 请求体时使用 `Content-Type: application/json`。

## 查找会话

用 `GET /api/v1/chat` 获取总览。响应包含 `directs`（私信）、`channels`（频道）、`groups`（已加入的群聊）、`directs_next` 和 `unread_count`。列表项的数值 `id` 是会话 ID。私信项里的 `with` 是对方的用户名，频道项里的 `joined` 表示你是否已加入。

| 要访问的内容 | 路径中的标识 |
| - | - |
| 读取或发送私信 | `/api/v1/messages/{username}` 使用对方的用户名。 |
| 读取或发送频道和群聊消息 | `/api/v1/chat/channels/{id}/messages` 使用会话 ID。 |
| 更新已读位置或免打扰 | `/api/v1/chat/conversations/{id}/...` 使用会话 ID，三种会话通用。 |
| 删除一条消息 | `/api/v1/chat/messages/{id}` 使用消息自己的 ID。 |

### 翻阅更多私信会话

用 `GET /api/v1/messages?limit=30` 单独取私信列表。响应含 `conversations`、`next` 和私信 `unread_count`。会话按最后一条消息时间从新到旧排，`limit` 默认为 30，范围是 1–100。将 `next` 的 `before`（RFC 3339 时间）和 `before_id`（会话 ID）一并传给下一页，时间放进查询参数时做 URL 编码。例如，`{"before":"2026-11-29T08:30:00Z","before_id":17}` 对应 `?before=2026-11-29T08%3A30%3A00Z&before_id=17`。也可以从总览的 `directs_next` 开始翻页。总览可能提前包含较早的未读会话，合并列表时按 `id` 去重。收到 `next: null` 即翻完，满页时可能还需发一次请求才能得到空页。

## 读取和翻阅消息

下例用 `GET /api/v1/messages/alex?limit=1&read=0` 读取最新一条私信。频道和群聊使用同样的参数和响应格式，例如 `GET /api/v1/chat/channels/28/messages?read=0`。

```json theme={null}
{
  "conversation_id": 17,
  "last_read_id": 96,
  "unread_count": 2,
  "next_before": 101,
  "messages": [
    {
      "id": 101,
      "from_me": false,
      "sender": "alex",
      "avatar": null,
      "html": "<p>明天见！</p>",
      "created_at": "2026-11-29T08:30:00Z"
    }
  ]
}
```

| 查询参数 | 用法 |
| - | - |
| `limit` | 每页 1–100 条，默认 30。 |
| 无游标 | 取最新一页，消息按 ID 从大到小排列。 |
| `before` | 传上一页的 `next_before`，取更早的消息，仍从新到旧。响应省略 `next_before` 时已到头。 |
| `after` | 传本地最新消息的 ID，取更晚的消息，按 ID 从小到大排列。若返回满页，用本页最后一条的 ID 继续取。 |

刚打开从未聊过的用户时返回 `{"messages":[]}`，首条成功发送的私信会建立会话。会话已有记录时，消息页会带 `conversation_id`。仅在当事人或已加入的成员取最新一页时返回 `last_read_id` 和 `unread_count`，两者反映本次读取前的位置和未读数。已删除的消息仍有一行记录，带 `deleted: true` 和空 `html`。系统消息带 `event`，引用消息带 `reply` 摘要。

## 发送或引用

用 `POST /api/v1/messages/{username}` 发私信，或用 `POST /api/v1/chat/channels/{id}/messages` 向频道或群聊发送。请求含 Markdown 原文 `raw`。可以省略 `reply_to`，也可以填同一会话中未删除的普通消息 ID。

```json theme={null}
{
  "raw": "好，**明天见**！",
  "reply_to": 101
}
```

成功返回 HTTP `201`，外层含 `message`，格式与读取时的消息项相同。

```json theme={null}
{
  "message": {
    "id": 102,
    "from_me": true,
    "sender": "sam",
    "avatar": null,
    "html": "<p>好，<strong>明天见</strong>！</p>",
    "created_at": "2026-11-29T08:31:00Z",
    "reply": { "id": 101, "sender": "alex", "excerpt": "明天见！" }
  }
}
```

正文须非空，私信最多 10,000 字符，频道或群聊最多 4,000 字符。私信受账号权限及对方的私信设置约束。已登录的用户可读取公开频道，发言前须加入并有发言权限。群聊的读取和发言都限成员。

## 已读、免打扰和成员操作

读取最新页或用 `after` 拉取新消息时，服务端默认推进当前用户的已读位置。用 `read=0` 只读取，等用户实际看到消息后再向 `PUT /api/v1/chat/conversations/{id}/read` 提交 `{"message_id":102}`。成功返回 `204`。用 `before` 读取旧消息不改变已读位置。

| 操作 | 请求与结果 |
| - | - |
| 聊天未读总数 | `GET /api/v1/chat/unread` 返回 `{"unread_count":2}`。免打扰的私信不计入总数，免打扰的频道和群聊只计入直接提及你的未读消息。 |
| 设置或取消免打扰 | `PUT /api/v1/chat/conversations/{id}/mute` 开启，`DELETE /api/v1/chat/conversations/{id}/mute` 关闭，均返回 `204`。 |
| 加入频道 | `PUT /api/v1/chat/channels/{id}/membership`，成功 `204`。 |
| 退出频道或群聊 | `DELETE /api/v1/chat/channels/{id}/membership`，成功 `204`。群主可用 `PUT /api/v1/chat/groups/{id}` 提交 `{"owner":"lee"}` 转让后退出，或用 `DELETE /api/v1/chat/groups/{id}` 解散群聊。 |
| 创建群聊 | `POST /api/v1/chat/groups` 提交 `{"name":"周末计划","usernames":["alex","lee"]}`，成功 `201`，例如 `{"group":{"id":42}}`。群名可省略，邀请至少两名其他成员。 |
| 查看群成员 | `GET /api/v1/chat/groups/{id}/members` 返回 `members`、`owner_me` 和 `max_members`。 |
| 删除消息 | `DELETE /api/v1/chat/messages/{id}`，成功 `204`。发送者可删自己的消息，群主和聊天管理人员可按权限删除。 |

## 常见错误

| HTTP | `error.code` | 含义 |
| - | - | - |
| `400` | `invalid_message` | 标记已读时需要正整数 `message_id`。 |
| `401` | `unauthorized` | 缺少有效会话。 |
| `403` | `blocked`、`reply_only`、`trust_level_too_low`、`silenced` | 对方的设置或当前账号权限限制发送。 |
| `403` | `not_member`、`staff_only` | 需要加入频道，或频道只允许站务发言。 |
| `404` | `not_found` | 用户、会话或消息不存在，或对当前用户不可见。 |
| `422` | `invalid` | 查看 `error.fields`，例如正文为空、引用目标不可用，或私信列表游标格式错误。 |
| `429` | `rate_limited` | 发送太频繁或达到额度，按 `Retry-After` 等待。 |


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