Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added listener to follow after a reply #6

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
return function (Dispatcher $events, Factory $views) {
$events->subscribe(Listener\AddClientAssets::class);
$events->subscribe(Listener\AddDiscussionSubscriptionAttribute::class);
$events->subscribe(Listener\ConfigureFollowAfterReplyUserPreferences::class);
$events->subscribe(Listener\FilterDiscussionListBySubscription::class);
$events->subscribe(Listener\SaveSubscriptionToDatabase::class);
$events->subscribe(Listener\SendNotificationWhenReplyIsPosted::class);
$events->subscribe(Listener\SubscribeWhenPostWasPosted::class);

$views->addNamespace('flarum-subscriptions', __DIR__.'/views');
};
$views->addNamespace('flarum-subscriptions', __DIR__ . '/views');
};
35 changes: 35 additions & 0 deletions src/Listener/ConfigureFollowAfterReplyUserPreferences.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Subscriptions\Listener;

use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Event\ConfigureUserPreferences;
use Flarum\Event\PrepareApiAttributes;
use Illuminate\Contracts\Events\Dispatcher;

class ConfigureFollowAfterReplyUserPreferences
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureUserPreferences::class, [$this, 'addUserPreference']);
}

/**
* @param ConfigureUserPreferences $event
*/
public function addUserPreference(ConfigureUserPreferences $event)
{
$event->add('followAfterReply', 'boolval', true);
}
}
50 changes: 50 additions & 0 deletions src/Listener/SubscribeWhenPostWasPosted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Subscriptions\Listener;

use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Event\DiscussionWillBeSaved;
use Flarum\Event\PostWasPosted;
use Illuminate\Contracts\Events\Dispatcher;

class SubscribeWhenPostWasPosted
{
use AssertPermissionTrait;

/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(PostWasPosted::class, [$this, 'whenPostWasPosted']);
}

/**
* @param PostWasPosted $event
*/
public function whenPostWasPosted(PostWasPosted $event)
{
$actor = $event->actor;

if ($actor && $actor->exists && $actor->getPreference('followAfterReply')) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 1 newline after opening brace; 2 found


$post = $event->post;
$discussion = $post->discussion;

$this->assertRegistered($actor);

$state = $discussion->stateFor($actor);

$state->subscription = 'follow';
$state->save();
}
}
}