TermResource.php 4.3 KB

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