1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-01-17 13:18:21 +01:00

Migrate note private status to visibility status (#165)

This commit is contained in:
Kovah 2022-06-24 00:03:23 +02:00
parent dba872faad
commit a92b513cc0
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
13 changed files with 116 additions and 33 deletions

View File

@ -28,7 +28,7 @@ class NoteController extends Controller
abort(403); abort(403);
} }
$data = $request->except(['_token']); $data = $request->validated();
NoteRepository::create($data); NoteRepository::create($data);
flash(trans('note.added_successfully'), 'success'); flash(trans('note.added_successfully'), 'success');

View File

@ -30,9 +30,9 @@ class NoteStoreRequest extends FormRequest
'note' => [ 'note' => [
'required', 'required',
], ],
'is_private' => [ 'visibility' => [
'sometimes', 'sometimes',
'boolean', 'integer',
], ],
]; ];
} }

View File

@ -27,9 +27,9 @@ class NoteUpdateRequest extends FormRequest
'note' => [ 'note' => [
'required', 'required',
], ],
'is_private' => [ 'visibility' => [
'sometimes', 'sometimes',
'boolean', 'integer',
], ],
]; ];
} }

View File

@ -30,9 +30,10 @@ use OwenIt\Auditing\Contracts\Auditable;
* @property string|null $deleted_at * @property string|null $deleted_at
* @property-read Collection|Link[] $links * @property-read Collection|Link[] $links
* @property-read User $user * @property-read User $user
* @method static Builder|Tag byUser(int $user_id = null) * @method static Builder|LinkList byUser(int $user_id = null)
* @method static Builder|Tag privateOnly() * @method static Builder|LinkList privateOnly()
* @method static Builder|Tag publicOnly() * @method static Builder|LinkList internalOnly()
* @method static Builder|LinkList publicOnly()
*/ */
class LinkList extends Model implements Auditable class LinkList extends Model implements Auditable
{ {

View File

@ -2,12 +2,12 @@
namespace App\Models; namespace App\Models;
use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str; use Illuminate\Support\Str;
/** /**
@ -18,30 +18,34 @@ use Illuminate\Support\Str;
* @property int $user_id * @property int $user_id
* @property int $link_id * @property int $link_id
* @property string $note * @property string $note
* @property int $is_private * @property int $visibility
* @property Carbon|null $created_at * @property Carbon|null $created_at
* @property Carbon|null $updated_at * @property Carbon|null $updated_at
* @property string|null $deleted_at * @property string|null $deleted_at
* @property-read Link $link * @property-read Link $link
* @property-read User $user * @property-read User $user
* @method static Builder|Link byUser($user_id = null) * @method static Builder|Note byUser($user_id = null)
* @method static Builder|Note privateOnly()
* @method static Builder|Note internalOnly()
* @method static Builder|Note publicOnly()
*/ */
class Note extends Model class Note extends Model
{ {
use SoftDeletes;
use HasFactory; use HasFactory;
use ScopesVisibility;
use SoftDeletes;
public $fillable = [ public $fillable = [
'user_id', 'user_id',
'link_id', 'link_id',
'note', 'note',
'is_private', 'visibility',
]; ];
protected $casts = [ protected $casts = [
'user_id' => 'integer', 'user_id' => 'integer',
'link_id' => 'integer', 'link_id' => 'integer',
'is_private' => 'boolean', 'visibility' => 'integer',
]; ];
/* /*

View File

@ -26,8 +26,6 @@ class NoteRepository
*/ */
public static function update(Note $note, array $data): Note public static function update(Note $note, array $data): Note
{ {
$data['is_private'] ??= false;
$note->update($data); $note->update($data);
return $note; return $note;

View File

@ -8,7 +8,7 @@ use Illuminate\View\Component;
class VisibilityToggle extends Component class VisibilityToggle extends Component
{ {
public function __construct(private ?int $existingValue = null) public function __construct(private ?int $existingValue = null, public string $inputClasses = '', public string $labelClasses = '')
{ {
} }

View File

@ -2,6 +2,7 @@
use App\Enums\ModelAttribute; use App\Enums\ModelAttribute;
use App\Models\LinkList; use App\Models\LinkList;
use App\Models\Note;
use App\Models\Tag; use App\Models\Tag;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
@ -18,6 +19,7 @@ class MigrateUserData extends Migration
$this->migrateLinkVisibility(); $this->migrateLinkVisibility();
$this->migrateListVisibility(); $this->migrateListVisibility();
$this->migrateTagVisibility(); $this->migrateTagVisibility();
$this->migrateNoteVisibility();
} }
protected function migrateLinkVisibility(): void protected function migrateLinkVisibility(): void
@ -81,4 +83,24 @@ class MigrateUserData extends Migration
$table->dropColumn(['is_private']); $table->dropColumn(['is_private']);
}); });
} }
protected function migrateNoteVisibility(): void
{
Schema::table('notes', function (Blueprint $table) {
$table->integer('visibility')->default(ModelAttribute::VISIBILITY_PRIVATE)->after('is_private');
});
Note::withTrashed()->get()->each(function ($note) {
$note->visibility = match ((bool)$note->is_private) {
true => ModelAttribute::VISIBILITY_PRIVATE,
false => $this->guestAccessEnabled
? ModelAttribute::VISIBILITY_PUBLIC : ModelAttribute::VISIBILITY_INTERNAL,
};
$note->saveQuietly();
});
Schema::table('notes', function (Blueprint $table) {
$table->dropColumn(['is_private']);
});
}
} }

