Skip to content

Commit 01b4eab

Browse files
committed
Merged main into remove-tailwind
2 parents a9b3c94 + 2c86c2a commit 01b4eab

19 files changed

+386
-228
lines changed

README.md

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ composer require kirschbaum-development/commentions
1818

1919
## Usage
2020

21-
1. Publish the migrations
21+
**1. Publish the migrations**
2222

2323
```bash
2424
php artisan vendor:publish --tag="commentions-migrations"
2525
```
2626

27-
2. In your `User` model implement the `Commenter` interface.
27+
**2. In your `User` model implement the `Commenter` interface.**
2828

2929
```php
3030
use Kirschbaum\Commentions\Contracts\Commenter;
@@ -35,7 +35,7 @@ class User extends Model implements Commenter
3535
}
3636
```
3737

38-
3. In the model you want to add comments, implement the `Commentable` interface and the `HasComments` trait.
38+
**3. In the model you want to add comments, implement the `Commentable` interface and the `HasComments` trait.**
3939

4040
```php
4141
use Kirschbaum\Commentions\HasComments;
@@ -47,7 +47,9 @@ class Project extends Model implements Commentable
4747
}
4848
```
4949

50-
4. Configure the Commentions paths in your Tailwind config:
50+
**4. Configure the Commentions paths in your Tailwind config:**
51+
52+
If you don't have a [custom theme](https://filamentphp.com/docs/3.x/panels/themes#creating-a-custom-theme), you will have to configure. This is Filament's [recommendation](https://filamentphp.com/docs/3.x/support/assets#using-tailwind-css-in-plugins) if you are using Plugins.
5153

5254
```js
5355
export default {
@@ -60,22 +62,22 @@ export default {
6062
}
6163
```
6264

63-
5. Register the Commentions plugin:
65+
**5. Register the Commentions plugin:**
66+
67+
You can register the plugin in your Panel(s) like so:
6468

6569
```php
6670
use Kirschbaum\Commentions\CommentionsPlugin;
6771

6872
return $panel
6973
->plugins([
70-
CommentionsPlugin::make()
71-
->disallowEdits() // Prevent users from editing their comments
72-
->disallowDeletes() // Prevent users from deleting their comments
74+
CommentionsPlugin::make(),
7375
])
7476
```
7577

76-
There are a couple of ways to use Commentions with Filament.
78+
**6. Use the component**
7779

78-
1. Register the component in your Filament Infolists:
80+
Infolists:
7981

8082
```php
8183
Infolists\Components\Section::make('Comments')
@@ -85,7 +87,7 @@ Infolists\Components\Section::make('Comments')
8587
]),
8688
```
8789

88-
2. Or in your table actions:
90+
Table actions:
8991

9092
```php
9193
use Kirschbaum\Commentions\Filament\Actions\CommentsTableAction;
@@ -96,7 +98,7 @@ use Kirschbaum\Commentions\Filament\Actions\CommentsTableAction;
9698
])
9799
```
98100

99-
3. Or as a header action:
101+
Or header actions:
100102

101103
```php
102104
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
@@ -121,36 +123,61 @@ If your `User` model lives in a different namespace than `App\Models\User`, you
121123
],
122124
```
123125

124-
### Disabling comment editing and deletion
126+
### Configuring the Comment model
127+
128+
If you need to customize the Comment model, you can extend the `\Kirschbaum\Commentions\Comment` class and then update the `comment.model` option in your `config/commentions.php` file:
129+
130+
```php
131+
'comment' => [
132+
'model' => \App\Models\Comment::class,
133+
// ...
134+
],
135+
```
125136

126-
By default, users can edit and delete their own comments. You can disable this functionality in two ways:
137+
### Configuring Comment permissions
127138

128-
#### 1. Using the plugin configuration
139+
By default, users can create comments, as well as edit and delete their own comments. You can adjust these permissions by implementing your own policy:
140+
141+
#### 1) Create a custom policy
129142

130143
```php
131-
use Kirschbaum\Commentions\CommentionsPlugin;
144+
namespace App\Policies;
132145

133-
return $panel
134-
->plugins([
135-
CommentionsPlugin::make()
136-
->disallowEdits() // Prevent users from editing their comments
137-
->disallowDeletes() // Prevent users from deleting their comments
138-
])
146+
use Kirschbaum\Commentions\Comment;
147+
use Kirschbaum\Commentions\Contracts\Commenter;
148+
use Kirschbaum\Commentions\Policies\CommentPolicy;
149+
150+
class CommentPolicy extends CommentPolicy
151+
{
152+
public function create(Commenter $user): bool
153+
{
154+
// TODO: Implement custom permission logic.
155+
}
156+
157+
public function update($user, Comment $comment): bool
158+
{
159+
// TODO: Implement custom permission logic.
160+
}
161+
162+
public function delete($user, Comment $comment): bool
163+
{
164+
// TODO: Implement custom permission logic.
165+
}
166+
}
139167
```
140168

141-
#### 2. Using the configuration file
169+
#### 2) Register your policy in the configuration file
142170

143-
Set the `allow_edits` and `allow_deletes` options in your `config/commentions.php` file:
171+
Update the `comment.policy` option in your `config/commentions.php` file:
144172

145173
```php
146-
/**
147-
* Comment editing/deleting options.
148-
*/
149-
'allow_edits' => false,
150-
'allow_deletes' => false,
174+
'comment' => [
175+
// ...
176+
'policy' => \App\Policies\CommentPolicy::class,
177+
],
151178
```
152179

153-
> **Note:** The plugin configuration takes precedence over the config file settings.
180+
### Configuring the Commenter name
154181

155182
By default, the `name` property will be used to render the mention names. You can customize it either by implementing the Filament `HasName` interface OR by implementing the optional `getCommenterName` method.
156183

@@ -278,6 +305,7 @@ If you discover any security related issues, please email security@kirschbaumdev
278305
## Credits
279306

280307
- [Luis Dalmolin](https://github.com/luisdalmolin)
308+
- [All contributors](https://github.com/kirschbaum-development/commentions/graphs/contributors)
281309

282310
## Sponsorship
283311

config/commentions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222

2323
/*
2424
|--------------------------------------------------------------------------
25-
| Comment Moderation
25+
| Comment model configuration
2626
|--------------------------------------------------------------------------
2727
*/
28-
'allow_edits' => true,
29-
'allow_deletes' => true,
28+
'comment' => [
29+
'model' => \Kirschbaum\Commentions\Comment::class,
30+
'policy' => \Kirschbaum\Commentions\Policies\CommentPolicy::class,
31+
],
3032

3133
/*
3234
|--------------------------------------------------------------------------

resources/views/comment.blade.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use('\Kirschbaum\Commentions\Config')
2+
13
<div class="flex items-start gap-x-4 border p-4 rounded-lg shadow-sm mb-2" id="filament-comment-{{ $comment->getId() }}">
24
@if ($avatar = $comment->getAuthorAvatar())
35
<img
@@ -32,16 +34,18 @@ class="text-xs text-gray-300 ml-1"
3234
@endif
3335
</div>
3436

35-
@if ($comment->isComment() && $comment->canEdit())
37+
@if ($comment->isComment() && Config::resolveAuthenticatedUser()?->canAny(['update', 'delete'], $comment))
3638
<div class="flex gap-x-1">
37-
<x-filament::icon-button
38-
icon="heroicon-s-pencil-square"
39-
wire:click="edit"
40-
size="xs"
41-
color="gray"
42-
/>
39+
@if (Config::resolveAuthenticatedUser()?->can('update', $comment))
40+
<x-filament::icon-button
41+
icon="heroicon-s-pencil-square"
42+
wire:click="edit"
43+
size="xs"
44+
color="gray"
45+
/>
46+
@endif
4347

44-
@if ($comment->canDelete())
48+
@if (Config::resolveAuthenticatedUser()?->can('delete', $comment))
4549
<x-filament::icon-button
4650
icon="heroicon-s-trash"
4751
wire:click="$dispatch('open-modal', { id: 'delete-comment-modal-{{ $comment->getId() }}' })"
@@ -90,7 +94,7 @@ class="text-xs text-gray-300 ml-1"
9094
@endif
9195
</div>
9296

93-
@if ($comment->isComment() && $comment->canDelete())
97+
@if ($comment->isComment() && \Kirschbaum\Commentions\Config::resolveAuthenticatedUser()?->can('delete', $comment))
9498
<x-filament::modal
9599
id="delete-comment-modal-{{ $comment->getId() }}"
96100
wire:model="showDeleteModal"

resources/views/comments.blade.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1+
@use('\Kirschbaum\Commentions\Config')
2+
13
<div class="space-y-2" x-data="{ wasFocused: false }">
2-
<form wire:submit.prevent="save" x-cloak>
3-
{{-- tiptap editor --}}
4-
<div class="relative tip-tap-container mb-2" x-on:click="wasFocused = true" wire:ignore>
5-
<div
6-
x-data="editor(@js($commentBody), @js($this->mentions), 'comments')"
7-
>
8-
<div x-ref="element"></div>
4+
@if (Config::resolveAuthenticatedUser()?->can('create', Config::getCommentModel()))
5+
<form wire:submit.prevent="save" x-cloak>
6+
{{-- tiptap editor --}}
7+
<div class="relative tip-tap-container mb-2" x-on:click="wasFocused = true" wire:ignore>
8+
<div
9+
x-data="editor(@js($commentBody), @js($this->mentions), 'comments')"
10+
>
11+
<div x-ref="element"></div>
12+
</div>
913
</div>
10-
</div>
1114

12-
<template x-if="wasFocused">
13-
<div>
14-
<x-filament::button
15-
wire:click="save"
16-
size="sm"
17-
>Save</x-filament::button>
15+
<template x-if="wasFocused">
16+
<div>
17+
<x-filament::button
18+
wire:click="save"
19+
size="sm"
20+
>Save</x-filament::button>
1821

19-
<x-filament::button
20-
x-on:click="wasFocused = false"
21-
wire:click="clear"
22-
size="sm"
23-
color="gray"
24-
>Cancel</x-filament::button>
25-
</div>
26-
</template>
27-
</form>
22+
<x-filament::button
23+
x-on:click="wasFocused = false"
24+
wire:click="clear"
25+
size="sm"
26+
color="gray"
27+
>Cancel</x-filament::button>
28+
</div>
29+
</template>
30+
</form>
31+
@endif
2832

2933
<livewire:commentions::comment-list
3034
:record="$record"

src/Actions/SaveComment.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
namespace Kirschbaum\Commentions\Actions;
44

5+
use Illuminate\Auth\Access\AuthorizationException;
56
use Illuminate\Database\Eloquent\Model;
67
use Kirschbaum\Commentions\Comment;
8+
use Kirschbaum\Commentions\Config;
79
use Kirschbaum\Commentions\Contracts\Commenter;
810
use Kirschbaum\Commentions\Events\UserWasMentionedEvent;
911

1012
class SaveComment
1113
{
14+
/**
15+
* @throws AuthorizationException
16+
*/
1217
public function __invoke(Model $commentable, Commenter $author, string $body): Comment
1318
{
19+
if ($author->cannot('create', Config::getCommentModel())) {
20+
throw new AuthorizationException('Cannot create comment');
21+
}
22+
1423
$comment = $commentable->comments()->create([
1524
'body' => $body,
1625
'author_id' => $author->getKey(),

src/Comment.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,6 @@ public function reactions(): HasMany
163163
return $this->hasMany(CommentReaction::class);
164164
}
165165

166-
public function canEdit(): bool
167-
{
168-
return Config::allowEdits() && $this->isAuthor(Config::resolveAuthenticatedUser());
169-
}
170-
171-
public function canDelete(): bool
172-
{
173-
return Config::allowDeletes() && $this->isAuthor(Config::resolveAuthenticatedUser());
174-
}
175-
176166
public function toggleReaction(string $reaction): void
177167
{
178168
ToggleCommentReaction::run($this, $reaction, Config::resolveAuthenticatedUser());

src/CommentReaction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CommentReaction extends Model
2323
/** @return BelongsTo<Comment> */
2424
public function comment(): BelongsTo
2525
{
26-
return $this->belongsTo(Comment::class);
26+
return $this->belongsTo(Config::getCommentModel());
2727
}
2828

2929
/** @return MorphTo<Commenter> */

src/CommentionsPlugin.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ public function register(Panel $panel): void {}
1616

1717
public function boot(Panel $panel): void {}
1818

19-
public function disallowEdits(): static
20-
{
21-
Config::allowEdits(false);
22-
23-
return $this;
24-
}
25-
26-
public function disallowDeletes(): static
27-
{
28-
Config::allowDeletes(false);
29-
30-
return $this;
31-
}
32-
3319
public static function make(): static
3420
{
3521
return app(static::class);

src/CommentionsServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Filament\Support\Assets\Css;
66
use Filament\Support\Assets\Js;
77
use Filament\Support\Facades\FilamentAsset;
8+
use Illuminate\Support\Facades\Gate;
9+
use Kirschbaum\Commentions\Comment as CommentModel;
810
use Kirschbaum\Commentions\Livewire\Comment;
911
use Kirschbaum\Commentions\Livewire\CommentList;
1012
use Kirschbaum\Commentions\Livewire\Comments;
@@ -54,5 +56,7 @@ public function packageBooted(): void
5456
],
5557
'kirschbaum-development/' . static::$name
5658
);
59+
60+
Gate::policy(CommentModel::class, config('commentions.comment.policy'));
5761
}
5862
}

0 commit comments

Comments
 (0)