Merge branch 'evented-api'

This commit is contained in:
Toby Zerner 2015-07-21 10:41:41 +09:30
commit 7fdb79fc89
31 changed files with 599 additions and 309 deletions

View File

@ -0,0 +1,32 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
[*.js]
indent_style = space
indent_size = 2
[*.{css,less}]
indent_style = space
indent_size = 2
[*.html]
indent_style = space
indent_size = 2
[*.{diff,md}]
trim_trailing_whitespace = false
[*.php]
indent_style = space
indent_size = 4

View File

@ -0,0 +1,5 @@
**/bower_components/**/*
**/node_modules/**/*
vendor/**/*
**/Gulpfile.js
**/dist/**/*

171
extensions/sticky/.eslintrc Normal file
View File

@ -0,0 +1,171 @@
{
"parser": "babel-eslint", // https://github.com/babel/babel-eslint
"env": { // http://eslint.org/docs/user-guide/configuring.html#specifying-environments
"browser": true // browser global variables
},
"ecmaFeatures": {
"arrowFunctions": true,
"blockBindings": true,
"classes": true,
"defaultParams": true,
"destructuring": true,
"forOf": true,
"generators": false,
"modules": true,
"objectLiteralComputedProperties": true,
"objectLiteralDuplicateProperties": false,
"objectLiteralShorthandMethods": true,
"objectLiteralShorthandProperties": true,
"spread": true,
"superInFunctions": true,
"templateStrings": true,
"jsx": true
},
"globals": {
"m": true,
"app": true,
"$": true,
"moment": true
},
"rules": {
/**
* Strict mode
*/
// babel inserts "use strict"; for us
"strict": [2, "never"], // http://eslint.org/docs/rules/strict
/**
* ES6
*/
"no-var": 2, // http://eslint.org/docs/rules/no-var
"prefer-const": 2, // http://eslint.org/docs/rules/prefer-const
/**
* Variables
*/
"no-shadow": 2, // http://eslint.org/docs/rules/no-shadow
"no-shadow-restricted-names": 2, // http://eslint.org/docs/rules/no-shadow-restricted-names
"no-unused-vars": [2, { // http://eslint.org/docs/rules/no-unused-vars
"vars": "local",
"args": "after-used"
}],
"no-use-before-define": 2, // http://eslint.org/docs/rules/no-use-before-define
/**
* Possible errors
*/
"comma-dangle": [2, "never"], // http://eslint.org/docs/rules/comma-dangle
"no-cond-assign": [2, "always"], // http://eslint.org/docs/rules/no-cond-assign
"no-console": 1, // http://eslint.org/docs/rules/no-console
"no-debugger": 1, // http://eslint.org/docs/rules/no-debugger
"no-alert": 1, // http://eslint.org/docs/rules/no-alert
"no-constant-condition": 1, // http://eslint.org/docs/rules/no-constant-condition
"no-dupe-keys": 2, // http://eslint.org/docs/rules/no-dupe-keys
"no-duplicate-case": 2, // http://eslint.org/docs/rules/no-duplicate-case
"no-empty": 2, // http://eslint.org/docs/rules/no-empty
"no-ex-assign": 2, // http://eslint.org/docs/rules/no-ex-assign
"no-extra-boolean-cast": 0, // http://eslint.org/docs/rules/no-extra-boolean-cast
"no-extra-semi": 2, // http://eslint.org/docs/rules/no-extra-semi
"no-func-assign": 2, // http://eslint.org/docs/rules/no-func-assign
"no-inner-declarations": 2, // http://eslint.org/docs/rules/no-inner-declarations
"no-invalid-regexp": 2, // http://eslint.org/docs/rules/no-invalid-regexp
"no-irregular-whitespace": 2, // http://eslint.org/docs/rules/no-irregular-whitespace
"no-obj-calls": 2, // http://eslint.org/docs/rules/no-obj-calls
"no-reserved-keys": 2, // http://eslint.org/docs/rules/no-reserved-keys
"no-sparse-arrays": 2, // http://eslint.org/docs/rules/no-sparse-arrays
"no-unreachable": 2, // http://eslint.org/docs/rules/no-unreachable
"use-isnan": 2, // http://eslint.org/docs/rules/use-isnan
"block-scoped-var": 2, // http://eslint.org/docs/rules/block-scoped-var
/**
* Best practices
*/
"consistent-return": 2, // http://eslint.org/docs/rules/consistent-return
"curly": [2, "multi-line"], // http://eslint.org/docs/rules/curly
"default-case": 2, // http://eslint.org/docs/rules/default-case
"dot-notation": [2, { // http://eslint.org/docs/rules/dot-notation
"allowKeywords": true
}],
"eqeqeq": 2, // http://eslint.org/docs/rules/eqeqeq
"no-caller": 2, // http://eslint.org/docs/rules/no-caller
"no-else-return": 2, // http://eslint.org/docs/rules/no-else-return
"no-eq-null": 2, // http://eslint.org/docs/rules/no-eq-null
"no-eval": 2, // http://eslint.org/docs/rules/no-eval
"no-extend-native": 2, // http://eslint.org/docs/rules/no-extend-native
"no-extra-bind": 2, // http://eslint.org/docs/rules/no-extra-bind
"no-fallthrough": 2, // http://eslint.org/docs/rules/no-fallthrough
"no-floating-decimal": 2, // http://eslint.org/docs/rules/no-floating-decimal
"no-implied-eval": 2, // http://eslint.org/docs/rules/no-implied-eval
"no-lone-blocks": 2, // http://eslint.org/docs/rules/no-lone-blocks
"no-loop-func": 2, // http://eslint.org/docs/rules/no-loop-func
"no-multi-str": 2, // http://eslint.org/docs/rules/no-multi-str
"no-native-reassign": 2, // http://eslint.org/docs/rules/no-native-reassign
"no-new": 2, // http://eslint.org/docs/rules/no-new
"no-new-func": 2, // http://eslint.org/docs/rules/no-new-func
"no-new-wrappers": 2, // http://eslint.org/docs/rules/no-new-wrappers
"no-octal": 2, // http://eslint.org/docs/rules/no-octal
"no-octal-escape": 2, // http://eslint.org/docs/rules/no-octal-escape
"no-param-reassign": 2, // http://eslint.org/docs/rules/no-param-reassign
"no-proto": 2, // http://eslint.org/docs/rules/no-proto
"no-redeclare": 2, // http://eslint.org/docs/rules/no-redeclare
"no-return-assign": 2, // http://eslint.org/docs/rules/no-return-assign
"no-self-compare": 2, // http://eslint.org/docs/rules/no-self-compare
"no-sequences": 2, // http://eslint.org/docs/rules/no-sequences
"no-throw-literal": 2, // http://eslint.org/docs/rules/no-throw-literal
"no-with": 2, // http://eslint.org/docs/rules/no-with
"radix": 2, // http://eslint.org/docs/rules/radix
"vars-on-top": 2, // http://eslint.org/docs/rules/vars-on-top
"wrap-iife": [2, "any"], // http://eslint.org/docs/rules/wrap-iife
"yoda": 2, // http://eslint.org/docs/rules/yoda
/**
* Style
*/
"indent": [2, 2], // http://eslint.org/docs/rules/indent
"brace-style": [2, // http://eslint.org/docs/rules/brace-style
"1tbs", {
"allowSingleLine": true
}],
"quotes": [
2, "single", "avoid-escape" // http://eslint.org/docs/rules/quotes
],
"camelcase": [2, { // http://eslint.org/docs/rules/camelcase
"properties": "never"
}],
"comma-spacing": [2, { // http://eslint.org/docs/rules/comma-spacing
"before": false,
"after": true
}],
"comma-style": [2, "last"], // http://eslint.org/docs/rules/comma-style
"eol-last": 2, // http://eslint.org/docs/rules/eol-last
"func-names": 1, // http://eslint.org/docs/rules/func-names
"key-spacing": [2, { // http://eslint.org/docs/rules/key-spacing
"beforeColon": false,
"afterColon": true
}],
"new-cap": [2, { // http://eslint.org/docs/rules/new-cap
"newIsCap": true
}],
"no-multiple-empty-lines": [2, { // http://eslint.org/docs/rules/no-multiple-empty-lines
"max": 2
}],
"no-new-object": 2, // http://eslint.org/docs/rules/no-new-object
"no-spaced-func": 2, // http://eslint.org/docs/rules/no-spaced-func
"no-trailing-spaces": 2, // http://eslint.org/docs/rules/no-trailing-spaces
"no-wrap-func": 2, // http://eslint.org/docs/rules/no-wrap-func
"no-underscore-dangle": 0, // http://eslint.org/docs/rules/no-underscore-dangle
"one-var": [2, "never"], // http://eslint.org/docs/rules/one-var
"padded-blocks": [2, "never"], // http://eslint.org/docs/rules/padded-blocks
"semi": [2, "always"], // http://eslint.org/docs/rules/semi
"semi-spacing": [2, { // http://eslint.org/docs/rules/semi-spacing
"before": false,
"after": true
}],
"space-after-keywords": 2, // http://eslint.org/docs/rules/space-after-keywords
"space-before-blocks": 2, // http://eslint.org/docs/rules/space-before-blocks
"space-before-function-paren": [2, "never"], // http://eslint.org/docs/rules/space-before-function-paren
"space-infix-ops": 2, // http://eslint.org/docs/rules/space-infix-ops
"space-return-throw-case": 2, // http://eslint.org/docs/rules/space-return-throw-case
"spaced-line-comment": 2, // http://eslint.org/docs/rules/spaced-line-comment
}
}

