mirror of
https://github.com/flarum/core.git
synced 2025-08-19 14:51:47 +02:00
Update for composer branch
This commit is contained in:
@@ -8,10 +8,10 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Likes\Events;
|
||||
namespace Flarum\Likes\Event;
|
||||
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
|
||||
class PostWasLiked
|
||||
{
|
@@ -8,10 +8,10 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Likes\Events;
|
||||
namespace Flarum\Likes\Event;
|
||||
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
|
||||
class PostWasUnliked
|
||||
{
|
@@ -1,26 +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\Likes;
|
||||
|
||||
use Flarum\Support\Extension as BaseExtension;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
class Extension extends BaseExtension
|
||||
{
|
||||
public function listen(Dispatcher $events)
|
||||
{
|
||||
$events->subscribe('Flarum\Likes\Listeners\AddClientAssets');
|
||||
$events->subscribe('Flarum\Likes\Listeners\AddModelRelationship');
|
||||
$events->subscribe('Flarum\Likes\Listeners\AddApiAttributes');
|
||||
$events->subscribe('Flarum\Likes\Listeners\PersistData');
|
||||
$events->subscribe('Flarum\Likes\Listeners\NotifyPostLiked');
|
||||
}
|
||||
}
|
47
extensions/likes/src/Listener/AddClientAssets.php
Executable file
47
extensions/likes/src/Listener/AddClientAssets.php
Executable file
@@ -0,0 +1,47 @@
|
||||
<?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\Likes\Listener;
|
||||
|
||||
use Flarum\Event\ConfigureClientView;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddClientAssets
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ConfigureClientView::class, [$this, 'addAssets']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureClientView $event
|
||||
*/
|
||||
public function addAssets(ConfigureClientView $event)
|
||||
{
|
||||
if ($event->isForum()) {
|
||||
$event->addAssets([
|
||||
__DIR__.'/../../js/forum/dist/extension.js',
|
||||
__DIR__.'/../../less/forum/extension.less'
|
||||
]);
|
||||
$event->addBootstrapper('flarum/likes/main');
|
||||
$event->addTranslations('flarum-likes.forum');
|
||||
}
|
||||
|
||||
if ($event->isAdmin()) {
|
||||
$event->addAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js'
|
||||
]);
|
||||
$event->addBootstrapper('flarum/likes/main');
|
||||
}
|
||||
}
|
||||
}
|
85
extensions/likes/src/Listener/AddPostLikesRelationship.php
Executable file
85
extensions/likes/src/Listener/AddPostLikesRelationship.php
Executable 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\Likes\Listener;
|
||||
|
||||
use Flarum\Api\Controller;
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Api\Serializer\UserBasicSerializer;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\ConfigureApiController;
|
||||
use Flarum\Event\GetApiRelationship;
|
||||
use Flarum\Event\GetModelRelationship;
|
||||
use Flarum\Event\PrepareApiAttributes;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddPostLikesRelationship
|
||||
{
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(GetModelRelationship::class, [$this, 'getModelRelationship']);
|
||||
$events->listen(GetApiRelationship::class, [$this, 'getApiAttributes']);
|
||||
$events->listen(PrepareApiAttributes::class, [$this, 'prepareApiAttributes']);
|
||||
$events->listen(ConfigureApiController::class, [$this, 'includeLikes']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GetModelRelationship $event
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany|null
|
||||
*/
|
||||
public function getModelRelationship(GetModelRelationship $event)
|
||||
{
|
||||
if ($event->isRelationship(Post::class, 'likes')) {
|
||||
return $event->model->belongsToMany(User::class, 'posts_likes', 'post_id', 'user_id', 'likes');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param GetApiRelationship $event
|
||||
* @return \Flarum\Api\Relationship\HasManyBuilder|null
|
||||
*/
|
||||
public function getApiAttributes(GetApiRelationship $event)
|
||||
{
|
||||
if ($event->isRelationship(PostSerializer::class, 'likes')) {
|
||||
return $event->serializer->hasMany(UserBasicSerializer::class, 'likes');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PrepareApiAttributes $event
|
||||
*/
|
||||
public function prepareApiAttributes(PrepareApiAttributes $event)
|
||||
{
|
||||
if ($event->isSerializer(PostSerializer::class)) {
|
||||
$event->attributes['canLike'] = (bool) $event->actor->can('like', $event->model);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConfigureApiController $event
|
||||
*/
|
||||
public function includeLikes(ConfigureApiController $event)
|
||||
{
|
||||
if ($event->isController(Controller\ShowDiscussionController::class)) {
|
||||
$event->addInclude('posts.likes');
|
||||
}
|
||||
|
||||
if ($event->isController(Controller\ListPostsController::class)
|
||||
|| $event->isController(Controller\ShowPostController::class)
|
||||
|| $event->isController(Controller\CreatePostController::class)
|
||||
|| $event->isController(Controller\UpdatePostController::class)) {
|
||||
$event->addInclude('likes');
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,24 +8,31 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Likes\Listeners;
|
||||
namespace Flarum\Likes\Listener;
|
||||
|
||||
use Flarum\Likes\Events\PostWasLiked;
|
||||
use Flarum\Likes\Events\PostWasUnliked;
|
||||
use Flarum\Events\PostWillBeSaved;
|
||||
use Flarum\Events\PostWasDeleted;
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Core\Access\AssertPermissionTrait;
|
||||
use Flarum\Event\PostWasDeleted;
|
||||
use Flarum\Event\PostWillBeSaved;
|
||||
use Flarum\Likes\Event\PostWasLiked;
|
||||
use Flarum\Likes\Event\PostWasUnliked;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class PersistData
|
||||
class SaveLikesToDatabase
|
||||
{
|
||||
use AssertPermissionTrait;
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(PostWillBeSaved::class, [$this, 'whenPostWillBeSaved']);
|
||||
$events->listen(PostWasDeleted::class, [$this, 'whenPostWasDeleted']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWillBeSaved $event
|
||||
*/
|
||||
public function whenPostWillBeSaved(PostWillBeSaved $event)
|
||||
{
|
||||
$post = $event->post;
|
||||
@@ -35,9 +42,7 @@ class PersistData
|
||||
$actor = $event->actor;
|
||||
$liked = (bool) $data['attributes']['isLiked'];
|
||||
|
||||
if (! $post->can($actor, 'like')) {
|
||||
throw new PermissionDeniedException;
|
||||
}
|
||||
$this->assertCan($actor, 'like', $post);
|
||||
|
||||
$currentlyLiked = $post->likes()->where('user_id', $actor->id)->exists();
|
||||
|
||||
@@ -53,6 +58,9 @@ class PersistData
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWasDeleted $event
|
||||
*/
|
||||
public function whenPostWasDeleted(PostWasDeleted $event)
|
||||
{
|
||||
$event->post->likes()->detach();
|
@@ -8,52 +8,72 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Likes\Listeners;
|
||||
namespace Flarum\Likes\Listener;
|
||||
|
||||
use Flarum\Likes\Notifications\PostLikedBlueprint;
|
||||
use Flarum\Events\RegisterNotificationTypes;
|
||||
use Flarum\Likes\Events\PostWasLiked;
|
||||
use Flarum\Likes\Events\PostWasUnliked;
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Notifications\NotificationSyncer;
|
||||
use Flarum\Api\Serializer\PostBasicSerializer;
|
||||
use Flarum\Core\Notification\NotificationSyncer;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\ConfigureNotificationTypes;
|
||||
use Flarum\Likes\Event\PostWasLiked;
|
||||
use Flarum\Likes\Event\PostWasUnliked;
|
||||
use Flarum\Likes\Notification\PostLikedBlueprint;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class NotifyPostLiked
|
||||
class SendNotificationWhenPostIsLiked
|
||||
{
|
||||
/**
|
||||
* @var NotificationSyncer
|
||||
*/
|
||||
protected $notifications;
|
||||
|
||||
/**
|
||||
* @param NotificationSyncer $notifications
|
||||
*/
|
||||
public function __construct(NotificationSyncer $notifications)
|
||||
{
|
||||
$this->notifications = $notifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(RegisterNotificationTypes::class, [$this, 'registerNotificationType']);
|
||||
$events->listen(ConfigureNotificationTypes::class, [$this, 'registerNotificationType']);
|
||||
$events->listen(PostWasLiked::class, [$this, 'whenPostWasLiked']);
|
||||
$events->listen(PostWasUnliked::class, [$this, 'whenPostWasUnliked']);
|
||||
}
|
||||
|
||||
public function registerNotificationType(RegisterNotificationTypes $event)
|
||||
/**
|
||||
* @param ConfigureNotificationTypes $event
|
||||
*/
|
||||
public function registerNotificationType(ConfigureNotificationTypes $event)
|
||||
{
|
||||
$event->register(
|
||||
'Flarum\Likes\Notifications\PostLikedBlueprint',
|
||||
'Flarum\Api\Serializers\PostBasicSerializer',
|
||||
['alert']
|
||||
);
|
||||
$event->add(PostLikedBlueprint::class, PostBasicSerializer::class, ['alert']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWasLiked $event
|
||||
*/
|
||||
public function whenPostWasLiked(PostWasLiked $event)
|
||||
{
|
||||
$this->sync($event->post, $event->user, [$event->post->user]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PostWasUnliked $event
|
||||
*/
|
||||
public function whenPostWasUnliked(PostWasUnliked $event)
|
||||
{
|
||||
$this->sync($event->post, $event->user, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Post $post
|
||||
* @param User $user
|
||||
* @param array $recipients
|
||||
*/
|
||||
public function sync(Post $post, User $user, array $recipients)
|
||||
{
|
||||
if ($post->user->id != $user->id) {
|
@@ -1,60 +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\Likes\Listeners;
|
||||
|
||||
use Flarum\Events\ApiAttributes;
|
||||
use Flarum\Events\ApiRelationship;
|
||||
use Flarum\Events\BuildApiAction;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Flarum\Api\Serializers\PostSerializer;
|
||||
use Flarum\Api\Actions\Discussions;
|
||||
use Flarum\Api\Actions\Posts;
|
||||
|
||||
class AddApiAttributes
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ApiAttributes::class, [$this, 'addAttributes']);
|
||||
$events->listen(ApiRelationship::class, [$this, 'addRelationship']);
|
||||
$events->listen(BuildApiAction::class, [$this, 'includeLikes']);
|
||||
}
|
||||
|
||||
public function addAttributes(ApiAttributes $event)
|
||||
{
|
||||
if ($event->serializer instanceof PostSerializer) {
|
||||
$event->attributes['canLike'] = (bool) $event->model->can($event->actor, 'like');
|
||||
}
|
||||
}
|
||||
|
||||
public function addRelationship(ApiRelationship $event)
|
||||
{
|
||||
if ($event->serializer instanceof PostSerializer &&
|
||||
$event->relationship === 'likes') {
|
||||
return $event->serializer->hasMany('Flarum\Api\Serializers\UserBasicSerializer', 'likes');
|
||||
}
|
||||
}
|
||||
|
||||
public function includeLikes(BuildApiAction $event)
|
||||
{
|
||||
$action = $event->action;
|
||||
|
||||
if ($action instanceof Discussions\ShowAction) {
|
||||
$event->addInclude('posts.likes');
|
||||
}
|
||||
|
||||
if ($action instanceof Posts\IndexAction ||
|
||||
$action instanceof Posts\ShowAction ||
|
||||
$action instanceof Posts\CreateAction ||
|
||||
$action instanceof Posts\UpdateAction) {
|
||||
$event->addInclude('likes');
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,57 +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\Likes\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('likes/main');
|
||||
|
||||
$event->forumTranslations([
|
||||
'likes.post_liked_notification',
|
||||
'likes.post_likes_modal_title',
|
||||
'likes.post_liked_by_self',
|
||||
'likes.post_liked_by',
|
||||
'likes.unlike_action',
|
||||
'likes.like_action',
|
||||
'likes.notify_post_liked',
|
||||
'likes.others',
|
||||
'likes.you'
|
||||
]);
|
||||
|
||||
$event->adminAssets([
|
||||
__DIR__.'/../../js/admin/dist/extension.js'
|
||||
]);
|
||||
|
||||
$event->adminBootstrapper('likes/main');
|
||||
}
|
||||
}
|
@@ -1,31 +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\Likes\Listeners;
|
||||
|
||||
use Flarum\Events\ModelRelationship;
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class AddModelRelationship
|
||||
{
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen(ModelRelationship::class, [$this, 'addRelationship']);
|
||||
}
|
||||
|
||||
public function addRelationship(ModelRelationship $event)
|
||||
{
|
||||
if ($event->model instanceof Post &&
|
||||
$event->relationship === 'likes') {
|
||||
return $event->model->belongsToMany('Flarum\Core\Users\User', 'posts_likes', 'post_id', 'user_id', 'likes');
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,46 +8,71 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Likes\Notifications;
|
||||
namespace Flarum\Likes\Notification;
|
||||
|
||||
use Flarum\Core\Posts\Post;
|
||||
use Flarum\Core\Users\User;
|
||||
use Flarum\Core\Notifications\Blueprint;
|
||||
use Flarum\Core\Post;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Core\Notification\BlueprintInterface;
|
||||
|
||||
class PostLikedBlueprint implements Blueprint
|
||||
class PostLikedBlueprint implements BlueprintInterface
|
||||
{
|
||||
/**
|
||||
* @var Post
|
||||
*/
|
||||
public $post;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* @param Post $post
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(Post $post, User $user)
|
||||
{
|
||||
$this->post = $post;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->post;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSender()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getType()
|
||||
{
|
||||
return 'postLiked';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return 'Flarum\Core\Posts\Post';
|
||||
return Post::class;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user