2
0

Discussion.php 10 KB

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