View File

@ -1,9 +1,5 @@
<?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\Sticky\StickyServiceProvider');
return 'Flarum\Sticky\Extension';

View File

@ -1,10 +1,8 @@
{
"name": "flarum-sticky",
"name": "sticky",
"title": "Sticky",
"description": "Pin discussions to the top of the list.",
"tags": [
"discussions"
],
"keywords": ["discussions"],
"version": "0.1.0",
"author": {
"name": "Toby Zerner",
@ -16,8 +14,8 @@
"php": ">=5.4.0",
"flarum": ">0.1.0"
},
"links": {
"github": "https://github.com/flarum/sticky",
"support": {
"source": "https://github.com/flarum/sticky",
"issues": "https://github.com/flarum/sticky/issues"
}
}

View File

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

View File

@ -1,81 +0,0 @@
import { extend } from 'flarum/extension-utils';
import Model from 'flarum/model';
import Discussion from 'flarum/models/discussion';
import DiscussionPage from 'flarum/components/discussion-page';
import DiscussionList from 'flarum/components/discussion-list';
import DiscussionListItem from 'flarum/components/discussion-list-item';
import Badge from 'flarum/components/badge';
import ActionButton from 'flarum/components/action-button';
import SettingsPage from 'flarum/components/settings-page';
import icon from 'flarum/helpers/icon';
import truncate from 'flarum/utils/truncate';
import app from 'flarum/app';
import DiscussionStickiedPost from 'flarum-sticky/components/discussion-stickied-post';
import DiscussionStickiedNotification from 'flarum-sticky/components/discussion-stickied-notification';
app.initializers.add('sticky', function() {
// Register components.
app.postComponentRegistry['discussionStickied'] = DiscussionStickiedPost;
app.notificationComponentRegistry['discussionStickied'] = DiscussionStickiedNotification;
Discussion.prototype.isSticky = Model.prop('isSticky');
Discussion.prototype.canSticky = Model.prop('canSticky');
// Add a sticky badge to discussions.
extend(Discussion.prototype, 'badges', function(badges) {
if (this.isSticky()) {
badges.add('sticky', Badge.component({
label: 'Sticky',
icon: 'thumb-tack',
className: 'badge-sticky',
}), {last: true});
}
});
function toggleSticky() {
this.save({isSticky: !this.isSticky()}).then(discussion => {
if (app.current instanceof DiscussionPage) {
app.current.stream.sync();
}
m.redraw();
});
}
// Add a sticky control to discussions.
extend(Discussion.prototype, 'moderationControls', function(items) {
if (this.canSticky()) {
items.add('sticky', ActionButton.component({
label: this.isSticky() ? 'Unsticky' : 'Sticky',
icon: 'thumb-tack',
onclick: toggleSticky.bind(this)
}));
}
});
// Add a notification preference.
extend(SettingsPage.prototype, 'notificationTypes', function(items) {
items.add('discussionStickied', {
name: 'discussionStickied',
label: [icon('thumb-tack'), ' Someone stickies a discussion I started']
}, {after: 'discussionRenamed'});
});
extend(DiscussionList.prototype, 'params', function(params) {
params.include.push('startPost');
});
extend(DiscussionListItem.prototype, 'infoItems', function(items) {
var discussion = this.props.discussion;
if (discussion.isSticky()) {
var startPost = discussion.startPost();
if (startPost) {
var excerpt = m('span', truncate(startPost.contentPlain(), 200));
excerpt.wrapperClass = 'discussion-excerpt';
items.add('excerpt', excerpt, {last: true});
}
}
});
});

View File

@ -0,0 +1,15 @@
import { extend } from 'flarum/extend';
import Discussion from 'flarum/models/Discussion';
import Badge from 'flarum/components/Badge';
export default function addStickyBadge() {
extend(Discussion.prototype, 'badges', function(badges) {
if (this.isSticky()) {
badges.add('sticky', Badge.component({
type: 'sticky',
label: app.trans('sticky.stickied'),
icon: 'thumb-tack'
}), 10);
}
});
}

View File

@ -0,0 +1,26 @@
import { extend } from 'flarum/extend';
import DiscussionControls from 'flarum/utils/DiscussionControls';
import DiscussionPage from 'flarum/components/DiscussionPage';
import Button from 'flarum/components/Button';
export default function addStickyControl() {
extend(DiscussionControls, 'moderationControls', function(items, discussion) {
if (discussion.canSticky()) {
items.add('sticky', Button.component({
children: app.trans(discussion.isSticky() ? 'sticky.unsticky' : 'sticky.sticky'),
icon: 'thumb-tack',
onclick: this.stickyAction.bind(discussion)
}));
}
});
DiscussionControls.stickyAction = function() {
this.save({isSticky: !this.isSticky()}).then(() => {
if (app.current instanceof DiscussionPage) {
app.current.stream.update();
}
m.redraw();
});
};
}

View File

@ -0,0 +1,24 @@
import { extend } from 'flarum/extend';
import DiscussionList from 'flarum/components/DiscussionList';
import DiscussionListItem from 'flarum/components/DiscussionListItem';
import { truncate } from 'flarum/utils/string';
export default function addStickyControl() {
extend(DiscussionList.prototype, 'requestParams', function(params) {
params.include.push('startPost');
});
extend(DiscussionListItem.prototype, 'infoItems', function(items) {
const discussion = this.props.discussion;
if (discussion.isSticky()) {
const startPost = discussion.startPost();
if (startPost) {
const excerpt = <span>{truncate(startPost.contentPlain(), 200)}</span>;
items.add('excerpt', excerpt, 100);
}
}
});
}

View File

@ -0,0 +1,15 @@
import Notification from 'flarum/components/Notification';
export default class DiscussionStickiedNotification extends Notification {
icon() {
return 'thumb-tack';
}
href() {
return app.route.discussion(notification.subject(), notification.content().postNumber);
}
content() {
return app.trans('sticky.discussion_stickied_notification', {user: this.props.notification.sender()});
}
}

View File

@ -0,0 +1,13 @@
import EventPost from 'flarum/components/EventPost';
export default class DiscussionStickiedPost extends EventPost {
icon() {
return 'thumb-tack';
}
descriptionKey() {
return this.props.post.content().sticky
? 'sticky.discussion_stickied_post'
: 'sticky.discussion_unstickied_post';
}
}

View File

@ -1,14 +0,0 @@
import Notification from 'flarum/components/notification';
import username from 'flarum/helpers/username';
export default class DiscussionStickiedNotification extends Notification {
view() {
var notification = this.props.notification;
return super.view({
href: app.route.discussion(notification.subject(), notification.content().postNumber),
icon: 'thumb-tack',
content: [username(notification.sender()), ' stickied']
});
}
}

View File

@ -1,9 +0,0 @@
import EventPost from 'flarum/components/event-post';
export default class DiscussionStickiedPost extends EventPost {
view() {
var post = this.props.post;
return super.view('thumb-tack', [post.content().sticky ? 'stickied' : 'unstickied', ' the discussion.']);
}
}

View File

@ -0,0 +1,29 @@
import { extend, notificationType } from 'flarum/extend';
import app from 'flarum/app';
import Model from 'flarum/Model';
import Discussion from 'flarum/models/Discussion';
import NotificationGrid from 'flarum/components/NotificationGrid';
import DiscussionStickiedPost from 'sticky/components/DiscussionStickiedPost';
import DiscussionStickiedNotification from 'sticky/components/DiscussionStickiedNotification';
import addStickyBadge from 'sticky/addStickyBadge';
import addStickyControl from 'sticky/addStickyControl';
import addStickyExcerpt from 'sticky/addStickyExcerpt';
app.postComponents.discussionStickied = DiscussionStickiedPost;
app.notificationComponents.discussionStickied = DiscussionStickiedNotification;
Discussion.prototype.isSticky = Model.attribute('isSticky');
Discussion.prototype.canSticky = Model.attribute('canSticky');
addStickyBadge();
addStickyControl();
addStickyExcerpt();
extend(NotificationGrid.prototype, 'notificationTypes', function(items) {
items.add('discussionStickied', {
name: 'discussionStickied',
icon: 'thumb-tack',
label: app.trans('sticky.notify_discussion_stickied')
});
});

View File

@ -0,0 +1,26 @@
.Badge--sticky {
background: #d13e32;
}
.DiscussionStickiedPost {
& .EventPost-icon,
& .EventPost-info,
& .EventPost-info a {
color: #d13e32;
}
}
.DiscussionListItem-info .item-excerpt {
margin-top: 8px;
margin-right: 20px;
white-space: normal;
font-size: 12px;
line-height: 1.5em;
color: @muted-more-color;
display: block;
.DiscussionPage-list & {
display: none;
}
@media @phone {
display: none;
}
}

View File

@ -1,27 +0,0 @@
.badge-sticky {
background: #d13e32;
}
.discussion-stickied-post {
& .post-icon, & .event-post-info, & .event-post-info a {
color: #d13e32;
}
}
.discussion-excerpt {
margin-top: 8px;
margin-right: 20px;
white-space: normal;
font-size: 12px;
line-height: 1.5em;
color: @fl-body-muted-more-color;
.discussion-summary .info > li& {
display: block;
.paned & {
display: none;
}
@media @phone {
display: none;
}
}
}

View File

@ -0,0 +1,8 @@
sticky:
discussion_stickied_notification: "{username} stickied"
discussion_stickied_post: "{username} stickied the discussion."
discussion_unstickied_post: "{username} unstickied the discussion."
notify_discussion_stickied: Someone stickies a discussion I started
stickied: Sticky
sticky: Sticky
unsticky: Unsticky

View File

@ -1,23 +1,23 @@
<?php namespace Flarum\Sticky\Events;
use Flarum\Core\Models\Discussion;
use Flarum\Core\Models\User;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
class DiscussionWasStickied
{
/**
* @var \Flarum\Core\Models\Discussion
* @var Discussion
*/
public $discussion;
/**
* @var \Flarum\Core\Models\User
* @var User
*/
public $user;
/**
* @param \Flarum\Core\Models\Discussion $discussion
* @param \Flarum\Core\Models\User $user
* @param Discussion $discussion
* @param User $user
*/
public function __construct(Discussion $discussion, User $user)
{

View File

@ -1,23 +1,23 @@
<?php namespace Flarum\Sticky\Events;
use Flarum\Core\Models\Discussion;
use Flarum\Core\Models\User;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
class DiscussionWasUnstickied
{
/**
* @var \Flarum\Core\Models\Discussion
* @var Discussion
*/
public $discussion;
/**
* @var \Flarum\Core\Models\User
* @var User
*/
public $user;
/**
* @param \Flarum\Core\Models\Discussion $discussion
* @param \Flarum\Core\Models\User $user
* @param Discussion $discussion
* @param User $user
*/
public function __construct(Discussion $discussion, User $user)
{

View File

@ -0,0 +1,16 @@
<?php namespace Flarum\Sticky;
use Flarum\Support\Extension as BaseExtension;
use Illuminate\Contracts\Events\Dispatcher;
class Extension extends BaseExtension
{
public function boot(Dispatcher $events)
{
$events->subscribe('Flarum\Sticky\Listeners\AddClientAssets');
$events->subscribe('Flarum\Sticky\Listeners\AddApiAttributes');
$events->subscribe('Flarum\Sticky\Listeners\PersistData');
$events->subscribe('Flarum\Sticky\Listeners\PinStickiedDiscussionsToTop');
$events->subscribe('Flarum\Sticky\Listeners\NotifyDiscussionStickied');
}
}

View File

@ -1,9 +1,9 @@
<?php namespace Flarum\Sticky;
<?php namespace Flarum\Sticky\Gambits;
use Flarum\Core\Search\SearcherInterface;
use Flarum\Core\Search\GambitAbstract;
use Flarum\Core\Search\Search;
use Flarum\Core\Search\RegexGambit;
class StickyGambit extends GambitAbstract
class StickyGambit extends RegexGambit
{
/**
* The gambit's regex pattern.
@ -20,8 +20,8 @@ class StickyGambit extends GambitAbstract
* @param \Flarum\Core\Search\SearcherInterface $searcher
* @return void
*/
protected function conditions(SearcherInterface $searcher, array $matches, $negate)
protected function conditions(Search $search, array $matches, $negate)
{
$searcher->getQuery()->where('is_sticky', ! $negate);
$search->getQuery()->where('is_sticky', ! $negate);
}
}

View File

@ -1,58 +0,0 @@
<?php namespace Flarum\Sticky\Handlers;
use Flarum\Sticky\DiscussionStickiedPost;
use Flarum\Sticky\DiscussionStickiedNotification;
use Flarum\Sticky\Events\DiscussionWasStickied;
use Flarum\Sticky\Events\DiscussionWasUnstickied;
use Flarum\Core\Notifications\NotificationSyncer;
use Flarum\Core\Models\Discussion;
use Flarum\Core\Models\User;
use Illuminate\Contracts\Events\Dispatcher;
class DiscussionStickiedNotifier
{
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\Sticky\Events\DiscussionWasStickied', __CLASS__.'@whenDiscussionWasStickied');
$events->listen('Flarum\Sticky\Events\DiscussionWasUnstickied', __CLASS__.'@whenDiscussionWasUnstickied');
}
public function whenDiscussionWasStickied(DiscussionWasStickied $event)
{
$this->stickyChanged($event->discussion, $event->user, true);
}
public function whenDiscussionWasUnstickied(DiscussionWasUnstickied $event)
{
$this->stickyChanged($event->discussion, $event->user, false);
}
protected function stickyChanged(Discussion $discussion, User $user, $isSticky)
{
$post = DiscussionStickiedPost::reply(
$discussion->id,
$user->id,
$isSticky
);
$post = $discussion->addPost($post);
if ($discussion->start_user_id !== $user->id) {
$notification = new DiscussionStickiedNotification($post);
$this->notifications->sync($notification, $post->exists ? [$discussion->startUser] : []);
}
}
}

View File

@ -0,0 +1,30 @@
<?php namespace Flarum\Sticky\Listeners;
use Flarum\Events\ApiAttributes;
use Flarum\Events\BuildApiAction;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Api\Serializers\DiscussionSerializer;
use Flarum\Api\Actions\Discussions\IndexAction as DiscussionsIndexAction;
class AddApiAttributes
{
public function subscribe(Dispatcher $events)
{
$events->listen(ApiAttributes::class, __CLASS__.'@addAttributes');
$events->listen(BuildApiAction::class, __CLASS__.'@includeStartPost');
}
public function addAttributes(ApiAttributes $event)
{
if ($event->serializer instanceof DiscussionSerializer) {
$event->attributes['isSticky'] = (bool) $event->model->is_sticky;
$event->attributes['canSticky'] = (bool) $event->model->can($event->actor, 'sticky');
}
}
public function includeStartPost(BuildApiAction $event)
{
if ($event->action instanceof DiscussionsIndexAction) {
$event->addInclude('startPost');
}
}
}

View File

@ -0,0 +1,39 @@
<?php namespace Flarum\Sticky\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/dist/extension.js',
__DIR__.'/../../less/extension.less'
]);
$event->forumBootstrapper('sticky/main');
$event->forumTranslations([
'sticky.discussion_stickied_notification',
'sticky.discussion_stickied_post',
'sticky.discussion_unstickied_post',
'sticky.notify_discussion_stickied',
'sticky.stickied',
'sticky.sticky',
'sticky.unsticky'
]);
}
}

