mirror of
https://github.com/flarum/core.git
synced 2025-08-19 23:01:56 +02:00
Initial commit
This commit is contained in:
27
extensions/likes/src/Events/PostWasLiked.php
Normal file
27
extensions/likes/src/Events/PostWasLiked.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php namespace Flarum\Likes\Events;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class PostWasLiked
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\Core\Models\Post
|
||||
*/
|
||||
public $post;
|
||||
|
||||
/**
|
||||
* @var \Flarum\Core\Models\User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* @param \Flarum\Core\Models\Post $post
|
||||
* @param \Flarum\Core\Models\User $user
|
||||
*/
|
||||
public function __construct(Post $post, User $user)
|
||||
{
|
||||
$this->post = $post;
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
27
extensions/likes/src/Events/PostWasUnliked.php
Normal file
27
extensions/likes/src/Events/PostWasUnliked.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php namespace Flarum\Likes\Events;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class PostWasUnliked
|
||||
{
|
||||
/**
|
||||
* @var \Flarum\Core\Models\Post
|
||||
*/
|
||||
public $post;
|
||||
|
||||
/**
|
||||
* @var \Flarum\Core\Models\User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* @param \Flarum\Core\Models\Post $post
|
||||
* @param \Flarum\Core\Models\User $user
|
||||
*/
|
||||
public function __construct(Post $post, User $user)
|
||||
{
|
||||
$this->post = $post;
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
47
extensions/likes/src/Handlers/LikedSaver.php
Executable file
47
extensions/likes/src/Handlers/LikedSaver.php
Executable file
@@ -0,0 +1,47 @@
|
||||
<?php namespace Flarum\Likes\Handlers;
|
||||
|
||||
use Flarum\Likes\Events\PostWasLiked;
|
||||
use Flarum\Likes\Events\PostWasUnliked;
|
||||
use Flarum\Core\Events\PostWillBeSaved;
|
||||
use Flarum\Core\Events\PostWasDeleted;
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
|
||||
class LikedSaver
|
||||
{
|
||||
public function subscribe($events)
|
||||
{
|
||||
$events->listen('Flarum\Core\Events\PostWillBeSaved', __CLASS__.'@whenPostWillBeSaved');
|
||||
$events->listen('Flarum\Core\Events\PostWasDeleted', __CLASS__.'@whenPostWasDeleted');
|
||||
}
|
||||
|
||||
public function whenPostWillBeSaved(PostWillBeSaved $event)
|
||||
{
|
||||
$post = $event->post;
|
||||
$data = $event->command->data;
|
||||
|
||||
if ($post->exists && isset($data['isLiked'])) {
|
||||
$user = $event->command->user;
|
||||
$liked = (bool) $data['isLiked'];
|
||||
|
||||
if (! $post->can($user, 'like')) {
|
||||
throw new PermissionDeniedException;
|
||||
}
|
||||
|
||||
if ($liked) {
|
||||
$post->likes()->attach($user->id);
|
||||
|
||||
$post->raise(new PostWasLiked($post, $user));
|
||||
} else {
|
||||
$post->likes()->detach($user->id);
|
||||
|
||||
$post->raise(new PostWasUnliked($post, $user));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function whenPostWasDeleted(PostWasDeleted $event)
|
||||
{
|
||||
$event->post->likes()->detach();
|
||||
}
|
||||
}
|
50
extensions/likes/src/Handlers/PostLikedNotifier.php
Executable file
50
extensions/likes/src/Handlers/PostLikedNotifier.php
Executable file
@@ -0,0 +1,50 @@
|
||||
<?php namespace Flarum\Likes\Handlers;
|
||||
|
||||
use Flarum\Likes\PostLikedNotification;
|
||||
use Flarum\Likes\Events\PostWasLiked;
|
||||
use Flarum\Likes\Events\PostWasUnliked;
|
||||
use Flarum\Core\Notifications\NotificationSyncer;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class PostLikedNotifier
|
||||
{
|
||||
protected $notifications;
|
||||
|
||||
public function __construct(NotificationSyncer $notifications)
|
||||
{
|
||||
$this->notifications = $notifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listeners for the subscriber.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen('Flarum\Likes\Events\PostWasLiked', __CLASS__.'@whenPostWasLiked');
|
||||
$events->listen('Flarum\Likes\Events\PostWasUnliked', __CLASS__.'@whenPostWasUnliked');
|
||||
}
|
||||
|
||||
public function whenPostWasLiked(PostWasLiked $event)
|
||||
{
|
||||
if ($event->post->user->id != $event->user->id) {
|
||||
$this->sync($event->post, $event->user, [$event->post->user]);
|
||||
}
|
||||
}
|
||||
|
||||
public function whenPostWasUnliked(PostWasUnliked $event)
|
||||
{
|
||||
if ($event->post->user->id != $event->user->id) {
|
||||
$this->sync($event->post, $event->user, []);
|
||||
}
|
||||
}
|
||||
|
||||
public function sync($post, $user, array $recipients)
|
||||
{
|
||||
$this->notifications->sync(
|
||||
new PostLikedNotification($post, $user),
|
||||
$recipients
|
||||
);
|
||||
}
|
||||
}
|
64
extensions/likes/src/LikesServiceProvider.php
Normal file
64
extensions/likes/src/LikesServiceProvider.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php namespace Flarum\Likes;
|
||||
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Extend;
|
||||
|
||||
class LikesServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application events.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->extend([
|
||||
(new Extend\Locale('en'))->translations(__DIR__.'/../locale/en.yml'),
|
||||
|
||||
(new Extend\ForumClient)
|
||||
->assets([
|
||||
__DIR__.'/../js/dist/extension.js',
|
||||
__DIR__.'/../less/extension.less'
|
||||
]),
|
||||
|
||||
(new Extend\Model('Flarum\Core\Models\Post'))
|
||||
->belongsToMany('likes', 'Flarum\Core\Models\User', 'posts_likes', 'post_id', 'user_id'),
|
||||
|
||||
(new Extend\ApiSerializer('Flarum\Api\Serializers\PostSerializer'))
|
||||
->hasMany('likes', 'Flarum\Api\Serializers\UserBasicSerializer')
|
||||
->attributes(function (&$attributes, $post, $user) {
|
||||
$attributes['canLike'] = $post->can($user, 'like');
|
||||
}),
|
||||
|
||||
(new Extend\ApiAction('Flarum\Api\Actions\Discussions\ShowAction'))
|
||||
->addInclude('posts.likes'),
|
||||
|
||||
(new Extend\ApiAction([
|
||||
'Flarum\Api\Actions\Posts\IndexAction',
|
||||
'Flarum\Api\Actions\Posts\ShowAction',
|
||||
'Flarum\Api\Actions\Posts\CreateAction',
|
||||
'Flarum\Api\Actions\Posts\UpdateAction'
|
||||
]))
|
||||
->addInclude('likes'),
|
||||
|
||||
new Extend\EventSubscriber('Flarum\Likes\Handlers\LikedSaver'),
|
||||
new Extend\EventSubscriber('Flarum\Likes\Handlers\PostLikedNotifier'),
|
||||
|
||||
(new Extend\NotificationType(
|
||||
'Flarum\Likes\PostLikedNotification',
|
||||
'Flarum\Api\Serializers\PostBasicSerializer'
|
||||
))
|
||||
->enableByDefault('alert')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
38
extensions/likes/src/PostLikedNotification.php
Normal file
38
extensions/likes/src/PostLikedNotification.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php namespace Flarum\Likes;
|
||||
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
use Flarum\Core\Notifications\NotificationAbstract;
|
||||
|
||||
class PostLikedNotification extends NotificationAbstract
|
||||
{
|
||||
public $post;
|
||||
|
||||
public $user;
|
||||
|
||||
public function __construct(Post $post, User $user)
|
||||
{
|
||||
$this->post = $post;
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->post;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
{
|
||||
return 'postLiked';
|
||||
}
|
||||
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return 'Flarum\Core\Models\Post';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user