mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 22:53:37 +08:00
feat: refactor subscriptions extension
This commit is contained in:
parent
6e753b4def
commit
aebd5278cb
|
@ -7,10 +7,8 @@
|
||||||
* LICENSE file that was distributed with this source code.
|
* LICENSE file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Flarum\Api\Serializer\BasicDiscussionSerializer;
|
use Flarum\Api\Resource;
|
||||||
use Flarum\Api\Serializer\DiscussionSerializer;
|
|
||||||
use Flarum\Approval\Event\PostWasApproved;
|
use Flarum\Approval\Event\PostWasApproved;
|
||||||
use Flarum\Discussion\Discussion;
|
|
||||||
use Flarum\Discussion\Event\Saving;
|
use Flarum\Discussion\Event\Saving;
|
||||||
use Flarum\Discussion\Search\DiscussionSearcher;
|
use Flarum\Discussion\Search\DiscussionSearcher;
|
||||||
use Flarum\Discussion\UserState;
|
use Flarum\Discussion\UserState;
|
||||||
|
@ -20,6 +18,7 @@ use Flarum\Post\Event\Hidden;
|
||||||
use Flarum\Post\Event\Posted;
|
use Flarum\Post\Event\Posted;
|
||||||
use Flarum\Post\Event\Restored;
|
use Flarum\Post\Event\Restored;
|
||||||
use Flarum\Search\Database\DatabaseSearchDriver;
|
use Flarum\Search\Database\DatabaseSearchDriver;
|
||||||
|
use Flarum\Subscriptions\Api\UserResourceFields;
|
||||||
use Flarum\Subscriptions\Filter\SubscriptionFilter;
|
use Flarum\Subscriptions\Filter\SubscriptionFilter;
|
||||||
use Flarum\Subscriptions\HideIgnoredFromAllDiscussionsPage;
|
use Flarum\Subscriptions\HideIgnoredFromAllDiscussionsPage;
|
||||||
use Flarum\Subscriptions\Listener;
|
use Flarum\Subscriptions\Listener;
|
||||||
|
@ -48,18 +47,11 @@ return [
|
||||||
->namespace('flarum-subscriptions', __DIR__.'/views'),
|
->namespace('flarum-subscriptions', __DIR__.'/views'),
|
||||||
|
|
||||||
(new Extend\Notification())
|
(new Extend\Notification())
|
||||||
->type(NewPostBlueprint::class, BasicDiscussionSerializer::class, ['alert', 'email'])
|
->type(NewPostBlueprint::class, ['alert', 'email'])
|
||||||
->beforeSending(FilterVisiblePostsBeforeSending::class),
|
->beforeSending(FilterVisiblePostsBeforeSending::class),
|
||||||
|
|
||||||
(new Extend\ApiSerializer(DiscussionSerializer::class))
|
(new Extend\ApiResource(Resource\DiscussionResource::class))
|
||||||
->attribute('subscription', function (DiscussionSerializer $serializer, Discussion $discussion) {
|
->fields(UserResourceFields::class),
|
||||||
if ($state = $discussion->state) {
|
|
||||||
return $state->subscription;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
|
|
||||||
(new Extend\User())
|
|
||||||
->registerPreference('followAfterReply', 'boolval', false),
|
|
||||||
|
|
||||||
(new Extend\Event())
|
(new Extend\Event())
|
||||||
->listen(Saving::class, Listener\SaveSubscriptionToDatabase::class)
|
->listen(Saving::class, Listener\SaveSubscriptionToDatabase::class)
|
||||||
|
@ -75,5 +67,6 @@ return [
|
||||||
->addMutator(DiscussionSearcher::class, HideIgnoredFromAllDiscussionsPage::class),
|
->addMutator(DiscussionSearcher::class, HideIgnoredFromAllDiscussionsPage::class),
|
||||||
|
|
||||||
(new Extend\User())
|
(new Extend\User())
|
||||||
|
->registerPreference('followAfterReply', 'boolval', false)
|
||||||
->registerPreference('flarum-subscriptions.notify_for_all_posts', 'boolval', false),
|
->registerPreference('flarum-subscriptions.notify_for_all_posts', 'boolval', false),
|
||||||
];
|
];
|
||||||
|
|
31
extensions/subscriptions/src/Api/UserResourceFields.php
Normal file
31
extensions/subscriptions/src/Api/UserResourceFields.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Flarum\Subscriptions\Api;
|
||||||
|
|
||||||
|
use Flarum\Api\Context;
|
||||||
|
use Flarum\Api\Endpoint;
|
||||||
|
use Flarum\Api\Schema;
|
||||||
|
use Flarum\Discussion\Discussion;
|
||||||
|
|
||||||
|
class UserResourceFields
|
||||||
|
{
|
||||||
|
public function __invoke(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
Schema\Str::make('subscription')
|
||||||
|
->writable(fn (Discussion $discussion, Context $context) => $context->endpoint instanceof Endpoint\Update && ! $context->getActor()->isGuest())
|
||||||
|
->nullable()
|
||||||
|
->get(fn (Discussion $discussion) => $discussion->state?->subscription)
|
||||||
|
->set(function (Discussion $discussion, ?string $subscription, Context $context) {
|
||||||
|
$actor = $context->getActor();
|
||||||
|
$state = $discussion->stateFor($actor);
|
||||||
|
|
||||||
|
if (! in_array($subscription, ['follow', 'ignore'])) {
|
||||||
|
$subscription = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$state->subscription = $subscription;
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -119,7 +119,7 @@ class ReplyNotificationTest extends TestCase
|
||||||
'authenticatedAs' => 1,
|
'authenticatedAs' => 1,
|
||||||
'json' => [
|
'json' => [
|
||||||
'data' => [
|
'data' => [
|
||||||
'type' => 'posts',
|
'type' => 'discussions',
|
||||||
'attributes' => [
|
'attributes' => [
|
||||||
'title' => 'ACME',
|
'title' => 'ACME',
|
||||||
],
|
],
|
||||||
|
@ -134,7 +134,7 @@ class ReplyNotificationTest extends TestCase
|
||||||
'authenticatedAs' => 1,
|
'authenticatedAs' => 1,
|
||||||
'json' => [
|
'json' => [
|
||||||
'data' => [
|
'data' => [
|
||||||
'type' => 'posts',
|
'type' => 'discussions',
|
||||||
'attributes' => [
|
'attributes' => [
|
||||||
'lastReadPostNumber' => 2,
|
'lastReadPostNumber' => 2,
|
||||||
],
|
],
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Subscriptions\Tests\integration\api\discussions;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Flarum\Extend\ModelVisibility;
|
||||||
|
use Flarum\Group\Group;
|
||||||
|
use Flarum\Post\Post;
|
||||||
|
use Flarum\Testing\integration\RetrievesAuthorizedUsers;
|
||||||
|
use Flarum\Testing\integration\TestCase;
|
||||||
|
use Flarum\User\User;
|
||||||
|
|
||||||
|
class SubscribeTest extends TestCase
|
||||||
|
{
|
||||||
|
use RetrievesAuthorizedUsers;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->extension('flarum-subscriptions');
|
||||||
|
|
||||||
|
$this->prepareDatabase([
|
||||||
|
'users' => [
|
||||||
|
$this->normalUser(),
|
||||||
|
['id' => 3, 'username' => 'acme', 'email' => 'acme@machine.local', 'is_email_confirmed' => 1, 'preferences' => json_encode(['flarum-subscriptions.notify_for_all_posts' => true])],
|
||||||
|
['id' => 4, 'username' => 'acme2', 'email' => 'acme2@machine.local', 'is_email_confirmed' => 1],
|
||||||
|
],
|
||||||
|
'discussions' => [
|
||||||
|
['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 1, 'comment_count' => 1, 'last_post_number' => 1, 'last_post_id' => 1],
|
||||||
|
['id' => 2, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 2, 'comment_count' => 1, 'last_post_number' => 1, 'last_post_id' => 2],
|
||||||
|
|
||||||
|
['id' => 33, 'title' => __CLASS__, 'created_at' => Carbon::now(), 'last_posted_at' => Carbon::now(), 'user_id' => 1, 'first_post_id' => 33, 'comment_count' => 6, 'last_post_number' => 6, 'last_post_id' => 38],
|
||||||
|
],
|
||||||
|
'posts' => [
|
||||||
|
['id' => 1, 'discussion_id' => 1, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 1],
|
||||||
|
['id' => 2, 'discussion_id' => 2, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 1],
|
||||||
|
|
||||||
|
['id' => 33, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 1],
|
||||||
|
['id' => 34, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 2],
|
||||||
|
['id' => 35, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 3],
|
||||||
|
['id' => 36, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 4],
|
||||||
|
['id' => 37, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 5],
|
||||||
|
['id' => 38, 'discussion_id' => 33, 'created_at' => Carbon::createFromDate(1975, 5, 21)->toDateTimeString(), 'user_id' => 1, 'type' => 'comment', 'content' => '<t><p>foo bar</p></t>', 'number' => 6],
|
||||||
|
],
|
||||||
|
'discussion_user' => [
|
||||||
|
['discussion_id' => 1, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'],
|
||||||
|
['discussion_id' => 1, 'user_id' => 2, 'last_read_post_number' => 1, 'subscription' => null],
|
||||||
|
['discussion_id' => 2, 'user_id' => 1, 'last_read_post_number' => 1, 'subscription' => 'follow'],
|
||||||
|
|
||||||
|
['discussion_id' => 33, 'user_id' => 2, 'last_read_post_number' => 1, 'subscription' => 'follow'],
|
||||||
|
['discussion_id' => 33, 'user_id' => 3, 'last_read_post_number' => 1, 'subscription' => 'ignore'],
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider provideStates
|
||||||
|
*/
|
||||||
|
public function can_subscribe_to_a_discussion(int $actorId, int $discussionId, ?string $newState)
|
||||||
|
{
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$response = $this->send(
|
||||||
|
$this->request('PATCH', '/api/discussions/'.$discussionId, [
|
||||||
|
'authenticatedAs' => $actorId,
|
||||||
|
'json' => [
|
||||||
|
'data' => [
|
||||||
|
'type' => 'discussions',
|
||||||
|
'attributes' => [
|
||||||
|
'subscription' => $newState,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
])
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->getStatusCode(), $response->getBody()->getContents());
|
||||||
|
$this->assertEquals($newState, $this->database()->table('discussion_user')->where('discussion_id', $discussionId)->where('user_id', $actorId)->value('subscription'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function provideStates()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'follow' => [2, 1, 'follow'],
|
||||||
|
'ignore' => [2, 1, 'ignore'],
|
||||||
|
'null' => [2, 1, null],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user