> For AI agents: the complete documentation index is available at https://docs.halo.run/llms.txt, the full documentation bundle is available at https://docs.halo.run/llms-full.txt.

# 评论

本页 API 自 Halo 2.0.0 起可用。

## getByName(name)

```js
commentFinder.getByName(name);
```

### 描述

根据 `metadata.name` 获取评论。

### 参数

1. `name:string` - 评论的唯一标识 `metadata.name`。

### 返回值

[#CommentVo](#commentvo)

### 示例

```html
<div th:with="comment = ${commentFinder.getByName('comment-foo')}">
  <span th:text="${comment.owner.displayName}"></span>
  <div th:text="${comment.spec.content}"></div>
</div>
```

## list(ref,page,size)

```js
commentFinder.list(ref, page, size);
```

### 描述

根据评论主体的引用（Ref）和分页参数获取评论列表，例如获取某篇文章下的评论。

其中以 Map 字面量传递 `ref` 的形式自 Halo 2.10.1 起可用。

### 参数

1. `ref:#Ref` - 评论主体的引用，即被评论对象（如文章、自定义页面）的 `group` / `version` / `kind` / `name`，而非评论自身的 `metadata.name`。其中 `version` 是该模型的 API 版本（如 `v1alpha1`）。
2. `page:int` - 分页页码，从 1 开始
3. `size:int` - 分页条数

- [#Ref](#ref)

### 返回值

[#ListResult\<CommentVo>](#listresultcommentvo)

### 示例

```html
<ul
  th:with="comments = ${commentFinder.list({ group: 'content.halo.run', version: 'v1alpha1', kind: 'Post', name: 'post-foo' },1,10)}"
>
  <li th:each="comment : ${comments.items}">
    <span th:text="${comment.owner.displayName}"></span>
    <div th:text="${comment.spec.content}"></div>
  </li>
</ul>
```

## listReply(commentName,page,size)

```js
commentFinder.listReply(commentName, page, size);
```

### 描述

根据评论的 `metadata.name` 和分页参数获取回复列表。

### 参数

1. `commentName:string` - 评论的唯一标识 `metadata.name`。
2. `page:int` - 分页页码，从 1 开始
3. `size:int` - 分页条数

### 返回值

[#ListResult\<ReplyVo>](#listresultreplyvo)

### 示例

```html
<ul th:with="replies = ${commentFinder.listReply('comment-foo',1,10)}">
  <li th:each="reply : ${replies.items}">
    <span th:text="${reply.owner.displayName}"></span>
    <div th:text="${reply.spec.content}"></div>
  </li>
</ul>
```

## 类型定义

### CommentVo

```jsonc title="CommentVo"
{
  "metadata": {
    "name": "string", // 唯一标识
    "labels": {
      "additionalProp1": "string",
    },
    "annotations": {
      "additionalProp1": "string",
    },
    "creationTimestamp": "2022-11-20T12:16:19.788Z", // 创建时间
  },
  "spec": {
    "raw": "string", // 原始文本，一般用于给编辑器使用
    "content": "string", // 最终渲染的文本
    "owner": {
      // 创建者关联
      "kind": "string",
      "name": "", // 公开查询中已脱敏
      "displayName": "string",
      "annotations": {
        "additionalProp1": "string",
        "email-hash": "string", // 邮箱的 SHA-256，仅在邮箱存在时提供
      },
    },
    "userAgent": "string", // 评论者 UserAgent 信息
    "ipAddress": "", // 公开查询中已脱敏
    "priority": 0, // 排序字段
    "top": false, // 是否置顶
    "allowNotification": true, // 是否允许通知
    "approved": false,
    "hidden": false,
    "subjectRef": {
      // 引用关联，比如文章、自定义页面
      "group": "string",
      "version": "string",
      "kind": "string",
      "name": "string",
    },
    "lastReadTime": "2022-11-20T12:16:19.788Z",
  },
  "status": {
    "lastReplyTime": "2022-11-20T12:16:19.788Z",
    "replyCount": 0, // 回复总数，包含未公开回复
    "visibleReplyCount": 0, // 已审核且未隐藏的公开回复数
    "unreadReplyCount": 0,
    "hasNewReply": true, // 是否有新回复
  },
  "owner": {
    // 创建者信息
    "kind": "string",
    "name": null, // 公开查询中已脱敏
    "displayName": "string",
    "avatar": "string",
    "email": null, // 公开查询中已脱敏
  },
  "stats": {
    "upvote": 0, // 点赞数量
  },
}
```

公开查询会清空评论者标识、邮箱和 IP 地址。需要展示评论者信息时，请使用 `owner.displayName`、`owner.avatar` 和 `owner.kind`；需要展示公开回复数时，请使用 `status.visibleReplyCount`。

### ListResult\<CommentVo>

```jsonc title="ListResult<CommentVo>"
{
  "page": 0, // 当前页码
  "size": 0, // 每页条数
  "total": 0, // 总条数
  "items": "List<#CommentVo>", // 评论列表数据
  "first": true, // 是否为第一页
  "last": true, // 是否为最后一页
  "hasNext": true, // 是否有下一页
  "hasPrevious": true, // 是否有上一页
  "totalPages": 0, // 总页数
}
```

- [#CommentVo](#commentvo)

### ReplyVo

```jsonc title="ReplyVo"
{
  "metadata": {
    "name": "string", // 唯一标识
    "labels": {
      "additionalProp1": "string",
    },
    "annotations": {
      "additionalProp1": "string",
    },
    "creationTimestamp": "2022-11-20T12:25:32.357Z", // 创建时间
  },
  "spec": {
    "raw": "string", // 原始文本，一般用于给编辑器使用
    "content": "string", // 最终渲染的文本
    "owner": {
      // 创建者关联
      "kind": "string",
      "name": "", // 公开查询中已脱敏
      "displayName": "string",
      "annotations": {
        "additionalProp1": "string",
        "email-hash": "string", // 邮箱的 SHA-256，仅在邮箱存在时提供
      },
    },
    "userAgent": "string", // 评论者 UserAgent 信息
    "ipAddress": "", // 公开查询中已脱敏
    "priority": 0, // 排序字段
    "top": false, // 是否置顶
    "allowNotification": true, // 是否允许通知
    "approved": false,
    "hidden": false,
    "commentName": "string", // 被回复的评论名称，即 Comment 的 metadata.name
    "quoteReply": "string", // 被回复的回复名称，即 Reply 的 metadata.name
  },
  "owner": {
    // 创建者信息
    "kind": "string",
    "name": null, // 公开查询中已脱敏
    "displayName": "string",
    "avatar": "string",
    "email": null, // 公开查询中已脱敏
  },
  "stats": {
    "upvote": 0, // 点赞数量
  },
}
```

### ListResult\<ReplyVo>

```jsonc title="ListResult<ReplyVo>"
{
  "page": 0, // 当前页码
  "size": 0, // 每页条数
  "total": 0, // 总条数
  "items": "List<#ReplyVo>", // 回复列表数据
  "first": true, // 是否为第一页
  "last": true, // 是否为最后一页
  "hasNext": true, // 是否有下一页
  "hasPrevious": true, // 是否有上一页
  "totalPages": 0, // 总页数
}
```

- [#ReplyVo](#replyvo)

### Ref

```json title="Ref"
{
  "group": "string",
  "kind": "string",
  "version": "string",
  "name": "string"
}
```
