Skip to content

Commit 6a85bca

Browse files
authored
fix: fix setup and dummy in case meilisearch not activated (monicahq/chandler#185)
1 parent c5939e0 commit 6a85bca

File tree

15 files changed

+145
-80
lines changed

15 files changed

+145
-80
lines changed

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LOG_CHANNEL=stack
2525
# Drivers
2626
BROADCAST_DRIVER=log
2727
CACHE_DRIVER=file
28-
QUEUE_CONNECTION=database
28+
QUEUE_CONNECTION=sync
2929
SESSION_DRIVER=file
3030
SESSION_LIFETIME=120
3131

@@ -51,7 +51,7 @@ MAIL_REPLY_TO_NAME="${APP_NAME}"
5151
# Read config/scout.php for more information.
5252
# Note that you have to use either Meilisearch or Algolia to enable search in
5353
# Monica. Searching requires a queue to be configured.
54-
SCOUT_DRIVER=meilisearch
54+
SCOUT_DRIVER=database
5555
SCOUT_QUEUE=true
5656
MEILISEARCH_HOST=
5757
MEILISEARCH_KEY=

app/Console/Commands/SetupApplication.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,11 @@ class SetupApplication extends Command
2121
*/
2222
protected $description = 'Setup everything that is needed for Monica to work';
2323

24-
/**
25-
* Create a new command instance.
26-
*
27-
* @return void
28-
*/
29-
public function __construct()
30-
{
31-
parent::__construct();
32-
}
33-
3424
/**
3525
* Execute the console command.
3626
*
3727
* @return void
28+
* @codeCoverageIgnore
3829
*/
3930
public function handle(): void
4031
{
@@ -45,26 +36,28 @@ public function handle(): void
4536
$this->line('|');
4637
$this->line('-----------------------------');
4738

48-
$this->line('-> Creating indexes on Meilisearch. Make sure Meilisearch is running.');
49-
50-
$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
51-
$index = $client->index('contacts');
52-
$index->updateFilterableAttributes([
53-
'id',
54-
'vault_id',
55-
]);
56-
$index = $client->index('notes');
57-
$index->updateFilterableAttributes([
58-
'id',
59-
'vault_id',
60-
'contact_id',
61-
]);
62-
$index = $client->index('groups');
63-
$index->updateFilterableAttributes([
64-
'id',
65-
'vault_id',
66-
]);
67-
68-
$this->line('✓ Indexes created');
39+
if (config('scout.driver') === 'meilisearch' && ($host = config('scout.meilisearch.host')) !== '') {
40+
$this->line('-> Creating indexes on Meilisearch. Make sure Meilisearch is running.');
41+
42+
$client = new Client($host, config('scout.meilisearch.key'));
43+
$index = $client->index('contacts');
44+
$index->updateFilterableAttributes([
45+
'id',
46+
'vault_id',
47+
]);
48+
$index = $client->index('notes');
49+
$index->updateFilterableAttributes([
50+
'id',
51+
'vault_id',
52+
'contact_id',
53+
]);
54+
$index = $client->index('groups');
55+
$index->updateFilterableAttributes([
56+
'id',
57+
'vault_id',
58+
]);
59+
60+
$this->line('✓ Indexes created');
61+
}
6962
}
7063
}

app/Console/Commands/SetupDummyAccount.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use App\Exceptions\EntryAlreadyExistException;
1212
use App\Models\Contact;
1313
use App\Models\ContactImportantDate;
14+
use App\Models\Group;
15+
use App\Models\Note;
1416
use App\Models\User;
1517
use App\Models\Vault;
1618
use App\Settings\CreateAccount\Services\CreateAccount;
@@ -41,16 +43,6 @@ class SetupDummyAccount extends Command
4143
*/
4244
protected $description = 'Prepare an account with fake data so users can play with it';
4345

