openapi: 3.0.3
info:
  title: 九桑开放 API
  version: 1.0.0
  description: |
    九桑内容与社区平台的 REST API，适用于 Web、Taro / 微信小程序与移动 App。
    除登录、注册与公开读取接口外，请通过 `Authorization: Bearer <token>` 携带登录凭证。
    列表接口统一支持 Payload 查询语法：`where[field][operator]=value`、`limit`、`page`、`sort`、`depth`。
servers:
  - url: http://localhost:3000
    description: 本地开发
  - url: https://jiusang.com
    description: 生产环境
tags:
  - name: 认证
  - name: 内容
  - name: 评论
  - name: 互动
  - name: 通知
  - name: 治理与配置
paths:
  /api/users/login:
    post:
      tags: [认证]
      summary: 登录
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [email, password]
              properties:
                email: { type: string, format: email }
                password: { type: string }
      responses:
        '200':
          description: 登录成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  message: { type: string }
                  token: { type: string }
                  exp: { type: integer }
                  user: { $ref: '#/components/schemas/User' }
  /api/users:
    post:
      tags: [认证]
      summary: 注册
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [name, email, password]
              properties:
                name: { type: string }
                email: { type: string, format: email }
                password: { type: string, minLength: 6 }
      responses:
        '201':
          description: 注册成功
          content:
            application/json:
              schema:
                type: object
                properties:
                  doc: { $ref: '#/components/schemas/User' }
                  message: { type: string }
  /api/users/logout:
    post:
      tags: [认证]
      summary: 退出登录
      security: [{ bearerAuth: [] }]
      responses:
        '200': { description: 已退出 }
  /api/users/me:
    get:
      tags: [认证]
      summary: 当前登录用户
      security: [{ bearerAuth: [] }]
      responses:
        '200':
          description: 当前用户
          content:
            application/json:
              schema:
                type: object
                properties:
                  user: { $ref: '#/components/schemas/User' }
        '401': { description: 未登录 }
  /api/users/{id}:
    get:
      tags: [认证]
      summary: 用户主页信息
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      responses:
        '200':
          description: 用户
          content:
            application/json:
              schema: { $ref: '#/components/schemas/User' }
    patch:
      tags: [认证]
      summary: 更新个人资料
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name: { type: string }
                bio: { type: string }
                avatar: { type: integer, description: 媒体 ID }
      responses:
        '200':
          description: 更新后的用户
          content:
            application/json:
              schema:
                type: object
                properties:
                  doc: { $ref: '#/components/schemas/User' }
  /api/contents:
    get:
      tags: [内容]
      summary: 内容列表
      parameters:
        - { name: 'where[type][equals]', in: query, schema: { type: string, enum: [article, post, video, resource] } }
        - { name: 'where[_status][equals]', in: query, schema: { type: string, enum: [published, draft] } }
        - { name: 'where[featured][equals]', in: query, schema: { type: boolean } }
        - { name: 'where[title][like]', in: query, schema: { type: string } }
        - { name: limit, in: query, schema: { type: integer, default: 10 } }
        - { name: page, in: query, schema: { type: integer, default: 1 } }
        - { name: sort, in: query, schema: { type: string, example: -publishedAt } }
        - { name: depth, in: query, schema: { type: integer, default: 1 } }
      responses:
        '200':
          description: 列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  docs:
                    type: array
                    items: { $ref: '#/components/schemas/Content' }
                  totalDocs: { type: integer }
                  totalPages: { type: integer }
                  page: { type: integer }
    post:
      tags: [内容]
      summary: 发布内容
      security: [{ bearerAuth: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [title, type, slug]
              properties:
                title: { type: string }
                type: { type: string, enum: [article, post, video, resource] }
                slug: { type: string }
                excerpt: { type: string }
                content: { type: object, description: Lexical 富文本 JSON }
                videoUrl: { type: string }
                duration: { type: integer }
                attachments: { type: array, items: { type: integer } }
                visibility: { type: string, enum: [public, loggedIn, private] }
                _status: { type: string, enum: [draft, published] }
      responses:
        '201':
          description: 已创建
          content:
            application/json:
              schema:
                type: object
                properties:
                  doc: { $ref: '#/components/schemas/Content' }
  /api/contents/{id}:
    get:
      tags: [内容]
      summary: 内容详情
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
        - { name: depth, in: query, schema: { type: integer, default: 2 } }
      responses:
        '200':
          description: 内容
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Content' }
        '404': { description: 不存在或无权查看 }
    patch:
      tags: [内容]
      summary: 更新内容
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      requestBody:
        content:
          application/json:
            schema: { $ref: '#/components/schemas/Content' }
      responses:
        '200': { description: 已更新 }
    delete:
      tags: [内容]
      summary: 删除内容
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      responses:
        '200': { description: 已删除 }
  /api/comments:
    get:
      tags: [评论]
      summary: 评论列表
      parameters:
        - { name: 'where[content][equals]', in: query, required: true, schema: { type: integer } }
        - { name: depth, in: query, schema: { type: integer, default: 1 } }
        - { name: sort, in: query, schema: { type: string, default: createdAt } }
      responses:
        '200':
          description: 列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  docs:
                    type: array
                    items: { $ref: '#/components/schemas/Comment' }
                  totalDocs: { type: integer }
    post:
      tags: [评论]
      summary: 发表评论 / 回复
      security: [{ bearerAuth: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [content, body]
              properties:
                content: { type: integer, description: 内容 ID }
                body: { type: string, maxLength: 2000 }
                parent: { type: integer, description: 被回复评论 ID }
      responses:
        '201': { description: 已发表 }
        '429': { description: 评论过于频繁 }
  /api/comments/{id}:
    delete:
      tags: [评论]
      summary: 删除评论
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      responses:
        '200': { description: 已删除 }
  /api/likes:
    get:
      tags: [互动]
      summary: 我的点赞
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: 'where[content][equals]', in: query, schema: { type: integer } }
      responses:
        '200':
          description: 列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  docs: { type: array, items: { $ref: '#/components/schemas/Like' } }
    post:
      tags: [互动]
      summary: 点赞
      security: [{ bearerAuth: [] }]
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required: [content]
              properties:
                content: { type: integer }
      responses:
        '201': { description: 已点赞 }
        '400': { description: 已点过赞 }
  /api/likes/{id}:
    delete:
      tags: [互动]
      summary: 取消点赞
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      responses:
        '200': { description: 已取消 }
  /api/bookmarks:
    get:
      tags: [互动]
      summary: 我的收藏
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: depth, in: query, schema: { type: integer, default: 2 } }
      responses:
        '200':
          description: 列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  docs: { type: array, items: { $ref: '#/components/schemas/Bookmark' } }
    post:
      tags: [互动]
      summary: 收藏
      security: [{ bearerAuth: [] }]
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required: [content]
              properties:
                content: { type: integer }
      responses:
        '201': { description: 已收藏 }
  /api/bookmarks/{id}:
    delete:
      tags: [互动]
      summary: 取消收藏
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      responses:
        '200': { description: 已取消 }
  /api/notifications:
    get:
      tags: [通知]
      summary: 我的通知
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: sort, in: query, schema: { type: string, default: -createdAt } }
        - { name: depth, in: query, schema: { type: integer, default: 2 } }
      responses:
        '200':
          description: 列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  docs: { type: array, items: { $ref: '#/components/schemas/Notification' } }
  /api/notifications/{id}:
    patch:
      tags: [通知]
      summary: 标记已读
      security: [{ bearerAuth: [] }]
      parameters:
        - { name: id, in: path, required: true, schema: { type: integer } }
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                read: { type: boolean }
      responses:
        '200': { description: 已更新 }
  /api/reports:
    post:
      tags: [治理与配置]
      summary: 举报内容或评论
      security: [{ bearerAuth: [] }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required: [targetType, reason]
              properties:
                targetType: { type: string, enum: [content, comment] }
                targetContent: { type: integer }
                targetComment: { type: integer }
                reason: { type: string, enum: [spam, illegal, infringement, abuse, other] }
                details: { type: string, maxLength: 500 }
      responses:
        '201': { description: 已提交 }
  /api/tags:
    get:
      tags: [治理与配置]
      summary: 标签列表
      responses:
        '200':
          description: 列表
          content:
            application/json:
              schema:
                type: object
                properties:
                  docs: { type: array, items: { $ref: '#/components/schemas/Tag' } }
  /api/categories:
    get:
      tags: [治理与配置]
      summary: 分类列表
      responses:
        '200': { description: 列表 }
  /api/globals/site-settings:
    get:
      tags: [治理与配置]
      summary: 站点设置
      responses:
        '200':
          description: 站点名称、标语、导航、页脚等
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    User:
      type: object
      properties:
        id: { type: integer }
        name: { type: string }
        email: { type: string }
        role: { type: string, enum: [admin, editor, user] }
        avatar: { type: object, nullable: true }
        bio: { type: string, nullable: true }
        status: { type: string, enum: [active, muted, banned] }
        createdAt: { type: string, format: date-time }
    Media:
      type: object
      properties:
        id: { type: integer }
        url: { type: string }
        filename: { type: string }
        mimeType: { type: string }
        filesize: { type: integer }
        width: { type: integer }
        height: { type: integer }
    Content:
      type: object
      properties:
        id: { type: integer }
        title: { type: string }
        type: { type: string, enum: [article, post, video, resource] }
        slug: { type: string }
        excerpt: { type: string, nullable: true }
        cover: { allOf: [{ $ref: '#/components/schemas/Media' }], nullable: true }
        content: { type: object, nullable: true, description: Lexical 富文本 JSON }
        videoUrl: { type: string, nullable: true }
        attachments: { type: array, items: { $ref: '#/components/schemas/Media' } }
        author: { allOf: [{ $ref: '#/components/schemas/User' }], nullable: true }
        tags: { type: array, items: { $ref: '#/components/schemas/Tag' } }
        visibility: { type: string, enum: [public, loggedIn, private] }
        viewCount: { type: integer }
        likeCount: { type: integer }
        commentCount: { type: integer }
        bookmarkCount: { type: integer }
        downloadCount: { type: integer }
        featured: { type: boolean }
        publishedAt: { type: string, format: date-time, nullable: true }
        _status: { type: string, enum: [draft, published] }
        createdAt: { type: string, format: date-time }
        updatedAt: { type: string, format: date-time }
    Comment:
      type: object
      properties:
        id: { type: integer }
        content: { type: integer }
        author: { allOf: [{ $ref: '#/components/schemas/User' }], nullable: true }
        parent: { type: integer, nullable: true }
        body: { type: string }
        status: { type: string, enum: [approved, pending, rejected] }
        createdAt: { type: string, format: date-time }
    Like:
      type: object
      properties:
        id: { type: integer }
        user: { type: integer }
        content: { type: integer }
        createdAt: { type: string, format: date-time }
    Bookmark:
      type: object
      properties:
        id: { type: integer }
        user: { type: integer }
        content:
          oneOf:
            - { $ref: '#/components/schemas/Content' }
            - { type: integer }
        createdAt: { type: string, format: date-time }
    Notification:
      type: object
      properties:
        id: { type: integer }
        recipient: { type: integer }
        actor: { allOf: [{ $ref: '#/components/schemas/User' }], nullable: true }
        type: { type: string, enum: [comment, reply, like, system] }
        content: { allOf: [{ $ref: '#/components/schemas/Content' }], nullable: true }
        read: { type: boolean }
        createdAt: { type: string, format: date-time }
    Tag:
      type: object
      properties:
        id: { type: integer }
        name: { type: string }
        slug: { type: string }