View File

@ -0,0 +1,71 @@
<?php namespace Flarum\Sticky\Listeners;
use Flarum\Events\RegisterPostTypes;
use Flarum\Events\RegisterNotificationTypes;
use Flarum\Sticky\Posts\DiscussionStickiedPost;
use Flarum\Sticky\Notifications\DiscussionStickiedBlueprint;
use Flarum\Sticky\Events\DiscussionWasStickied;
use Flarum\Sticky\Events\DiscussionWasUnstickied;
use Flarum\Core\Notifications\NotificationSyncer;
use Flarum\Core\Discussions\Discussion;
use Flarum\Core\Users\User;
use Illuminate\Contracts\Events\Dispatcher;
class NotifyDiscussionStickied
{
protected $notifications;
public function __construct(NotificationSyncer $notifications)
{
$this->notifications = $notifications;
}
public function subscribe(Dispatcher $events)
{
$events->listen(RegisterPostTypes::class, __CLASS__.'@registerPostType');
$events->listen(RegisterNotificationTypes::class, __CLASS__.'@registerNotificationType');
$events->listen(DiscussionWasStickied::class, __CLASS__.'@whenDiscussionWasStickied');
$events->listen(DiscussionWasUnstickied::class, __CLASS__.'@whenDiscussionWasUnstickied');
}
public function registerPostType(RegisterPostTypes $event)
{
$event->register('Flarum\Sticky\Posts\DiscussionStickiedPost');
}
public function registerNotificationType(RegisterNotificationTypes $event)
{
$event->register(
'Flarum\Sticky\Notifications\DiscussionStickiedBlueprint',
'Flarum\Api\Serializers\DiscussionBasicSerializer',
['alert']
);
}
public function whenDiscussionWasStickied(DiscussionWasStickied $event)
{
$this->stickyChanged($event->discussion, $event->user, true);
}
public function whenDiscussionWasUnstickied(DiscussionWasUnstickied $event)
{
$this->stickyChanged($event->discussion, $event->user, false);
}
protected function stickyChanged(Discussion $discussion, User $user, $isSticky)
{
$post = DiscussionStickiedPost::reply(
$discussion->id,
$user->id,
$isSticky
);
$post = $discussion->mergePost($post);
if ($discussion->start_user_id !== $user->id) {
$notification = new DiscussionStickiedBlueprint($post);
$this->notifications->sync($notification, $post->exists ? [$discussion->startUser] : []);
}
}
}

