Просмотр файла app/Models/Message.php

Размер файла: 3.38Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use App\Traits\UploadTrait;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\HasMany;
  12. use Illuminate\Database\Eloquent\Relations\MorphMany;
  13. use Illuminate\Support\Collection;
  14.  
  15. /**
  16. * Class Message
  17. *
  18. * @property int $id
  19. * @property int $user_id
  20. * @property int $author_id
  21. * @property string $text
  22. * @property int $created_at
  23. *
  24. * @property-read User $author
  25. * @property-read Collection<File> $files
  26. * @property-read Collection<Dialogue> $dialogues
  27. */
  28. class Message extends BaseModel
  29. {
  30. use UploadTrait;
  31.  
  32. public const IN = 'in'; // Принятые
  33. public const OUT = 'out'; // Отправленные
  34.  
  35. /**
  36. * Indicates if the model should be timestamped.
  37. *
  38. * @var bool
  39. */
  40. public $timestamps = false;
  41.  
  42. /**
  43. * The attributes that aren't mass assignable.
  44. *
  45. * @var array
  46. */
  47. protected $guarded = [];
  48.  
  49. /**
  50. * Morph name
  51. *
  52. * @var string
  53. */
  54. public static $morphName = 'messages';
  55.  
  56. /**
  57. * Директория загрузки файлов
  58. *
  59. * @var string
  60. */
  61. public $uploadPath = '/uploads/messages';
  62.  
  63. /**
  64. * Возвращает связь пользователей
  65. *
  66. * @return BelongsTo
  67. */
  68. public function author(): BelongsTo
  69. {
  70. return $this->belongsTo(User::class, 'author_id')->withDefault();
  71. }
  72.  
  73. /**
  74. * Возвращает загруженные файлы
  75. *
  76. * @return MorphMany
  77. */
  78. public function files(): MorphMany
  79. {
  80. return $this->morphMany(File::class, 'relate');
  81. }
  82.  
  83. /**
  84. * @return HasMany
  85. */
  86. public function dialogues(): HasMany
  87. {
  88. return $this->hasMany(Dialogue::class);
  89. }
  90.  
  91. /**
  92. * Create dialogue
  93. *
  94. * @param User $user
  95. * @param User|null $author
  96. * @param string $text
  97. * @param bool $withAuthor
  98. *
  99. * @return Builder|Model
  100. */
  101. public function createDialogue(User $user, ?User $author, string $text, bool $withAuthor)
  102. {
  103. $authorId = $author->id ?? 0;
  104.  
  105. $message = self::query()->create([
  106. 'user_id' => $user->id,
  107. 'author_id' => $authorId,
  108. 'text' => $text,
  109. 'created_at' => SITETIME,
  110. ]);
  111.  
  112. Dialogue::query()->create([
  113. 'message_id' => $message->id,
  114. 'user_id' => $user->id,
  115. 'author_id' => $authorId,
  116. 'type' => self::IN,
  117. 'created_at' => SITETIME,
  118. ]);
  119.  
  120. if ($authorId && $withAuthor) {
  121. Dialogue::query()->create([
  122. 'message_id' => $message->id,
  123. 'user_id' => $authorId,
  124. 'author_id' => $user->id,
  125. 'type' => self::OUT,
  126. 'reading' => 1,
  127. 'created_at' => SITETIME,
  128. ]);
  129. }
  130.  
  131. $user->increment('newprivat');
  132.  
  133. return $message;
  134. }
  135.  
  136. /**
  137. * Удаление сообщения и загруженных файлов
  138. *
  139. * @return bool|null
  140. */
  141. public function delete(): ?bool
  142. {
  143. $this->files->each(static function (File $file) {
  144. $file->delete();
  145. });
  146.  
  147. return parent::delete();
  148. }
  149. }