Cleanup, add discussion moved notification

This commit is contained in:
Toby Zerner 2015-05-05 11:45:31 +09:30
parent 5919d16edf
commit 360402c5b1
13 changed files with 342 additions and 85 deletions

View File

@ -1,5 +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 'vendor/autoload.php';
$this->app->register('Flarum\Categories\CategoriesServiceProvider');
// Register our service provider with the Flarum application. In here we can
// register bindings and execute code when the application boots.
$app->register('Flarum\Categories\CategoriesServiceProvider');

View File

@ -16,6 +16,7 @@ import icon from 'flarum/helpers/icon';
import CategoriesPage from 'categories/components/categories-page';
import Category from 'categories/category';
import PostDiscussionMoved from 'categories/components/post-discussion-moved';
import NotificationDiscussionMoved from 'categories/components/notification-discussion-moved';
import app from 'flarum/app';
@ -31,6 +32,7 @@ app.initializers.add('categories', function() {
// app.routes['category.filter'] = ['/c/:slug/:filter', IndexPage.component({category: true})];
app.postComponentRegistry['discussionMoved'] = PostDiscussionMoved;
app.notificationComponentRegistry['discussionMoved'] = NotificationDiscussionMoved;
app.store.model('categories', Category);
extend(DiscussionList.prototype, 'infoItems', function(items, discussion) {

View File

@ -0,0 +1,27 @@
import Notification from 'flarum/components/notification';
import avatar from 'flarum/helpers/avatar';
import icon from 'flarum/helpers/icon';
import username from 'flarum/helpers/username';
import humanTime from 'flarum/helpers/human-time';
export default class NotificationDiscussionMoved extends Notification {
content() {
var notification = this.props.notification;
var discussion = notification.subject();
var category = discussion.category();
return m('a', {href: app.route('discussion.near', {
id: discussion.id(),
slug: discussion.slug(),
near: notification.content().postNumber
}), config: m.route}, [
avatar(notification.sender()),
m('h3.notification-title', discussion.title()),
m('div.notification-info', [
icon('arrow-right'),
' Moved to ', m('span.category', {style: 'color: '+category.color()}, category.title()), ' by ', username(notification.sender()),
' ', humanTime(notification.time())
])
]);
}
}

View File

@ -1,35 +1,27 @@
<?php namespace Flarum\Categories\Handlers;
<?php namespace Flarum\Categories;
use Flarum\Api\Events\SerializeRelationship;
use Flarum\Api\Serializers\DiscussionSerializer;
use Flarum\Support\Actor;
use Flarum\Forum\Events\RenderView;
use Flarum\Categories\Events\DiscussionWasMoved;
use Flarum\Core\Events\ModelCall;
use Flarum\Core\Events\RegisterDiscussionGambits;
use Flarum\Core\Models\Discussion;
use Flarum\Categories\Category;
use Flarum\Categories\CategorySerializer;
use Flarum\Categories\DiscussionMovedPost;
use Flarum\Core\Events\DiscussionWillBeSaved;
use Flarum\Core\Models\Discussion;
use Flarum\Api\Events\SerializeRelationship;
use Flarum\Api\Serializers\DiscussionSerializer;
use Flarum\Forum\Events\RenderView;
class Handler
class CategoriesHandler
{
public function __construct(Actor $actor)
{
$this->actor = $actor;
}
public function subscribe($events)
{
$events->listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum');
$events->listen('Flarum\Api\Events\SerializeRelationship', __CLASS__.'@serializeRelationship');
$events->listen('Flarum\Core\Events\RegisterDiscussionGambits', __CLASS__.'@registerGambits');
$events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@saveCategoryToDiscussion');
$events->listen('Flarum\Core\Events\RegisterDiscussionGambits', __CLASS__.'@registerDiscussionGambits');
$events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved');
}
public function renderForum(RenderView $event)
{
$root = __DIR__.'/../..';
$root = __DIR__.'/..';
$event->assets->addFile([
$root.'/js/dist/extension.js',
@ -40,7 +32,7 @@ class Handler
$event->view->data = array_merge($event->view->data, $serializer->collection(Category::orderBy('position')->get())->toArray());
}
public function saveCategoryToDiscussion(DiscussionWillBeSaved $event)
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
{
if (isset($event->command->data['links']['category']['linkage'])) {
$linkage = $event->command->data['links']['category']['linkage'];
@ -56,37 +48,11 @@ class Handler
}
$discussion->category_id = $categoryId;
if ($discussion->exists) {
$lastPost = $discussion->posts()->orderBy('time', 'desc')->first();
if ($lastPost instanceof DiscussionMovedPost) {
if ($lastPost->content[0] == $categoryId) {
$lastPost->delete();
$discussion->postWasRemoved($lastPost);
} else {
$newContent = $lastPost->content;
$newContent[1] = $categoryId;
$lastPost->content = $newContent;
$lastPost->save();
$discussion->postWasAdded($lastPost);
}
} else {
$post = DiscussionMovedPost::reply(
$discussion->id,
$user->id,
$oldCategoryId,
$categoryId
);
$post->save();
$discussion->postWasAdded($post);
}
}
$discussion->raise(new DiscussionWasMoved($discussion, $user, $oldCategoryId));
}
}
public function registerGambits(RegisterDiscussionGambits $event)
public function registerDiscussionGambits(RegisterDiscussionGambits $event)
{
$event->gambits->add('Flarum\Categories\CategoryGambit');
}

View File

@ -2,10 +2,11 @@
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Api\Actions\Discussions\IndexAction;
use Flarum\Api\Actions\Discussions\ShowAction;
use Flarum\Core\Models\Post;
use Flarum\Core\Models\Discussion;
use Flarum\Core\Notifications\Notifier;
use Flarum\Api\Actions\Discussions\IndexAction as DiscussionsIndexAction;
use Flarum\Api\Actions\Discussions\ShowAction as DiscussionsShowAction;
class CategoriesServiceProvider extends ServiceProvider
{
@ -14,23 +15,31 @@ class CategoriesServiceProvider extends ServiceProvider
*
* @return void
*/
public function boot(Dispatcher $events)
public function boot(Dispatcher $events, Notifier $notifier)
{
$events->subscribe('Flarum\Categories\Handlers\Handler');
IndexAction::$include['category'] = true;
ShowAction::$include['category'] = true;
Post::addType('discussionMoved', 'Flarum\Categories\DiscussionMovedPost');
$events->subscribe('Flarum\Categories\CategoriesHandler');
$events->subscribe('Flarum\Categories\DiscussionMovedNotifier');
// Add the category relationship to the Discussion model, and include
// it in discussion-related API actions by default.
Discussion::addRelationship('category', function ($model) {
return $model->belongsTo('Flarum\Categories\Category', null, null, 'category');
});
DiscussionsIndexAction::$include['category'] = true;
DiscussionsShowAction::$include['category'] = true;
// Add a new post and notification type to represent a discussion
// being moved from one category to another.
Post::addType('Flarum\Categories\DiscussionMovedPost');
$notifier->registerType('Flarum\Categories\DiscussionMovedNotification', ['alert' => true]);
}
public function register()
{
//
$this->app->bind(
'Flarum\Categories\CategoryRepositoryInterface',
'Flarum\Categories\EloquentCategoryRepository'
);
}
}

View File

@ -8,17 +8,39 @@ class CategoryGambit extends GambitAbstract
{
/**
* The gambit's regex pattern.
*
* @var string
*/
protected $pattern = 'category:(.+)';
/**
* @var \Flarum\Categories\CategoryRepositoryInterface
*/
protected $categories;
/**
* Instantiate the gambit.
*
* @param \Flarum\Categories\CategoryRepositoryInterface $categories
*/
public function __construct(CategoryRepositoryInterface $categories)
{
$this->categories = $categories;
}
/**
* Apply conditions to the searcher, given matches from the gambit's
* regex.
*
* @param array $matches The matches from the gambit's regex.
* @param \Flarum\Core\Search\SearcherInterface $searcher
* @return void
*/
public function conditions($matches, SearcherInterface $searcher)
{
$slug = trim($matches[1], '"');
// @todo implement categories repository
// $id = $this->categories->getIdForSlug($slug);
$id = Category::whereSlug($slug)->pluck('id');
$id = $this->categories->getIdForSlug($slug);
$searcher->query()->where('category_id', $id);
}

View File

@ -0,0 +1,24 @@
<?php namespace Flarum\Categories;
use Flarum\Core\Models\User;
interface CategoryRepositoryInterface
{
/**
* Find all categories, optionally making sure they are visible to a
* certain user.
*
* @param \Flarum\Core\Models\User|null $user
* @return \Illuminate\Database\Eloquent\Collection
*/
public function find(User $user = null);
/**
* Get the ID of a category with the given slug.
*
* @param string $slug
* @param \Flarum\Core\Models\User|null $user
* @return integer
*/
public function getIdForSlug($slug, User $user = null);
}

View File

@ -11,6 +11,12 @@ class CategorySerializer extends BaseSerializer
*/
protected $type = 'categories';
/**
* Serialize category attributes to be exposed in the API.
*
* @param \Flarum\Categories\Category $category
* @return array
*/
protected function attributes($category)
{
$attributes = [

View File

@ -0,0 +1,39 @@
<?php namespace Flarum\Categories;
use Flarum\Core\Models\User;
use Flarum\Core\Notifications\Types\Notification;
use Flarum\Core\Notifications\Types\AlertableNotification;
class DiscussionMovedNotification extends Notification implements AlertableNotification
{
public $post;
public function __construct(User $recipient, User $sender, DiscussionMovedPost $post)
{
$this->post = $post;
parent::__construct($recipient, $sender);
}
public function getSubject()
{
return $this->post->discussion;
}
public function getAlertData()
{
return [
'postNumber' => $this->post->number
];
}
public static function getType()
{
return 'discussionMoved';
}
public static function getSubjectModel()
{
return 'Flarum\Core\Models\Discussion';
}
}

View File

@ -0,0 +1,58 @@
<?php namespace Flarum\Categories;
use Flarum\Categories\Events\DiscussionWasMoved;
use Flarum\Core\Notifications\Notifier;
use Illuminate\Contracts\Events\Dispatcher;
class DiscussionMovedNotifier
{
protected $notifier;
public function __construct(Notifier $notifier)
{
$this->notifier = $notifier;
}
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen('Flarum\Categories\Events\DiscussionWasMoved', __CLASS__.'@whenDiscussionWasMoved');
}
public function whenDiscussionWasMoved(DiscussionWasMoved $event)
{
$post = $this->createPost($event);
$post = $event->discussion->addPost($post);
if ($event->discussion->start_user_id !== $event->user->id) {
$this->sendNotification($event, $post);
}
}
protected function createPost(DiscussionWasMoved $event)
{
return DiscussionMovedPost::reply(
$event->discussion->id,
$event->user->id,
$event->oldCategoryId,
$event->discussion->category_id
);
}
protected function sendNotification(DiscussionWasMoved $event, DiscussionMovedPost $post)
{
$notification = new DiscussionMovedNotification(
$event->discussion->startUser,
$event->user,
$post,
$event->discussion->category_id
);
$this->notifier->send($notification);
}
}

View File

@ -1,49 +1,63 @@
<?php namespace Flarum\Categories;
use Flarum\Core\Models\Post;
use Flarum\Core\Models\Model;
use Flarum\Core\Models\ActivityPost;
class DiscussionMovedPost extends Post
class DiscussionMovedPost extends ActivityPost
{
/**
* The type of post this is, to be stored in the posts table.
*
* @var string
*/
public static $type = 'discussionMoved';
/**
* Merge the post into another post of the same type.
*
* @param \Flarum\Core\Models\Model $previous
* @return boolean true if the post was merged, false if it was deleted.
*/
protected function mergeInto(Model $previous)
{
if ($previous->content[0] == $this->content[1]) {
return false;
}
$previous->content = static::buildContent($previous->content[0], $this->content[1]);
return true;
}
/**
* Create a new instance in reply to a discussion.
*
* @param int $discussionId
* @param int $userId
* @param string $oldTitle
* @param string $newTitle
* @param integer $discussionId
* @param integer $userId
* @param integer $oldCategoryId
* @param integer $newCategoryId
* @return static
*/
public static function reply($discussionId, $userId, $oldCategoryId, $newCategoryId)
{
$post = new static;
$post->content = [$oldCategoryId, $newCategoryId];
$post->content = static::buildContent($oldCategoryId, $newCategoryId);
$post->time = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = 'discussionMoved';
return $post;
}
/**
* Unserialize the content attribute.
* Build the content attribute.
*
* @param string $value
* @return string
* @param boolean $oldCategoryId The old category ID.
* @param boolean $newCategoryId The new category ID.
* @return array
*/
public function getContentAttribute($value)
public static function buildContent($oldCategoryId, $newCategoryId)
{
return json_decode($value);
}
/**
* Serialize the content attribute.
*
* @param string $value
*/
public function setContentAttribute($value)
{
$this->attributes['content'] = json_encode($value);
return [$oldCategoryId, $newCategoryId];
}
}

View File

@ -0,0 +1,52 @@
<?php namespace Flarum\Categories;
use Illuminate\Database\Eloquent\Builder;
use Flarum\Core\Models\User;
use Flarum\Categories\Category;
class EloquentCategoryRepository implements CategoryRepositoryInterface
{
/**
* Find all categories, optionally making sure they are visible to a
* certain user.
*
* @param \Flarum\Core\Models\User|null $user
* @return \Illuminate\Database\Eloquent\Collection
*/
public function find(User $user = null)
{
$query = Category::newQuery();
return $this->scopeVisibleForUser($query, $user)->get();
}
/**
* Get the ID of a category with the given slug.
*
* @param string $slug
* @param \Flarum\Core\Models\User|null $user
* @return integer
*/
public function getIdForSlug($slug, User $user = null)
{
$query = Category::where('slug', 'like', $slug);
return $this->scopeVisibleForUser($query, $user)->pluck('id');
}
/**
* Scope a query to only include records that are visible to a user.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Flarum\Core\Models\User $user
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function scopeVisibleForUser(Builder $query, User $user = null)
{
if ($user !== null) {
$query->whereCan($user, 'view');
}
return $query;
}
}

View File

@ -0,0 +1,34 @@
<?php namespace Flarum\Categories\Events;
use Flarum\Core\Models\Discussion;
use Flarum\Core\Models\User;
class DiscussionWasMoved
{
/**
* @var \Flarum\Core\Models\Discussion
*/
public $discussion;
/**
* @var \Flarum\Core\Models\User
*/
public $user;
/**
* @var integer
*/
public $oldCategoryId;
/**
* @param \Flarum\Core\Models\Discussion $discussion
* @param \Flarum\Core\Models\User $user
* @param \Flarum\Categories\Category $oldCategory
*/
public function __construct(Discussion $discussion, User $user, $oldCategoryId)
{
$this->discussion = $discussion;
$this->user = $user;
$this->oldCategoryId = $oldCategoryId;
}
}