mirror of
https://github.com/flarum/core.git
synced 2025-10-14 00:15:51 +02:00
Extract new Flarum\Post namespace
This commit is contained in:
@@ -1,116 +0,0 @@
|
||||
<?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\Access;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Event\ScopePostVisibility;
|
||||
use Flarum\Event\ScopePrivatePostVisibility;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AbstractPolicy;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class PostPolicy extends AbstractPolicy
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $model = Post::class;
|
||||
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected $events;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, Dispatcher $events)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->events = $events;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
parent::subscribe($events);
|
||||
|
||||
$events->listen(ScopePostVisibility::class, [$this, 'scopePostVisibility']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @param string $ability
|
||||
* @param Post $post
|
||||
* @return bool|null
|
||||
*/
|
||||
public function after(User $actor, $ability, Post $post)
|
||||
{
|
||||
if ($actor->can($ability.'Posts', $post->discussion)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ScopePostVisibility $event
|
||||
*/
|
||||
public function scopePostVisibility(ScopePostVisibility $event)
|
||||
{
|
||||
// Hide private posts per default.
|
||||
$event->query->where(function ($query) use ($event) {
|
||||
$query->where('posts.is_private', false);
|
||||
|
||||
$this->events->fire(
|
||||
new ScopePrivatePostVisibility($event->discussion, $query, $event->actor)
|
||||
);
|
||||
});
|
||||
|
||||
// When fetching a discussion's posts: if the user doesn't have permission
|
||||
// to moderate the discussion, then they can't see posts that have been
|
||||
// hidden by someone other than themself.
|
||||
if ($event->actor->cannot('editPosts', $event->discussion)) {
|
||||
$event->query->where(function ($query) use ($event) {
|
||||
$query->whereNull('hide_time')
|
||||
->orWhere('user_id', $event->actor->id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @param Post $post
|
||||
* @return bool|null
|
||||
*/
|
||||
public function edit(User $actor, Post $post)
|
||||
{
|
||||
// A post is allowed to be edited if the user has permission to moderate
|
||||
// the discussion which it's in, or if they are the author and the post
|
||||
// hasn't been deleted by someone else.
|
||||
if ($post->user_id == $actor->id && (! $post->hide_time || $post->hide_user_id == $actor->id)) {
|
||||
$allowEditing = $this->settings->get('allow_post_editing');
|
||||
|
||||
if ($allowEditing === '-1'
|
||||
|| ($allowEditing === 'reply' && $post->number >= $post->discussion->last_post_number)
|
||||
|| ($post->time->diffInMinutes(new Carbon) < $allowEditing)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -13,9 +13,9 @@ namespace Flarum\Core\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\Core\Repository\PostRepository;
|
||||
use Flarum\Post\PostRepository;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Event\PostWillBeDeleted;
|
||||
use Flarum\Post\Event\Deleting;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DeletePostHandler
|
||||
@@ -24,13 +24,13 @@ class DeletePostHandler
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var PostRepository
|
||||
* @var \Flarum\Post\PostRepository
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param PostRepository $posts
|
||||
* @param \Flarum\Post\PostRepository $posts
|
||||
*/
|
||||
public function __construct(Dispatcher $events, PostRepository $posts)
|
||||
{
|
||||
@@ -52,7 +52,7 @@ class DeletePostHandler
|
||||
$this->assertCan($actor, 'delete', $post);
|
||||
|
||||
$this->events->fire(
|
||||
new PostWillBeDeleted($post, $actor, $command->data)
|
||||
new Deleting($post, $actor, $command->data)
|
||||
);
|
||||
|
||||
$post->delete();
|
||||
|
@@ -12,11 +12,11 @@
|
||||
namespace Flarum\Core\Command;
|
||||
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\Core\Post\CommentPost;
|
||||
use Flarum\Core\Repository\PostRepository;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Post\PostRepository;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Core\Validator\PostValidator;
|
||||
use Flarum\Event\PostWillBeSaved;
|
||||
use Flarum\Post\PostValidator;
|
||||
use Flarum\Post\Event\Saving;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class EditPostHandler
|
||||
@@ -25,19 +25,19 @@ class EditPostHandler
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @var PostRepository
|
||||
* @var \Flarum\Post\PostRepository
|
||||
*/
|
||||
protected $posts;
|
||||
|
||||
/**
|
||||
* @var PostValidator
|
||||
* @var \Flarum\Post\PostValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param PostRepository $posts
|
||||
* @param PostValidator $validator
|
||||
* @param \Flarum\Post\PostValidator $validator
|
||||
*/
|
||||
public function __construct(Dispatcher $events, PostRepository $posts, PostValidator $validator)
|
||||
{
|
||||
@@ -79,7 +79,7 @@ class EditPostHandler
|
||||
}
|
||||
|
||||
$this->events->fire(
|
||||
new PostWillBeSaved($post, $actor, $data)
|
||||
new Saving($post, $actor, $data)
|
||||
);
|
||||
|
||||
$this->validator->assertValid($post->getDirty());
|
||||
|
@@ -14,11 +14,11 @@ namespace Flarum\Core\Command;
|
||||
use DateTime;
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\Core\Notification\NotificationSyncer;
|
||||
use Flarum\Core\Post\CommentPost;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Core\Repository\DiscussionRepository;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\Core\Validator\PostValidator;
|
||||
use Flarum\Event\PostWillBeSaved;
|
||||
use Flarum\Post\PostValidator;
|
||||
use Flarum\Post\Event\Saving;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class PostReplyHandler
|
||||
@@ -37,7 +37,7 @@ class PostReplyHandler
|
||||
protected $notifications;
|
||||
|
||||
/**
|
||||
* @var PostValidator
|
||||
* @var \Flarum\Post\PostValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
@@ -96,7 +96,7 @@ class PostReplyHandler
|
||||
}
|
||||
|
||||
$this->events->fire(
|
||||
new PostWillBeSaved($post, $actor, $command->data)
|
||||
new Saving($post, $actor, $command->data)
|
||||
);
|
||||
|
||||
$this->validator->assertValid($post->getAttributes());
|
||||
|
@@ -11,11 +11,12 @@
|
||||
|
||||
namespace Flarum\Core;
|
||||
|
||||
use Flarum\Core\Post\CommentPost;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Event\ConfigurePostTypes;
|
||||
use Flarum\Event\ConfigureUserPreferences;
|
||||
use Flarum\Event\GetPermission;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\User\Gate;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
@@ -114,7 +115,7 @@ class CoreServiceProvider extends AbstractServiceProvider
|
||||
|
||||
$events->subscribe('Flarum\Core\Access\DiscussionPolicy');
|
||||
$events->subscribe('Flarum\Core\Access\GroupPolicy');
|
||||
$events->subscribe('Flarum\Core\Access\PostPolicy');
|
||||
$events->subscribe('Flarum\Post\PostPolicy');
|
||||
$events->subscribe('Flarum\User\UserPolicy');
|
||||
|
||||
$events->listen(ConfigureUserPreferences::class, [$this, 'configureUserPreferences']);
|
||||
@@ -123,8 +124,8 @@ class CoreServiceProvider extends AbstractServiceProvider
|
||||
public function registerPostTypes()
|
||||
{
|
||||
$models = [
|
||||
'Flarum\Core\Post\CommentPost',
|
||||
'Flarum\Core\Post\DiscussionRenamedPost'
|
||||
'Flarum\Post\Post\CommentPost',
|
||||
'Flarum\Post\Post\DiscussionRenamedPost'
|
||||
];
|
||||
|
||||
$this->app->make('events')->fire(
|
||||
|
@@ -11,7 +11,7 @@
|
||||
|
||||
namespace Flarum\Core;
|
||||
|
||||
use Flarum\Core\Post\MergeableInterface;
|
||||
use Flarum\Post\MergeableInterface;
|
||||
use Flarum\Foundation\EventGeneratorTrait;
|
||||
use Flarum\Database\ScopeVisibilityTrait;
|
||||
use Flarum\Database\AbstractModel;
|
||||
@@ -20,8 +20,9 @@ use Flarum\Event\DiscussionWasHidden;
|
||||
use Flarum\Event\DiscussionWasRenamed;
|
||||
use Flarum\Event\DiscussionWasRestored;
|
||||
use Flarum\Event\DiscussionWasStarted;
|
||||
use Flarum\Event\PostWasDeleted;
|
||||
use Flarum\Post\Event\Deleted;
|
||||
use Flarum\Event\ScopePostVisibility;
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\User\Guest;
|
||||
use Flarum\User\User;
|
||||
use Flarum\Util\Str;
|
||||
@@ -109,7 +110,7 @@ class Discussion extends AbstractModel
|
||||
$posts = $discussion->posts()->allTypes();
|
||||
|
||||
foreach ($posts->get() as $post) {
|
||||
$discussion->raise(new PostWasDeleted($post));
|
||||
$discussion->raise(new Deleted($post));
|
||||
}
|
||||
|
||||
$posts->delete();
|
||||
@@ -272,7 +273,7 @@ class Discussion extends AbstractModel
|
||||
* DiscussionRenamedPost, and delete if the title has been reverted
|
||||
* completely.)
|
||||
*
|
||||
* @param MergeableInterface $post The post to save.
|
||||
* @param \Flarum\Post\MergeableInterface $post The post to save.
|
||||
* @return Post The resulting post. It may or may not be the same post as
|
||||
* was originally intended to be saved. It also may not exist, if the
|
||||
* merge logic resulted in deletion.
|
||||
@@ -303,7 +304,7 @@ class Discussion extends AbstractModel
|
||||
*/
|
||||
public function posts()
|
||||
{
|
||||
return $this->hasMany('Flarum\Core\Post');
|
||||
return $this->hasMany('Flarum\Post\Post');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -355,7 +356,7 @@ class Discussion extends AbstractModel
|
||||
*/
|
||||
public function startPost()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Post', 'start_post_id');
|
||||
return $this->belongsTo('Flarum\Post\Post', 'start_post_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -375,7 +376,7 @@ class Discussion extends AbstractModel
|
||||
*/
|
||||
public function lastPost()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Post', 'last_post_id');
|
||||
return $this->belongsTo('Flarum\Post\Post', 'last_post_id');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,18 +0,0 @@
|
||||
<?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\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class FloodingException extends Exception
|
||||
{
|
||||
}
|
@@ -11,11 +11,11 @@
|
||||
|
||||
namespace Flarum\Core\Listener;
|
||||
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Event\PostWasDeleted;
|
||||
use Flarum\Event\PostWasHidden;
|
||||
use Flarum\Event\PostWasPosted;
|
||||
use Flarum\Event\PostWasRestored;
|
||||
use Flarum\Post\Post;
|
||||
use Flarum\Post\Event\Deleted;
|
||||
use Flarum\Post\Event\Hidden;
|
||||
use Flarum\Post\Event\Posted;
|
||||
use Flarum\Post\Event\Restored;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DiscussionMetadataUpdater
|
||||
@@ -25,16 +25,16 @@ class DiscussionMetadataUpdater
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWasPosted::class, [$this, 'whenPostWasPosted']);
|
||||
$events->listen(PostWasDeleted::class, [$this, 'whenPostWasDeleted']);
|
||||
$events->listen(PostWasHidden::class, [$this, 'whenPostWasHidden']);
|
||||
$events->listen(PostWasRestored::class, [$this, 'whenPostWasRestored']);
|
||||
$events->listen(Posted::class, [$this, 'whenPostWasPosted']);
|
||||
$events->listen(Deleted::class, [$this, 'whenPostWasDeleted']);
|
||||
$events->listen(Hidden::class, [$this, 'whenPostWasHidden']);
|
||||
$events->listen(Restored::class, [$this, 'whenPostWasRestored']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWasPosted $event
|
||||
* @param Posted $event
|
||||
*/
|
||||
public function whenPostWasPosted(PostWasPosted $event)
|
||||
public function whenPostWasPosted(Posted $event)
|
||||
{
|
||||
$discussion = $event->post->discussion;
|
||||
|
||||
@@ -47,9 +47,9 @@ class DiscussionMetadataUpdater
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Flarum\Event\PostWasDeleted $event
|
||||
* @param \Flarum\Post\Event\Deleted $event
|
||||
*/
|
||||
public function whenPostWasDeleted(PostWasDeleted $event)
|
||||
public function whenPostWasDeleted(Deleted $event)
|
||||
{
|
||||
$this->removePost($event->post);
|
||||
|
||||
@@ -61,17 +61,17 @@ class DiscussionMetadataUpdater
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWasHidden $event
|
||||
* @param \Flarum\Post\Event\Hidden $event
|
||||
*/
|
||||
public function whenPostWasHidden(PostWasHidden $event)
|
||||
public function whenPostWasHidden(Hidden $event)
|
||||
{
|
||||
$this->removePost($event->post);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWasRestored $event
|
||||
* @param Restored $event
|
||||
*/
|
||||
public function whenPostWasRestored(PostWasRestored $event)
|
||||
public function whenPostWasRestored(Restored $event)
|
||||
{
|
||||
$discussion = $event->post->discussion;
|
||||
|
||||
|
@@ -13,7 +13,7 @@ namespace Flarum\Core\Listener;
|
||||
|
||||
use Flarum\Core\Notification\DiscussionRenamedBlueprint;
|
||||
use Flarum\Core\Notification\NotificationSyncer;
|
||||
use Flarum\Core\Post\DiscussionRenamedPost;
|
||||
use Flarum\Post\DiscussionRenamedPost;
|
||||
use Flarum\Event\DiscussionWasRenamed;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
|
@@ -11,12 +11,12 @@
|
||||
|
||||
namespace Flarum\Core\Notification;
|
||||
|
||||
use Flarum\Core\Post\DiscussionRenamedPost;
|
||||
use Flarum\Post\DiscussionRenamedPost;
|
||||
|
||||
class DiscussionRenamedBlueprint implements BlueprintInterface
|
||||
{
|
||||
/**
|
||||
* @var DiscussionRenamedPost
|
||||
* @var \Flarum\Post\DiscussionRenamedPost
|
||||
*/
|
||||
protected $post;
|
||||
|
||||
|
@@ -1,225 +0,0 @@
|
||||
<?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;
|
||||
|
||||
use Flarum\Core\Post\RegisteredTypesScope;
|
||||
use Flarum\Foundation\EventGeneratorTrait;
|
||||
use Flarum\Database\ScopeVisibilityTrait;
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\Event\PostWasDeleted;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $discussion_id
|
||||
* @property int $number
|
||||
* @property \Carbon\Carbon $time
|
||||
* @property int|null $user_id
|
||||
* @property string|null $type
|
||||
* @property string|null $content
|
||||
* @property \Carbon\Carbon|null $edit_time
|
||||
* @property int|null $edit_user_id
|
||||
* @property \Carbon\Carbon|null $hide_time
|
||||
* @property int|null $hide_user_id
|
||||
* @property \Flarum\Core\Discussion|null $discussion
|
||||
* @property User|null $user
|
||||
* @property User|null $editUser
|
||||
* @property User|null $hideUser
|
||||
* @property string $ip_address
|
||||
* @property bool $is_private
|
||||
*/
|
||||
class Post extends AbstractModel
|
||||
{
|
||||
use EventGeneratorTrait;
|
||||
use ScopeVisibilityTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $table = 'posts';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $dates = ['time', 'edit_time', 'hide_time'];
|
||||
|
||||
/**
|
||||
* Casts properties to a specific type.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_private' => 'boolean'
|
||||
];
|
||||
|
||||
/**
|
||||
* A map of post types, as specified in the `type` column, to their
|
||||
* classes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $models = [];
|
||||
|
||||
/**
|
||||
* The type of post this is, to be stored in the posts table.
|
||||
*
|
||||
* Should be overwritten by subclasses with the value that is
|
||||
* to be stored in the database, which will then be used for
|
||||
* mapping the hydrated model instance to the proper subtype.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $type = '';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// When a post is created, set its type according to the value of the
|
||||
// subclass. Also give it an auto-incrementing number within the
|
||||
// discussion.
|
||||
static::creating(function (Post $post) {
|
||||
$post->type = $post::$type;
|
||||
$post->number = ++$post->discussion->number_index;
|
||||
$post->discussion->save();
|
||||
});
|
||||
|
||||
static::deleted(function (Post $post) {
|
||||
$post->raise(new PostWasDeleted($post));
|
||||
});
|
||||
|
||||
static::addGlobalScope(new RegisteredTypesScope);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether or not this post is visible to the given user.
|
||||
*
|
||||
* @param User $user
|
||||
* @return bool
|
||||
*/
|
||||
public function isVisibleTo(User $user)
|
||||
{
|
||||
$discussion = $this->discussion()->whereVisibleTo($user)->first();
|
||||
|
||||
if ($discussion) {
|
||||
$this->setRelation('discussion', $discussion);
|
||||
|
||||
return (bool) $discussion->postsVisibleTo($user)->where('id', $this->id)->count();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the post's discussion.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function discussion()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Discussion', 'discussion_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the post's author.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Flarum\User\User', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the user who edited the post.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function editUser()
|
||||
{
|
||||
return $this->belongsTo('Flarum\User\User', 'edit_user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the user who hid the post.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function hideUser()
|
||||
{
|
||||
return $this->belongsTo('Flarum\User\User', 'hide_user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all posts, regardless of their type, by removing the
|
||||
* `RegisteredTypesScope` global scope constraints applied on this model.
|
||||
*
|
||||
* @param Builder $query
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeAllTypes(Builder $query)
|
||||
{
|
||||
return $this->removeGlobalScopes($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new model instance according to the post's type.
|
||||
*
|
||||
* @param array $attributes
|
||||
* @param string|null $connection
|
||||
* @return static|object
|
||||
*/
|
||||
public function newFromBuilder($attributes = [], $connection = null)
|
||||
{
|
||||
$attributes = (array) $attributes;
|
||||
|
||||
if (! empty($attributes['type'])
|
||||
&& isset(static::$models[$attributes['type']])
|
||||
&& class_exists($class = static::$models[$attributes['type']])
|
||||
) {
|
||||
/** @var Post $instance */
|
||||
$instance = new $class;
|
||||
$instance->exists = true;
|
||||
$instance->setRawAttributes($attributes, true);
|
||||
$instance->setConnection($connection ?: $this->connection);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
return parent::newFromBuilder($attributes, $connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type-to-model map.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getModels()
|
||||
{
|
||||
return static::$models;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the model for the given post type.
|
||||
*
|
||||
* @param string $type The post type.
|
||||
* @param string $model The class name of the model for that type.
|
||||
* @return void
|
||||
*/
|
||||
public static function setModel($type, $model)
|
||||
{
|
||||
static::$models[$type] = $model;
|
||||
}
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
<?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\Post;
|
||||
|
||||
use Flarum\Core\Post;
|
||||
|
||||
abstract class AbstractEventPost extends Post
|
||||
{
|
||||
/**
|
||||
* Unserialize the content attribute from the database's JSON value.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getContentAttribute($value)
|
||||
{
|
||||
return json_decode($value, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the content attribute to be stored in the database as JSON.
|
||||
*
|
||||
* @param string $value
|
||||
*/
|
||||
public function setContentAttribute($value)
|
||||
{
|
||||
$this->attributes['content'] = json_encode($value);
|
||||
}
|
||||
}
|
@@ -1,186 +0,0 @@
|
||||
<?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\Post;
|
||||
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Event\PostWasHidden;
|
||||
use Flarum\Event\PostWasPosted;
|
||||
use Flarum\Event\PostWasRestored;
|
||||
use Flarum\Event\PostWasRevised;
|
||||
use Flarum\Formatter\Formatter;
|
||||
use Flarum\User\User;
|
||||
|
||||
/**
|
||||
* A standard comment in a discussion.
|
||||
*
|
||||
* @property string $parsed_content
|
||||
* @property string $content_html
|
||||
*/
|
||||
class CommentPost extends Post
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $type = 'comment';
|
||||
|
||||
/**
|
||||
* The text formatter instance.
|
||||
*
|
||||
* @var \Flarum\Formatter\Formatter
|
||||
*/
|
||||
protected static $formatter;
|
||||
|
||||
/**
|
||||
* Create a new instance in reply to a discussion.
|
||||
*
|
||||
* @param int $discussionId
|
||||
* @param string $content
|
||||
* @param int $userId
|
||||
* @param string $ipAddress
|
||||
* @return static
|
||||
*/
|
||||
public static function reply($discussionId, $content, $userId, $ipAddress)
|
||||
{
|
||||
$post = new static;
|
||||
|
||||
$post->time = time();
|
||||
$post->discussion_id = $discussionId;
|
||||
$post->user_id = $userId;
|
||||
$post->type = static::$type;
|
||||
$post->ip_address = $ipAddress;
|
||||
|
||||
// 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->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->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 \Flarum\Formatter\Formatter
|
||||
*/
|
||||
public static function getFormatter()
|
||||
{
|
||||
return static::$formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the text formatter instance.
|
||||
*
|
||||
* @param \Flarum\Formatter\Formatter $formatter
|
||||
*/
|
||||
public static function setFormatter(Formatter $formatter)
|
||||
{
|
||||
static::$formatter = $formatter;
|
||||
}
|
||||
}
|
@@ -1,87 +0,0 @@
|
||||
<?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\Post;
|
||||
|
||||
use Flarum\Core\Post;
|
||||
|
||||
/**
|
||||
* A post which indicates that a discussion's title was changed.
|
||||
*
|
||||
* The content is stored as a sequential array containing the old title and the
|
||||
* new title.
|
||||
*/
|
||||
class DiscussionRenamedPost extends AbstractEventPost implements MergeableInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $type = 'discussionRenamed';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function saveAfter(Post $previous = null)
|
||||
{
|
||||
// If the previous post is another 'discussion renamed' 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 title, delete it. Otherwise, update
|
||||
// its content.
|
||||
if ($previous instanceof static && $this->user_id === $previous->user_id) {
|
||||
if ($previous->content[0] == $this->content[1]) {
|
||||
$previous->delete();
|
||||
} else {
|
||||
$previous->content = static::buildContent($previous->content[0], $this->content[1]);
|
||||
|
||||
$previous->save();
|
||||
}
|
||||
|
||||
return $previous;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance in reply to a discussion.
|
||||
*
|
||||
* @param int $discussionId
|
||||
* @param int $userId
|
||||
* @param string $oldTitle
|
||||
* @param string $newTitle
|
||||
* @return static
|
||||
*/
|
||||
public static function reply($discussionId, $userId, $oldTitle, $newTitle)
|
||||
{
|
||||
$post = new static;
|
||||
|
||||
$post->content = static::buildContent($oldTitle, $newTitle);
|
||||
$post->time = time();
|
||||
$post->discussion_id = $discussionId;
|
||||
$post->user_id = $userId;
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the content attribute.
|
||||
*
|
||||
* @param string $oldTitle The old title of the discussion.
|
||||
* @param string $newTitle The new title of the discussion.
|
||||
* @return array
|
||||
*/
|
||||
protected static function buildContent($oldTitle, $newTitle)
|
||||
{
|
||||
return [$oldTitle, $newTitle];
|
||||
}
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
<?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\Post;
|
||||
|
||||
use DateTime;
|
||||
use Flarum\Core\Exception\FloodingException;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\User\User;
|
||||
|
||||
class Floodgate
|
||||
{
|
||||
/**
|
||||
* @param User $actor
|
||||
* @throws FloodingException
|
||||
*/
|
||||
public function assertNotFlooding(User $actor)
|
||||
{
|
||||
if ($this->isFlooding($actor)) {
|
||||
throw new FloodingException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
* @return bool
|
||||
*/
|
||||
public function isFlooding(User $actor)
|
||||
{
|
||||
return Post::where('user_id', $actor->id)->where('time', '>=', new DateTime('-10 seconds'))->exists();
|
||||
}
|
||||
}
|
@@ -1,35 +0,0 @@
|
||||
<?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\Post;
|
||||
|
||||
use Flarum\Core\Post;
|
||||
|
||||
/**
|
||||
* A post that has the ability to be merged into an adjacent post.
|
||||
*
|
||||
* This is only implemented by certain types of posts. For example,
|
||||
* if a "discussion renamed" post is posted immediately after another
|
||||
* "discussion renamed" post, then the new one will be merged into the old one.
|
||||
*/
|
||||
interface MergeableInterface
|
||||
{
|
||||
/**
|
||||
* Save the model, given that it is going to appear immediately after the
|
||||
* passed model.
|
||||
*
|
||||
* @param Post|null $previous
|
||||
* @return Post The model resulting after the merge. If the merge is
|
||||
* unsuccessful, this should be the current model instance. Otherwise,
|
||||
* it should be the model that was merged into.
|
||||
*/
|
||||
public function saveAfter(Post $previous = null);
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
<?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\Post;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\ScopeInterface;
|
||||
|
||||
class RegisteredTypesScope implements ScopeInterface
|
||||
{
|
||||
/**
|
||||
* The index at which we added a where clause.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $whereIndex;
|
||||
|
||||
/**
|
||||
* The index at which we added where bindings.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $bindingIndex;
|
||||
|
||||
/**
|
||||
* The number of where bindings we added.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $bindingCount;
|
||||
|
||||
/**
|
||||
* Apply the scope to a given Eloquent query builder.
|
||||
*
|
||||
* @param Builder $builder
|
||||
* @param Model $post
|
||||
* @return void
|
||||
*/
|
||||
public function apply(Builder $builder, Model $post)
|
||||
{
|
||||
$query = $builder->getQuery();
|
||||
|
||||
$this->whereIndex = count($query->wheres);
|
||||
$this->bindingIndex = count($query->getRawBindings()['where']);
|
||||
|
||||
$types = array_keys($post::getModels());
|
||||
$this->bindingCount = count($types);
|
||||
$query->whereIn('type', $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the scope from the given Eloquent query builder.
|
||||
*
|
||||
* @param Builder $builder
|
||||
* @param Model $post
|
||||
* @return void
|
||||
*/
|
||||
public function remove(Builder $builder, Model $post)
|
||||
{
|
||||
$query = $builder->getQuery();
|
||||
|
||||
unset($query->wheres[$this->whereIndex]);
|
||||
$query->wheres = array_values($query->wheres);
|
||||
|
||||
$whereBindings = $query->getRawBindings()['where'];
|
||||
array_splice($whereBindings, $this->bindingIndex, $this->bindingCount);
|
||||
$query->setBindings(array_values($whereBindings));
|
||||
}
|
||||
}
|
@@ -1,185 +0,0 @@
|
||||
<?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\Repository;
|
||||
|
||||
use Flarum\Core\Discussion;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Event\ScopePostVisibility;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
|
||||
class PostRepository
|
||||
{
|
||||
/**
|
||||
* Get a new query builder for the posts table.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
return Post::query();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a post by ID, optionally making sure it is visible to a certain
|
||||
* user, or throw an exception.
|
||||
*
|
||||
* @param int $id
|
||||
* @param \Flarum\User\User $actor
|
||||
* @return \Flarum\Core\Post
|
||||
*
|
||||
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||
*/
|
||||
public function findOrFail($id, User $actor = null)
|
||||
{
|
||||
$posts = $this->findByIds([$id], $actor);
|
||||
|
||||
if (! count($posts)) {
|
||||
throw new ModelNotFoundException;
|
||||
}
|
||||
|
||||
return $posts->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find posts that match certain conditions, optionally making sure they
|
||||
* are visible to a certain user, and/or using other criteria.
|
||||
*
|
||||
* @param array $where
|
||||
* @param \Flarum\User\User|null $actor
|
||||
* @param array $sort
|
||||
* @param int $count
|
||||
* @param int $start
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function findWhere(array $where = [], User $actor = null, $sort = [], $count = null, $start = 0)
|
||||
{
|
||||
$query = Post::where($where)
|
||||
->skip($start)
|
||||
->take($count);
|
||||
|
||||
foreach ((array) $sort as $field => $order) {
|
||||
$query->orderBy($field, $order);
|
||||
}
|
||||
|
||||
$ids = $query->lists('id')->all();
|
||||
|
||||
return $this->findByIds($ids, $actor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find posts by their IDs, optionally making sure they are visible to a
|
||||
* certain user.
|
||||
*
|
||||
* @param array $ids
|
||||
* @param \Flarum\User\User|null $actor
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function findByIds(array $ids, User $actor = null)
|
||||
{
|
||||
$posts = $this->queryIds($ids, $actor)->get();
|
||||
|
||||
$posts = $posts->sort(function ($a, $b) use ($ids) {
|
||||
$aPos = array_search($a->id, $ids);
|
||||
$bPos = array_search($b->id, $ids);
|
||||
|
||||
if ($aPos === $bPos) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $aPos < $bPos ? -1 : 1;
|
||||
});
|
||||
|
||||
return $posts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter a list of post IDs to only include posts that are visible to a
|
||||
* certain user.
|
||||
*
|
||||
* @param array $ids
|
||||
* @param User $actor
|
||||
* @return array
|
||||
*/
|
||||
public function filterVisibleIds(array $ids, User $actor)
|
||||
{
|
||||
return $this->queryIds($ids, $actor)->lists('id')->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position within a discussion where a post with a certain number
|
||||
* is. If the post with that number does not exist, the index of the
|
||||
* closest post to it will be returned.
|
||||
*
|
||||
* @param int $discussionId
|
||||
* @param int $number
|
||||
* @param \Flarum\User\User|null $actor
|
||||
* @return int
|
||||
*/
|
||||
public function getIndexForNumber($discussionId, $number, User $actor = null)
|
||||
{
|
||||
$query = Discussion::find($discussionId)
|
||||
->postsVisibleTo($actor)
|
||||
->where('time', '<', function ($query) use ($discussionId, $number) {
|
||||
$query->select('time')
|
||||
->from('posts')
|
||||
->where('discussion_id', $discussionId)
|
||||
->whereNotNull('number')
|
||||
->take(1)
|
||||
|
||||
// We don't add $number as a binding because for some
|
||||
// reason doing so makes the bindings go out of order.
|
||||
->orderByRaw('ABS(CAST(number AS SIGNED) - '.(int) $number.')');
|
||||
});
|
||||
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ids
|
||||
* @param User|null $actor
|
||||
* @return mixed
|
||||
*/
|
||||
protected function queryIds(array $ids, User $actor = null)
|
||||
{
|
||||
$discussions = $this->getDiscussionsForPosts($ids, $actor);
|
||||
|
||||
return Post::whereIn('id', $ids)
|
||||
->where(function ($query) use ($discussions, $actor) {
|
||||
foreach ($discussions as $discussion) {
|
||||
$query->orWhere(function ($query) use ($discussion, $actor) {
|
||||
$query->where('discussion_id', $discussion->id);
|
||||
|
||||
event(new ScopePostVisibility($discussion, $query, $actor));
|
||||
});
|
||||
}
|
||||
|
||||
$query->orWhereRaw('FALSE');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $postIds
|
||||
* @param User $actor
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getDiscussionsForPosts($postIds, User $actor)
|
||||
{
|
||||
return Discussion::query()
|
||||
->select('discussions.*')
|
||||
->join('posts', 'posts.discussion_id', '=', 'discussions.id')
|
||||
->whereIn('posts.id', $postIds)
|
||||
->groupBy('discussions.id')
|
||||
->whereVisibleTo($actor)
|
||||
->get();
|
||||
}
|
||||
}
|
@@ -13,7 +13,7 @@ namespace Flarum\Core\Search\Discussion;
|
||||
|
||||
use Flarum\Core\Discussion;
|
||||
use Flarum\Core\Repository\DiscussionRepository;
|
||||
use Flarum\Core\Repository\PostRepository;
|
||||
use Flarum\Post\PostRepository;
|
||||
use Flarum\Core\Search\ApplySearchParametersTrait;
|
||||
use Flarum\Core\Search\GambitManager;
|
||||
use Flarum\Core\Search\SearchCriteria;
|
||||
|
@@ -1,24 +0,0 @@
|
||||
<?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\Validator;
|
||||
|
||||
use Flarum\Foundation\AbstractValidator;
|
||||
|
||||
class PostValidator extends AbstractValidator
|
||||
{
|
||||
protected $rules = [
|
||||
'content' => [
|
||||
'required',
|
||||
'max:65535'
|
||||
]
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user