ChannelService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Channel;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Str;
  6. /**
  7. * channel 的纯能力(v2 / v3 共享):按 uid / 名字取 channel、取系统 channel、建用户自定义书的 channel。
  8. *
  9. * 只放与请求、响应形状、权限无关的逻辑。**权限判定不在这里**:
  10. * v3 用 {@see V3\ChannelAbilityService}(认团队角色与共享),
  11. * v2 的旧口径(只认拥有者本人 + 共享)留在 `App\Http\Api\ChannelApi`,随 v4 下线删除。
  12. *
  13. * 取代 `App\Http\Api\ChannelApi` 的同名逻辑,那边只剩转发壳。
  14. */
  15. class ChannelService
  16. {
  17. /**
  18. * channel 摘要;不是 uuid 或不存在返回 false。
  19. *
  20. * `studio_id` 是 owner_uid(v2 的字段名,转发壳要原样返回)。
  21. *
  22. * @return array{id: string, name: string, type: string, lang: string, studio_id: string, status: int}|false
  23. */
  24. public function summary(?string $uid): array|false
  25. {
  26. if (! Str::isUuid($uid)) {
  27. return false;
  28. }
  29. $channel = Channel::where('uid', $uid)->first(['uid', 'name', 'type', 'lang', 'owner_uid', 'status']);
  30. return $channel ? $this->toSummary($uid, $channel) : false;
  31. }
  32. /**
  33. * 批量取摘要,按传入顺序一一对应;查不到的位置是 false。一次查询。
  34. *
  35. * @param array<int, string|null> $uids
  36. * @return list<array{id: string, name: string, type: string, lang: string, studio_id: string, status: int}|false>
  37. */
  38. public function summaries(array $uids): array
  39. {
  40. $valid = array_values(array_filter($uids, fn ($uid) => Str::isUuid($uid)));
  41. $found = $valid === []
  42. ? collect()
  43. : Channel::whereIn('uid', array_unique($valid))
  44. ->get(['uid', 'name', 'type', 'lang', 'owner_uid', 'status'])
  45. ->keyBy(fn (Channel $channel) => (string) $channel->uid);
  46. $result = [];
  47. foreach ($uids as $uid) {
  48. $channel = Str::isUuid($uid) ? $found->get($uid) : null;
  49. $result[] = $channel ? $this->toSummary($uid, $channel) : false;
  50. }
  51. return $result;
  52. }
  53. /**
  54. * 系统 channel(root 账号名下、按名字找)的 uid;找不到时试 $fallback,都没有返回 false。
  55. */
  56. public function systemUid(string $name, string $fallback = ''): string|false
  57. {
  58. $uid = $this->rootChannelUid($name);
  59. if ($uid === false && $fallback !== '') {
  60. $uid = $this->rootChannelUid($fallback);
  61. }
  62. return $uid;
  63. }
  64. /**
  65. * 按名字取 channel(不限拥有者,取第一条)。
  66. *
  67. * @throws \Exception channel 不存在
  68. */
  69. public function byNameOrFail(string $name): Channel
  70. {
  71. $channel = Channel::where('name', $name)->first();
  72. if (! $channel) {
  73. throw new \Exception('channel is invalid');
  74. }
  75. return $channel;
  76. }
  77. /**
  78. * 某个 studio 某个语言的「自定义书」channel(`_user_book_{lang}`)的 uid,没有就建一个。
  79. * 建失败返回 false。
  80. */
  81. public function userBookUid(string $studioUid, string $lang, int $status): string|false
  82. {
  83. $name = '_user_book_'.$lang;
  84. $existing = Channel::where('owner_uid', $studioUid)->where('name', $name)->value('uid');
  85. if ($existing) {
  86. return (string) $existing;
  87. }
  88. $channel = new Channel;
  89. $channel->id = app('snowflake')->id();
  90. $channel->uid = (string) Str::uuid();
  91. $channel->owner_uid = $studioUid;
  92. $channel->name = $name;
  93. $channel->type = 'original';
  94. $channel->lang = $lang;
  95. $channel->editor_id = 0;
  96. $channel->is_system = true;
  97. $channel->create_time = time() * 1000;
  98. $channel->modify_time = time() * 1000;
  99. $channel->status = $status;
  100. if (! $channel->save()) {
  101. Log::error('copy user book : create channel fail.', ['channel' => $name, 'studioId' => $studioUid]);
  102. return false;
  103. }
  104. Log::debug('copy user book : create channel success name='.$name);
  105. return $channel->uid;
  106. }
  107. private function rootChannelUid(string $name): string|false
  108. {
  109. $uid = Channel::where('name', $name)
  110. ->where('owner_uid', config('mint.admin.root_uuid'))
  111. ->value('uid');
  112. return $uid ? (string) $uid : false;
  113. }
  114. /**
  115. * @return array{id: string, name: string, type: string, lang: string, studio_id: string, status: int}
  116. */
  117. private function toSummary(string $uid, Channel $channel): array
  118. {
  119. return [
  120. 'id' => $uid,
  121. 'name' => $channel->name,
  122. 'type' => $channel->type,
  123. 'lang' => $channel->lang,
  124. 'studio_id' => $channel->owner_uid,
  125. 'status' => $channel->status,
  126. ];
  127. }
  128. }