Discussion.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. class Discussion extends Model
  7. {
  8. use HasFactory;
  9. protected $primaryKey = 'id';
  10. protected $casts = [
  11. 'id' => 'string',
  12. 'pos_start' => 'integer',
  13. 'pos_end' => 'integer',
  14. ];
  15. // 批量填充
  16. protected $fillable = [
  17. 'res_id',
  18. 'res_type',
  19. 'type',
  20. 'tpl_id',
  21. 'title',
  22. 'content',
  23. 'content_type',
  24. 'parent',
  25. 'editor_uid',
  26. 'pos_start',
  27. 'pos_end',
  28. 'quote_exact',
  29. 'quote_prefix',
  30. 'quote_suffix',
  31. ];
  32. /** 注释书对应:content 是下一层句子模板,渲染时带 <cite> 出处 */
  33. public const TYPE_COMMENTARY = 'commentary';
  34. /** 普通边注:content 就是注解正文,没有出处 */
  35. public const TYPE_NOTE = 'note';
  36. /** 会被注入阅读页的批注类型 */
  37. public const INJECTED_TYPES = [self::TYPE_COMMENTARY, self::TYPE_NOTE];
  38. /**
  39. * 这两类批注会被注入到所挂句子的阅读页里(PaliContentService::
  40. * injectAnnotationNotes),阅读页按 (book, para, channel) 缓存,所以增改删它们
  41. * 都要清掉那一段的缓存。改之前是、改之后不是(或反过来)的也要清。
  42. */
  43. protected static function booted(): void
  44. {
  45. $forget = function (Discussion $discussion) {
  46. $wasInjected = in_array($discussion->getOriginal('type'), self::INJECTED_TYPES, true);
  47. if (! in_array($discussion->type, self::INJECTED_TYPES, true) && ! $wasInjected) {
  48. return;
  49. }
  50. if ($discussion->res_type !== 'sentence' || empty($discussion->res_id)) {
  51. return;
  52. }
  53. $sentence = Sentence::where('uid', $discussion->res_id)
  54. ->first(['book_id', 'paragraph', 'channel_uid']);
  55. if (! $sentence) {
  56. return;
  57. }
  58. PaliContentService::forgetParagraph(
  59. (int) $sentence->book_id,
  60. (int) $sentence->paragraph,
  61. (string) $sentence->channel_uid
  62. );
  63. };
  64. static::saved($forget);
  65. static::deleted($forget);
  66. }
  67. // 设置默认值
  68. protected $attributes = [
  69. 'content_type' => 'markdown',
  70. 'type' => 'discussion',
  71. ];
  72. }