Skip to content

Commit

Permalink
fix: fix setup and dummy in case meilisearch not activated (monicahq/…
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Aug 5, 2022
1 parent c5939e0 commit 6a85bca
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .env.example
Expand Up @@ -25,7 +25,7 @@ LOG_CHANNEL=stack
# Drivers
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=database
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

Expand All @@ -51,7 +51,7 @@ MAIL_REPLY_TO_NAME="${APP_NAME}"
# Read config/scout.php for more information.
# Note that you have to use either Meilisearch or Algolia to enable search in
# Monica. Searching requires a queue to be configured.
SCOUT_DRIVER=meilisearch
SCOUT_DRIVER=database
SCOUT_QUEUE=true
MEILISEARCH_HOST=
MEILISEARCH_KEY=
Expand Down
55 changes: 24 additions & 31 deletions app/Console/Commands/SetupApplication.php
Expand Up @@ -21,20 +21,11 @@ class SetupApplication extends Command
*/
protected $description = 'Setup everything that is needed for Monica to work';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
* @codeCoverageIgnore
*/
public function handle(): void
{
Expand All @@ -45,26 +36,28 @@ public function handle(): void
$this->line('|');
$this->line('-----------------------------');

$this->line('-> Creating indexes on Meilisearch. Make sure Meilisearch is running.');

$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
$index = $client->index('contacts');
$index->updateFilterableAttributes([
'id',
'vault_id',
]);
$index = $client->index('notes');
$index->updateFilterableAttributes([
'id',
'vault_id',
'contact_id',
]);
$index = $client->index('groups');
$index->updateFilterableAttributes([
'id',
'vault_id',
]);

$this->line('✓ Indexes created');
if (config('scout.driver') === 'meilisearch' && ($host = config('scout.meilisearch.host')) !== '') {
$this->line('-> Creating indexes on Meilisearch. Make sure Meilisearch is running.');

$client = new Client($host, config('scout.meilisearch.key'));
$index = $client->index('contacts');
$index->updateFilterableAttributes([
'id',
'vault_id',
]);
$index = $client->index('notes');
$index->updateFilterableAttributes([
'id',
'vault_id',
'contact_id',
]);
$index = $client->index('groups');
$index->updateFilterableAttributes([
'id',
'vault_id',
]);

$this->line('✓ Indexes created');
}
}
}
20 changes: 6 additions & 14 deletions app/Console/Commands/SetupDummyAccount.php
Expand Up @@ -11,6 +11,8 @@
use App\Exceptions\EntryAlreadyExistException;
use App\Models\Contact;
use App\Models\ContactImportantDate;
use App\Models\Group;
use App\Models\Note;
use App\Models\User;
use App\Models\Vault;
use App\Settings\CreateAccount\Services\CreateAccount;
Expand Down Expand Up @@ -41,16 +43,6 @@ class SetupDummyAccount extends Command
*/
protected $description = 'Prepare an account with fake data so users can play with it';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
Expand Down Expand Up @@ -81,9 +73,10 @@ private function start(): void

private function wipeAndMigrateDB(): void
{
shell_exec('curl -X DELETE "'.config('scout.meilisearch.host').'/indexes/notes"');
shell_exec('curl -X DELETE "'.config('scout.meilisearch.host').'/indexes/contacts"');
shell_exec('curl -X DELETE "'.config('scout.meilisearch.host').'/indexes/groups"');
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Note::class]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Contact::class]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Group::class]);

$this->artisan('☐ Reset search engine', 'monica:setup');
$this->artisan('☐ Migration of the database', 'migrate:fresh');
$this->artisan('☐ Symlink the storage folder', 'storage:link');
Expand Down Expand Up @@ -124,7 +117,6 @@ private function createFirstUsers(): void
]);
$this->firstUser->email_verified_at = Carbon::now();
$this->firstUser->save();
sleep(5);
}

private function createVaults(): void
Expand Down
27 changes: 27 additions & 0 deletions app/Helpers/ScoutHelper.php
@@ -0,0 +1,27 @@
<?php

namespace App\Helpers;

class ScoutHelper
{
/**
* When updating a model, this method determines if we should update the search index.
*
* @return bool
* @codeCoverageIgnore
*/
public static function activated()
{
switch (config('scout.driver')) {
case 'algolia':
return config('scout.algolia.id') !== '';
case 'meilisearch':
return config('scout.meilisearch.host') !== '';
case 'database':
case 'collection':
return true;
default:
return false;
}
}
}
19 changes: 0 additions & 19 deletions app/Listeners/LocaleUpdatedListener.php

This file was deleted.

