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
|
|
|
*/
|
|
|
|
|
2015-10-11 09:13:13 +08:00
|
|
|
namespace Flarum\Sticky\Post;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2023-01-20 04:49:38 +08:00
|
|
|
use Carbon\Carbon;
|
2017-10-04 01:45:54 +08:00
|
|
|
use Flarum\Post\AbstractEventPost;
|
|
|
|
use Flarum\Post\MergeableInterface;
|
2017-10-02 01:55:07 +08:00
|
|
|
use Flarum\Post\Post;
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2015-10-11 09:13:13 +08:00
|
|
|
class DiscussionStickiedPost extends AbstractEventPost implements MergeableInterface
|
2015-05-07 20:56:02 +08:00
|
|
|
{
|
2023-05-30 18:36:12 +08:00
|
|
|
public static string $type = 'discussionStickied';
|
2015-05-07 20:56:02 +08:00
|
|
|
|
2023-05-30 18:36:12 +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;
|
|
|
|
}
|
|
|
|
|
2023-05-30 18:36:12 +08:00
|
|
|
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);
|
2023-01-20 04:49:38 +08:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2023-05-30 18:36:12 +08:00
|
|
|
public static function buildContent(bool $isSticky): array
|
2015-05-07 20:56:02 +08:00
|
|
|
{
|
2023-05-30 18:36:12 +08:00
|
|
|
return ['sticky' => $isSticky];
|
2015-05-07 20:56:02 +08:00
|
|
|
}
|
|
|
|
}
|