Skip to content

Commit 44af989

Browse files
committed
ran /review-branch and made a plan to make changes
1 parent 21b2942 commit 44af989

8 files changed

Lines changed: 159 additions & 21 deletions

File tree

config/filament-forum.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@
4646
'slug' => 'forum-posts',
4747
],
4848

49+
'reactions' => [
50+
'available' => [
51+
'👍' => 'Like',
52+
'❤️' => 'Love',
53+
'😂' => 'Laugh',
54+
'😮' => 'Wow',
55+
'😢' => 'Sad',
56+
'😡' => 'Angry',
57+
],
58+
],
59+
4960
];

src/Events/PostWasReacted.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Tapp\FilamentForum\Events;
44

55
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Contracts\Auth\Authenticatable;
7+
use Illuminate\Database\Eloquent\Model;
68
use Illuminate\Foundation\Events\Dispatchable;
79
use Illuminate\Queue\SerializesModels;
810
use Tapp\FilamentForum\Models\ForumPost;
@@ -18,7 +20,7 @@ class PostWasReacted
1820
* Create a new event instance.
1921
*/
2022
public function __construct(
21-
public $reactor,
23+
public Model|Authenticatable $reactor,
2224
public ForumPost $post,
2325
public ForumPostReaction $reaction,
2426
) {}

src/Filament/Resources/ForumPosts/Tables/ForumPostsTable.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public static function configure(Table $table): Table
3333
'default' => 1,
3434
'sm' => 1,
3535
])
36-
->filters([
37-
//
38-
])
3936
->recordActions([
4037
// Tables\Actions\ViewAction::make(),
4138
EditAction::make()

src/Livewire/ForumCommentReactions.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tapp\FilamentForum\Livewire;
44

55
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\Log;
67
use Livewire\Component;
78
use Tapp\FilamentForum\Models\ForumComment;
89

@@ -16,18 +17,19 @@ class ForumCommentReactions extends Component
1617

1718
public bool $showReactionPicker = false;
1819

19-
public array $availableReactions = [
20-
'👍' => 'Like',
21-
'❤️' => 'Love',
22-
'😂' => 'Laugh',
23-
'😮' => 'Wow',
24-
'😢' => 'Sad',
25-
'😡' => 'Angry',
26-
];
20+
public array $availableReactions = [];
2721

2822
public function mount(ForumComment $comment): void
2923
{
3024
$this->comment = $comment;
25+
$this->availableReactions = config('filament-forum.reactions.available', [
26+
'👍' => 'Like',
27+
'❤️' => 'Love',
28+
'😂' => 'Laugh',
29+
'😮' => 'Wow',
30+
'😢' => 'Sad',
31+
'😡' => 'Angry',
32+
]);
3133
$this->loadReactions();
3234
}
3335

@@ -67,7 +69,10 @@ protected function loadReactions(): void
6769
? $this->comment->getUserReactions(Auth::user())->toArray()
6870
: [];
6971
} catch (\Exception $e) {
70-
// Fallback to empty arrays if there's an issue
72+
Log::warning('Failed to load forum comment reactions', [
73+
'exception' => $e->getMessage(),
74+
'comment_id' => $this->comment->id ?? null,
75+
]);
7176
$this->reactionCounts = [];
7277
$this->userReactions = [];
7378
}

src/Livewire/ForumPostReactions.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tapp\FilamentForum\Livewire;
44

55
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\Log;
67
use Livewire\Component;
78
use Tapp\FilamentForum\Models\ForumPost;
89

@@ -16,18 +17,19 @@ class ForumPostReactions extends Component
1617

1718
public bool $showReactionPicker = false;
1819

19-
public array $availableReactions = [
20-
'👍' => 'Like',
21-
'❤️' => 'Love',
22-
'😂' => 'Laugh',
23-
'😮' => 'Wow',
24-
'😢' => 'Sad',
25-
'😡' => 'Angry',
26-
];
20+
public array $availableReactions = [];
2721

2822
public function mount(ForumPost $post): void
2923
{
3024
$this->post = $post;
25+
$this->availableReactions = config('filament-forum.reactions.available', [
26+
'👍' => 'Like',
27+
'❤️' => 'Love',
28+
'😂' => 'Laugh',
29+
'😮' => 'Wow',
30+
'😢' => 'Sad',
31+
'😡' => 'Angry',
32+
]);
3133
$this->loadReactions();
3234
}
3335

@@ -66,6 +68,10 @@ protected function loadReactions(): void
6668
? $this->post->getUserReactions(Auth::user())->toArray()
6769
: [];
6870
} catch (\Exception $e) {
71+
Log::warning('Failed to load forum post reactions', [
72+
'exception' => $e->getMessage(),
73+
'post_id' => $this->post->id ?? null,
74+
]);
6975
$this->reactionCounts = [];
7076
$this->userReactions = [];
7177
}

src/Models/ForumComment.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public function registerMediaConversions(?Media $media = null): void
135135

