Parcourir la source

✨ (api-v13): v3 术语的 channel 改用 ChannelService 摘要,加 type / lang / studio_id / status

整页一次 ChannelService::summaries(),取代预加载 channel 关系(只有 uid、name);
空串 channal 的老数据由 summaries() 的 uuid 判断兜住。字段只增不减。
重新生成 OpenAPI 规格与 dashboard-v6 的 schema.d.ts。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
visuddhinanda il y a 22 heures
Parent
commit
0795aa58ee

+ 6 - 4
api-v13/app/Http/Resources/V3/TermResource.php

@@ -4,6 +4,7 @@ namespace App\Http\Resources\V3;
 
 use App\Http\Resources\Concerns\ResolvesActor;
 use App\Models\DhammaTerm;
+use App\Services\ChannelService;
 use App\Services\Markup\MarkupService;
 use App\Services\V3\TermService;
 use Illuminate\Http\JsonResponse;
@@ -12,10 +13,11 @@ use Illuminate\Http\Request;
 /**
  * 术语 / 百科词条(`dhamma_terms` 表)。
  *
- * **本类不查库。** 编辑者、协作者、分类从预加载的关系上取;`abilities`、`summary_text`、`body`
+ * **本类不查库。** 编辑者、协作者、分类从预加载的关系上取;`channel`、`abilities`、`summary_text`、`body`
  * 由 {@see TermService} 挂上。
  *
- * - `id` 是 guid(不暴露雪花 id);`channel_id` 为空的是 studio 级术语,`channel` 是它的名字摘要
+ * - `id` 是 guid(不暴露雪花 id);`channel_id` 为空的是 studio 级术语,`channel` 是所属 channel 的摘要({@see ChannelService::summary()},
+ *   `studio_id` 是 channel 拥有者的 uuid)
  * - `summary` / `note` 是源码,方言见 `content_type`;`body` 是渲染后的「摘要 + 正文」节点树({@see MarkupService}),
  *   单条里有,按词查带 `with_body` 时每条也有;单条带 `format=html` 时换成 HTML 字符串 `html`(给 WebView 显示的客户端)
  * - `summary_text` 是纯文本摘要:有 summary 用它,没有取正文第一段
@@ -37,7 +39,7 @@ class TermResource extends BaseResource
      *     other_meaning: string|null,
      *     language: string,
      *     channel_id: string|null,
-     *     channel: array{id: string, name: string}|null,
+     *     channel: array{id: string, name: string, type: string, lang: string, studio_id: string, status: int}|null,
      *     owner_id: string,
      *     quality: string|null,
      *     content_type: string,
@@ -69,7 +71,7 @@ class TermResource extends BaseResource
             'other_meaning' => $this->other_meaning,
             'language' => (string) $this->language,
             'channel_id' => $this->channelUid(),
-            'channel' => $this->channel ? ['id' => (string) $this->channel->uid, 'name' => (string) $this->channel->name] : null,
+            'channel' => $this->channel_summary ?? null,
             'owner_id' => (string) $this->owner,
             'quality' => $this->quality,
             'content_type' => $this->effectiveContentType(),

+ 7 - 9
api-v13/app/Services/V3/TermService.php

@@ -7,6 +7,7 @@ use App\Facades\RedisCache;
 use App\Models\Channel;
 use App\Models\DhammaTerm;
 use App\Models\TermCategory;
+use App\Services\ChannelService;
 use App\Services\Markup\Format;
 use App\Services\Markup\MarkupService;
 use App\Services\Markup\RenderOptions;
@@ -60,6 +61,7 @@ class TermService
         private readonly AgentGrantService $agents,
         private readonly MarkupService $markup,
         private readonly UserService $users,
+        private readonly ChannelService $channelSummaries,
     ) {}
 
     /* ------------------------------------------------------------------ 读 */
