1
0
mirror of https://github.com/flarum/core.git synced 2025-10-21 19:56:29 +02:00

Extract new Flarum\Post namespace

This commit is contained in:
Franz Liedke
2017-06-24 13:43:33 +02:00
parent 3481798875
commit 66abd7ecfd
38 changed files with 146 additions and 154 deletions

36
src/Post/AbstractEventPost.php Executable file
View File

@@ -0,0 +1,36 @@
<?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\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);
}
}

185
src/Post/CommentPost.php Executable file
View File

@@ -0,0 +1,185 @@
<?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\Post;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
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 Posted($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 Revised($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 Hidden($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 Restored($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;
}
}

View File

@@ -0,0 +1,85 @@
<?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\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];
}
}

View File

@@ -0,0 +1,37 @@
<?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\Post\Event;
use Flarum\Post\Post;
use Flarum\User\User;
class Deleted
{
/**
* @var \Flarum\Post\Post
*/
public $post;
/**
* @var User
*/
public $actor;
/**
* @param \Flarum\Post\Post $post
*/
public function __construct(Post $post, User $actor = null)
{
$this->post = $post;
$this->actor = $actor;
}
}

View File

@@ -0,0 +1,51 @@
<?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\Post\Event;
use Flarum\Post\Post;
use Flarum\User\User;
class Deleting
{
/**
* The post that is going to be deleted.
*
* @var \Flarum\Post\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 \Flarum\Post\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;
}
}

37
src/Post/Event/Hidden.php Normal file
View File

@@ -0,0 +1,37 @@
<?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\Post\Event;
use Flarum\Post\CommentPost;
use Flarum\User\User;
class Hidden
{
/**
* @var CommentPost
*/
public $post;
/**
* @var User
*/
public $actor;
/**
* @param CommentPost $post
*/
public function __construct(CommentPost $post, User $actor = null)
{
$this->post = $post;
$this->actor = $actor;
}
}

37
src/Post/Event/Posted.php Normal file
View File

@@ -0,0 +1,37 @@
<?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\Post\Event;
use Flarum\Post\Post;
use Flarum\User\User;
class Posted
{
/**
* @var \Flarum\Post\Post
*/
public $post;
/**
* @var User
*/
public $actor;
/**
* @param \Flarum\Post\Post $post
*/
public function __construct(Post $post, User $actor = null)
{
$this->post = $post;
$this->actor = $actor;
}
}

View File

@@ -0,0 +1,37 @@
<?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\Post\Event;
use Flarum\Post\CommentPost;
use Flarum\User\User;
class Restored
{
/**
* @var \Flarum\Post\CommentPost
*/
public $post;
/**
* @var User
*/
public $actor;
/**
* @param \Flarum\Post\CommentPost $post
*/
public function __construct(CommentPost $post, User $actor = null)
{
$this->post = $post;
$this->actor = $actor;
}
}

View File

@@ -0,0 +1,37 @@
<?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\Post\Event;
use Flarum\Post\CommentPost;
use Flarum\User\User;
class Revised
{
/**
* @var \Flarum\Post\CommentPost
*/
public $post;
/**
* @var User
*/
public $actor;
/**
* @param \Flarum\Post\CommentPost $post
*/
public function __construct(CommentPost $post, User $actor = null)
{
$this->post = $post;
$this->actor = $actor;
}
}

51
src/Post/Event/Saving.php Normal file
View File

@@ -0,0 +1,51 @@
<?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\Post\Event;
use Flarum\Post\Post;
use Flarum\User\User;
class Saving
{
/**
* The post that will be saved.
*
* @var \Flarum\Post\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 \Flarum\Post\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,18 @@
<?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\Post\Exception;
use Exception;
class FloodingException extends Exception
{
}

39
src/Post/Floodgate.php Normal file
View File

@@ -0,0 +1,39 @@
<?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\Post;
use DateTime;
use Flarum\Post\Exception\FloodingException;
use Flarum\User\User;
class Floodgate
{
/**
* @param User $actor
* @throws \Flarum\Post\Exception\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();
}
}

View File

@@ -0,0 +1,33 @@
<?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\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 \Flarum\Post\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);
}

225
src/Post/Post.php Executable file
View File

@@ -0,0 +1,225 @@
<?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\Post;
use Flarum\Core\User;
use Flarum\Foundation\EventGeneratorTrait;
use Flarum\Database\ScopeVisibilityTrait;
use Flarum\Database\AbstractModel;
use Flarum\Post\Event\Deleted;
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 Deleted($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;
}
}

115
src/Post/PostPolicy.php Normal file
View File

@@ -0,0 +1,115 @@
<?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\Post;
use Carbon\Carbon;
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 \Flarum\Post\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;
}
}
}
}

184
src/Post/PostRepository.php Normal file
View File

@@ -0,0 +1,184 @@
<?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\Post;
use Flarum\Core\Discussion;
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();
}
}

View File

@@ -0,0 +1,24 @@
<?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\Post;
use Flarum\Foundation\AbstractValidator;
class PostValidator extends AbstractValidator
{
protected $rules = [
'content' => [
'required',
'max:65535'
]
];
}

View File

@@ -0,0 +1,78 @@
<?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\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));
}
}