Skip to content

Commit 278d086

Browse files
committed
fixes
1 parent 5cb1a51 commit 278d086

30 files changed

+541
-180
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
"autoload": {
3434
"psr-4": {
3535
"Yuges\\Reactable\\": "src",
36-
"Yuges\\Reactable\\Database\\Seeders\\": "database/seeders/",
37-
"Yuges\\Reactable\\Database\\Factories\\": "database/factories/"
36+
"Database\\Seeders\\": "database/seeders/",
37+
"Database\\Factories\\": "database/factories/"
3838
}
3939
},
4040
"autoload-dev": {

config/reactable.php

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,62 @@
11
<?php
22

3+
// Config for yuges/reactable
4+
35
return [
46
/*
5-
* FQCN (Fully Qualified Class Name) of the models to use for comments
7+
* FQCN (Fully Qualified Class Name) of the models to use for reactions
68
*/
79
'models' => [
10+
'type' => [
11+
'key' => [
12+
'has' => true,
13+
'type' => Yuges\Package\Enums\KeyType::BigInteger,
14+
],
15+
'table' => 'reaction_types',
16+
'class' => Yuges\Reactable\Models\ReactionType::class,
17+
'observer' => Yuges\Reactable\Observers\ReactionTypeObserver::class,
18+
],
819
'reaction' => [
9-
'default' => Yuges\Reactable\Models\Reaction::class,
10-
'type' => Yuges\Reactable\Models\ReactionType::class,
20+
'key' => [
21+
'has' => true,
22+
'type' => Yuges\Package\Enums\KeyType::BigInteger,
23+
],
24+
'table' => 'reactions',
25+
'class' => Yuges\Reactable\Models\Reaction::class,
26+
'observer' => Yuges\Reactable\Observers\ReactionObserver::class,
27+
],
28+
'reactable' => [
29+
'key' => [
30+
'has' => false,
31+
'type' => Yuges\Package\Enums\KeyType::BigInteger,
32+
],
33+
'allowed' => [
34+
'classes' => [
35+
// \App\Models\User::class,
36+
],
37+
],
38+
'relation' => [
39+
'name' => 'reactable',
40+
],
41+
'observer' => Yuges\Reactable\Observers\ReactableObserver::class,
1142
],
1243
'reactor' => [
13-
'default' => \App\Models\User::class,
44+
'key' => [
45+
'has' => true,
46+
'type' => Yuges\Package\Enums\KeyType::BigInteger,
47+
],
48+
'default' => [
49+
'class' => \App\Models\User::class,
50+
],
1451
'allowed' => [
15-
\App\Models\User::class,
52+
'classes' => [
53+
\App\Models\User::class,
54+
],
55+
],
56+
'relation' => [
57+
'name' => 'reactor',
1658
],
59+
'observer' => Yuges\Reactable\Observers\ReactorObserver::class,
1760
],
1861
],
1962

database/migrations/0000_00_00_000000_create_reactions_table.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

database/migrations/0000_00_00_000000_create_reaction_types_table.php renamed to database/migrations/000_create_reaction_types_table.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
22

3+
use Yuges\Package\Enums\KeyType;
34
use Yuges\Reactable\Config\Config;
4-
use Illuminate\Support\Facades\Schema;
55
use Yuges\Reactable\Models\ReactionType;
6-
use Illuminate\Database\Schema\Blueprint;
7-
use Illuminate\Database\Migrations\Migration;
6+
use Yuges\Package\Database\Schema\Schema;
7+
use Yuges\Package\Database\Schema\Blueprint;
8+
use Yuges\Package\Database\Migrations\Migration;
89

910
return new class extends Migration
1011
{
11-
public function __construct(protected string $table = 'reaction_types')
12+
public function __construct()
1213
{
13-
$this->$table = Config::getReactionTypeClass(ReactionType::class)::getTableName();
14+
$this->table = Config::getReactionTypeClass(ReactionType::class)::getTableName();
1415
}
1516

1617
public function up(): void
@@ -20,7 +21,9 @@ public function up(): void
2021
}
2122

2223
Schema::create($this->table, function (Blueprint $table) {
23-
$table->id();
24+
if (Config::getReactionTypeKeyHas(true)) {
25+
$table->key(Config::getReactionTypeKeyType(KeyType::BigInteger));
26+
}
2427

2528
$table->string('name')->unique();
2629
$table->string('icon')->unique();
@@ -29,9 +32,4 @@ public function up(): void
2932
$table->timestamps();
3033
});
3134
}
32-
33-
public function down(): void
34-
{
35-
Schema::dropIfExists($this->table);
36-
}
3735
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
use Yuges\Package\Enums\KeyType;
4+
use Yuges\Reactable\Config\Config;
5+
use Yuges\Reactable\Models\Reaction;
6+
use Yuges\Reactable\Models\ReactionType;
7+
use Yuges\Package\Database\Schema\Schema;
8+
use Yuges\Package\Database\Schema\Blueprint;
9+
use Yuges\Package\Database\Migrations\Migration;
10+
11+
return new class extends Migration
12+
{
13+
public function __construct()
14+
{
15+
$this->table = Config::getReactionClass(Reaction::class)::getTableName();
16+
}
17+
18+
public function up(): void
19+
{
20+
if (Schema::hasTable($this->table)) {
21+
return;
22+
}
23+
24+
Schema::create($this->table, function (Blueprint $table) {
25+
if (Config::getReactionKeyHas(true)) {
26+
$table->key(Config::getReactionKeyType(KeyType::BigInteger));
27+
}
28+
29+
Config::getPermissionsAnonymous(false)
30+
? $table->nullableKeyMorphs(
31+
Config::getReactorKeyType(KeyType::BigInteger),
32+
Config::getReactorRelationName('reactor'),
33+
)
34+
: $table->keyMorphs(
35+
Config::getReactorKeyType(KeyType::BigInteger),
36+
Config::getReactorRelationName('reactor'),
37+
);
38+
39+
$table->keyMorphs(
40+
Config::getReactableKeyType(KeyType::BigInteger),
41+
Config::getReactableRelationName('reactable'),
42+
);
43+
44+
$table
45+
->foreignIdFor(Config::getReactionTypeClass(ReactionType::class))
46+
->constrained()
47+
->cascadeOnUpdate()
48+
->cascadeOnDelete();
49+
50+
$table->timestamps();
51+
52+
if (! Config::getPermissionsDuplicate(false)) {
53+
$table->unique([
54+
'reactor_id',
55+
'reactor_type',
56+
'reactable_id',
57+
'reactable_type',
58+
]);
59+
}
60+
});
61+
}
62+
};

src/Actions/CreateReactionAction.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function create(Reactable $reactable): self
2424
return new static($reactable);
2525
}
2626

