mirror of
https://github.com/flarum/core.git
synced 2025-09-08 23:20:54 +02:00
Initial commit
This commit is contained in:
31
extensions/approval/src/Events/PostWasApproved.php
Normal file
31
extensions/approval/src/Events/PostWasApproved.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?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\Approval\Events;
|
||||
|
||||
use Flarum\Core\Posts\Post;
|
||||
|
||||
class PostWasApproved
|
||||
{
|
||||
/**
|
||||
* The post that was approved.
|
||||
*
|
||||
* @var Post
|
||||
*/
|
||||
public $post;
|
||||
|
||||
/**
|
||||
* @param Post $post
|
||||
*/
|
||||
public function __construct(Post $post)
|
||||
{
|
||||
$this->post = $post;
|
||||
}
|
||||
}
|
16
extensions/approval/src/Extension.php
Normal file
16
extensions/approval/src/Extension.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php namespace Flarum\Approval;
|
||||
|
||||
use Flarum\Support\Extension as BaseExtension;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
class Extension extends BaseExtension
|
||||
{
|
||||
public function listen(Dispatcher $events)
|
||||
{
|
||||
$events->subscribe('Flarum\Approval\Listeners\AddClientAssets');
|
||||
$events->subscribe('Flarum\Approval\Listeners\AddApiAttributes');
|
||||
$events->subscribe('Flarum\Approval\Listeners\HideUnapprovedContent');
|
||||
$events->subscribe('Flarum\Approval\Listeners\UnapproveNewContent');
|
||||
$events->subscribe('Flarum\Approval\Listeners\ApproveContent');
|
||||
}
|
||||
}
|
26
extensions/approval/src/Listeners/AddApiAttributes.php
Normal file
26
extensions/approval/src/Listeners/AddApiAttributes.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php namespace Flarum\Approval\Listeners;
|
||||
|
||||
use Flarum\Events\ApiAttributes;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
use Flarum\Api\Serializers\PostSerializer;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddApiAttributes
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ApiAttributes::class, [$this, 'addApiAttributes']);
|
||||
}
|
||||
|
||||
public function addApiAttributes(ApiAttributes $event)
|
||||
{
|
||||
if ($event->serializer instanceof DiscussionSerializer ||
|
||||
$event->serializer instanceof PostSerializer) {
|
||||
$event->attributes['isApproved'] = (bool) $event->model->is_approved;
|
||||
}
|
||||
|
||||
if ($event->serializer instanceof PostSerializer) {
|
||||
$event->attributes['canApprove'] = (bool) $event->model->discussion->can($event->actor, 'approvePosts');
|
||||
}
|
||||
}
|
||||
}
|
44
extensions/approval/src/Listeners/AddClientAssets.php
Normal file
44
extensions/approval/src/Listeners/AddClientAssets.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php namespace Flarum\Approval\Listeners;
|
||||
|
||||
use Flarum\Events\RegisterLocales;
|
||||
use Flarum\Events\BuildClientView;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddClientAssets
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(RegisterLocales::class, [$this, 'addLocale']);
|
||||
$events->listen(BuildClientView::class, [$this, 'addAssets']);
|
||||
}
|
||||
|
||||
public function addLocale(RegisterLocales $event)
|
||||
{
|
||||
$event->addTranslations('en', __DIR__.'/../../locale/en.yml');
|
||||
}
|
||||
|
||||
public function addAssets(BuildClientView $event)
|
||||
{
|
||||
$event->forumAssets([
|
||||
__DIR__.'/../../js/forum/dist/extension.js',
|
||||
__DIR__.'/../../less/forum/extension.less'
|
||||
]);
|
||||
|
||||
$event->forumBootstrapper('approval/main');
|
||||
|
||||
$event->forumTranslations([
|
||||
// 'approval.hello_world'
|
||||
]);
|
||||
|
||||
$event->adminAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js',
|
||||
__DIR__.'/../../less/admin/extension.less'
|
||||
]);
|
||||
|
||||
$event->adminBootstrapper('approval/main');
|
||||
|
||||
$event->adminTranslations([
|
||||
// 'approval.hello_world'
|
||||
]);
|
||||
}
|
||||
}
|
47
extensions/approval/src/Listeners/ApproveContent.php
Normal file
47
extensions/approval/src/Listeners/ApproveContent.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php namespace Flarum\Approval\Listeners;
|
||||
|
||||
use Flarum\Events\PostWillBeSaved;
|
||||
use Flarum\Approval\Events\PostWasApproved;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class ApproveContent
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWillBeSaved::class, [$this, 'approvePost']);
|
||||
$events->listen(PostWasApproved::class, [$this, 'approveDiscussion']);
|
||||
}
|
||||
|
||||
public function approvePost(PostWillBeSaved $event)
|
||||
{
|
||||
$attributes = $event->data['attributes'];
|
||||
$post = $event->post;
|
||||
|
||||
if (isset($attributes['isApproved'])) {
|
||||
$post->assertCan($event->actor, 'approve');
|
||||
|
||||
$isApproved = (bool) $attributes['isApproved'];
|
||||
} elseif (! empty($attributes['isHidden']) && $post->can($event->actor, 'approve')) {
|
||||
$isApproved = true;
|
||||
}
|
||||
|
||||
if (! empty($isApproved)) {
|
||||
$post->is_approved = true;
|
||||
|
||||
$post->raise(new PostWasApproved($post));
|
||||
}
|
||||
}
|
||||
|
||||
public function approveDiscussion(PostWasApproved $event)
|
||||
{
|
||||
$post = $event->post;
|
||||
|
||||
$post->discussion->refreshCommentsCount();
|
||||
$post->discussion->refreshLastPost();
|
||||
|
||||
if ($post->number == 1) {
|
||||
$post->discussion->is_approved = true;
|
||||
$post->discussion->save();
|
||||
}
|
||||
}
|
||||
}
|
44
extensions/approval/src/Listeners/HideUnapprovedContent.php
Normal file
44
extensions/approval/src/Listeners/HideUnapprovedContent.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php namespace Flarum\Approval\Listeners;
|
||||
|
||||
use Flarum\Events\ScopeModelVisibility;
|
||||
use Flarum\Events\ScopePostVisibility;
|
||||
use Flarum\Events\ScopeHiddenDiscussionVisibility;
|
||||
use Flarum\Core\Discussions\Discussion;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class HideUnapprovedContent
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ScopeModelVisibility::class, [$this, 'hideUnapprovedDiscussions']);
|
||||
$events->listen(ScopePostVisibility::class, [$this, 'hideUnapprovedPosts']);
|
||||
}
|
||||
|
||||
public function hideUnapprovedDiscussions(ScopeModelVisibility $event)
|
||||
{
|
||||
if ($event->model instanceof Discussion) {
|
||||
$user = $event->actor;
|
||||
|
||||
if (! $user->hasPermission('discussion.editPosts')) {
|
||||
$event->query->where(function ($query) use ($user) {
|
||||
$query->where('discussions.is_approved', 1)
|
||||
->orWhere('start_user_id', $user->id);
|
||||
|
||||
event(new ScopeHiddenDiscussionVisibility($query, $user, 'discussion.editPosts'));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function hideUnapprovedPosts(ScopePostVisibility $event)
|
||||
{
|
||||
if ($event->discussion->can($event->actor, 'editPosts')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$event->query->where(function ($query) use ($event) {
|
||||
$query->where('posts.is_approved', 1)
|
||||
->orWhere('user_id', $event->actor->id);
|
||||
});
|
||||
}
|
||||
}
|
47
extensions/approval/src/Listeners/UnapproveNewContent.php
Normal file
47
extensions/approval/src/Listeners/UnapproveNewContent.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php namespace Flarum\Approval\Listeners;
|
||||
|
||||
use Flarum\Events\PostWillBeSaved;
|
||||
use Flarum\Flags\Flag;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class UnapproveNewContent
|
||||
{
|
||||
private $savingPost;
|
||||
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWillBeSaved::class, [$this, 'unapproveNewPosts']);
|
||||
}
|
||||
|
||||
public function unapproveNewPosts(PostWillBeSaved $event)
|
||||
{
|
||||
$post = $event->post;
|
||||
|
||||
if (! $post->exists) {
|
||||
if ($post->discussion->can($event->actor, 'replyWithoutApproval')) {
|
||||
if ($post->is_approved === null) {
|
||||
$post->is_approved = true;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$post->is_approved = false;
|
||||
|
||||
$post->afterSave(function ($post) {
|
||||
if ($post->number == 1) {
|
||||
$post->discussion->is_approved = false;
|
||||
$post->discussion->save();
|
||||
}
|
||||
|
||||
$flag = new Flag;
|
||||
|
||||
$flag->post_id = $post->id;
|
||||
$flag->type = 'approval';
|
||||
$flag->time = time();
|
||||
|
||||
$flag->save();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user