ReactionResource.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Http\Resources\Concerns\ResolvesActor;
  4. use App\Models\Reaction;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 一条 reaction 记录。
  8. *
  9. * user 是操作者的公开摘要。操作者可能是人类用户(`user_infos`)也可能是 AI 助手
  10. * (`ai_models`),两张表的形状不同,这里只承诺稳定的核心字段。
  11. *
  12. * **本类不查库。** 操作者从 `user` / `aiModel` 两条已预加载的关系上取——
  13. * 控制器负责 `->with([...])`,见 {@see Reaction::user()}。
  14. * {@see ResolvesActor::actor()} 只把已加载的模型拼成摘要,不碰数据库。
  15. */
  16. class ReactionResource extends BaseResource
  17. {
  18. use ResolvesActor;
  19. /**
  20. * @param Request $request
  21. * @return array{
  22. * id: string,
  23. * type: string,
  24. * target_id: string,
  25. * target_type: string,
  26. * context: string|null,
  27. * created_at: string,
  28. * updated_at: string,
  29. * user: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null
  30. * }
  31. */
  32. public function toArray($request): array
  33. {
  34. return [
  35. 'id' => $this->id,
  36. 'type' => $this->type,
  37. 'target_id' => $this->target_id,
  38. 'target_type' => $this->target_type,
  39. 'context' => $this->context,
  40. 'created_at' => $this->created_at,
  41. 'updated_at' => $this->updated_at,
  42. 'user' => $this->actor($this->user, $this->aiModel),
  43. ];
  44. }
  45. }