framework/extensions/sticky/src/Post/DiscussionStickiedPost.php

61 lines
1.6 KiB
PHP
Raw Normal View History

2016-02-26 11:56:55 +08:00
<?php
2015-09-04 11:00:25 +08:00
/*
* This file is part of Flarum.
*
2019-11-30 07:02:29 +08:00
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
2015-09-04 11:00:25 +08:00
*/
namespace Flarum\Sticky\Post;
2015-05-07 20:56:02 +08:00
use Carbon\Carbon;
2017-10-04 01:45:54 +08:00
use Flarum\Post\AbstractEventPost;
use Flarum\Post\MergeableInterface;
use Flarum\Post\Post;
2015-05-07 20:56:02 +08:00
class DiscussionStickiedPost extends AbstractEventPost implements MergeableInterface
2015-05-07 20:56:02 +08:00
{
public static string $type = 'discussionStickied';
2015-05-07 20:56:02 +08:00
public function saveAfter(Post $previous = null): static
2015-05-07 20:56:02 +08:00
{
2015-07-20 16:44:42 +08:00
// If the previous post is another 'discussion stickied' post, and it's
// by the same user, then we can merge this post into it. If we find
// that we've in fact reverted the sticky status, delete it. Otherwise,
// update its content.
if ($previous instanceof static && $this->user_id === $previous->user_id) {
2015-05-07 20:56:02 +08:00
if ($previous->content['sticky'] != $this->content['sticky']) {
2015-07-20 16:44:42 +08:00
$previous->delete();
} else {
$previous->content = $this->content;
$previous->save();
2015-05-07 20:56:02 +08:00
}
return $previous;
}
2015-07-20 16:44:42 +08:00
$this->save();
2015-05-07 20:56:02 +08:00
return $this;
}
public static function reply(int $discussionId, int $userId, bool $isSticky): static
2015-05-07 20:56:02 +08:00
{
$post = new static;
2016-02-26 11:56:55 +08:00
$post->content = static::buildContent($isSticky);
$post->created_at = Carbon::now();
2015-05-07 20:56:02 +08:00
$post->discussion_id = $discussionId;
2016-02-26 11:56:55 +08:00
$post->user_id = $userId;
2015-05-07 20:56:02 +08:00
return $post;
}
public static function buildContent(bool $isSticky): array
2015-05-07 20:56:02 +08:00
{
return ['sticky' => $isSticky];
2015-05-07 20:56:02 +08:00
}
}