Skip to content

Commit 3daca6f

Browse files
authored
Documentation of New Features, Improvements in Package installation (#30)
- Added documentation for the new features - Added a method to HasComments to fetch the current thread's users for mentions - Updated the installation command to back up the config file before publishing a new one during upgrades
2 parents f242675 + 29059e8 commit 3daca6f

File tree

3 files changed

+107
-23
lines changed

3 files changed

+107
-23
lines changed

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Run the installation command and follow the prompts:
2424
```bash
2525
php artisan nested-comments:install
2626
```
27+
During the installation, you will be asked if you would like to publish and replace the config file.
28+
This is important especially if you are upgrading the package to a newer version in which the config file structure has changed.
29+
No worries, if you have customizations in your config file that you would like to keep, your current config file will be backed up to `config/nested-comments.php.bak` before the new config file is published.
30+
31+
You will also be asked if you would like to re-publish the package's assets. This is also important in case the package's styles and scripts have changed in the new version.
2732

2833
Adjust the configuration file as necessary, then run migrations.
2934

@@ -210,8 +215,9 @@ $record = Conference::find(1); // Get your record from the database then,
210215
```
211216

212217
### Mentions
213-
The package uses Filament TipTap Editor which supports mentions. You can mention users in your comments by typing `@` followed by the user's name. The package will automatically resolve the user and send them a notification.
214-
You can customize how to fetch mentions by changing the `.closures.getMentionsUsing` closure in the config file. Two sample methods have been included in the main class for getting all users in the DB or only users that have been mentioned in the current thread (default). Customize this however you wish.
218+
The package uses Filament TipTap Editor which supports mentions. You can mention users in your comments by typing `@` followed by the user's name.
219+
In the future, the package will support sending notifications to the mentioned users via database notifications if supported.
220+
For more on how to customize the mentions, see the [Package Customization](#customize-how-to-get-the-mention-items) section below.
215221

216222
**Get only users mentioned in the current thread:**
217223

@@ -327,7 +333,70 @@ The two components can be used anywhere, in resource pages, custom pages, action
327333

328334
## Package Customization
329335
You can customize the package by changing most of the default values in the config file after publishing it.
336+
Additionally, you can customize how the package interacts with your models by overriding some methods in your commentable model.
337+
338+
### Customize how to get the Comment Author's Name
339+
You can customize how to get the comment author's name by overriding the `getUserName` method in your commentable model.
340+
By default, the package uses the `name` attribute of the user model, but you can change this to any other attribute or method that returns a string.
341+
342+
This name will be displayed in the comment card, and it will also be used to mention the user in the comment text.
343+
344+
```php
345+
// e.g in your Post model or any other model that uses the HasComments trait
346+
use Coolsam\NestedComments\Traits\HasComments;
347+
348+
public function getUserName(Model|Authenticatable|null $user): string
349+
{
350+
return $user?->getAttribute('username') ?? $user?->getAttribute('guest_name') ?? 'Guest';
351+
}
352+
```
353+
354+
### Customize the User's Avatar
355+
You can customize the user's avatar by overriding the `getUserAvatar` method in your commentable model.
356+
357+
By default, the package uses [ui-avatars](https://ui-avatars.com) to generate the avatar based on the user's name, but you can change this to any other method that returns a URL to the user's avatar image.
358+
359+
```php
360+
// e.g in your Post model or any other model that uses the HasComments trait
361+
use Coolsam\NestedComments\Traits\HasComments;
330362

363+
public function getUserAvatar(Model|Authenticatable|string|null $user): ?string
364+
{
365+
// return 'https://yourprofile.url.png';
366+
return $user->getAttribute('profile_url') // get your user's profile url here, assuming you have defined it in your user's model.
367+
}
368+
```
369+
370+
### Customize how to get the Mention Items
371+
You can customize how to get the mention items by overriding and changing the `getMentionsQuery` method in your commentable model.
372+
By default, the package gets mention items from all users in your database.
373+
For example, if you would only like to mention users who have commented on the current thread, you can do so by changing the method to return only those users.
374+
There is a handy method included in the default class to achieve this. Alternatively, you can go wild and mention fruits instead of users! The choice is within your freedom.
375+
376+
```php
377+
// e.g in your Post model or any other model that uses the HasComments trait
378+
use Coolsam\NestedComments\Traits\HasComments;
379+
380+
public function getMentionsQuery(string $query): Builder
381+
{
382+
return app(NestedComments::class)->getCurrentThreadUsersQuery($query, $this);
383+
}
384+
```
385+
386+
### Customize the Supported Emoji Reactions
387+
You can customize the supported emoji reactions by changing the `reactions` array in the config file.
388+
Decent defaults are provided, but you can change them to any emojis you prefer.
389+
390+
```php
391+
return [
392+
'👍',
393+
'❤️',
394+
'😂',
395+
'😮',
396+
'😢',
397+
'😡',
398+
];
399+
```
331400
## Testing
332401

333402
```bash

src/NestedComments.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FilamentTiptapEditor\Data\MentionItem;
88
use Illuminate\Contracts\Auth\Authenticatable;
99
use Illuminate\Contracts\View\Factory;
10+
use Illuminate\Database\Eloquent\Builder;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Foundation\Application;
1213
use Illuminate\Support\Facades\Auth;
@@ -100,7 +101,7 @@ public function getUserMentions(string $query): array
100101
})->toArray();
101102
}
102103

