mirror of
https://github.com/flarum/core.git
synced 2025-10-09 22:16:51 +02:00
- Allow users to see their own posts, even if they have been hidden by someone else - Don't require hiding a post to be necessarily attributed to a user - Hide discussions with zero posts, unless the user can edit posts, or they are the discussion author
189 lines
4.0 KiB
PHP
Executable File
189 lines
4.0 KiB
PHP
Executable File
<?php
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Flarum\Core\Posts;
|
|
|
|
use DomainException;
|
|
use Flarum\Core\Formatter\Formatter;
|
|
use Flarum\Events\PostWasPosted;
|
|
use Flarum\Events\PostWasRevised;
|
|
use Flarum\Events\PostWasHidden;
|
|
use Flarum\Events\PostWasRestored;
|
|
use Flarum\Core\Users\User;
|
|
|
|
/**
|
|
* A standard comment in a discussion.
|
|
*/
|
|
class CommentPost extends Post
|
|
{
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static $type = 'comment';
|
|
|
|
/**
|
|
* The text formatter instance.
|
|
*
|
|
* @var Formatter
|
|
*/
|
|
protected static $formatter;
|
|
|
|
/**
|
|
* Create a new instance in reply to a discussion.
|
|
*
|
|
* @param int $discussionId
|
|
* @param string $content
|
|
* @param int $userId
|
|
* @return static
|
|
*/
|
|
public static function reply($discussionId, $content, $userId)
|
|
{
|
|
$post = new static;
|
|
|
|
$post->time = time();
|
|
$post->discussion_id = $discussionId;
|
|
$post->user_id = $userId;
|
|
$post->type = static::$type;
|
|
|
|
// Set content last, as the parsing may rely on other post attributes.
|
|
$post->content = $content;
|
|
|
|
$post->raise(new PostWasPosted($post));
|
|
|
|
return $post;
|
|
}
|
|
|
|
/**
|
|
* Revise the post's content.
|
|
*
|
|
* @param string $content
|
|
* @param User $actor
|
|
* @return $this
|
|
*/
|
|
public function revise($content, User $actor)
|
|
{
|
|
if ($this->content !== $content) {
|
|
$this->content = $content;
|
|
|
|
$this->edit_time = time();
|
|
$this->edit_user_id = $actor->id;
|
|
|
|
$this->raise(new PostWasRevised($this));
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Hide the post.
|
|
*
|
|
* @param User $actor
|
|
* @return $this
|
|
*/
|
|
public function hide(User $actor = null)
|
|
{
|
|
if ($this->number == 1) {
|
|
throw new DomainException('Cannot hide the first post of a discussion');
|
|
}
|
|
|
|
if (! $this->hide_time) {
|
|
$this->hide_time = time();
|
|
$this->hide_user_id = $actor ? $actor->id : null;
|
|
|
|
$this->raise(new PostWasHidden($this));
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Restore the post.
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function restore()
|
|
{
|
|
if ($this->number == 1) {
|
|
throw new DomainException('Cannot restore the first post of a discussion');
|
|
}
|
|
|
|
if ($this->hide_time !== null) {
|
|
$this->hide_time = null;
|
|
$this->hide_user_id = null;
|
|
|
|
$this->raise(new PostWasRestored($this));
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Unparse the parsed content.
|
|
*
|
|
* @param string $value
|
|
* @return string
|
|
*/
|
|
public function getContentAttribute($value)
|
|
{
|
|
return static::$formatter->unparse($value);
|
|
}
|
|
|
|
/**
|
|
* Get the parsed/raw content.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getParsedContentAttribute()
|
|
{
|
|
return $this->attributes['content'];
|
|
}
|
|
|
|
/**
|
|
* Parse the content before it is saved to the database.
|
|
*
|
|
* @param string $value
|
|
*/
|
|
public function setContentAttribute($value)
|
|
{
|
|
$this->attributes['content'] = $value ? static::$formatter->parse($value, $this) : null;
|
|
}
|
|
|
|
/**
|
|
* Get the content rendered as HTML.
|
|
*
|
|
* @param string $value
|
|
* @return string
|
|
*/
|
|
public function getContentHtmlAttribute($value)
|
|
{
|
|
return static::$formatter->render($this->attributes['content'], $this);
|
|
}
|
|
|
|
/**
|
|
* Get the text formatter instance.
|
|
*
|
|
* @return Formatter
|
|
*/
|
|
public static function getFormatter()
|
|
{
|
|
return static::$formatter;
|
|
}
|
|
|
|
/**
|
|
* Set the text formatter instance.
|
|
*
|
|
* @param Formatter $formatter
|
|
*/
|
|
public static function setFormatter(Formatter $formatter)
|
|
{
|
|
static::$formatter = $formatter;
|
|
}
|
|
}
|