Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,20 @@ class User extends Authenticatable implements Commenter, HasName, HasAvatar
}
```

If your users do not implement `HasAvatar`, Commentions will consult an avatar provider before falling back to `ui-avatars.com`. By default it uses the current Filament panel's default provider (set via `Panel::defaultAvatarProvider(...)`). To force a specific provider regardless of panel context, set the `avatar_provider` config key:

```php
// config/commentions.php
use Filament\AvatarProviders\GravatarProvider;

return [
// ...
'avatar_provider' => GravatarProvider::class,
];
```

Any class exposing a `get(Model|Authenticatable $user): string` method works.

### Customizing TipTap Editor Styles

You can customize the TipTap editor CSS classes used using the `Config` class.
Expand Down
17 changes: 17 additions & 0 deletions config/commentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@
'allowed' => ['👍', '❤️', '😂', '😮', '😢', '🤔'],
],

/*
|--------------------------------------------------------------------------
| Avatar provider
|--------------------------------------------------------------------------
|
| Class name of a Filament-compatible avatar provider used to resolve a
| URL for the comment author when the user does not implement HasAvatar.
| Must expose a `get(Model|Authenticatable $user): string` method.
| Examples: Filament\AvatarProviders\UiAvatarsProvider,
| Filament\AvatarProviders\GravatarProvider.
|
| When null, Commentions falls back to the active Filament panel's
| default avatar provider (if any), and finally to ui-avatars.com.
|
*/
'avatar_provider' => null,

/*
|--------------------------------------------------------------------------
| Subscriptions
Expand Down
38 changes: 34 additions & 4 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Carbon\CarbonInterface;
use Closure;
use DateTime;
use Filament\Facades\Filament;
use Filament\Models\Contracts\HasAvatar;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -121,14 +122,18 @@ public function getAuthorName(): string

public function getAuthorAvatar(): string
{
$avatar = null;

if ($this->author instanceof HasAvatar) {
$avatar = $this->author->getFilamentAvatarUrl();

if (! is_null($avatar)) {
return $avatar;
}
}

if (! is_null($avatar)) {
return $avatar;
$providerAvatar = $this->resolveAvatarFromProvider();

if (! is_null($providerAvatar)) {
return $providerAvatar;
}

$name = str(Manager::getName($this->author))
Expand Down Expand Up @@ -183,6 +188,31 @@ public function getContentHash(): string
]));
}

protected function resolveAvatarFromProvider(): ?string
{
$providerClass = Config::getAvatarProvider();

if ($providerClass === null) {
try {
if (Filament::getCurrentPanel() !== null) {
$providerClass = Filament::getDefaultAvatarProvider();
}
} catch (\Throwable) {
return null;
}
}

if ($providerClass === null) {
return null;
}

try {
return app($providerClass)->get($this->author);
} catch (\Throwable) {
return null;
}
}

protected static function newFactory()
{
return CommentFactory::new();
Expand Down
5 changes: 5 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public static function getAllowedReactions(): array
return config('commentions.reactions.allowed', ['👍']);
}

public static function getAvatarProvider(): ?string
{
return config('commentions.avatar_provider');
}

public static function resolveTipTapCssClassesUsing(Closure $callback): void
{
static::$resolveTipTapCssClasses = $callback;
Expand Down
28 changes: 28 additions & 0 deletions tests/AvatarProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Kirschbaum\Commentions\Comment as CommentModel;
use Tests\Models\Post;
use Tests\Models\StubAvatarProvider;
use Tests\Models\User;

test('falls back to ui-avatars when no provider is configured', function () {
config()->set('commentions.avatar_provider', null);

$user = User::factory()->create(['name' => 'Jane Doe']);
$post = Post::factory()->create();
$comment = CommentModel::factory()->author($user)->commentable($post)->create();

expect($comment->getAuthorAvatar())
->toStartWith('https://ui-avatars.com/api/?');
});

test('uses configured avatar provider when set', function () {
config()->set('commentions.avatar_provider', StubAvatarProvider::class);

$user = User::factory()->create(['name' => 'Jane Doe']);
$post = Post::factory()->create();
$comment = CommentModel::factory()->author($user)->commentable($post)->create();

expect($comment->getAuthorAvatar())
->toBe('https://stub.test/avatar/Jane+Doe');
});
14 changes: 14 additions & 0 deletions tests/Models/StubAvatarProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Tests\Models;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;

class StubAvatarProvider
{
public function get(Model|Authenticatable $user): string
{
return 'https://stub.test/avatar/' . urlencode($user->name);
}
}
Loading