44-
/**
45-
* Create a new command instance.
46-
*
47-
* @return void
48-
*/
49-
public function __construct()
50-
{
51-
parent::__construct();
52-
}
53-
5446
/**
5547
* Execute the console command.
5648
*
@@ -81,9 +73,10 @@ private function start(): void
8173

8274
private function wipeAndMigrateDB(): void
8375
{
84-
shell_exec('curl -X DELETE "'.config('scout.meilisearch.host').'/indexes/notes"');
85-
shell_exec('curl -X DELETE "'.config('scout.meilisearch.host').'/indexes/contacts"');
86-
shell_exec('curl -X DELETE "'.config('scout.meilisearch.host').'/indexes/groups"');
76+
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Note::class]);
77+
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Contact::class]);
78+
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Group::class]);
79+
8780
$this->artisan('☐ Reset search engine', 'monica:setup');
8881
$this->artisan('☐ Migration of the database', 'migrate:fresh');
8982
$this->artisan('☐ Symlink the storage folder', 'storage:link');
@@ -124,7 +117,6 @@ private function createFirstUsers(): void
124117
]);
125118
$this->firstUser->email_verified_at = Carbon::now();
126119
$this->firstUser->save();
127-
sleep(5);
128120
}
129121

130122
private function createVaults(): void

app/Helpers/ScoutHelper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Helpers;
4+
5+
class ScoutHelper
6+
{
7+
/**
8+
* When updating a model, this method determines if we should update the search index.
9+
*
10+
* @return bool
11+
* @codeCoverageIgnore
12+
*/
13+
public static function activated()
14+
{
15+
switch (config('scout.driver')) {
16+
case 'algolia':
17+
return config('scout.algolia.id') !== '';
18+
case 'meilisearch':
19+
return config('scout.meilisearch.host') !== '';
20+
case 'database':
21+
case 'collection':
22+
return true;
23+
default:
24+
return false;
25+
}
26+
}
27+
}

app/Listeners/LocaleUpdatedListener.php

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

app/Models/Contact.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
use App\Helpers\AvatarHelper;
66
use App\Helpers\ImportantDateHelper;
77
use App\Helpers\NameHelper;
8+
use App\Helpers\ScoutHelper;
89
use Illuminate\Database\Eloquent\Casts\Attribute;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1213
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1314
use Illuminate\Database\Eloquent\Relations\HasMany;
1415
use Illuminate\Support\Facades\Auth;
16+
use Laravel\Scout\Attributes\SearchUsingFullText;
17+
use Laravel\Scout\Attributes\SearchUsingPrefix;
1518
use Laravel\Scout\Searchable;
1619

1720
class Contact extends Model
@@ -64,7 +67,10 @@ class Contact extends Model
6467
* Get the indexable data array for the model.
6568
*
6669
* @return array
70+
* @codeCoverageIgnore
6771
*/
72+
#[SearchUsingPrefix(['id', 'vault_id'])]
73+
#[SearchUsingFullText(['first_name', 'last_name', 'middle_name', 'nickname', 'maiden_name'])]
6874
public function toSearchableArray(): array
6975
{
7076
return [
@@ -75,10 +81,6 @@ public function toSearchableArray(): array
7581
'middle_name' => $this->middle_name,
7682
'nickname' => $this->nickname,
7783
'maiden_name' => $this->maiden_name,
78-
'url' => route('contact.show', [
79-
'vault' => $this->vault_id,
80-
'contact' => $this->id,
81-
]),
8284
];
8385
}
8486

@@ -106,6 +108,16 @@ protected static function boot(): void
106108
});
107109
}
108110

