Skip to content

Commit

Permalink
Merge pull request #2211 from bakaphp/feat-block-users
Browse files Browse the repository at this point in the history
feat: add block users
  • Loading branch information
kaioken authored Oct 18, 2024
2 parents 32d4602 + c99ecc7 commit f9a2625
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/GraphQL/Social/Builders/Users/BlockUserBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\GraphQL\Social\Builders\Users;

use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Database\Eloquent\Builder;
use Kanvas\Users\Models\Users;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class BlockUserBuilder
{
public function getUsers(
mixed $root,
array $args,
GraphQLContext $context,
ResolveInfo $resolveInfo
): Builder {
$socialDb = config('database.connections.social.database');

return Users::query()
->join($socialDb . '.blocked_users', 'users.id', '=', 'blocked_users.blocked_users_id')
->where('blocked_users.is_deleted', 0)
->select('users.*');
}
}
37 changes: 37 additions & 0 deletions app/GraphQL/Social/Mutations/Users/BlockUserManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\GraphQL\Social\Mutations\Users;

use Kanvas\Apps\Models\Apps;
use Kanvas\Social\Users\Models\BlockUser;
use Kanvas\Users\Models\Users;
use Kanvas\Users\Repositories\UsersRepository;

class BlockUserManagement
{
public function block($rootValue, array $req): bool
{
$user = auth()->user();
$blockUserId = $req['id'];
$blockUser = Users::getById($blockUserId);
$app = app(Apps::class);

UsersRepository::belongsToThisApp($blockUser, $app);

return $user->block($blockUser, $app) instanceof BlockUser;
}

public function unBlock($rootValue, array $req): bool
{
$user = auth()->user();
$blockUserId = $req['id'];
$blockUser = Users::getById($blockUserId);
$app = app(Apps::class);

UsersRepository::belongsToThisApp($blockUser, $app);

return $user->unBlock($blockUser, $app) instanceof BlockUser;
}
}
28 changes: 28 additions & 0 deletions database/migrations/Social/2024_10_18_013445_add_block_users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
public function up(): void
{
Schema::create('blocked_users', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('users_id')->index();
$table->unsignedBigInteger('blocked_users_id')->index();
$table->unsignedBigInteger('apps_id')->index();
$table->dateTime('created_at')->useCurrent()->index();
$table->dateTime('updated_at')->useCurrent()->index();
$table->boolean('is_deleted')->default(0)->nullable()->index();

$table->index(['users_id', 'blocked_users_id', 'apps_id'], 'blocked_users_index');
$table->index(['users_id', 'blocked_users_id', 'is_deleted']);
});
}

public function down(): void
{
Schema::dropIfExists('blocked_users');
}
};
22 changes: 22 additions & 0 deletions graphql/schemas/Social/user.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extend type Mutation @guard {
blockUser(id: ID!): Boolean
@field(
resolver: "App\\GraphQL\\Social\\Mutations\\Users\\BlockUserManagement@block"
)
unBlockUser(id: ID!): Boolean
@field(
resolver: "App\\GraphQL\\Social\\Mutations\\Users\\BlockUserManagement@unBlock"
)
}

extend type Query @guard {
blockedUsers(
where: _ @whereConditions(columns: ["id", "blocked_users_id"])
orderBy: _ @orderBy(columns: ["created_at", "id"])
): [User!]!
@paginate(
scopes: ["fromApp", "fromUser", "notDeleted"]
builder: "App\\GraphQL\\Social\\Builders\\Users\\BlockUserBuilder@getUsers"
defaultCount: 25
)
}
23 changes: 23 additions & 0 deletions src/Domains/Social/Users/Models/BlockUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Kanvas\Social\Users\Models;

use Baka\Traits\SoftDeletesTrait;
use Kanvas\Social\Models\BaseModel;

/**
* class BlockUser
* @property int $users_id
* @property int $blocked_users_id
* @property int $apps_id
* @property string $created_at
* @property bool $is_deleted
*/
class BlockUser extends BaseModel
{
use SoftDeletesTrait;

protected $table = 'blocked_users';
}
60 changes: 60 additions & 0 deletions src/Domains/Social/Users/Traits/CanBlockUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Kanvas\Social\Users\Traits;

use Baka\Users\Contracts\UserInterface;
use Illuminate\Support\Facades\DB;
use Kanvas\Apps\Models\Apps as ModelsApps;
use Kanvas\Social\Users\Models\BlockUser;

