Initial commit

This commit is contained in:
Toby Zerner 2015-06-23 10:25:24 +09:30
commit e57eb5a14f
20 changed files with 494 additions and 0 deletions

4
extensions/likes/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/vendor
composer.phar
.DS_Store
Thumbs.db

View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2014-2015 Toby Zerner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,9 @@
<?php
// Require the extension's composer autoload file. This will enable all of our
// classes in the src directory to be autoloaded.
require __DIR__.'/vendor/autoload.php';
// Register our service provider with the Flarum application. In here we can
// register bindings and execute code when the application boots.
return $this->app->register('Flarum\Likes\LikesServiceProvider');

View File

@ -0,0 +1,7 @@
{
"autoload": {
"psr-4": {
"Flarum\\Likes\\": "src/"
}
}
}

View File

@ -0,0 +1,16 @@
{
"name": "flarum-likes",
"title": "Likes",
"description": "Allows users to like posts.",
"tags": [],
"version": "0.1.0",
"author": {
"name": "Toby Zerner",
"email": "toby@flarum.org'"
},
"license": "MIT",
"require": {
"php": ">=5.4.0",
"flarum": ">0.1.0"
}
}

3
extensions/likes/js/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
bower_components
node_modules
dist

View File

@ -0,0 +1,5 @@
var gulp = require('flarum-gulp');
gulp({
modulePrefix: 'flarum-likes'
});

96
extensions/likes/js/bootstrap.js vendored Normal file
View File

@ -0,0 +1,96 @@
import { extend, override } from 'flarum/extension-utils';
import app from 'flarum/app';
import Post from 'flarum/models/post';
import Model from 'flarum/model';
import DiscussionPage from 'flarum/components/discussion-page';
import ActionButton from 'flarum/components/action-button';
import CommentPost from 'flarum/components/comment-post';
import punctuate from 'flarum/helpers/punctuate';
import username from 'flarum/helpers/username';
import PostLikedNotification from 'flarum-likes/components/post-liked-notification';
import PostLikesModal from 'flarum-likes/components/post-likes-modal';
app.initializers.add('flarum-likes', function() {
app.notificationComponentRegistry['postLiked'] = PostLikedNotification;
Post.prototype.canLike = Model.prop('canLike');
Post.prototype.likes = Model.many('likes');
extend(DiscussionPage.prototype, 'params', function(params) {
params.include.push('posts.likes');
});
extend(CommentPost.prototype, 'footerItems', function(items) {
var post = this.props.post;
var likes = post.likes();
if (likes && likes.length) {
var limit = 3;
var names = likes.slice(0, limit).map(user => {
return m('a', {
href: app.route.user(user),
config: m.route
}, [
app.session.user() && user === app.session.user() ? 'You' : username(user)
])
});
if (likes.length > limit + 1) {
names.push(
m('a', {
href: '#',
onclick: function(e) {
e.preventDefault();
app.modal.show(new PostLikesModal({ post }));
}
}, (likes.length - limit)+' others')
);
}
items.add('liked',
m('div.liked-by', [
punctuate(names),
' like this.'
])
);
}
});
extend(CommentPost.prototype, 'actionItems', function(items) {
var post = this.props.post;
if (post.isHidden() || !post.canLike()) return;
var isLiked = app.session.user() && post.likes().some(user => user === app.session.user());
items.add('like',
ActionButton.component({
icon: 'thumbs-o-up',
label: isLiked ? 'Unlike' : 'Like',
onclick: () => {
isLiked = !isLiked;
post.save({ isLiked });
var linkage = post.data().links.likes.linkage;
linkage.some((like, i) => {
if (like.id == app.session.user().id()) {
linkage.splice(i, 1);
return true;
}
});
if (isLiked) {
linkage.unshift({ type: 'users', id: app.session.user().id() });
}
m.redraw();
}
})
);
});
});

View File

@ -0,0 +1,7 @@
{
"private": true,
"devDependencies": {
"gulp": "^3.8.11",
"flarum-gulp": "git+https://github.com/flarum/gulp.git"
}
}

View File

@ -0,0 +1,16 @@
import Notification from 'flarum/components/notification';
import username from 'flarum/helpers/username';
export default class PostLikedNotification extends Notification {
view() {
var notification = this.props.notification;
var post = notification.subject();
var auc = notification.additionalUnreadCount();
return super.view({
href: app.route.post(post),
icon: 'thumbs-o-up',
content: [username(notification.sender()), auc ? ' and '+auc+' others' : '', ' liked your post #', post.number()]
});
}
}

View File

@ -0,0 +1,24 @@
import FormModal from 'flarum/components/form-modal';
import avatar from 'flarum/helpers/avatar';
import username from 'flarum/helpers/username';
export default class PostLikesModal extends FormModal {
view() {
var post = this.props.post;
return super.view({
className: 'post-likes-modal',
title: 'Users Who Like This',
body: [
m('ul.post-likes-list', [
post.likes().map(user =>
m('li', m('a', {href: app.route.user(user), config: m.route}, [
avatar(user),
username(user)
]))
)
])
]
});
}
}

View File

View File

@ -0,0 +1,2 @@
flarum-likes:
# hello_world: Hello, world!

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLikesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
app('db')->getSchemaBuilder()->create('posts_likes', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->primary(['post_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
app('db')->getSchemaBuilder()->drop('posts_likes');
}
}

View 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;
}
}

View 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;
}
}

View 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();
}
}

View 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
);
}
}

View 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()
{
//
}
}

View 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';
}
}