View File

@ -1,24 +1,24 @@
<?php namespace Flarum\Sticky\Handlers;
<?php namespace Flarum\Sticky\Listeners;
use Flarum\Sticky\Events\DiscussionWasStickied;
use Flarum\Sticky\Events\DiscussionWasUnstickied;
use Flarum\Core\Events\DiscussionWillBeSaved;
use Flarum\Events\DiscussionWillBeSaved;
class StickySaver
class PersistData
{
public function subscribe($events)
{
$events->listen('Flarum\Core\Events\DiscussionWillBeSaved', __CLASS__.'@whenDiscussionWillBeSaved');
$events->listen(DiscussionWillBeSaved::class, __CLASS__.'@whenDiscussionWillBeSaved');
}
public function whenDiscussionWillBeSaved(DiscussionWillBeSaved $event)
{
if (isset($event->command->data['isSticky'])) {
$isSticky = (bool) $event->command->data['isSticky'];
if (isset($event->data['attributes']['isSticky'])) {
$isSticky = (bool) $event->data['attributes']['isSticky'];
$discussion = $event->discussion;
$user = $event->command->user;
$actor = $event->actor;
$discussion->assertCan($user, 'sticky');
$discussion->assertCan($actor, 'sticky');
if ((bool) $discussion->is_sticky === $isSticky) {
return;
@ -28,8 +28,8 @@ class StickySaver
$discussion->raise(
$discussion->is_sticky
? new DiscussionWasStickied($discussion, $user)
: new DiscussionWasUnstickied($discussion, $user)
? new DiscussionWasStickied($discussion, $actor)
: new DiscussionWasUnstickied($discussion, $actor)
);
}
}

View File

@ -1,25 +1,33 @@
<?php namespace Flarum\Sticky\Handlers;
<?php namespace Flarum\Sticky\Listeners;
use Flarum\Core\Events\DiscussionSearchWillBePerformed;
use Flarum\Tags\TagGambit;
use Flarum\Events\RegisterDiscussionGambits;
use Flarum\Events\DiscussionSearchWillBePerformed;
use Flarum\Tags\Gambits\TagGambit;
use Illuminate\Contracts\Events\Dispatcher;
class StickySearchModifier
class PinStickiedDiscussionsToTop
{
public function subscribe($events)
public function subscribe(Dispatcher $events)
{
$events->listen('Flarum\Core\Events\DiscussionSearchWillBePerformed', __CLASS__.'@reorderSearch');
$events->listen(RegisterDiscussionGambits::class, __CLASS__.'@registerStickyGambit');
$events->listen(DiscussionSearchWillBePerformed::class, __CLASS__.'@reorderSearch');
}
public function registerStickyGambit(RegisterDiscussionGambits $event)
{
$event->gambits->add('Flarum\Sticky\Gambits\StickyGambit');
}
public function reorderSearch(DiscussionSearchWillBePerformed $event)
{
if ($event->criteria->sort === null) {
$query = $event->searcher->getQuery();
$query = $event->search->getQuery();
if (!is_array($query->orders)) {
if (! is_array($query->orders)) {
$query->orders = [];
}
foreach ($event->searcher->getActiveGambits() as $gambit) {
foreach ($event->search->getActiveGambits() as $gambit) {
if ($gambit instanceof TagGambit) {
array_unshift($query->orders, ['column' => 'is_sticky', 'direction' => 'desc']);
return;
@ -29,7 +37,7 @@ class StickySearchModifier
$query->leftJoin('users_discussions', function ($join) use ($event) {
$join->on('users_discussions.discussion_id', '=', 'discussions.id')
->where('discussions.is_sticky', '=', true)
->where('users_discussions.user_id', '=', $event->criteria->user->id);
->where('users_discussions.user_id', '=', $event->search->getActor()->id);
});
// might be quicker to do a subquery in the order clause than a join?
array_unshift(

View File

@ -1,8 +1,8 @@
<?php namespace Flarum\Sticky;
<?php namespace Flarum\Sticky\Notifications;
use Flarum\Core\Notifications\NotificationAbstract;
use Flarum\Core\Notifications\Blueprint;
class DiscussionStickiedNotification extends NotificationAbstract
class DiscussionStickiedBlueprint implements Blueprint
{
protected $post;
@ -11,16 +11,16 @@ class DiscussionStickiedNotification extends NotificationAbstract
$this->post = $post;
}
public function getSubject()
{
return $this->post->discussion;
}
public function getSender()
{
return $this->post->user;
}
public function getSubject()
{
return $this->post->discussion;
}
public function getData()
{
return ['postNumber' => (int) $this->post->number];
@ -33,6 +33,6 @@ class DiscussionStickiedNotification extends NotificationAbstract
public static function getSubjectModel()
{
return 'Flarum\Core\Models\Discussion';
return 'Flarum\Core\Discussions\Discussion';
}
}

View File

@ -1,35 +1,33 @@
<?php namespace Flarum\Sticky;
<?php namespace Flarum\Sticky\Posts;
use Flarum\Core\Models\Model;
use Flarum\Core\Models\EventPost;
use Flarum\Core\Posts\Post;
use Flarum\Core\Posts\EventPost;
use Flarum\Core\Posts\MergeablePost;
class DiscussionStickiedPost extends EventPost
class DiscussionStickiedPost extends EventPost implements MergeablePost
{
/**
* The type of post this is, to be stored in the posts table.
*
* @var string
*/
public static $type = 'discussionStickied';
/**
* Merge the post into another post of the same type.
*
* @param \Flarum\Core\Models\DiscussionRenamedPost $previous
* @return \Flarum\Core\Models\Model|null The final model, or null if the
* previous post was deleted.
*/
protected function mergeInto(Model $previous)
public function saveAfter(Post $previous)
{
if ($this->user_id === $previous->user_id) {
// If the previous post is another 'discussion stickied' post, and it's
// by the same user, then we can merge this post into it. If we find
// that we've in fact reverted the sticky status, delete it. Otherwise,
// update its content.
if ($previous instanceof static && $this->user_id === $previous->user_id) {
if ($previous->content['sticky'] != $this->content['sticky']) {
return;
$previous->delete();
} else {
$previous->content = $this->content;
$previous->save();
}
$previous->content = $this->content;
return $previous;
}
$this->save();
return $this;
}

View File

@ -1,41 +0,0 @@
<?php namespace Flarum\Sticky;
use Flarum\Support\ServiceProvider;
use Flarum\Extend;
class StickyServiceProvider extends ServiceProvider
{
public function boot()
{
$this->extend(
new Extend\EventSubscriber([
'Flarum\Sticky\Handlers\StickySaver',
'Flarum\Sticky\Handlers\StickySearchModifier',
'Flarum\Sticky\Handlers\DiscussionStickiedNotifier'
]),
(new Extend\ForumClient())
->assets([
__DIR__.'/../js/dist/extension.js',
__DIR__.'/../less/sticky.less'
]),
new Extend\PostType('Flarum\Sticky\DiscussionStickiedPost'),
(new Extend\ApiSerializer('Flarum\Api\Serializers\DiscussionSerializer'))
->attributes(function (&$attributes, $model, $user) {
$attributes['isSticky'] = (bool) $model->is_sticky;
$attributes['canSticky'] = (bool) $model->can($user, 'sticky');
}),
// include discussion start posts by default
(new Extend\ApiAction('Flarum\Api\Actions\Discussions\IndexAction'))
->addInclude('startPost'),
new Extend\DiscussionGambit('Flarum\Sticky\StickyGambit'),
(new Extend\NotificationType('Flarum\Sticky\DiscussionStickiedNotification', 'Flarum\Api\Serializers\DiscussionBasicSerializer'))
->enableByDefault('alert')
);
}
}