View File

@ -9,6 +9,8 @@ return [
'update' => 'Update Note', 'update' => 'Update Note',
'delete' => 'Delete Note', 'delete' => 'Delete Note',
'public' => 'Public Note',
'internal' => 'Internal Note',
'private' => 'Private Note', 'private' => 'Private Note',
'note_content' => 'Note Content', 'note_content' => 'Note Content',

View File

@ -1,6 +1,6 @@
<div {{ $attributes }}> <div {{ $attributes }}>
<label class="form-label" for="visibility">@lang('linkace.visibility')</label> <label class="form-label {{ $labelClasses }}" for="visibility">@lang('linkace.visibility')</label>
<select id="visibility" name="visibility" class="form-select{{ $errors->has('visibility') ? ' is-invalid' : '' }}"> <select id="visibility" name="visibility" class="form-select {{ $inputClasses }}{{ $errors->has('visibility') ? ' is-invalid' : '' }}">
<option value="{{ $public }}" {{ $publicSelected ? 'selected' : '' }}> <option value="{{ $public }}" {{ $publicSelected ? 'selected' : '' }}>
@lang('attributes.visibility.' . $public) @lang('attributes.visibility.' . $public)
</option> </option>

View File

@ -27,13 +27,9 @@
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
<div class="form-check ms-auto me-3"> <x-forms.visibility-toggle :existing-value="$note->visibility"
<input class="form-check-input" type="checkbox" id="is_private" name="is_private" value="1" class="ms-auto me-3 d-flex align-items-center" input-classes="form-select-sm"
@if($note->is_private) checked @endif> label-classes="mb-0 me-2 small"/>
<label class="form-check-label" for="is_private">
<small>@lang('note.private')</small>
</label>
</div>
<button type="submit" class="btn btn-sm btn-primary"> <button type="submit" class="btn btn-sm btn-primary">
<x-icon.save class="me-2"/> @lang('note.edit') <x-icon.save class="me-2"/> @lang('note.edit')

View File

@ -23,13 +23,8 @@
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
<div class="form-check ms-auto me-3"> <x-forms.visibility-toggle class="ms-auto me-3 d-flex align-items-center" input-classes="form-select-sm"
<input class="form-check-input" type="checkbox" id="is_private" name="is_private" value="1" label-classes="mb-0 me-2 small"/>
@if($link->is_private || usersettings('notes_private_default')) checked @endif>
<label class="form-check-label" for="is_private">
<small>@lang('note.private')</small>
</label>
</div>
<button type="submit" class="btn btn-sm btn-primary"> <button type="submit" class="btn btn-sm btn-primary">
<x-icon.save class="me-2"/> @lang('note.add') <x-icon.save class="me-2"/> @lang('note.add')

View File

@ -4,6 +4,7 @@ namespace Tests\Migrations;
use App\Models\Link; use App\Models\Link;
use App\Models\LinkList; use App\Models\LinkList;
use App\Models\Note;
use App\Models\Tag; use App\Models\Tag;
use App\Settings\SystemSettings; use App\Settings\SystemSettings;
use Tests\TestCase; use Tests\TestCase;
@ -195,4 +196,68 @@ class UserDataMigrationTest extends TestCase
'visibility' => 1, // is public 'visibility' => 1, // is public
]); ]);
} }
public function testNoteVisibilityMigration(): void
{
$this->migrateUpTo('2022_06_23_112431_migrate_user_data.php');
Note::unguard();
Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A private note',
'is_private' => true,
]);
Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A public note',
'is_private' => false,
]);
$this->artisan('migrate');
$this->assertDatabaseHas('notes', [
'note' => 'A private note',
'visibility' => 3, // is private
]);
$this->assertDatabaseHas('notes', [
'note' => 'A public note',
'visibility' => 2, // is internal
]);
}
public function testNoteVisibilityMigrationWithEnabledGuestMode(): void
{
$this->migrateUpTo('2022_06_23_112431_migrate_user_data.php');
SystemSettings::fake(['guest_access_enabled' => true]);
Note::unguard();
Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A private note',
'is_private' => true,
]);
Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A public note',
'is_private' => false,
]);
$this->artisan('migrate');
$this->assertDatabaseHas('notes', [
'note' => 'A private note',
'visibility' => 3, // is private
]);
$this->assertDatabaseHas('notes', [
'note' => 'A public note',
'visibility' => 1, // is public
]);
}
} }