111+
/**
112+
* When updating a model, this method determines if we should update the search index.
113+
*
114+
* @return bool
115+
*/
116+
public function searchIndexShouldBeUpdated()
117+
{
118+
return ScoutHelper::activated();
119+
}
120+
109121
/**
110122
* Get the vault associated with the contact.
111123
*

app/Models/Group.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace App\Models;
44

5+
use App\Helpers\ScoutHelper;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
910
use Illuminate\Database\Eloquent\Relations\MorphOne;
11+
use Laravel\Scout\Attributes\SearchUsingFullText;
12+
use Laravel\Scout\Attributes\SearchUsingPrefix;
1013
use Laravel\Scout\Searchable;
1114

1215
class Group extends Model
@@ -31,7 +34,10 @@ class Group extends Model
3134
* Get the indexable data array for the model.
3235
*
3336
* @return array
37+
* @codeCoverageIgnore
3438
*/
39+
#[SearchUsingPrefix(['id', 'vault_id'])]
40+
#[SearchUsingFullText(['name'])]
3541
public function toSearchableArray(): array
3642
{
3743
return [
@@ -41,6 +47,16 @@ public function toSearchableArray(): array
4147
];
4248
}
4349

50+
/**
51+
* When updating a model, this method determines if we should update the search index.
52+
*
53+
* @return bool
54+
*/
55+
public function searchIndexShouldBeUpdated()
56+
{
57+
return ScoutHelper::activated();
58+
}
59+
4460
/**
4561
* Get the vault associated with the group.
4662
*

app/Models/Loan.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Helpers\ScoutHelper;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -57,6 +58,16 @@ class Loan extends Model
5758
'settled' => 'boolean',
5859
];
5960

61+
/**
62+
* When updating a model, this method determines if we should update the search index.
63+
*
64+
* @return bool
65+
*/
66+
public function searchIndexShouldBeUpdated()
67+
{
68+
return ScoutHelper::activated();
69+
}
70+
6071
/**
6172
* Get the vault associated with the loan.
6273
*

app/Models/Note.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace App\Models;
44

5+
use App\Helpers\ScoutHelper;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89
use Illuminate\Database\Eloquent\Relations\MorphOne;
10+
use Laravel\Scout\Attributes\SearchUsingFullText;
11+
use Laravel\Scout\Attributes\SearchUsingPrefix;
912
use Laravel\Scout\Searchable;
1013

1114
class Note extends Model
@@ -20,6 +23,7 @@ class Note extends Model
2023
*/
2124
protected $fillable = [
2225
'contact_id',
26+
'vault_id',
2327
'author_id',
2428
'emotion_id',
2529
'title',
@@ -30,18 +34,29 @@ class Note extends Model
3034
* Get the indexable data array for the model.
3135
*
3236
* @return array
37+
* @codeCoverageIgnore
3338
*/
39+
#[SearchUsingPrefix(['id', 'vault_id'])]
40+
#[SearchUsingFullText(['title', 'body'])]
3441
public function toSearchableArray(): array
3542
{
36-
$array = [
43+
return [
3744
'id' => $this->id,
38-
'vault_id' => $this->contact->vault_id,
39-
'contact_id' => $this->contact->id,
45+
'vault_id' => $this->vault_id,
46+
'contact_id' => $this->contact_id,
4047
'title' => $this->title,
4148
'body' => $this->body,
4249
];
50+
}
4351

44-
return $array;
52+
/**
53+
* When updating a model, this method determines if we should update the search index.
54+
*
55+
* @return bool
56+
*/
57+
public function searchIndexShouldBeUpdated()
58+
{
59+
return ScoutHelper::activated();
4560
}
4661

4762
/**

app/Providers/EventServiceProvider.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
use App\Contact\ManageDocuments\Events\FileDeleted;
66
use App\Contact\ManageDocuments\Listeners\DeleteFileInStorage;
7-
use App\Listeners\LocaleUpdatedListener;
87
use Illuminate\Auth\Events\Registered;
98
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
10-
use Illuminate\Foundation\Events\LocaleUpdated;
119
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
12-
use Illuminate\Support\Facades\Event;
1310

1411
class EventServiceProvider extends ServiceProvider
1512
{
@@ -22,9 +19,6 @@ class EventServiceProvider extends ServiceProvider
2219
Registered::class => [
2320
SendEmailVerificationNotification::class,
2421
],
25-
LocaleUpdated::class => [
26-
LocaleUpdatedListener::class,
27-
],
2822
FileDeleted::class => [
2923
DeleteFileInStorage::class,
3024
],

0 commit comments

Comments
 (0)