mirror of https://github.com/movim/movim
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
71 lines
1.7 KiB
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Draft extends Model
|
|
{
|
|
public const LENGTH_LIMIT = 16384; // ~64kb in UTF8 with 4 bytes per character
|
|
protected $with = ['reply'];
|
|
private $embeds = [];
|
|
|
|
public function save(array $options = [])
|
|
{
|
|
parent::save($options);
|
|
$this->embeds()->saveMany($this->embeds);
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function embeds()
|
|
{
|
|
return $this->hasMany(DraftEmbed::class);
|
|
}
|
|
|
|
public function reply()
|
|
{
|
|
return $this->hasOne(Post::class, 'id', 'reply_id');
|
|
}
|
|
|
|
public function isNotEmpty()
|
|
{
|
|
return !empty($this->title);
|
|
}
|
|
|
|
public function isSmallEnough()
|
|
{
|
|
return mb_strlen($this->title) < self::LENGTH_LIMIT
|
|
&& mb_strlen($this->content) < self::LENGTH_LIMIT;
|
|
}
|
|
|
|
public function tryFillPost()
|
|
{
|
|
$post = Post::where('server', $this->server)
|
|
->where('node', $this->node)
|
|
->where('nodeid', $this->nodeid)
|
|
->first();
|
|
|
|
if ($post) {
|
|
$this->title = $post->title;
|
|
$this->content = $post->contentraw;
|
|
$this->open = $post->open;
|
|
|
|
$reply = $post->getReply();
|
|
if ($reply) {
|
|
$this->reply_id = $reply->id;
|
|
}
|
|
|
|
foreach ($post->attachments()->whereIn('rel', ['enclosure', 'related'])->get() as $attachment) {
|
|
if ($attachment->category != 'embed') {
|
|
$embed = new DraftEmbed;
|
|
$embed->url = $attachment->href;
|
|
array_push($this->embeds, $embed);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|