SentResource.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\MdRender;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Api\SuggestionApi;
  7. use App\Http\Api\UserApi;
  8. use Illuminate\Contracts\Support\Arrayable;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Resources\Json\JsonResource;
  11. use Illuminate\Support\Facades\Log;
  12. use Illuminate\Support\Str;
  13. class SentResource extends JsonResource
  14. {
  15. /**
  16. * Transform the resource into an array.
  17. *
  18. * @param Request $request
  19. * @return array|Arrayable|\JsonSerializable
  20. */
  21. public function toArray($request)
  22. {
  23. $channel = ChannelApi::getById($this->channel_uid);
  24. if (! $channel) {
  25. Log::error('channel left', ['data' => $this->channel_uid, 'uid' => $this->uid]);
  26. }
  27. if ($request->input('mode', 'read') === 'read') {
  28. $mode = 'read';
  29. } else {
  30. $mode = 'edit';
  31. }
  32. $data = [
  33. 'id' => $this->uid,
  34. 'content' => $this->content,
  35. 'content_type' => $this->content_type,
  36. 'html' => '',
  37. 'book' => $this->book_id,
  38. 'paragraph' => $this->paragraph,
  39. 'word_start' => $this->word_start,
  40. 'word_end' => $this->word_end,
  41. 'editor' => UserApi::getByUuid($this->editor_uid),
  42. 'fork_at' => $this->fork_at,
  43. 'updated_at' => $this->updated_at,
  44. ];
  45. if ($channel) {
  46. $data['channel'] = $channel;
  47. $data['studio'] = StudioApi::getById($channel['studio_id']);
  48. }
  49. if ($request->has('channels')) {
  50. $channels = explode(',', $request->input('channels'));
  51. } else {
  52. $channels = [$this->channel_uid];
  53. }
  54. // TODO 找出channel id = '' 的原因
  55. $mChannels = [];
  56. foreach ($channels as $key => $value) {
  57. if (Str::isUuid($value)) {
  58. $mChannels[] = $value;
  59. }
  60. }
  61. if ($request->input('html', true)) {
  62. $data['html'] = MdRender::render(
  63. $this->content,
  64. $mChannels,
  65. null,
  66. $mode,
  67. $channel['type'],
  68. $this->content_type,
  69. $request->input('format', 'react')
  70. );
  71. }
  72. if ($request->input('mode') === 'edit' || $request->input('mode') === 'wbw') {
  73. $data['suggestionCount'] = SuggestionApi::getCountBySent(
  74. $this->book_id,
  75. $this->paragraph,
  76. $this->word_start,
  77. $this->word_end,
  78. $this->channel_uid
  79. );
  80. }
  81. if (isset($this->acceptor_uid) && ! empty($this->acceptor_uid)) {
  82. $data['acceptor'] = UserApi::getByUuid($this->acceptor_uid);
  83. $data['pr_edit_at'] = $this->pr_edit_at;
  84. }
  85. return $data;
  86. }
  87. }