Discussion.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /**
  33. * 注释记录(type='note')会被注入到所挂句子的阅读页里(PaliContentService::
  34. * injectAnnotationNotes),阅读页按 (book, para, channel) 缓存,所以增改删 note
  35. * 都要清掉那一段的缓存。改之前是 note、改之后不是(或反过来)的也要清。
  36. */
  37. protected static function booted(): void
  38. {
  39. $forget = function (Discussion $discussion) {
  40. $wasNote = $discussion->getOriginal('type') === 'note';
  41. if ($discussion->type !== 'note' && ! $wasNote) {
  42. return;
  43. }
  44. if ($discussion->res_type !== 'sentence' || empty($discussion->res_id)) {
  45. return;
  46. }
  47. $sentence = Sentence::where('uid', $discussion->res_id)
  48. ->first(['book_id', 'paragraph', 'channel_uid']);
  49. if (! $sentence) {
  50. return;
  51. }
  52. PaliContentService::forgetParagraph(
  53. (int) $sentence->book_id,
  54. (int) $sentence->paragraph,
  55. (string) $sentence->channel_uid
  56. );
  57. };
  58. static::saved($forget);
  59. static::deleted($forget);
  60. }
  61. // 设置默认值
  62. protected $attributes = [
  63. 'content_type' => 'markdown',
  64. 'type' => 'discussion',
  65. ];
  66. }