mirror of
https://github.com/flarum/framework.git
synced 2024-11-30 05:13:37 +08:00
Add event post when a discussion's tags are changed
This commit is contained in:
parent
e3b26b48a9
commit
bec1f73c36
3
extensions/tags/js/bootstrap.js
vendored
3
extensions/tags/js/bootstrap.js
vendored
|
@ -5,6 +5,7 @@ import IndexPage from 'flarum/components/index-page';
|
|||
|
||||
import Tag from 'flarum-tags/models/tag';
|
||||
import TagsPage from 'flarum-tags/components/tags-page';
|
||||
import DiscussionTaggedPost from 'flarum-tags/components/discussion-tagged-post';
|
||||
import addTagList from 'flarum-tags/add-tag-list';
|
||||
import addTagFilter from 'flarum-tags/add-tag-filter';
|
||||
import addTagLabels from 'flarum-tags/add-tag-labels';
|
||||
|
@ -21,6 +22,8 @@ app.initializers.add('flarum-tags', function() {
|
|||
Discussion.prototype.tags = Model.many('tags');
|
||||
Discussion.prototype.canTag = Model.prop('canTag');
|
||||
|
||||
app.postComponentRegistry['discussionTagged'] = DiscussionTaggedPost;
|
||||
|
||||
// Add a list of tags to the index navigation.
|
||||
addTagList();
|
||||
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
import Notification from 'flarum/components/notification';
|
||||
import username from 'flarum/helpers/username';
|
||||
import categoryLabel from 'flarum-categories/helpers/category-label';
|
||||
|
||||
export default class DiscussionMovedNotification extends Notification {
|
||||
view() {
|
||||
var notification = this.props.notification;
|
||||
var discussion = notification.subject();
|
||||
|
||||
return super.view({
|
||||
href: app.route.discussion(discussion, notification.content().postNumber),
|
||||
icon: 'arrow-right',
|
||||
content: [username(notification.sender()), ' moved to ', categoryLabel(discussion.category())]
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,12 +1,25 @@
|
|||
import EventPost from 'flarum/components/event-post';
|
||||
import categoryLabel from 'flarum-categories/helpers/category-label';
|
||||
import tagsLabel from 'flarum-tags/helpers/tags-label';
|
||||
|
||||
export default class DiscussionMovedPost extends EventPost {
|
||||
export default class DiscussionTaggedPost extends EventPost {
|
||||
view() {
|
||||
var post = this.props.post;
|
||||
var oldCategory = app.store.getById('categories', post.content()[0]);
|
||||
var newCategory = app.store.getById('categories', post.content()[1]);
|
||||
var oldTags = post.content()[0];
|
||||
var newTags = post.content()[1];
|
||||
|
||||
return super.view('arrow-right', ['moved the discussion from ', categoryLabel(oldCategory), ' to ', categoryLabel(newCategory), '.']);
|
||||
var added = newTags.filter(tag => oldTags.indexOf(tag) === -1).map(id => app.store.getById('tags', id));
|
||||
var removed = oldTags.filter(tag => newTags.indexOf(tag) === -1).map(id => app.store.getById('tags', id));
|
||||
var total = added.concat(removed);
|
||||
|
||||
var build = function(verb, tags, only) {
|
||||
return tags.length ? [verb, ' ', only && tags.length == 1 ? 'the ' : '', tagsLabel(tags)] : '';
|
||||
};
|
||||
|
||||
return super.view('tag', [
|
||||
build('added', added, !removed.length),
|
||||
added.length && removed.length ? ' and ' : '',
|
||||
build('removed', removed, !added.length),
|
||||
total.length ? (total.length == 1 ? ' tag.' : ' tags.') : ''
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,15 +33,14 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
.discussion-moved-post & {
|
||||
margin: 0 2px;
|
||||
}
|
||||
}
|
||||
.tags-label {
|
||||
.discussion-summary & {
|
||||
margin-right: 10px;
|
||||
}
|
||||
.discussion-tagged-post & {
|
||||
margin: 0 2px;
|
||||
}
|
||||
|
||||
& .tag-label {
|
||||
border-radius: 0;
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
<?php namespace Flarum\Categories;
|
||||
|
||||
use Flarum\Core\Notifications\NotificationAbstract;
|
||||
|
||||
class DiscussionMovedNotification extends NotificationAbstract
|
||||
{
|
||||
protected $post;
|
||||
|
||||
public function __construct(DiscussionMovedPost $post)
|
||||
{
|
||||
$this->post = $post;
|
||||
}
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->post->discussion;
|
||||
}
|
||||
|
||||
public function getSender()
|
||||
{
|
||||
return $this->post->user;
|
||||
}
|
||||
|
||||
public function getData()
|
||||
{
|
||||
return ['postNumber' => (int) $this->post->number];
|
||||
}
|
||||
|
||||
public static function getType()
|
||||
{
|
||||
return 'discussionMoved';
|
||||
}
|
||||
|
||||
public static function getSubjectModel()
|
||||
{
|
||||
return 'Flarum\Core\Models\Discussion';
|
||||
}
|
||||
}
|
|
@ -1,16 +1,16 @@
|
|||
<?php namespace Flarum\Categories;
|
||||
<?php namespace Flarum\Tags;
|
||||
|
||||
use Flarum\Core\Models\Model;
|
||||
use Flarum\Core\Models\EventPost;
|
||||
|
||||
class DiscussionMovedPost extends EventPost
|
||||
class DiscussionTaggedPost extends EventPost
|
||||
{
|
||||
/**
|
||||
* The type of post this is, to be stored in the posts table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $type = 'discussionMoved';
|
||||
public static $type = 'discussionTagged';
|
||||
|
||||
/**
|
||||
* Merge the post into another post of the same type.
|
||||
|
@ -27,6 +27,8 @@ class DiscussionMovedPost extends EventPost
|
|||
}
|
||||
|
||||
$previous->content = static::buildContent($previous->content[0], $this->content[1]);
|
||||
$previous->time = $this->time;
|
||||
|
||||
return $previous;
|
||||
}
|
||||
|
||||
|
@ -38,15 +40,15 @@ class DiscussionMovedPost extends EventPost
|
|||
*
|
||||
* @param integer $discussionId
|
||||
* @param integer $userId
|
||||
* @param integer $oldCategoryId
|
||||
* @param integer $newCategoryId
|
||||
* @param array $oldTagIds
|
||||
* @param array $newTagIds
|
||||
* @return static
|
||||
*/
|
||||
public static function reply($discussionId, $userId, $oldCategoryId, $newCategoryId)
|
||||
public static function reply($discussionId, $userId, array $oldTagIds, array $newTagIds)
|
||||
{
|
||||
$post = new static;
|
||||
|
||||
$post->content = static::buildContent($oldCategoryId, $newCategoryId);
|
||||
$post->content = static::buildContent($oldTagIds, $newTagIds);
|
||||
$post->time = time();
|
||||
$post->discussion_id = $discussionId;
|
||||
$post->user_id = $userId;
|
||||
|
@ -57,12 +59,12 @@ class DiscussionMovedPost extends EventPost
|
|||
/**
|
||||
* Build the content attribute.
|
||||
*
|
||||
* @param boolean $oldCategoryId The old category ID.
|
||||
* @param boolean $newCategoryId The new category ID.
|
||||
* @param array $oldTagIds
|
||||
* @param array $newTagIds
|
||||
* @return array
|
||||
*/
|
||||
public static function buildContent($oldCategoryId, $newCategoryId)
|
||||
public static function buildContent(array $oldTagIds, array $newTagIds)
|
||||
{
|
||||
return [$oldCategoryId, $newCategoryId];
|
||||
return [$oldTagIds, $newTagIds];
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
<?php namespace Flarum\Categories\Handlers;
|
||||
|
||||
use Flarum\Categories\DiscussionMovedPost;
|
||||
use Flarum\Categories\DiscussionMovedNotification;
|
||||
use Flarum\Categories\Events\DiscussionWasMoved;
|
||||
use Flarum\Core\Notifications\NotificationSyncer;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DiscussionMovedNotifier
|
||||
{
|
||||
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\Categories\Events\DiscussionWasMoved', __CLASS__.'@whenDiscussionWasMoved');
|
||||
}
|
||||
|
||||
public function whenDiscussionWasMoved(DiscussionWasMoved $event)
|
||||
{
|
||||
$post = DiscussionMovedPost::reply(
|
||||
$event->discussion->id,
|
||||
$event->user->id,
|
||||
$event->oldCategoryId,
|
||||
$event->discussion->category_id
|
||||
);
|
||||
|
||||
$post = $event->discussion->addPost($post);
|
||||
|
||||
if ($event->discussion->start_user_id !== $event->user->id) {
|
||||
$notification = new DiscussionMovedNotification($post);
|
||||
|
||||
$this->notifications->sync($notification, $post->exists ? [$event->discussion->startUser] : []);
|
||||
}
|
||||
}
|
||||
}
|
31
extensions/tags/src/Handlers/DiscussionTaggedNotifier.php
Executable file
31
extensions/tags/src/Handlers/DiscussionTaggedNotifier.php
Executable file
|
@ -0,0 +1,31 @@
|
|||
<?php namespace Flarum\Tags\Handlers;
|
||||
|
||||
use Flarum\Tags\DiscussionTaggedPost;
|
||||
use Flarum\Tags\Events\DiscussionWasTagged;
|
||||
use Flarum\Core\Notifications\NotificationSyncer;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
class DiscussionTaggedNotifier
|
||||
{
|
||||
/**
|
||||
* Register the listeners for the subscriber.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen('Flarum\Tags\Events\DiscussionWasTagged', __CLASS__.'@whenDiscussionWasTagged');
|
||||
}
|
||||
|
||||
public function whenDiscussionWasTagged(DiscussionWasTagged $event)
|
||||
{
|
||||
$post = DiscussionTaggedPost::reply(
|
||||
$event->discussion->id,
|
||||
$event->user->id,
|
||||
array_pluck($event->oldTags, 'id'),
|
||||
$event->discussion->tags()->lists('id')
|
||||
);
|
||||
|
||||
$post = $event->discussion->addPost($post);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ use Flarum\Extend\SerializeRelationship;
|
|||
use Flarum\Extend\ApiInclude;
|
||||
use Flarum\Extend\Permission;
|
||||
use Flarum\Extend\DiscussionGambit;
|
||||
use Flarum\Extend\PostType;
|
||||
|
||||
class TagsServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
@ -18,14 +19,14 @@ class TagsServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->extend(
|
||||
$this->extend([
|
||||
new ForumAssets([
|
||||
__DIR__.'/../js/dist/extension.js',
|
||||
__DIR__.'/../less/extension.less'
|
||||
]),
|
||||
|
||||
new EventSubscribers([
|
||||
// 'Flarum\Tags\Handlers\DiscussionTaggedNotifier',
|
||||
'Flarum\Tags\Handlers\DiscussionTaggedNotifier',
|
||||
'Flarum\Tags\Handlers\TagPreloader',
|
||||
'Flarum\Tags\Handlers\TagSaver'
|
||||
]),
|
||||
|
@ -45,8 +46,10 @@ class TagsServiceProvider extends ServiceProvider
|
|||
// @todo add limitations to time etc. according to a config setting
|
||||
}),
|
||||
|
||||
new DiscussionGambit('Flarum\Tags\TagGambit')
|
||||
);
|
||||
new DiscussionGambit('Flarum\Tags\TagGambit'),
|
||||
|
||||
new PostType('Flarum\Tags\DiscussionTaggedPost')
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user