Skip to content

Commit

Permalink
chore: enable testing (#33)
Browse files Browse the repository at this point in the history
* enable testing, test forum actor relation

* Apply fixes from StyleCI

* use all 1.x supported php versions

* revert php versions, needed to to gdpr ext compat

* test following

* Apply fixes from StyleCI

* use basic user serializer

* Apply fixes from StyleCI

* test notification was sent

* simplify

* test unfollowing, counts

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Feb 1, 2024
1 parent 1c2ba6f commit 7ae506d
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
run:
uses: flarum/framework/.github/workflows/[email protected]
with:
enable_backend_testing: false
enable_backend_testing: true
enable_phpstan: true
php_versions: '["8.0", "8.1", "8.2", "8.3"]'

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
vendor
composer.lock
.phpunit.result.cache
27 changes: 8 additions & 19 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Flarum\Api\Serializer\CurrentUserSerializer;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Api\Serializer\UserSerializer;
use Flarum\Database\AbstractModel;
use Flarum\Discussion\Event as DiscussionEvent;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Extend;
Expand All @@ -29,7 +28,6 @@
use Flarum\User\Filter\UserFilterer;
use Flarum\User\Search\UserSearcher;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

return [
(new Extend\Frontend('forum'))
Expand All @@ -42,14 +40,8 @@
new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\Model(User::class))
->relationship('followedUsers', function (AbstractModel $model): BelongsToMany {
return $model->belongsToMany(User::class, 'user_followers', 'user_id', 'followed_user_id')
->withPivot('subscription');
})
->relationship('followedBy', function (AbstractModel $model): BelongsToMany {
return $model->belongsToMany(User::class, 'user_followers', 'followed_user_id', 'user_id')
->withPivot('subscription');
}),
->belongsToMany('followedUsers', User::class, 'user_followers', 'user_id', 'followed_user_id')
->belongsToMany('followedBy', User::class, 'user_followers', 'followed_user_id', 'user_id'),

(new Extend\View())
->namespace('ianm-follow-users', __DIR__.'/resources/views'),
Expand Down Expand Up @@ -83,10 +75,13 @@
->modelPolicy(User::class, Access\UserPolicy::class),

(new Extend\ApiSerializer(CurrentUserSerializer::class))
->hasMany('followedUsers', UserSerializer::class),
->hasMany('followedUsers', BasicUserSerializer::class),

(new Extend\ApiSerializer(BasicUserSerializer::class))
->attributes(Api\AddBasicUserAttributes::class),

(new Extend\ApiSerializer(UserSerializer::class))
->attributes(AddUserAttributes::class),
->attributes(Api\AddUserAttributes::class),

(new Extend\ApiController(ListUsersController::class))
->prepareDataForSerialization(function (ListUsersController $controller, $data, $request) {
Expand All @@ -98,13 +93,7 @@
->addInclude(['followedUsers', 'followedBy']),

(new Extend\ApiController(ShowUserController::class))
->prepareDataForSerialization(function (ShowUserController $controller, User $data, $request) {
$actor = RequestUtil::getActor($request);
$actor->load('followedUsers');
$data->load('followedUsers');

return $data;
})
->prepareDataForSerialization(Api\LoadRelations::class)
->addInclude(['followedUsers', 'followedBy']),

(new Extend\ApiController(ShowForumController::class))
Expand Down
4 changes: 2 additions & 2 deletions migrations/2021_07_14_000002_migrate_old_subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
*
*/

use IanM\FollowUsers\FollowState;
use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
FollowState::whereNull('subscription')
$schema->getConnection()->table('user_followers')
->whereNull('subscription')
->update(['subscription' => 'follow']);
},

Expand Down
17 changes: 7 additions & 10 deletions src/AddUserAttributes.php → src/Api/AddBasicUserAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
*
*/

namespace IanM\FollowUsers;
namespace IanM\FollowUsers\Api;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use IanM\FollowUsers\FollowState;

class AddUserAttributes
class AddBasicUserAttributes
{
/**
* @var SettingsRepositoryInterface
Expand All @@ -28,17 +29,13 @@ public function __construct(SettingsRepositoryInterface $settings)
$this->settings = $settings;
}

public function __invoke(UserSerializer $serializer, User $user, array $attributes): array
public function __invoke(BasicUserSerializer $serializer, User $user, array $attributes): array
{
$actor = $serializer->getActor();

$attributes['followed'] = FollowState::for($actor, $user);
$attributes['canBeFollowed'] = $actor->can('follow', $user);

$attributes['followingCount'] = FollowState::getFollowingCount($user);
$attributes['followed'] = FollowState::for($serializer->getActor(), $user);

if ((bool) $this->settings->get('ianm-follow-users.stats-on-profile')) {
$attributes['followerCount'] = FollowState::getFollowerCount($user);
$attributes['followingCount'] = FollowState::getFollowingCount($user);
}

return $attributes;
Expand Down
26 changes: 26 additions & 0 deletions src/Api/AddUserAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of ianm/follow-users
*
* Copyright (c) Ian Morland.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\FollowUsers\Api;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\User\User;

class AddUserAttributes
{
public function __invoke(UserSerializer $serializer, User $user, array $attributes): array
{
$attributes['canBeFollowed'] = $serializer->getActor()->can('follow', $user);

return $attributes;
}
}
31 changes: 31 additions & 0 deletions src/Api/LoadRelations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of ianm/follow-users
*
* Copyright (c) Ian Morland.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\FollowUsers\Api;

use Flarum\Api\Controller\AbstractSerializeController;
use Flarum\Http\RequestUtil;
use Flarum\User\User;
use Psr\Http\Message\ServerRequestInterface;

class LoadRelations
{
public function __invoke(AbstractSerializeController $controller, User $data, ServerRequestInterface $request): User
{
RequestUtil::getActor($request)
->load('followedUsers');

$data->load('followedUsers');

return $data;
}
}
5 changes: 5 additions & 0 deletions src/FollowState.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class FollowState extends AbstractModel

protected $fillable = ['user_id', 'followed_user_id', 'subscription', 'updated_at'];

protected $casts = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

/**
* Get the follow user subscription state for the given User.
*
Expand Down
1 change: 0 additions & 1 deletion src/Jobs/SendNotificationWhenDiscussionIsStarted.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function handle(NotificationSyncer $notifications)
{
$firstPost = $this->discussion->firstPost ?? $this->discussion->posts()->orderBy('number')->first();

/** @phpstan-ignore-next-line */
if (!$firstPost || null === $this->discussion->user->followedBy) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion src/Jobs/SendNotificationWhenFollowerPosted.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function __construct(Post $post, int $lastPostNumber)

public function handle(NotificationSyncer $notifications)
{
/** @phpstan-ignore-next-line */
if (!$this->post->exists || null === $this->post->user->followedBy) {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Listeners/SaveFollowedToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ public function handle(Saving $event)
/**
* @param User $user
*
* @return BelongsToMany
* @return BelongsToMany<User>
*/
protected function followedUsers(User $user): BelongsToMany
{
/** @phpstan-ignore-next-line */
return $user->followedUsers();
}
}
2 changes: 0 additions & 2 deletions src/Query/FollowUsersDiscussionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ protected function constrain(Builder $query, User $actor, bool $negate)

/**
* @var BelongsToMany $followed
*
* @phpstan-ignore-next-line
*/
$followed = $actor->followedUsers();

Expand Down
1 change: 0 additions & 1 deletion src/Query/FollowedUsersFilterGambit.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function filter(FilterState $filterState, string $filterValue, bool $nega
protected function constrain(Builder $query, User $actor, bool $negate)
{
$query->where(function ($query) use ($actor, $negate) {
/** @phpstan-ignore-next-line */
$ids = $actor->followedUsers()->pluck('users.id');
if ($negate) {
$query->whereNotIn('id', $ids);
Expand Down
Loading

0 comments on commit 7ae506d

Please sign in to comment.