136136
public function toggleReaction(string $reaction, $user): void
137137
{
138+
$allowedTypes = array_keys(config('filament-forum.reactions.available', []));
139+
if (! in_array($reaction, $allowedTypes, true)) {
140+
return;
141+
}
142+
138143
$existingReaction = $this->reactions()
139144
->where('reactor_id', $user->getKey())
140145
->where('reactor_type', get_class($user))

src/Models/ForumPost.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public function reactions(): HasMany
6565

6666
public function toggleReaction(string $reaction, $user): void
6767
{
68+
$allowedTypes = array_keys(config('filament-forum.reactions.available', []));
69+
if (! in_array($reaction, $allowedTypes, true)) {
70+
return;
71+
}
72+
6873
$existingReaction = $this->reactions()
6974
->where('reactor_id', $user->getKey())
7075
->where('reactor_type', get_class($user))
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
use Filament\Facades\Filament;
4+
use Tapp\FilamentForum\Models\Forum;
5+
use Tapp\FilamentForum\Models\ForumPost;
6+
use Tapp\FilamentForum\Models\ForumPostReaction;
7+
8+
beforeEach(function () {
9+
$this->userModel = config('filament-forum.user.model');
10+
$this->tenantModel = config('filament-forum.tenancy.model');
11+
});
12+
13+
it('can toggle reaction on forum post', function () {
14+
$tenantModel = $this->tenantModel;
15+
$userModel = $this->userModel;
16+
17+
$team = $tenantModel::factory()->create();
18+
$user = $userModel::factory()->create();
19+
20+
actingAs($user);
21+
Filament::setTenant($team);
22+
23+
$tenantColumn = Forum::getTenantColumnName();
24+
$forum = Forum::factory()->create([$tenantColumn => $team->id]);
25+
$post = ForumPost::factory()->create([
26+
'forum_id' => $forum->id,
27+
'user_id' => $user->id,
28+
$tenantColumn => $team->id,
29+
]);
30+
31+
$post->toggleReaction('👍', $user);
32+
33+
expect($post->getReactionCounts())->toBe(['👍' => 1]);
34+
expect($post->getUserReactions($user)->toArray())->toBe(['👍']);
35+
expect(ForumPostReaction::where('forum_post_id', $post->id)->count())->toBe(1);
36+
37+
$post->toggleReaction('👍', $user);
38+
39+
expect($post->getReactionCounts())->toBe([]);
40+
expect($post->getUserReactions($user)->toArray())->toBe([]);
41+
expect(ForumPostReaction::where('forum_post_id', $post->id)->count())->toBe(0);
42+
});
43+
44+
it('rejects invalid reaction types', function () {
45+
$tenantModel = $this->tenantModel;
46+
$userModel = $this->userModel;
47+
48+
$team = $tenantModel::factory()->create();
49+
$user = $userModel::factory()->create();
50+
51+
actingAs($user);
52+
Filament::setTenant($team);
53+
54+
$tenantColumn = Forum::getTenantColumnName();
55+
$forum = Forum::factory()->create([$tenantColumn => $team->id]);
56+
$post = ForumPost::factory()->create([
57+
'forum_id' => $forum->id,
58+
'user_id' => $user->id,
59+
$tenantColumn => $team->id,
60+
]);
61+
62+
$post->toggleReaction('invalid-reaction-type', $user);
63+
64+
expect(ForumPostReaction::where('forum_post_id', $post->id)->count())->toBe(0);
65+
});
66+
67+
it('scopes reactions to current tenant', function () {
68+
$tenantModel = $this->tenantModel;
69+
$userModel = $this->userModel;
70+
71+
$team1 = $tenantModel::factory()->create();
72+
$team2 = $tenantModel::factory()->create();
73+
$user = $userModel::factory()->create();
74+
75+
actingAs($user);
76+
77+
$tenantColumn = Forum::getTenantColumnName();
78+
79+
$forum1 = Forum::factory()->create([$tenantColumn => $team1->id]);
80+
$forum2 = Forum::factory()->create([$tenantColumn => $team2->id]);
81+
82+
$post1 = ForumPost::factory()->create([
83+
'forum_id' => $forum1->id,
84+
'user_id' => $user->id,
85+
$tenantColumn => $team1->id,
86+
]);
87+
88+
$post2 = ForumPost::factory()->create([
89+
'forum_id' => $forum2->id,
90+
'user_id' => $user->id,
91+
$tenantColumn => $team2->id,
92+
]);
93+
94+
Filament::setTenant($team1);
95+
$post1->toggleReaction('❤️', $user);
96+
97+
Filament::setTenant($team2);
98+
$post2->toggleReaction('😂', $user);
99+
100+
expect(ForumPostReaction::count())->toBe(2);
101+
102+
$reaction1 = ForumPostReaction::where('forum_post_id', $post1->id)->first();
103+
$reaction2 = ForumPostReaction::where('forum_post_id', $post2->id)->first();
104+
105+
expect($reaction1->{$tenantColumn})->toBe($team1->id);
106+
expect($reaction2->{$tenantColumn})->toBe($team2->id);
107+
});

0 commit comments

Comments
 (0)