@@ -475,21 +477,17 @@ class TermService
     }
 
     /**
-     * 预加载 {@see self::relations()} 与所属 channel。
-     *
-     * channel 只给真属于某个 channel 的加载:studio 级的老数据 channal 是空串,
-     * 统一 `with('channel')` 会拿空串去比 uuid 列,PostgreSQL 直接报错。
+     * 预加载 {@see self::relations()},并挂上所属 channel 的摘要 `channel_summary`({@see ChannelService::summaries()},
+     * 整批一次查询;studio 级术语与 channel 已不存在的是 null)。
      *
      * @param  EloquentCollection<int, DhammaTerm>  $terms
      */
     private function loadRelations(EloquentCollection $terms): void
     {
         $terms->load($this->relations());
-        $terms->filter(fn (DhammaTerm $term) => $term->channelUid() !== null)->load('channel:uid,name');
-        $terms->each(function (DhammaTerm $term) {
-            if ($term->channelUid() === null) {
-                $term->setRelation('channel', null);
-            }
+        $summaries = $this->channelSummaries->summaries($terms->map(fn (DhammaTerm $term) => $term->channelUid())->all());
+        $terms->values()->each(function (DhammaTerm $term, int $i) use ($summaries) {
+            $term->channel_summary = $summaries[$i] ?: null;
         });
     }
 

+ 2 - 0
api-v13/tests/Feature/TermV3Test.php

@@ -155,6 +155,8 @@ it('shows a term with ETag, and hides terms of private channels', function () {
     expect($res->headers->get('ETag'))->toBe($term['etag']);
     expect($res->json('data.abilities'))->toBe(['update' => false, 'delete' => false]);
     expect($res->json('data.summary_text'))->toBe('正文第一段。');
+    expect($res->json('data.channel'))->toMatchArray(['id' => $channel, 'studio_id' => $owner])
+        ->toHaveKeys(['name', 'type', 'lang', 'status']);
 
     Channel::where('uid', $channel)->update(['status' => Channel::STATUS_PRIVATE]);
     $this->getJson("/api/v3/terms/{$term['id']}")->assertForbidden();

+ 40 - 0
dashboard-v6/src/api/schema.d.ts

@@ -37927,6 +37927,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -38043,6 +38047,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -38104,6 +38112,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42072,6 +42084,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42191,6 +42207,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42252,6 +42272,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42414,6 +42438,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42498,6 +42526,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42583,6 +42615,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;
@@ -42696,6 +42732,10 @@ export interface operations {
                             channel?: {
                                 id?: string;
                                 name?: string;
+                                type?: string;
+                                lang?: string;
+                                studio_id?: string;
+                                status?: number;
                             } | null;
                             owner_id?: string;
                             quality?: string | null;

+ 9 - 1
openapi/public/assets/protocol/resources/auto/v3-channels-channel-terms.yaml

@@ -111,7 +111,7 @@ get:
                       nullable: true
                     channel:
                       type: object
-                      properties: { id: { type: string }, name: { type: string } }
+                      properties: { id: { type: string }, name: { type: string }, type: { type: string }, lang: { type: string }, studio_id: { type: string }, status: { type: integer } }
                       nullable: true
                     owner_id:
                       type: string
@@ -277,6 +277,10 @@ post:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string
@@ -376,6 +380,10 @@ post:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string

+ 9 - 1
openapi/public/assets/protocol/resources/auto/v3-studios-studio-terms.yaml

@@ -115,7 +115,7 @@ get:
                       nullable: true
                     channel:
                       type: object
-                      properties: { id: { type: string }, name: { type: string } }
+                      properties: { id: { type: string }, name: { type: string }, type: { type: string }, lang: { type: string }, studio_id: { type: string }, status: { type: integer } }
                       nullable: true
                     owner_id:
                       type: string
@@ -287,6 +287,10 @@ post:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string
@@ -386,6 +390,10 @@ post:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string

+ 12 - 0
openapi/public/assets/protocol/resources/auto/v3-terms-term.yaml

@@ -80,6 +80,10 @@ get:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string
@@ -254,6 +258,10 @@ patch:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string
@@ -396,6 +404,10 @@ delete:
                     properties:
                       id: { type: string }
                       name: { type: string }
+                      type: { type: string }
+                      lang: { type: string }
+                      studio_id: { type: string }
+                      status: { type: integer }
                     nullable: true
                   owner_id:
                     type: string

+ 1 - 1
openapi/public/assets/protocol/resources/auto/v3-terms.yaml

@@ -89,7 +89,7 @@ get:
                       nullable: true
                     channel:
                       type: object
-                      properties: { id: { type: string }, name: { type: string } }
+                      properties: { id: { type: string }, name: { type: string }, type: { type: string }, lang: { type: string }, studio_id: { type: string }, status: { type: integer } }
                       nullable: true
                     owner_id:
                       type: string