2015-08-27 07:40:18 +08:00
|
|
|
<?php
|
2016-11-29 13:03:53 +08:00
|
|
|
|
2015-08-26 14:48:58 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2017-06-24 19:43:33 +08:00
|
|
|
namespace Flarum\Post;
|
2015-07-04 10:54:48 +08:00
|
|
|
|
2017-06-24 19:25:49 +08:00
|
|
|
use Flarum\Foundation\EventGeneratorTrait;
|
2017-06-24 18:09:22 +08:00
|
|
|
use Flarum\Database\ScopeVisibilityTrait;
|
2016-02-10 18:00:37 +08:00
|
|
|
use Flarum\Database\AbstractModel;
|
2017-06-24 19:43:33 +08:00
|
|
|
use Flarum\Post\Event\Deleted;
|
2017-06-24 21:04:46 +08:00
|
|
|
use Flarum\User\User;
|
2015-07-04 10:54:48 +08:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
2015-07-07 17:50:18 +08:00
|
|
|
/**
|
Major refactor and improvements
- Reorganised all namespaces and class names for consistency and structure. Following PSR bylaws (Abstract prefix, Interface/Trait suffix).
- Move models into root of Core, because writing `use Flarum\Core\Discussion` is nice. Namespace the rest by type. (Namespacing by entity was too arbitrary.)
- Moved some non-domain stuff out of Core: Database, Formatter, Settings.
- Renamed config table and all references to "settings" for consistency.
- Remove Core class and add url()/isInstalled()/inDebugMode() as instance methods of Foundation\Application.
- Cleanup, docblocking, etc.
- Improvements to HTTP architecture
- API and forum/admin Actions are now actually all the same thing (simple PSR-7 Request handlers), renamed to Controllers.
- Upgrade to tobscure/json-api 0.2 branch.
- Where possible, moved generic functionality to tobscure/json-api (e.g. pagination links). I'm quite happy with the backend balance now re: #262
- Improvements to other architecture
- Use Illuminate's Auth\Access\Gate interface/implementation instead of our old Locked trait. We still use events to actually determine the permissions though. Our Policy classes are actually glorified event subscribers.
- Extract model validation into Core\Validator classes.
- Make post visibility permission stuff much more efficient and DRY.
- Renamed Flarum\Event classes for consistency. ref #246
- `Configure` prefix for events dedicated to configuring an object.
- `Get` prefix for events whose listeners should return something.
- `Prepare` prefix when a variable is passed by reference so it can be modified.
- `Scope` prefix when a query builder is passed.
- Miscellaneous improvements/bug-fixes. I'm easily distracted!
- Increase default height of post composer.
- Improve post stream redraw flickering in Safari by keying loading post placeholders with their IDs. ref #451
- Use a PHP JavaScript minification library for minifying TextFormatter's JavaScript, instead of ClosureCompilerService (can't rely on external service!)
- Use UrlGenerator properly in various places. closes #123
- Make Api\Client return Response object. closes #128
- Allow extensions to specify custom icon images.
- Allow external API/admin URLs to be optionally specified in config.php. If the value or "url" is an array, we look for the corresponding path inside. Otherwise, we append the path to the base URL, using the corresponding value in "paths" if present. closes #244
2015-10-08 11:58:02 +08:00
|
|
|
* @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
|
2017-06-24 19:48:04 +08:00
|
|
|
* @property \Flarum\Discussion\Discussion|null $discussion
|
Major refactor and improvements
- Reorganised all namespaces and class names for consistency and structure. Following PSR bylaws (Abstract prefix, Interface/Trait suffix).
- Move models into root of Core, because writing `use Flarum\Core\Discussion` is nice. Namespace the rest by type. (Namespacing by entity was too arbitrary.)
- Moved some non-domain stuff out of Core: Database, Formatter, Settings.
- Renamed config table and all references to "settings" for consistency.
- Remove Core class and add url()/isInstalled()/inDebugMode() as instance methods of Foundation\Application.
- Cleanup, docblocking, etc.
- Improvements to HTTP architecture
- API and forum/admin Actions are now actually all the same thing (simple PSR-7 Request handlers), renamed to Controllers.
- Upgrade to tobscure/json-api 0.2 branch.
- Where possible, moved generic functionality to tobscure/json-api (e.g. pagination links). I'm quite happy with the backend balance now re: #262
- Improvements to other architecture
- Use Illuminate's Auth\Access\Gate interface/implementation instead of our old Locked trait. We still use events to actually determine the permissions though. Our Policy classes are actually glorified event subscribers.
- Extract model validation into Core\Validator classes.
- Make post visibility permission stuff much more efficient and DRY.
- Renamed Flarum\Event classes for consistency. ref #246
- `Configure` prefix for events dedicated to configuring an object.
- `Get` prefix for events whose listeners should return something.
- `Prepare` prefix when a variable is passed by reference so it can be modified.
- `Scope` prefix when a query builder is passed.
- Miscellaneous improvements/bug-fixes. I'm easily distracted!
- Increase default height of post composer.
- Improve post stream redraw flickering in Safari by keying loading post placeholders with their IDs. ref #451
- Use a PHP JavaScript minification library for minifying TextFormatter's JavaScript, instead of ClosureCompilerService (can't rely on external service!)
- Use UrlGenerator properly in various places. closes #123
- Make Api\Client return Response object. closes #128
- Allow extensions to specify custom icon images.
- Allow external API/admin URLs to be optionally specified in config.php. If the value or "url" is an array, we look for the corresponding path inside. Otherwise, we append the path to the base URL, using the corresponding value in "paths" if present. closes #244
2015-10-08 11:58:02 +08:00
|
|
|
* @property User|null $user
|
|
|
|
* @property User|null $editUser
|
|
|
|
* @property User|null $hideUser
|
2015-12-23 20:54:58 +08:00
|
|
|
* @property string $ip_address
|
2017-05-27 12:49:15 +08:00
|
|
|
* @property bool $is_private
|
2015-07-07 17:50:18 +08:00
|
|
|
*/
|
Major refactor and improvements
- Reorganised all namespaces and class names for consistency and structure. Following PSR bylaws (Abstract prefix, Interface/Trait suffix).
- Move models into root of Core, because writing `use Flarum\Core\Discussion` is nice. Namespace the rest by type. (Namespacing by entity was too arbitrary.)
- Moved some non-domain stuff out of Core: Database, Formatter, Settings.
- Renamed config table and all references to "settings" for consistency.
- Remove Core class and add url()/isInstalled()/inDebugMode() as instance methods of Foundation\Application.
- Cleanup, docblocking, etc.
- Improvements to HTTP architecture
- API and forum/admin Actions are now actually all the same thing (simple PSR-7 Request handlers), renamed to Controllers.
- Upgrade to tobscure/json-api 0.2 branch.
- Where possible, moved generic functionality to tobscure/json-api (e.g. pagination links). I'm quite happy with the backend balance now re: #262
- Improvements to other architecture
- Use Illuminate's Auth\Access\Gate interface/implementation instead of our old Locked trait. We still use events to actually determine the permissions though. Our Policy classes are actually glorified event subscribers.
- Extract model validation into Core\Validator classes.
- Make post visibility permission stuff much more efficient and DRY.
- Renamed Flarum\Event classes for consistency. ref #246
- `Configure` prefix for events dedicated to configuring an object.
- `Get` prefix for events whose listeners should return something.
- `Prepare` prefix when a variable is passed by reference so it can be modified.
- `Scope` prefix when a query builder is passed.
- Miscellaneous improvements/bug-fixes. I'm easily distracted!
- Increase default height of post composer.
- Improve post stream redraw flickering in Safari by keying loading post placeholders with their IDs. ref #451
- Use a PHP JavaScript minification library for minifying TextFormatter's JavaScript, instead of ClosureCompilerService (can't rely on external service!)
- Use UrlGenerator properly in various places. closes #123
- Make Api\Client return Response object. closes #128
- Allow extensions to specify custom icon images.
- Allow external API/admin URLs to be optionally specified in config.php. If the value or "url" is an array, we look for the corresponding path inside. Otherwise, we append the path to the base URL, using the corresponding value in "paths" if present. closes #244
2015-10-08 11:58:02 +08:00
|
|
|
class Post extends AbstractModel
|
2015-07-04 10:54:48 +08:00
|
|
|
{
|
2017-06-24 19:33:42 +08:00
|
|
|
use EventGeneratorTrait;
|
Major refactor and improvements
- Reorganised all namespaces and class names for consistency and structure. Following PSR bylaws (Abstract prefix, Interface/Trait suffix).
- Move models into root of Core, because writing `use Flarum\Core\Discussion` is nice. Namespace the rest by type. (Namespacing by entity was too arbitrary.)
- Moved some non-domain stuff out of Core: Database, Formatter, Settings.
- Renamed config table and all references to "settings" for consistency.
- Remove Core class and add url()/isInstalled()/inDebugMode() as instance methods of Foundation\Application.
- Cleanup, docblocking, etc.
- Improvements to HTTP architecture
- API and forum/admin Actions are now actually all the same thing (simple PSR-7 Request handlers), renamed to Controllers.
- Upgrade to tobscure/json-api 0.2 branch.
- Where possible, moved generic functionality to tobscure/json-api (e.g. pagination links). I'm quite happy with the backend balance now re: #262
- Improvements to other architecture
- Use Illuminate's Auth\Access\Gate interface/implementation instead of our old Locked trait. We still use events to actually determine the permissions though. Our Policy classes are actually glorified event subscribers.
- Extract model validation into Core\Validator classes.
- Make post visibility permission stuff much more efficient and DRY.
- Renamed Flarum\Event classes for consistency. ref #246
- `Configure` prefix for events dedicated to configuring an object.
- `Get` prefix for events whose listeners should return something.
- `Prepare` prefix when a variable is passed by reference so it can be modified.
- `Scope` prefix when a query builder is passed.
- Miscellaneous improvements/bug-fixes. I'm easily distracted!
- Increase default height of post composer.
- Improve post stream redraw flickering in Safari by keying loading post placeholders with their IDs. ref #451
- Use a PHP JavaScript minification library for minifying TextFormatter's JavaScript, instead of ClosureCompilerService (can't rely on external service!)
- Use UrlGenerator properly in various places. closes #123
- Make Api\Client return Response object. closes #128
- Allow extensions to specify custom icon images.
- Allow external API/admin URLs to be optionally specified in config.php. If the value or "url" is an array, we look for the corresponding path inside. Otherwise, we append the path to the base URL, using the corresponding value in "paths" if present. closes #244
2015-10-08 11:58:02 +08:00
|
|
|
use ScopeVisibilityTrait;
|
2015-07-05 10:55:08 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected $table = 'posts';
|
2015-07-04 10:54:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2015-07-18 21:29:47 +08:00
|
|
|
protected $dates = ['time', 'edit_time', 'hide_time'];
|
2015-07-04 10:54:48 +08:00
|
|
|
|
2017-05-27 12:49:15 +08:00
|
|
|
/**
|
|
|
|
* Casts properties to a specific type.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'is_private' => 'boolean'
|
|
|
|
];
|
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-06-24 19:43:33 +08:00
|
|
|
$post->raise(new Deleted($post));
|
2015-07-04 10:54:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
static::addGlobalScope(new RegisteredTypesScope);
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:17:46 +08:00
|
|
|
/**
|
|
|
|
* Determine whether or not this post is visible to the given user.
|
|
|
|
*
|
|
|
|
* @param User $user
|
2016-02-26 11:09:39 +08:00
|
|
|
* @return bool
|
2015-08-04 16:17:46 +08:00
|
|
|
*/
|
|
|
|
public function isVisibleTo(User $user)
|
|
|
|
{
|
|
|
|
$discussion = $this->discussion()->whereVisibleTo($user)->first();
|
|
|
|
|
|
|
|
if ($discussion) {
|
|
|
|
$this->setRelation('discussion', $discussion);
|
|
|
|
|
2015-09-04 10:53:17 +08:00
|
|
|
return (bool) $discussion->postsVisibleTo($user)->where('id', $this->id)->count();
|
2015-08-04 16:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
/**
|
|
|
|
* Define the relationship with the post's discussion.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function discussion()
|
|
|
|
{
|
2017-06-24 19:48:04 +08:00
|
|
|
return $this->belongsTo('Flarum\Discussion\Discussion', 'discussion_id');
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the relationship with the post's author.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function user()
|
|
|
|
{
|
2017-06-24 18:55:22 +08:00
|
|
|
return $this->belongsTo('Flarum\User\User', 'user_id');
|
2015-07-04 10:54:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-06 14:56:27 +08:00
|
|
|
/**
|
|
|
|
* Define the relationship with the user who edited the post.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function editUser()
|
|
|
|
{
|
2017-06-24 18:55:22 +08:00
|
|
|
return $this->belongsTo('Flarum\User\User', 'edit_user_id');
|
2015-07-06 14:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define the relationship with the user who hid the post.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function hideUser()
|
|
|
|
{
|
2017-06-24 18:55:22 +08:00
|
|
|
return $this->belongsTo('Flarum\User\User', 'hide_user_id');
|
2015-07-06 14:56:27 +08:00
|
|
|
}
|
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
/**
|
|
|
|
* 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']])
|
|
|
|
) {
|
2015-07-18 21:29:47 +08:00
|
|
|
/** @var Post $instance */
|
2015-07-04 10:54:48 +08:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|