Discussion.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Models;
  3. use App\Services\PaliContentService;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. /**
  10. * 表 `discussions`。
  11. *
  12. * 这一张表装着几种东西:评论线程(discussion / qa / help / issue)、阅读页批注
  13. * (note / commentary,会被注入阅读页),以及 v2 TaskStatusController 写进来的任务
  14. * 状态日志(res_type=task)。v3 的 `/v3/discussions` 管前两种({@see self::COMMENT_TYPES})。
  15. *
  16. * 2026-10-15 起软删除:删除话题时这一行留作「墓碑」,回复不受影响。
  17. */
  18. class Discussion extends Model
  19. {
  20. use HasFactory;
  21. use SoftDeletes;
  22. protected $primaryKey = 'id';
  23. /**
  24. * 主键是 uuid,由库的默认值生成(所以 incrementing 仍为 true:靠 insert … returning id
  25. * 拿回主键)。keyType 必须声明为 string:默认的 int 会让 whereIn 走 whereIntegerInRaw,
  26. * 把 uuid 转成整数(loadCount / 预加载时出现 `id in (2)`)。
  27. */
  28. protected $keyType = 'string';
  29. protected $casts = [
  30. 'id' => 'string',
  31. 'pos_start' => 'integer',
  32. 'pos_end' => 'integer',
  33. ];
  34. // 批量填充
  35. protected $fillable = [
  36. 'res_id',
  37. 'res_type',
  38. 'type',
  39. 'tpl_id',
  40. 'title',
  41. 'content',
  42. 'content_type',
  43. 'parent',
  44. 'editor_uid',
  45. 'pos_start',
  46. 'pos_end',
  47. 'quote_exact',
  48. 'quote_prefix',
  49. 'quote_suffix',
  50. ];
  51. /**
  52. * 义注 / 复注对应(巴利注释书,不是普通评论):content 是注释书的句子模板
  53. * `{{book-para-start-end}}`(可多句并列),渲染成那几句的译文并带 <cite> 出处。
  54. */
  55. public const TYPE_COMMENTARY = 'commentary';
  56. /** commentary 的 content 格式:一个或多个句子模板(与 PaliContentService 注入时的判定一致) */
  57. public const COMMENTARY_PATTERN = '/^(?:\{\{\d+-\d+-\d+-\d+\}\}\s*)+$/';
  58. /** 普通边注:content 就是注解正文,没有出处 */
  59. public const TYPE_NOTE = 'note';
  60. /** 批注类型:作为边注注入阅读页,没有回复以外的线程属性(开关、severity) */
  61. public const INJECTED_TYPES = [self::TYPE_COMMENTARY, self::TYPE_NOTE];
  62. /** 术语讨论:针对句子里某个词的译法提问或建议,带锚点、只能挂在句子上 */
  63. public const TYPE_GLOSSARY = 'glossary';
  64. /** 评论线程:话题 + 一层回复,有开关状态 */
  65. public const THREAD_TYPES = ['discussion', 'qa', 'help', 'issue', self::TYPE_GLOSSARY];
  66. /** 可以带锚点(pos_* / quote_*)、定位到句子文本某一段的类型:批注 + issue / glossary */
  67. public const ANCHORED_TYPES = [self::TYPE_COMMENTARY, self::TYPE_NOTE, 'issue', self::TYPE_GLOSSARY];
  68. /**
  69. * 会被注入阅读页的全部类型({@see PaliContentService::injectAnnotationNotes()}):
  70. * 批注插边注,glossary 换成术语,issue 高亮。issue / glossary 只注入未关闭的。
  71. */
  72. public const READING_TYPES = [self::TYPE_COMMENTARY, self::TYPE_NOTE, 'issue', self::TYPE_GLOSSARY];
  73. /** 只能挂在句子上的类型:批注 + glossary */
  74. public const SENTENCE_ONLY_TYPES = [self::TYPE_COMMENTARY, self::TYPE_NOTE, self::TYPE_GLOSSARY];
  75. /**
  76. * `/v3/discussions` 管的全部类型:评论线程({@see self::THREAD_TYPES})+ 批注
  77. * ({@see self::INJECTED_TYPES})。批注锚定在句子的某个位置、注入阅读页,没有回复和开关。
  78. */
  79. public const COMMENT_TYPES = ['discussion', 'qa', 'help', 'issue', 'glossary', 'note', 'commentary'];
  80. /** 各类型允许的 severity 取值;不在表里的类型不允许设 severity */
  81. public const SEVERITIES = [
  82. 'issue' => ['error', 'warning', 'suggestion'],
  83. 'help' => ['urgent', 'normal'],
  84. ];
  85. /** 话题状态:active 未关闭,close 已关闭(v4 在读这两个值,不能改) */
  86. public const STATUSES = ['active', 'close'];
  87. /**
  88. * 挂靠目标的类型(res_type)。v3 对外叫 target_type。
  89. * chapter / channel / feedback 在两个前端里都没有调用方,不收。
  90. */
  91. public const TARGET_TYPES = ['sentence', 'wbw', 'article', 'term', 'task'];
  92. /**
  93. * 当前查看者能做的动作,由 `App\Services\V3\DiscussionService` 算好后挂上,
  94. * 供 Resource 读取(Resource 不查库)。声明成属性而不是 attribute,不会被存进表。
  95. *
  96. * @var array{update: bool, delete: bool, close: bool, reply: bool}|null
  97. */
  98. public ?array $abilities = null;
  99. /**
  100. * 正文渲染结果(只有挂在句子上的才有,需要句子所在的 channel 才能渲染模板),
  101. * 同样由 Service 算好后挂上。
  102. *
  103. * @var array{html: string, summary: string}|null
  104. */
  105. public ?array $rendered = null;
  106. /**
  107. * 编辑者(人类用户)。editor_uid 对 user_infos.userid。
  108. */
  109. public function user(): BelongsTo
  110. {
  111. return $this->belongsTo(UserInfo::class, 'editor_uid', 'userid');
  112. }
  113. /**
  114. * 编辑者(AI 助手)。与 {@see self::user()} 共用 uuid 空间,二者择一。
  115. */
  116. public function aiModel(): BelongsTo
  117. {
  118. return $this->belongsTo(AiModel::class, 'editor_uid', 'uid');
  119. }
  120. /**
  121. * 协作者:人类授权代写的 AI 模型(co_editor_uid)。此时 editor 是授权人。
  122. * 存量数据里 AI 写的行没有它,editor_uid 直接是模型,见 {@see self::aiModel()}。
  123. */
  124. public function coEditor(): BelongsTo
  125. {
  126. return $this->belongsTo(AiModel::class, 'co_editor_uid', 'uid');
  127. }
  128. /**
  129. * 话题下的回复(只有一层)。软删的回复自动排除。
  130. */
  131. public function replies(): HasMany
  132. {
  133. return $this->hasMany(Discussion::class, 'parent');
  134. }
  135. /**
  136. * 这几类会被注入到所挂句子的阅读页里({@see self::READING_TYPES}),阅读页按
  137. * (book, para, channel) 缓存,所以增改删它们(含关闭 / 重开)都要清掉那一段的缓存。
  138. * 改之前是、改之后不是(或反过来)的也要清。
  139. */
  140. protected static function booted(): void
  141. {
  142. $forget = function (Discussion $discussion) {
  143. $wasInjected = in_array($discussion->getOriginal('type'), self::READING_TYPES, true);
  144. if (! in_array($discussion->type, self::READING_TYPES, true) && ! $wasInjected) {
  145. return;
  146. }
  147. if ($discussion->res_type !== 'sentence' || empty($discussion->res_id)) {
  148. return;
  149. }
  150. $sentence = Sentence::where('uid', $discussion->res_id)
  151. ->first(['book_id', 'paragraph', 'channel_uid']);
  152. if (! $sentence) {
  153. return;
  154. }
  155. PaliContentService::forgetParagraph(
  156. (int) $sentence->book_id,
  157. (int) $sentence->paragraph,
  158. (string) $sentence->channel_uid
  159. );
  160. };
  161. static::saved($forget);
  162. static::deleted($forget);
  163. }
  164. // 设置默认值
  165. protected $attributes = [
  166. 'content_type' => 'markdown',
  167. 'type' => 'discussion',
  168. ];
  169. }