You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Draft extends Model
  5. {
  6. public const LENGTH_LIMIT = 16384; // ~64kb in UTF8 with 4 bytes per character
  7. protected $with = ['reply'];
  8. private $embeds = [];
  9. public function save(array $options = [])
  10. {
  11. parent::save($options);
  12. $this->embeds()->saveMany($this->embeds);
  13. }
  14. public function user()
  15. {
  16. return $this->belongsTo(User::class);
  17. }
  18. public function embeds()
  19. {
  20. return $this->hasMany(DraftEmbed::class);
  21. }
  22. public function reply()
  23. {
  24. return $this->hasOne(Post::class, 'id', 'reply_id');
  25. }
  26. public function isNotEmpty()
  27. {
  28. return !empty($this->title);
  29. }
  30. public function isSmallEnough()
  31. {
  32. return mb_strlen($this->title) < self::LENGTH_LIMIT
  33. && mb_strlen($this->content) < self::LENGTH_LIMIT;
  34. }
  35. public function tryFillPost()
  36. {
  37. $post = Post::where('server', $this->server)
  38. ->where('node', $this->node)
  39. ->where('nodeid', $this->nodeid)
  40. ->first();
  41. if ($post) {
  42. $this->title = $post->title;
  43. $this->content = $post->contentraw;
  44. $this->open = $post->open;
  45. $reply = $post->getReply();
  46. if ($reply) {
  47. $this->reply_id = $reply->id;
  48. }
  49. foreach ($post->attachments()->whereIn('rel', ['enclosure', 'related'])->get() as $attachment) {
  50. if ($attachment->category != 'embed') {
  51. $embed = new DraftEmbed;
  52. $embed->url = $attachment->href;
  53. array_push($this->embeds, $embed);
  54. }
  55. }
  56. }
  57. }
  58. }