> ## 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 API 搜索内容、上传文件、管理通知和订阅实时未读数

示例使用本地站点。需要登录时，将已有的 NodeLoc 会话令牌放在 `NODELOC_SESSION_TOKEN`，或在同源浏览器请求中使用会话 Cookie。认证与通用错误约定见 [社区 API v1 概览](https://docs.nodeloc.com/api-reference/v1/overview.md)。

```bash theme={null}
BASE_URL='http://127.0.0.1:8090'
```

## 搜索

`GET /api/v1/search` 可匿名调用，结果按当前读者的可见权限筛选。

| 参数 | 用法 |
| - | - |
| `q` | 搜索词，必填。为空时返回 `422 invalid`，字段为 `q`。 |
| `type` | 默认为 `all`，返回各类结果的预览。使用 `posts`、`topics`、`users`、`nodes` 或 `tags` 翻阅指定类别。 |
| `node` | 按节点 `slug` 限定帖子或主题结果。节点不可见时可能返回 `404`。 |
| `sort` | 帖子默认按相关度排序。传 `new` 按发帖时间倒序。 |
| `page` | 从 1 开始，每页 20 条，最多第 10 页。 |
| `offset` 和 `limit` | 可用于帖子和主题搜索。传其中任意一个即使用这套分页，`limit` 默认 20，范围 1–50。服务端按 `floor(offset / limit) + 1` 换算页码，最多第 10 页。 |

```bash theme={null}
curl --silent --show-error --fail-with-body --get "$BASE_URL/api/v1/search" \
  --data-urlencode 'q=VPS' \
  --data-urlencode 'type=posts' \
  --data-urlencode 'node=vps' \
  --data-urlencode 'page=1'
```

响应含 `posts`、`topics`、`users`、`nodes` 和 `tags` 数组。`topics` 从匹配的帖子按主题去重，因此条数可能少于 `posts`。用 `has_more` 和返回的 `next_page` 或 `next_offset` 翻页，到末页时后续位置字段省略。

```json theme={null}
{
  "q": "VPS",
  "type": "posts",
  "page": 1,
  "has_more": false,
  "posts": [{"id": 6, "topic_id": 2, "post_number": 1, "node_slug": "vps"}],
  "topics": [{"id": 2, "title": "年付 VPS 选购经验交流"}],
  "users": [],
  "nodes": [],
  "tags": []
}
```

搜索每个登录账号每分钟最多 30 次，匿名请求按 IP 计数。收到 `429 rate_limited` 时按照响应的 `Retry-After` 等待。

## 上传图片和附件

`POST /api/v1/uploads` 需要登录。用 `multipart/form-data` 发送 `file`。默认接受 JPEG、PNG、GIF、WebP 图片。非图片附件需要 `uploads.attach` 权限，例如上传 PDF 时额外传表单字段 `kind=attachment`。单文件上限为 15 MiB。

```bash theme={null}
curl --silent --show-error --fail-with-body \
  --request POST "$BASE_URL/api/v1/uploads" \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  --form 'file=@./photo.jpg'
```

cURL 会为 multipart 请求生成正确的 `Content-Type` 与边界。成功返回 HTTP 201 及 `upload` 对象。

```json theme={null}
{
  "upload": {
    "id": 321,
    "url": "/uploads/original/ab/abcdef0123456789abcdef0123456789abcdef01.jpg",
    "width": 1200,
    "height": 800,
    "image": true,
    "filename": "photo.jpg",
    "size": 348123
  }
}
```

`url` 是站点相对路径，`size` 以字节为单位，附件的 `image` 为 `false`，宽高为 `null`。可将图片路径写入 [发帖正文](https://docs.nodeloc.com/api-reference/v1/content.md) 的 Markdown 图片语法，将附件路径写成下载链接。文件校验失败会返回 `422 invalid`，详细信息在 `error.fields.file`。附件权限不足返回 `403 trust_level_too_low`，达到每日上传限额返回 `429 upload_daily_limit`。

## 获取通知并标为已读

`GET /api/v1/notifications` 只返回当前用户的通知。省略 `filter` 可查看全部，也可以按 `unread`、`reply`、`mention`、`vote`、`follow`、`points`、`system` 或 `moderation` 筛选。`limit` 默认 30，范围 1–50。将 `next_before` 作为下一次的 `before`，值为 `null` 时结束。`unread` 是该账号的总未读数。

```bash theme={null}
curl --silent --show-error --fail-with-body \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  "$BASE_URL/api/v1/notifications?filter=unread&limit=10"
```

```json theme={null}
{
  "notifications": [
    {
      "id": 240,
      "kind": "reply",
      "topic_id": 2,
      "post_number": 3,
      "post_id": 57,
      "data": {"reply_to": "topic"},
      "read": false
    }
  ],
  "unread": 1,
  "next_before": null
}
```

将列表中的通知 `id` 传给 `POST /api/v1/notifications/read` 可标记一条。`{"up_to_id":240}` 标记编号小于等于 240 的通知，`{}` 标记全部。成功返回 HTTP 204，响应体为空。

```bash theme={null}
curl --silent --show-error --fail-with-body \
  --request POST "$BASE_URL/api/v1/notifications/read" \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  --header 'Content-Type: application/json' \
  --data '{"id":240}'
```

## 订阅未读数

`GET /api/v1/notifications/stream` 使用 Server-Sent Events（SSE）。首次连接会发送当前未读数，之后在通知发生变动时发送新的总数。浏览器可用同源 `EventSource` 监听 `unread` 事件，原生客户端可用支持 Bearer 头的 SSE 客户端。

```bash theme={null}
curl --silent --show-error --fail-with-body --no-buffer \
  --header "Authorization: Bearer $NODELOC_SESSION_TOKEN" \
  --header 'Accept: text/event-stream' \
  "$BASE_URL/api/v1/notifications/stream"
```

```text theme={null}
event: unread
data: {"unread":1}

: ping

```

将事件的 `data` 解析为 JSON，用其中的 `unread` 更新角标。通知列表打开时可重新请求列表。心跳 `: ping` 约每 25 秒发送一次，连接最多保持 30 分钟。重连后从首次 `unread` 更新状态，并重新获取有需要的列表。


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