103-
public function getUserMentionsQuery(string $query): \Illuminate\Database\Eloquent\Builder
104+
public function getUserMentionsQuery(string $query): Builder
104105
{
105106
$userModel = config('nested-comments.models.user', config('auth.providers.users.model', 'App\\Models\\User'));
106107

@@ -109,7 +110,22 @@ public function getUserMentionsQuery(string $query): \Illuminate\Database\Eloque
109110
->orWhere('email', 'like', "%{$query}%");
110111
}
111112

112-
public function getCurrentThreadUsers(string $searchQuery, $commentable): mixed
113+
public function getCurrentThreadUsers(string $searchQuery, $commentable): array
114+
{
115+
return $this->getCurrentThreadUsersQuery($searchQuery, $commentable)
116+
->take(20)
117+
->get()
118+
->map(function ($user) {
119+
return new MentionItem(
120+
id: $user->getKey(),
121+
label: $this->getUserName($user),
122+
image: $this->getDefaultUserAvatar($user),
123+
roundedImage: true,
124+
);
125+
})->toArray();
126+
}
127+
128+
public function getCurrentThreadUsersQuery(string $searchQuery, $commentable): Builder
113129
{
114130
$userModel = config('nested-comments.models.user', config('auth.providers.users.model', 'App\\Models\\User'));
115131
$ids = [];
@@ -123,17 +139,7 @@ public function getCurrentThreadUsers(string $searchQuery, $commentable): mixed
123139
fn ($q) => $q
124140
->where('name', 'like', "%{$searchQuery}%")
125141
->orWhere('email', 'like', "%{$searchQuery}%")
126-
)
127-
->take(20)
128-
->get()
129-
->map(function ($user) {
130-
return new MentionItem(
131-
id: $user->getKey(),
132-
label: $this->getUserName($user),
133-
image: $this->getDefaultUserAvatar($user),
134-
roundedImage: true,
135-
);
136-
})->toArray();
142+
);
137143
}
138144

139145
public function renderCommentsComponent(Model $record): \Illuminate\Contracts\View\View | Application | Factory | View

src/NestedCommentsServiceProvider.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Spatie\LaravelPackageTools\Package;
2626
use Spatie\LaravelPackageTools\PackageServiceProvider;
2727

28+
use function Laravel\Prompts\confirm;
29+
2830
class NestedCommentsServiceProvider extends PackageServiceProvider
2931
{
3032
public static string $name = 'nested-comments';
@@ -44,14 +46,20 @@ public function configurePackage(Package $package): void
4446
$command
4547
->startWith(function (Command $command) {
4648
$command->comment('Publishing config file...');
47-
$forceConfig = $command->confirm(__('Do you want to override existing config file?'), false);
48-
Artisan::call('vendor:publish', [
49-
'--tag' => 'nested-comments-config',
50-
'--force' => $forceConfig,
51-
]);
52-
$command->info('Config file published successfully.');
53-
54-
$forceAssets = $command->confirm(__('Do you want to override existing assets with new assets?'), true);
49+
if (confirm(__('Do you want to publish and overwrite the config file? (The existing file will be backed up to .bak)'))) {
50+
// check if the config file exists and back it up by copying to .bak
51+
if (file_exists(config_path('nested-comments.php'))) {
52+
$command->info('Backing up existing config to .bak file');
53+
// copy the config file to .bak
54+
copy(config_path('nested-comments.php'), config_path('nested-comments.php.bak'));
55+
}
56+
$command->call('vendor:publish', [
57+
'--tag' => 'nested-comments-config',
58+
'--force' => true,
59+
]);
60+
}
61+
62+
$forceAssets = confirm(__('Do you want to override existing assets with new assets? (important if you are doing an upgrade)'), true);
5563
if ($forceAssets) {
5664
// Delete the existing assets in public/css/coolsam and public/js/coolsam
5765
$filesystem = app(Filesystem::class);
@@ -60,6 +68,7 @@ public function configurePackage(Package $package): void
6068
Artisan::call('filament:assets');
6169
}
6270
})
71+
->publishConfigFile()
6372
->publishAssets()
6473
->publishMigrations()
6574
->askToRunMigrations()

0 commit comments

Comments
 (0)