trait CanBlockUser
{
public function block(UserInterface $blocked, ModelsApps $app): BlockUser
{
return DB::transaction(function () use ($blocked, $app) {
$blockedUser = $this->findBlockedUser($this, $blocked, $app);

if (! $blockedUser) {
$blockedUser = new BlockUser();
$blockedUser->users_id = $this->getId();
$blockedUser->blocked_users_id = $blocked->getId();
$blockedUser->apps_id = $app->getId();
$blockedUser->saveOrFail();
} else {
$blockedUser->is_deleted = 0;
$blockedUser->saveOrFail();
}

return $blockedUser;
});
}

public function unBlock(UserInterface $blockedUser, ModelsApps $app): ?BlockUser
{
return DB::transaction(function () use ($blockedUser, $app) {
$blockedUser = $this->findBlockedUser($this, $blockedUser, $app);

if ($blockedUser) {
$blockedUser->delete();
}

return $blockedUser;
});
}

public function isBlocked(UserInterface $blockedUser, ModelsApps $app): bool
{
return $this->findBlockedUser($this, $blockedUser, $app) !== null;
}

private function findBlockedUser(UserInterface $user, UserInterface $blockedUser, ModelsApps $app): ?BlockUser
{
return BlockUser::fromApp($app)
->where('users_id', $user->getId())
->where('blocked_users_id', $blockedUser->getId())
->withTrashed()
->first();
}
}
2 changes: 2 additions & 0 deletions src/Kanvas/Users/Models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Kanvas\Social\Follows\Traits\FollowersTrait;
use Kanvas\Social\Interactions\Traits\LikableTrait;
use Kanvas\Social\Messages\Models\Message;
use Kanvas\Social\Users\Traits\CanBlockUser;
use Kanvas\SystemModules\Models\SystemModules;
use Kanvas\Users\Enums\UserConfigEnum;
use Kanvas\Users\Factories\UsersFactory;
Expand Down Expand Up @@ -124,6 +125,7 @@ class Users extends Authenticatable implements UserInterface, ContractsAuthentic
use HasFilesystemTrait;
use KanvasModelTrait;
use HasNotificationSettings;
use CanBlockUser;
use Searchable {
search as public traitSearch;
}
Expand Down
102 changes: 102 additions & 0 deletions tests/GraphQL/Social/BlockUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Tests\GraphQL\Social;

use Kanvas\AccessControlList\Enums\RolesEnums;
use Kanvas\AccessControlList\Repositories\RolesRepository;
use Kanvas\Apps\Models\Apps;
use Kanvas\Auth\Actions\RegisterUsersAppAction;
use Kanvas\Users\Actions\AssignCompanyAction;
use Kanvas\Users\Models\Users;
use Tests\TestCase;

class BlockUserTest extends TestCase
{
public function testBlockedUsers()
{
$response = $this->graphQL(
'
query BlockedUsers {
blockedUsers {
data {
id
displayname
}
}
}
'
);

$response->assertJsonStructure([
'data' => [
'blockedUsers' => [
'data' => [
'*' => [
'id',
'displayname',
],
],
],
],
]);
}

public function testBlockUser()
{
$user = Users::factory()->create();
$branch = auth()->user()->getCurrentBranch();
(new RegisterUsersAppAction($user, app(Apps::class)))->execute($user->password);
//add user to current company
(new AssignCompanyAction(
$user,
$branch,
RolesRepository::getByNameFromCompany(RolesEnums::ADMIN->value),
app(Apps::class)
))->execute();
$this->graphQL(
'
mutation BlockUser($id: ID!) {
blockUser(id: $id)
}
',
[
'id' => $user->id,
]
)->assertJson([
'data' => [
'blockUser' => true,
],
]);
}

public function testUnBlockUser()
{
$user = Users::factory()->create();
$branch = auth()->user()->getCurrentBranch();
(new RegisterUsersAppAction($user, app(Apps::class)))->execute($user->password);
//add user to current company
(new AssignCompanyAction(
$user,
$branch,
RolesRepository::getByNameFromCompany(RolesEnums::ADMIN->value),
app(Apps::class)
))->execute();

$this->graphQL(
'
mutation UnBlockUser($id: ID!) {
unBlockUser(id: $id)
}
',
[
'id' => $user->id,
]
)->assertJson([
'data' => [
'unBlockUser' => false,
],
]);
}
}

0 comments on commit f9a2625

Please sign in to comment.