DiscussionResource.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Http\Resources\Concerns\ResolvesActor;
  4. use App\Models\Discussion;
  5. use App\Services\Markup\MarkupService;
  6. use App\Services\Markup\Render\TreeRenderer;
  7. use App\Services\V3\DiscussionService;
  8. use Illuminate\Http\Request;
  9. /**
  10. * 一条评论:话题(parent_id 为 null)或回复;或一条批注(note / commentary);或一条译文评估
  11. * (assessment:score 是 0–100 的分数,severity 是评估方法,content 是理由)。
  12. *
  13. * 批注与 issue / glossary 可带锚点(pos_start / pos_end / quote_*),指明它挂在句子文本的
  14. * 哪一段;其它评论线程的这几个字段为 null。glossary 的 title 是术语词头。
  15. *
  16. * 删除了但还挂着回复的话题以「墓碑」出现:deleted_at 有值,title / content 为 null,
  17. * 回复照常可读(与 Google Drive 的 comments.deleted、GitHub 的 deletedAt 同一做法)。
  18. *
  19. * editor 是对内容负责的人;AI 凭人类授权代写时,co_editor 是那个模型(仿 git 的
  20. * Co-Authored-By)。存量数据里 AI 写的行没有 co_editor,editor 直接是模型。
  21. *
  22. * **本类不查库。** 编辑者从预加载的 `user` / `aiModel` / `coEditor` 关系上取;权限与正文渲染
  23. * 由 {@see DiscussionService} 算好挂在模型的 `abilities` / `rendered` 属性上。
  24. *
  25. * `sentence` 只在按经文范围列出时有(`GET /v3/tipitaka-discussions/{channel}`):所在句子的坐标,
  26. * 由查询顺带取出(`sentence_*` 属性),本类不另查。
  27. *
  28. * `body` 是渲染后的正文({@see MarkupService} 的节点树,节点形状见
  29. * {@see TreeRenderer}),`summary` 是纯文本摘要;两者只在挂在句子上、
  30. * 未删除、有正文时给(正文里的模板要用句子所在的 channel 解析)。取代了 v3 早期的 `html`
  31. * (v2 MdRender 的 MdTpl HTML 字符串)。
  32. *
  33. * `content_type` 是正文实际的方言:v3 写入的是 `markdown-e2`(存在 content_type_v2 列),
  34. * v2 写入的是原 content_type(markdown / text / html),见 {@see Discussion::effectiveContentType()}。
  35. *
  36. * 字段名改了两处(v2 → v3):res_type / res_id → target_type / target_id,
  37. * parent → parent_id;children_count → reply_count(只有话题有)。
  38. *
  39. * @mixin Discussion
  40. */
  41. class DiscussionResource extends BaseResource
  42. {
  43. use ResolvesActor;
  44. /**
  45. * @param Request $request
  46. * @return array{
  47. * id: string,
  48. * target_type: string,
  49. * target_id: string,
  50. * parent_id: string|null,
  51. * type: string,
  52. * severity: string|null,
  53. * score: int|null,
  54. * status: string,
  55. * title: string|null,
  56. * content: string|null,
  57. * content_type: string,
  58. * body?: list<MarkupNode>,
  59. * summary?: string,
  60. * tpl_id: string|null,
  61. * pos_start: int|null,
  62. * pos_end: int|null,
  63. * quote_exact: string|null,
  64. * quote_prefix: string|null,
  65. * quote_suffix: string|null,
  66. * reply_count?: int,
  67. * sentence?: array{book: int, paragraph: int, word_start: int, word_end: int},
  68. * editor: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  69. * co_editor: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  70. * abilities: array{update: bool, delete: bool, close: bool, reply: bool},
  71. * created_at: string,
  72. * updated_at: string,
  73. * deleted_at: string|null
  74. * }
  75. */
  76. public function toArray($request): array
  77. {
  78. $tombstone = $this->trashed();
  79. return [
  80. 'id' => (string) $this->id,
  81. 'target_type' => $this->res_type,
  82. 'target_id' => $this->res_id,
  83. 'parent_id' => $this->parent,
  84. 'type' => $this->type,
  85. 'severity' => $this->severity,
  86. 'score' => $this->score,
  87. 'status' => $this->status,
  88. 'title' => $tombstone ? null : $this->title,
  89. 'content' => $tombstone ? null : $this->content,
  90. 'content_type' => $this->effectiveContentType(),
  91. 'body' => $this->when($this->rendered !== null, fn () => $this->rendered['body']),
  92. 'summary' => $this->when($this->rendered !== null, fn () => $this->rendered['summary']),
  93. 'tpl_id' => $this->tpl_id,
  94. 'pos_start' => $this->pos_start,
  95. 'pos_end' => $this->pos_end,
  96. 'quote_exact' => $tombstone ? null : $this->quote_exact,
  97. 'quote_prefix' => $tombstone ? null : $this->quote_prefix,
  98. 'quote_suffix' => $tombstone ? null : $this->quote_suffix,
  99. 'reply_count' => $this->when($this->parent === null, fn () => (int) $this->reply_count),
  100. 'sentence' => $this->when(isset($this->sentence_book), fn () => [
  101. 'book' => (int) $this->sentence_book,
  102. 'paragraph' => (int) $this->sentence_paragraph,
  103. 'word_start' => (int) $this->sentence_word_start,
  104. 'word_end' => (int) $this->sentence_word_end,
  105. ]),
  106. 'editor' => $this->actor($this->user, $this->aiModel),
  107. // AI 代写时的模型;人类自己写的、存量数据都为 null
  108. 'co_editor' => $this->coEditor ? $this->actor(null, $this->coEditor) : null,
  109. 'abilities' => $this->abilities ?? ['update' => false, 'delete' => false, 'close' => false, 'reply' => false],
  110. 'created_at' => $this->created_at,
  111. 'updated_at' => $this->updated_at,
  112. 'deleted_at' => $this->deleted_at,
  113. ];
  114. }
  115. }