1
0
mirror of https://github.com/flarum/core.git synced 2025-08-19 23:01:56 +02:00

Update for new extension API; implement l10n

This commit is contained in:
Toby Zerner
2015-07-22 10:15:08 +09:30
parent fb9ed378e0
commit 9c384bee98
30 changed files with 690 additions and 340 deletions

View File

@@ -1,23 +1,23 @@
<?php namespace Flarum\Likes\Events;
use Flarum\Core\Models\Post;
use Flarum\Core\Models\User;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class PostWasLiked
{
/**
* @var \Flarum\Core\Models\Post
* @var Post
*/
public $post;
/**
* @var \Flarum\Core\Models\User
* @var User
*/
public $user;
/**
* @param \Flarum\Core\Models\Post $post
* @param \Flarum\Core\Models\User $user
* @param Post $post
* @param User $user
*/
public function __construct(Post $post, User $user)
{

View File

@@ -1,23 +1,23 @@
<?php namespace Flarum\Likes\Events;
use Flarum\Core\Models\Post;
use Flarum\Core\Models\User;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
class PostWasUnliked
{
/**
* @var \Flarum\Core\Models\Post
* @var Post
*/
public $post;
/**
* @var \Flarum\Core\Models\User
* @var User
*/
public $user;
/**
* @param \Flarum\Core\Models\Post $post
* @param \Flarum\Core\Models\User $user
* @param Post $post
* @param User $user
*/
public function __construct(Post $post, User $user)
{

View File

@@ -0,0 +1,16 @@
<?php namespace Flarum\Likes;
use Flarum\Support\Extension as BaseExtension;
use Illuminate\Events\Dispatcher;
class Extension extends BaseExtension
{
public function boot(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');
}
}

View File

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

View File

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

View File

@@ -1,64 +0,0 @@
<?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()
{
//
}
}

View File

@@ -0,0 +1,50 @@
<?php 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, __CLASS__.'@addAttributes');
$events->listen(ApiRelationship::class, __CLASS__.'@addRelationship');
$events->listen(BuildApiAction::class, __CLASS__.'@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');
}
}
}

View File

@@ -0,0 +1,40 @@
<?php 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, __CLASS__.'@addLocale');
$events->listen(BuildClientView::class, __CLASS__.'@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'
]);
}
}

View File

@@ -0,0 +1,21 @@
<?php 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, __CLASS__.'@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');
}
}
}

View File

@@ -0,0 +1,56 @@
<?php namespace Flarum\Likes\Listeners;
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 Illuminate\Contracts\Events\Dispatcher;
class NotifyPostLiked
{
protected $notifications;
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
public function subscribe(Dispatcher $events)
{
$events->listen(RegisterNotificationTypes::class, __CLASS__.'@registerNotificationType');
$events->listen(PostWasLiked::class, __CLASS__.'@whenPostWasLiked');
$events->listen(PostWasUnliked::class, __CLASS__.'@whenPostWasUnliked');
}
public function registerNotificationType(RegisterNotificationTypes $event)
{
$event->register(
'Flarum\Likes\Notifications\PostLikedBlueprint',
'Flarum\Api\Serializers\PostBasicSerializer',
['alert']
);
}
public function whenPostWasLiked(PostWasLiked $event)
{
$this->sync($event->post, $event->user, [$event->post->user]);
}
public function whenPostWasUnliked(PostWasUnliked $event)
{
$this->sync($event->post, $event->user, []);
}
public function sync(Post $post, User $user, array $recipients)
{
if ($post->user->id != $user->id) {
$this->notifications->sync(
new PostLikedBlueprint($post, $user),
$recipients
);
}
}
}

View File

@@ -0,0 +1,48 @@
<?php namespace Flarum\Likes\Listeners;
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 Illuminate\Contracts\Events\Dispatcher;
class PersistData
{
public function subscribe(Dispatcher $events)
{
$events->listen(PostWillBeSaved::class, __CLASS__.'@whenPostWillBeSaved');
$events->listen(PostWasDeleted::class, __CLASS__.'@whenPostWasDeleted');
}
public function whenPostWillBeSaved(PostWillBeSaved $event)
{
$post = $event->post;
$data = $event->data;
if ($post->exists && isset($data['attributes']['isLiked'])) {
$actor = $event->actor;
$liked = (bool) $data['attributes']['isLiked'];
if (! $post->can($actor, 'like')) {
throw new PermissionDeniedException;
}
if ($liked) {
$post->likes()->attach($actor->id);
$post->raise(new PostWasLiked($post, $actor));
} else {
$post->likes()->detach($actor->id);
$post->raise(new PostWasUnliked($post, $actor));
}
}
}
public function whenPostWasDeleted(PostWasDeleted $event)
{
$event->post->likes()->detach();
}
}

View File

@@ -1,10 +1,10 @@
<?php namespace Flarum\Likes;
<?php namespace Flarum\Likes\Notifications;
use Flarum\Core\Models\Post;
use Flarum\Core\Models\User;
use Flarum\Core\Notifications\NotificationAbstract;
use Flarum\Core\Posts\Post;
use Flarum\Core\Users\User;
use Flarum\Core\Notifications\Blueprint;
class PostLikedNotification extends NotificationAbstract
class PostLikedBlueprint implements Blueprint
{
public $post;
@@ -26,6 +26,11 @@ class PostLikedNotification extends NotificationAbstract
return $this->user;
}
public function getData()
{
return null;
}
public static function getType()
{
return 'postLiked';
@@ -33,6 +38,6 @@ class PostLikedNotification extends NotificationAbstract
public static function getSubjectModel()
{
return 'Flarum\Core\Models\Post';
return 'Flarum\Core\Posts\Post';
}
}