1
0
mirror of https://github.com/flarum/core.git synced 2025-10-11 06:54:26 +02:00

Massive refactor

- Use contextual namespaces within Flarum\Core
- Clean up and docblock everything
- Refactor Activity/Notification blueprint stuff
- Refactor Formatter stuff
- Refactor Search stuff
- Upgrade to JSON-API 1.0
- Removed “addedPosts” and “removedPosts” relationships from discussion
API. This was used for adding/removing event posts after renaming a
discussion etc. Instead we should make an additional request to get all
new posts

Todo:
- Fix Extenders and extensions
- Get rid of repository interfaces
- Fix other bugs I’ve inevitably introduced
This commit is contained in:
Toby Zerner
2015-07-04 12:24:48 +09:30
parent 12dd550a14
commit a74b40fe47
324 changed files with 6443 additions and 4197 deletions

View File

@@ -0,0 +1,41 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Users\User;
class DeletePost
{
/**
* The ID of the post to delete.
*
* @var int
*/
public $postId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* Any other user input associated with the action. This is unused by
* default, but may be used by extensions.
*
* @var array
*/
public $data;
/**
* @param int $postId The ID of the post to delete.
* @param User $actor The user performing the action.
* @param array $data Any other user input associated with the action. This
* is unused by default, but may be used by extensions.
*/
public function __construct($postId, User $actor, array $data = [])
{
$this->postId = $postId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,44 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Posts\PostRepositoryInterface;
use Flarum\Core\Posts\Events\PostWillBeDeleted;
use Flarum\Core\Support\DispatchesEvents;
class DeletePostHandler
{
use DispatchesEvents;
/**
* @var PostRepositoryInterface
*/
protected $posts;
/**
* @param PostRepositoryInterface $posts
*/
public function __construct(PostRepositoryInterface $posts)
{
$this->posts = $posts;
}
/**
* @param DeletePost $command
* @return \Flarum\Core\Posts\Post
*/
public function handle(DeletePost $command)
{
$actor = $command->actor;
$post = $this->posts->findOrFail($command->postId, $actor);
$post->assertCan($actor, 'delete');
event(new PostWillBeDeleted($post, $actor, $command->data));
$post->delete();
$this->dispatchEventsFor($post);
return $post;
}
}

View File

@@ -0,0 +1,39 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Users\User;
class EditPost
{
/**
* The ID of the post to edit.
*
* @var int
*/
public $postId;
/**
* The user performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to update on the post.
*
* @var array
*/
public $data;
/**
* @param int $postId The ID of the post to edit.
* @param User $actor The user performing the action.
* @param array $data The attributes to update on the post.
*/
public function __construct($postId, User $actor, array $data)
{
$this->postId = $postId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,65 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Posts\PostRepositoryInterface;
use Flarum\Core\Posts\Events\PostWillBeSaved;
use Flarum\Core\Support\DispatchesEvents;
use Flarum\Core\Posts\CommentPost;
class EditPostHandler
{
use DispatchesEvents;
/**
* @var PostRepositoryInterface
*/
protected $posts;
/**
* @param PostRepositoryInterface $posts
*/
public function __construct(PostRepositoryInterface $posts)
{
$this->posts = $posts;
}
/**
* @param EditPost $command
* @return \Flarum\Core\Posts\Post
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(EditPost $command)
{
$actor = $command->actor;
$data = $command->data;
$post = $this->posts->findOrFail($command->postId, $actor);
if ($post instanceof CommentPost) {
$attributes = array_get($data, 'attributes', []);
if (isset($attributes['content'])) {
$post->assertCan($actor, 'edit');
$post->revise($attributes['content'], $actor);
}
if (isset($attributes['isHidden'])) {
$post->assertCan($actor, 'edit');
if ($attributes['isHidden']) {
$post->hide($actor);
} else {
$post->restore();
}
}
}
event(new PostWillBeSaved($post, $actor, $data));
$post->save();
$this->dispatchEventsFor($post);
return $post;
}
}

View File

@@ -0,0 +1,39 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Users\User;
class PostReply
{
/**
* The ID of the discussion to post the reply to.
*
* @var int
*/
public $discussionId;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to assign to the new post.
*
* @var array
*/
public $data;
/**
* @param int $discussionId The ID of the discussion to post the reply to.
* @param User $actor The user who is performing the action.
* @param array $data The attributes to assign to the new post.
*/
public function __construct($discussionId, User $actor, array $data)
{
$this->discussionId = $discussionId;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,70 @@
<?php namespace Flarum\Core\Posts\Commands;
use Flarum\Core\Posts\Events\PostWillBeSaved;
use Flarum\Core\Discussions\DiscussionRepositoryInterface;
use Flarum\Core\Posts\CommentPost;
use Flarum\Core\Support\DispatchesEvents;
use Flarum\Core\Notifications\NotificationSyncer;
class PostReplyHandler
{
use DispatchesEvents;
/**
* @var DiscussionRepositoryInterface
*/
protected $discussions;
/**
* @var NotificationSyncer
*/
protected $notifications;
/**
* @param DiscussionRepositoryInterface $discussions
* @param NotificationSyncer $notifications
*/
public function __construct(DiscussionRepositoryInterface $discussions, NotificationSyncer $notifications)
{
$this->discussions = $discussions;
$this->notifications = $notifications;
}
/**
* @param PostReply $command
* @return CommentPost
* @throws \Flarum\Core\Exceptions\PermissionDeniedException
*/
public function handle(PostReply $command)
{
$actor = $command->actor;
// Make sure the user has permission to reply to this discussion. First,
// make sure the discussion exists and that the user has permission to
// view it; if not, fail with a ModelNotFound exception so we don't give
// away the existence of the discussion. If the user is allowed to view
// it, check if they have permission to reply.
$discussion = $this->discussions->findOrFail($command->discussionId, $actor);
$discussion->assertCan($actor, 'reply');
// Create a new Post entity, persist it, and dispatch domain events.
// Before persistence, though, fire an event to give plugins an
// opportunity to alter the post entity based on data in the command.
$post = CommentPost::reply(
$command->discussionId,
array_get($command->data, 'attributes.content'),
$actor->id
);
event(new PostWillBeSaved($post, $actor, $command->data));
$post->save();
$this->notifications->onePerUser(function () use ($post) {
$this->dispatchEventsFor($post);
});
return $post;
}
}

182
src/Core/Posts/CommentPost.php Executable file
View File

@@ -0,0 +1,182 @@
<?php namespace Flarum\Core\Posts;
use DomainException;
use Flarum\Core\Formatter\FormatterManager;
use Flarum\Core\Posts\Events\PostWasPosted;
use Flarum\Core\Posts\Events\PostWasRevised;
use Flarum\Core\Posts\Events\PostWasHidden;
use Flarum\Core\Posts\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 FormatterManager
*/
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->content = $content;
$post->time = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = static::$type;
$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->content_html = static::formatContent($this);
$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)
{
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->id;
$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;
}
/**
* Get the content formatted as HTML.
*
* @param string $value
* @return string
*/
public function getContentHtmlAttribute($value)
{
if (! $value) {
$this->content_html = $value = static::formatContent($this);
$this->save();
}
return $value;
}
/**
* Define the relationship with the user who edited the post.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function editUser()
{
return $this->belongsTo('Flarum\Core\Users\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\Core\Users\User', 'hide_user_id');
}
/**
* Get text formatter instance.
*
* @return FormatterManager
*/
public static function getFormatter()
{
return static::$formatter;
}
/**
* Set text formatter instance.
*
* @param FormatterManager $formatter
*/
public static function setFormatter(FormatterManager $formatter)
{
static::$formatter = $formatter;
}
/**
* Format a string of post content using the set formatter.
*
* @param CommentPost $post
* @return string
*/
protected static function formatContent(CommentPost $post)
{
return static::$formatter->format($post->content, $post);
}
}

View File

@@ -0,0 +1,74 @@
<?php namespace Flarum\Core\Posts;
/**
* 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 EventPost implements MergeablePost
{
/**
* @inheritdoc
*/
public static $type = 'discussionRenamed';
/**
* @inheritdoc
*/
public function saveAfter(Post $previous)
{
// 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];
}
}

View File

@@ -0,0 +1,149 @@
<?php namespace Flarum\Core\Posts;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core\Users\User;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Discussions\Search\Fulltext\DriverInterface;
// TODO: In some cases, the use of a post repository incurs extra query expense,
// because for every post retrieved we need to check if the discussion it's in
// is visible. Especially when retrieving a discussion's posts, we can end up
// with an inefficient chain of queries like this:
// 1. Api\Discussions\ShowAction: get discussion (will exit if not visible)
// 2. Discussion@postsVisibleTo: get discussion tags (for post visibility purposes)
// 3. Discussion@postsVisibleTo: get post IDs
// 4. EloquentPostRepository@getIndexForNumber: get discussion
// 5. EloquentPostRepository@getIndexForNumber: get discussion tags (for post visibility purposes)
// 6. EloquentPostRepository@getIndexForNumber: get post index for number
// 7. EloquentPostRepository@findWhere: get post IDs for discussion to check for discussion visibility
// 8. EloquentPostRepository@findWhere: get post IDs in visible discussions
// 9. EloquentPostRepository@findWhere: get posts
// 10. EloquentPostRepository@findWhere: eager load discussion onto posts
// 11. EloquentPostRepository@findWhere: get discussion tags to filter visible posts
// 12. Api\Discussions\ShowAction: eager load users
// 13. Api\Discussions\ShowAction: eager load groups
// 14. Api\Discussions\ShowAction: eager load mentions
// 14. Serializers\DiscussionSerializer: load discussion-user state
class EloquentPostRepository implements PostRepositoryInterface
{
protected $fulltext;
public function __construct(DriverInterface $fulltext)
{
$this->fulltext = $fulltext;
}
/**
* {@inheritdoc}
*/
public function findOrFail($id, User $actor = null)
{
$posts = $this->findByIds([$id], $actor);
if (! count($posts)) {
throw new ModelNotFoundException;
}
return $posts->first();
}
/**
* {@inheritdoc}
*/
public function findWhere($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');
return $this->findByIds($ids, $actor);
}
/**
* {@inheritdoc}
*/
public function findByIds(array $ids, User $actor = null)
{
$ids = $this->filterDiscussionVisibleTo($ids, $actor);
$posts = Post::with('discussion')->whereIn('id', (array) $ids)->get();
return $this->filterVisibleTo($posts, $actor);
}
/**
* {@inheritdoc}
*/
public function findByContent($string, User $actor = null)
{
$ids = $this->fulltext->match($string);
$ids = $this->filterDiscussionVisibleTo($ids, $actor);
$query = Post::select('id', 'discussion_id')->whereIn('id', $ids);
foreach ($ids as $id) {
$query->orderByRaw('id != ?', [$id]);
}
$posts = $query->get();
return $this->filterVisibleTo($posts, $actor);
}
/**
* {@inheritdoc}
*/
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();
}
protected function filterDiscussionVisibleTo($ids, User $actor)
{
// For each post ID, we need to make sure that the discussion it's in
// is visible to the user.
if ($actor) {
$ids = Discussion::join('posts', 'discussions.id', '=', 'posts.discussion_id')
->whereIn('posts.id', $ids)
->whereVisibleTo($actor)
->get(['posts.id'])
->lists('id');
}
return $ids;
}
protected function filterVisibleTo($posts, User $actor)
{
if ($actor) {
$posts = $posts->filter(function ($post) use ($actor) {
return $post->can($actor, 'view');
});
}
return $posts;
}
}

25
src/Core/Posts/EventPost.php Executable file
View File

@@ -0,0 +1,25 @@
<?php namespace Flarum\Core\Posts;
abstract class EventPost 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);
}
}