20 changes: 16 additions & 4 deletions app/Models/Contact.php
Expand Up @@ -5,13 +5,16 @@
use App\Helpers\AvatarHelper;
use App\Helpers\ImportantDateHelper;
use App\Helpers\NameHelper;
use App\Helpers\ScoutHelper;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Auth;
use Laravel\Scout\Attributes\SearchUsingFullText;
use Laravel\Scout\Attributes\SearchUsingPrefix;
use Laravel\Scout\Searchable;

class Contact extends Model
Expand Down Expand Up @@ -64,7 +67,10 @@ class Contact extends Model
* Get the indexable data array for the model.
*
* @return array
* @codeCoverageIgnore
*/
#[SearchUsingPrefix(['id', 'vault_id'])]
#[SearchUsingFullText(['first_name', 'last_name', 'middle_name', 'nickname', 'maiden_name'])]
public function toSearchableArray(): array
{
return [
Expand All @@ -75,10 +81,6 @@ public function toSearchableArray(): array
'middle_name' => $this->middle_name,
'nickname' => $this->nickname,
'maiden_name' => $this->maiden_name,
'url' => route('contact.show', [
'vault' => $this->vault_id,
'contact' => $this->id,
]),
];
}

Expand Down Expand Up @@ -106,6 +108,16 @@ protected static function boot(): void
});
}

/**
* When updating a model, this method determines if we should update the search index.
*
* @return bool
*/
public function searchIndexShouldBeUpdated()
{
return ScoutHelper::activated();
}

/**
* Get the vault associated with the contact.
*
Expand Down
16 changes: 16 additions & 0 deletions app/Models/Group.php
Expand Up @@ -2,11 +2,14 @@

namespace App\Models;

use App\Helpers\ScoutHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Laravel\Scout\Attributes\SearchUsingFullText;
use Laravel\Scout\Attributes\SearchUsingPrefix;
use Laravel\Scout\Searchable;

class Group extends Model
Expand All @@ -31,7 +34,10 @@ class Group extends Model
* Get the indexable data array for the model.
*
* @return array
* @codeCoverageIgnore
*/
#[SearchUsingPrefix(['id', 'vault_id'])]
#[SearchUsingFullText(['name'])]
public function toSearchableArray(): array
{
return [
Expand All @@ -41,6 +47,16 @@ public function toSearchableArray(): array
];
}

/**
* When updating a model, this method determines if we should update the search index.
*
* @return bool
*/
public function searchIndexShouldBeUpdated()
{
return ScoutHelper::activated();
}

/**
* Get the vault associated with the group.
*
Expand Down
11 changes: 11 additions & 0 deletions app/Models/Loan.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Helpers\ScoutHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -57,6 +58,16 @@ class Loan extends Model
'settled' => 'boolean',
];

/**
* When updating a model, this method determines if we should update the search index.
*
* @return bool
*/
public function searchIndexShouldBeUpdated()
{
return ScoutHelper::activated();
}

/**
* Get the vault associated with the loan.
*
Expand Down
23 changes: 19 additions & 4 deletions app/Models/Note.php
Expand Up @@ -2,10 +2,13 @@

namespace App\Models;

use App\Helpers\ScoutHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Laravel\Scout\Attributes\SearchUsingFullText;
use Laravel\Scout\Attributes\SearchUsingPrefix;
use Laravel\Scout\Searchable;

class Note extends Model
Expand All @@ -20,6 +23,7 @@ class Note extends Model
*/
protected $fillable = [
'contact_id',
'vault_id',
'author_id',
'emotion_id',
'title',
Expand All @@ -30,18 +34,29 @@ class Note extends Model
* Get the indexable data array for the model.
*
* @return array
* @codeCoverageIgnore
*/
#[SearchUsingPrefix(['id', 'vault_id'])]
#[SearchUsingFullText(['title', 'body'])]
public function toSearchableArray(): array
{
$array = [
return [
'id' => $this->id,
'vault_id' => $this->contact->vault_id,
'contact_id' => $this->contact->id,
'vault_id' => $this->vault_id,
'contact_id' => $this->contact_id,
'title' => $this->title,
'body' => $this->body,
];
}

return $array;
/**
* When updating a model, this method determines if we should update the search index.
*
* @return bool
*/
public function searchIndexShouldBeUpdated()
{
return ScoutHelper::activated();
}

/**
Expand Down
6 changes: 0 additions & 6 deletions app/Providers/EventServiceProvider.php
Expand Up @@ -4,12 +4,9 @@

use App\Contact\ManageDocuments\Events\FileDeleted;
use App\Contact\ManageDocuments\Listeners\DeleteFileInStorage;
use App\Listeners\LocaleUpdatedListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
Expand All @@ -22,9 +19,6 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
LocaleUpdated::class => [
LocaleUpdatedListener::class,
],
FileDeleted::class => [
DeleteFileInStorage::class,
],
Expand Down

0 comments on commit 6a85bca

Please sign in to comment.