TermResource.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Http\Resources\Concerns\ResolvesActor;
  4. use App\Models\DhammaTerm;
  5. use App\Services\ChannelService;
  6. use App\Services\Markup\MarkupService;
  7. use App\Services\V3\TermService;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Http\Request;
  10. /**
  11. * 术语 / 百科词条(`dhamma_terms` 表)。
  12. *
  13. * **本类不查库。** 编辑者、协作者、分类从预加载的关系上取;`channel`、`abilities`、`summary_text`、`body`
  14. * 由 {@see TermService} 挂上。
  15. *
  16. * - `id` 是 guid(不暴露雪花 id);`channel_id` 为空的是 studio 级术语,`channel` 是所属 channel 的摘要({@see ChannelService::summary()},
  17. * `studio_id` 是 channel 拥有者的 uuid)
  18. * - `summary` / `note` 是源码,方言见 `content_type`;`body` 是渲染后的「摘要 + 正文」节点树({@see MarkupService}),
  19. * 单条里有,按词查带 `with_body` 时每条也有;单条带 `format=html` 时换成 HTML 字符串 `html`(给 WebView 显示的客户端)
  20. * - `summary_text` 是纯文本摘要:有 summary 用它,没有取正文第一段
  21. * - `categories` 取自正文的 `{{category|…}}`
  22. * - `etag` 是乐观锁的版本标记:修改时原样放进 `If-Match` 请求头(单条响应也带 `ETag` 头)
  23. *
  24. * @mixin DhammaTerm
  25. */
  26. class TermResource extends BaseResource
  27. {
  28. use ResolvesActor;
  29. /**
  30. * @return array{
  31. * id: string,
  32. * word: string,
  33. * tag: string|null,
  34. * meaning: string,
  35. * other_meaning: string|null,
  36. * language: string,
  37. * channel_id: string|null,
  38. * channel: array{id: string, name: string, type: string, lang: string, studio_id: string, status: int}|null,
  39. * owner_id: string,
  40. * quality: string|null,
  41. * content_type: string,
  42. * summary: string|null,
  43. * note: string|null,
  44. * redirect: string|null,
  45. * categories: list<string>,
  46. * summary_text: string,
  47. * body?: list<MarkupNode>,
  48. * html?: string,
  49. * editor: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  50. * co_editor: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  51. * etag: string,
  52. * abilities: array{update: bool, delete: bool},
  53. * created_at: string,
  54. * updated_at: string,
  55. * deleted_at: string|null
  56. * }
  57. */
  58. public function toArray(Request $request): array
  59. {
  60. $tag = trim((string) $this->tag);
  61. return [
  62. 'id' => (string) $this->guid,
  63. 'word' => (string) $this->word,
  64. 'tag' => $tag === '' ? null : $tag,
  65. 'meaning' => (string) $this->meaning,
  66. 'other_meaning' => $this->other_meaning,
  67. 'language' => (string) $this->language,
  68. 'channel_id' => $this->channelUid(),
  69. 'channel' => $this->channel_summary ?? null,
  70. 'owner_id' => (string) $this->owner,
  71. 'quality' => $this->quality,
  72. 'content_type' => $this->effectiveContentType(),
  73. 'summary' => $this->summary,
  74. 'note' => $this->note,
  75. 'redirect' => $this->redirect !== '' ? $this->redirect : null,
  76. 'categories' => $this->categories->pluck('category')->values()->all(),
  77. 'summary_text' => (string) ($this->summary_text ?? ''),
  78. 'body' => $this->when(isset($this->body), fn () => $this->body),
  79. 'html' => $this->when(isset($this->html), fn () => $this->html),
  80. 'editor' => $this->actor($this->user, $this->aiModel),
  81. 'co_editor' => $this->coEditor ? $this->actor(null, $this->coEditor) : null,
  82. 'etag' => $this->etag(),
  83. 'abilities' => [
  84. 'update' => (bool) ($this->abilities['update'] ?? false),
  85. 'delete' => (bool) ($this->abilities['delete'] ?? false),
  86. ],
  87. 'created_at' => $this->created_at?->toIso8601ZuluString(),
  88. 'updated_at' => $this->updated_at?->toIso8601ZuluString(),
  89. 'deleted_at' => $this->deleted_at?->toIso8601ZuluString(),
  90. ];
  91. }
  92. /**
  93. * 单条响应带 ETag 头(列表不带)。
  94. */
  95. public function withResponse(Request $request, JsonResponse $response): void
  96. {
  97. $response->header('ETag', $this->etag());
  98. }
  99. }