View File

@@ -0,0 +1,21 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
class PostWasDeleted
{
/**
* The post that was deleted.
*
* @var Post
*/
public $post;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
}

View File

@@ -0,0 +1,21 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\CommentPost;
class PostWasHidden
{
/**
* The post that was hidden.
*
* @var CommentPost
*/
public $post;
/**
* @param CommentPost $post
*/
public function __construct(CommentPost $post)
{
$this->post = $post;
}
}

View File

@@ -0,0 +1,21 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
class PostWasPosted
{
/**
* The post that was posted.
*
* @var Post
*/
public $post;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
}

View File

@@ -0,0 +1,21 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\CommentPost;
class PostWasRestored
{
/**
* The post that was restored.
*
* @var CommentPost
*/
public $post;
/**
* @param CommentPost $post The post that was restored.
*/
public function __construct(CommentPost $post)
{
$this->post = $post;
}
}

View File

@@ -0,0 +1,21 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\CommentPost;
class PostWasRevised
{
/**
* The post that was revised.
*
* @var CommentPost
*/
public $post;
/**
* @param CommentPost $post The post that was revised.
*/
public function __construct(CommentPost $post)
{
$this->post = $post;
}
}

View File

@@ -0,0 +1,40 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class PostWillBeDeleted
{
/**
* The post that is going to be deleted.
*
* @var Post
*/
public $post;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* Any user input associated with the command.
*
* @var array
*/
public $data;
/**
* @param Post $post
* @param User $actor
* @param array $data
*/
public function __construct(Post $post, User $actor, array $data)
{
$this->post = $post;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,40 @@
<?php namespace Flarum\Core\Posts\Events;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class PostWillBeSaved
{
/**
* The post that will be saved.
*
* @var Post
*/
public $post;
/**
* The user who is performing the action.
*
* @var User
*/
public $actor;
/**
* The attributes to update on the post.
*
* @var array
*/
public $data;
/**
* @param Post $post
* @param User $actor
* @param array $data
*/
public function __construct(Post $post, User $actor, array $data = [])
{
$this->post = $post;
$this->actor = $actor;
$this->data = $data;
}
}

View File

@@ -0,0 +1,22 @@
<?php namespace Flarum\Core\Posts;
/**
* 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 MergeablePost
{
/**
* Save the model, given that it is going to appear immediately after the
* passed model.
*
* @param Post $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);
}

172
src/Core/Posts/Post.php Executable file
View File

@@ -0,0 +1,172 @@
<?php namespace Flarum\Core\Posts;
use DomainException;
use Flarum\Core\Posts\Events\PostWasDeleted;
use Flarum\Core\Model;
use Flarum\Core\Support\Locked;
use Flarum\Core\Exceptions\ValidationFailureException;
use Flarum\Core\Support\EventGenerator;
use Illuminate\Database\Eloquent\Builder;
class Post extends Model
{
use EventGenerator;
use Locked;
/**
* The validation rules for this model.
*
* @var array
*/
public static $rules = [
'discussion_id' => 'required|integer',
'time' => 'required|date',
'content' => 'required',
'number' => 'integer',
'user_id' => 'integer',
'edit_time' => 'date',
'edit_user_id' => 'integer',
'hide_time' => 'date',
'hide_user_id' => 'integer',
];
/**
* {@inheritdoc}
*/
protected $table = 'posts';
/**
* {@inheritdoc}
*/
protected static $dateAttributes = ['time', 'edit_time', 'hide_time'];
/**
* 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();
});
// Don't allow the first post in a discussion to be deleted, because
// it doesn't make sense. The discussion must be deleted instead.
static::deleting(function (Post $post) {
if ($post->number == 1) {
throw new DomainException('Cannot delete the first post of a discussion');
}
});
static::deleted(function (Post $post) {
$post->raise(new PostWasDeleted($post));
});
static::addGlobalScope(new RegisteredTypesScope);
}
/**
* Define the relationship with the post's discussion.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function discussion()
{
return $this->belongsTo('Flarum\Core\Discussions\Discussion', 'discussion_id');
}
/**
* Define the relationship with the post's author.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('Flarum\Core\Users\User', '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']])
) {
$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;
}
}

View File

@@ -0,0 +1,63 @@
<?php namespace Flarum\Core\Posts;
use Flarum\Core\Users\User;
interface PostRepositoryInterface
{
/**
* Find a post by ID, optionally making sure it is visible to a certain
* user, or throw an exception.
*
* @param integer $id
* @param \Flarum\Core\Users\User $actor
* @return \Flarum\Core\Posts\Post
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFail($id, User $actor = null);
/**
* 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\Core\Users\User|null $actor
* @param array $sort
* @param integer $count
* @param integer $start
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findWhere($where = [], User $actor = null, $sort = [], $count = null, $start = 0);
/**
* Find posts by their IDs, optionally making sure they are visible to a
* certain user.
*
* @param array $ids
* @param \Flarum\Core\Users\User|null $actor
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByIds(array $ids, User $actor = null);
/**
* Find posts by matching a string of words against their content,
* optionally making sure they are visible to a certain user.
*
* @param string $string
* @param \Flarum\Core\Users\User|null $actor
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByContent($string, User $actor = null);
/**
* 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 integer $discussionId
* @param integer $number
* @param \Flarum\Core\Users\User|null $actor
* @return integer
*/
public function getIndexForNumber($discussionId, $number, User $actor = null);
}

View File

@@ -0,0 +1,68 @@
<?php namespace Flarum\Core\Posts;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
use Flarum\Support\ServiceProvider;
use Flarum\Extend;
class PostsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->extend([
new Extend\PostType('Flarum\Core\Posts\CommentPost'),
new Extend\PostType('Flarum\Core\Posts\DiscussionRenamedPost')
]);
CommentPost::setFormatter($this->app->make('flarum.formatter'));
Post::allow('*', function ($post, $user, $action) {
return $post->discussion->can($user, $action.'Posts') ?: null;
});
// 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.
Discussion::addPostVisibilityScope(function ($query, User $user, Discussion $discussion) {
if (! $discussion->can($user, 'editPosts')) {
$query->where(function ($query) use ($user) {
$query->whereNull('hide_user_id')
->orWhere('hide_user_id', $user->id);
});
}
});
Post::allow('view', function ($post, $user) {
return ! $post->hide_user_id || $post->can($user, 'edit') ?: null;
});
// 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.
Post::allow('edit', function ($post, $user) {
if ($post->discussion->can($user, 'editPosts') ||
($post->user_id == $user->id && (! $post->hide_user_id || $post->hide_user_id == $user->id))
) {
return true;
}
});
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind(
'Flarum\Core\Posts\PostRepositoryInterface',
'Flarum\Core\Posts\EloquentPostRepository'
);
}
}

View File

@@ -0,0 +1,67 @@
<?php namespace Flarum\Core\Posts;
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
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));
}
}