27-
public function execute(int|string|ReactionType|ReactionTypeEnum $type, Reactor $reactor = null): Reaction
27+
public function execute(int|string|ReactionType|ReactionTypeEnum $type, ?Reactor $reactor = null): Reaction
2828
{
2929
$reactor ??= $this->getDefaultReactor();
3030

@@ -50,12 +50,16 @@ public function execute(int|string|ReactionType|ReactionTypeEnum $type, Reactor
5050
return $this->reactable->reactions()->create($attributes);
5151
}
5252

53-
$reaction = $this->reactable->reactions()->getQuery()->whereMorphedTo('reactor', $reactor)->first();
53+
/** @var ?Reaction */
54+
$reaction = $this->reactable->reactions()->getQuery()->whereMorphedTo(
55+
Config::getReactorRelationName('reactor'),
56+
$reactor,
57+
)->first();
5458

5559
return $reaction ?? $this->reactable->reactions()->create($attributes);
5660
}
5761

58-
public function validateReactor(Reactor $reactor = null): void
62+
public function validateReactor(?Reactor $reactor = null): void
5963
{
6064
if (! $reactor) {
6165
return;

src/Actions/ToggleReactionAction.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function create(Reactable $reactable): self
2424
return new static($reactable);
2525
}
2626

27-
public function execute(int|string|ReactionType|ReactionTypeEnum $type, Reactor $reactor = null): ?Reaction
27+
public function execute(int|string|ReactionType|ReactionTypeEnum $type, ?Reactor $reactor = null): ?Reaction
2828
{
2929
$reactor ??= $this->getDefaultReactor();
3030

@@ -40,19 +40,34 @@ public function execute(int|string|ReactionType|ReactionTypeEnum $type, Reactor
4040
throw new Exception('Type of reaction not found');
4141
}
4242

43-
$reaction = $this->reactable->reactions()->getQuery()->whereMorphedTo('reactor', $reactor)->first();
43+
$attributes = [
44+
'reactor_id' => $reactor?->getKey() ?? null,
45+
'reactor_type' => $reactor?->getMorphClass() ?? null,
46+
'reaction_type_id' => $type->getKey(),
47+
];
48+
49+
if (Config::getPermissionsDuplicate()) {
50+
return $this->reactable->reactions()->create($attributes);
51+
}
52+
53+
/** @var ?Reaction */
54+
$reaction = $this->reactable->reactions()->getQuery()->whereMorphedTo(
55+
Config::getReactorRelationName('reactor'),
56+
$reactor,
57+
)->first();
4458

4559
if ($reaction) {
4660
$reaction->delete();
4761

48-
return null;
62+
if ($reaction->reaction_type_id === $type->id) {
63+
return null;
64+
}
4965
}
5066

51-
return Config::getCreateReactionAction($this->reactable)->execute($type, $reactor);
52-
67+
return $this->reactable->reactions()->create($attributes);
5368
}
5469

55-
public function validateReactor(Reactor $reactor = null): void
70+
public function validateReactor(?Reactor $reactor = null): void
5671
{
5772
if (! $reactor) {
5873
return;

0 commit comments

Comments
 (0)