From 7a5df410da4f81b81d1727a59eb43759ba6cac30 Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 21:35:19 +0100 Subject: [PATCH 1/8] Initial commit --- app/Concerns/HasEditLog.php | 44 +++++++++++++++++++ .../Controllers/Auth/GithubController.php | 3 +- app/Models/Article.php | 2 + app/Models/EditLog.php | 35 +++++++++++++++ app/Models/Reply.php | 2 + app/Models/Thread.php | 2 + ...21_11_05_194336_create_edit_logs_table.php | 28 ++++++++++++ resources/views/articles/show.blade.php | 8 ++++ 8 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 app/Concerns/HasEditLog.php create mode 100644 app/Models/EditLog.php create mode 100644 database/migrations/2021_11_05_194336_create_edit_logs_table.php diff --git a/app/Concerns/HasEditLog.php b/app/Concerns/HasEditLog.php new file mode 100644 index 000000000..57326e1e7 --- /dev/null +++ b/app/Concerns/HasEditLog.php @@ -0,0 +1,44 @@ + auth()->id(), + 'editable_id' => $model->id, + 'editable_type' => $model::class, + 'edited_at' => now(), + ]); + + $cacheKey = sprintf('%s-%s', Str::slug($model::class), $model->id); + if(Cache::has($cacheKey)) { + Cache::forget($cacheKey); + } + }); + } + + public function editLogs(): MorphMany + { + return $this->morphMany(EditLog::class, 'editable'); + } + + public function getLatestEditLogAttribute() + { + $cacheKey = sprintf('%s-%s', Str::slug($this::class), $this->id); + echo $cacheKey; + + //return Cache::rememberForever($cacheKey, function() { + return $this->editLogs()->latest('edited_at')->first(); + //}); + } +} diff --git a/app/Http/Controllers/Auth/GithubController.php b/app/Http/Controllers/Auth/GithubController.php index 9ea29e49c..2eeb39084 100644 --- a/app/Http/Controllers/Auth/GithubController.php +++ b/app/Http/Controllers/Auth/GithubController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; +use App\Http\Requests\Request; use App\Jobs\UpdateProfile; use App\Models\User; use App\Social\GithubUser; @@ -18,7 +19,7 @@ class GithubController extends Controller /** * Redirect the user to the GitHub authentication page. */ - public function redirectToProvider() + public function redirectToProvider(Request $request) { return Socialite::driver('github')->redirect(); } diff --git a/app/Models/Article.php b/app/Models/Article.php index ef0f41851..3a21e7644 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Concerns\HasAuthor; +use App\Concerns\HasEditLog; use App\Concerns\HasLikes; use App\Concerns\HasSlug; use App\Concerns\HasTags; @@ -19,6 +20,7 @@ final class Article extends Model { use HasFactory; use HasAuthor; + use HasEditLog; use HasSlug; use HasLikes; use HasTimestamps; diff --git a/app/Models/EditLog.php b/app/Models/EditLog.php new file mode 100644 index 000000000..f217210a1 --- /dev/null +++ b/app/Models/EditLog.php @@ -0,0 +1,35 @@ +morphTo(); + } +} diff --git a/app/Models/Reply.php b/app/Models/Reply.php index 4dacf3f9c..ee7c3022d 100644 --- a/app/Models/Reply.php +++ b/app/Models/Reply.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Concerns\HasAuthor; +use App\Concerns\HasEditLog; use App\Concerns\HasLikes; use App\Concerns\HasTimestamps; use Illuminate\Database\Eloquent\Builder; @@ -16,6 +17,7 @@ final class Reply extends Model { use HasFactory; use HasAuthor; + use HasEditLog; use HasLikes; use HasTimestamps; diff --git a/app/Models/Thread.php b/app/Models/Thread.php index 44fa510bb..2babc5f77 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Concerns\HasAuthor; +use App\Concerns\HasEditLog; use App\Concerns\HasLikes; use App\Concerns\HasSlug; use App\Concerns\HasTags; @@ -30,6 +31,7 @@ final class Thread extends Model implements ReplyAble, SubscriptionAble, Feedabl { use HasFactory; use HasAuthor; + use HasEditLog; use HasLikes; use HasSlug; use HasTags; diff --git a/database/migrations/2021_11_05_194336_create_edit_logs_table.php b/database/migrations/2021_11_05_194336_create_edit_logs_table.php new file mode 100644 index 000000000..cb85206d4 --- /dev/null +++ b/database/migrations/2021_11_05_194336_create_edit_logs_table.php @@ -0,0 +1,28 @@ +id(); + + $table->foreignIdFor(User::class, 'author_id'); + + $table->unsignedBigInteger('editable_id'); + $table->string('editable_type'); + + $table->timestamp('edited_at'); + }); + } +} diff --git a/resources/views/articles/show.blade.php b/resources/views/articles/show.blade.php index b1ae87cbd..d1acf1042 100644 --- a/resources/views/articles/show.blade.php +++ b/resources/views/articles/show.blade.php @@ -84,6 +84,14 @@ class="prose prose-lg text-gray-800 prose-lio" {!! $article->body() !!} + @if($article->latestEditLog) +
+ Last edit by + {{ $article->latestEditLog->author()->name() }} + on {{ $article->latestEditLog->edited_at->format('j M, Y') }} +
+ @endif +
From c237a48e7ad47c5096e88b7f7646eb27fa164027 Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 22:29:50 +0100 Subject: [PATCH 2/8] Add last edit by to articles, replies and threads --- app/Concerns/HasEditLog.php | 17 +++++++++-------- app/Models/{EditLog.php => Edit.php} | 2 +- ...=> 2021_11_05_194336_create_edits_table.php} | 4 ++-- resources/views/articles/show.blade.php | 8 ++++---- .../views/components/threads/reply.blade.php | 8 ++++++++ .../views/components/threads/thread.blade.php | 8 ++++++++ 6 files changed, 32 insertions(+), 15 deletions(-) rename app/Models/{EditLog.php => Edit.php} (95%) rename database/migrations/{2021_11_05_194336_create_edit_logs_table.php => 2021_11_05_194336_create_edits_table.php} (82%) diff --git a/app/Concerns/HasEditLog.php b/app/Concerns/HasEditLog.php index 57326e1e7..0747f3dfb 100644 --- a/app/Concerns/HasEditLog.php +++ b/app/Concerns/HasEditLog.php @@ -2,10 +2,12 @@ namespace App\Concerns; -use App\Models\EditLog; +use App\Models\Edit; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; +use function constant; +use function json_encode; use function sprintf; trait HasEditLog @@ -13,10 +15,10 @@ trait HasEditLog public static function bootHasEditLog() { static::updating(function($model) { - EditLog::create([ + Edit::create([ 'author_id' => auth()->id(), 'editable_id' => $model->id, - 'editable_type' => $model::class, + 'editable_type' => constant($model::class . '::TABLE') ?? $model::class, 'edited_at' => now(), ]); @@ -27,18 +29,17 @@ public static function bootHasEditLog() }); } - public function editLogs(): MorphMany + public function edits(): MorphMany { - return $this->morphMany(EditLog::class, 'editable'); + return $this->morphMany(Edit::class, 'editable'); } - public function getLatestEditLogAttribute() + public function getLatestEditAttribute() { $cacheKey = sprintf('%s-%s', Str::slug($this::class), $this->id); - echo $cacheKey; //return Cache::rememberForever($cacheKey, function() { - return $this->editLogs()->latest('edited_at')->first(); + return $this->edits()->latest('edited_at')->first(); //}); } } diff --git a/app/Models/EditLog.php b/app/Models/Edit.php similarity index 95% rename from app/Models/EditLog.php rename to app/Models/Edit.php index f217210a1..4227edbfa 100644 --- a/app/Models/EditLog.php +++ b/app/Models/Edit.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class EditLog extends Model +class Edit extends Model { use HasAuthor; use HasFactory; diff --git a/database/migrations/2021_11_05_194336_create_edit_logs_table.php b/database/migrations/2021_11_05_194336_create_edits_table.php similarity index 82% rename from database/migrations/2021_11_05_194336_create_edit_logs_table.php rename to database/migrations/2021_11_05_194336_create_edits_table.php index cb85206d4..8ae7ceb79 100644 --- a/database/migrations/2021_11_05_194336_create_edit_logs_table.php +++ b/database/migrations/2021_11_05_194336_create_edits_table.php @@ -5,7 +5,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateEditLogsTable extends Migration +class CreateEditsTable extends Migration { /** * Run the migrations. @@ -14,7 +14,7 @@ class CreateEditLogsTable extends Migration */ public function up() { - Schema::create('edit_logs', function (Blueprint $table) { + Schema::create('edits', function (Blueprint $table) { $table->id(); $table->foreignIdFor(User::class, 'author_id'); diff --git a/resources/views/articles/show.blade.php b/resources/views/articles/show.blade.php index d1acf1042..aeae071e9 100644 --- a/resources/views/articles/show.blade.php +++ b/resources/views/articles/show.blade.php @@ -84,11 +84,11 @@ class="prose prose-lg text-gray-800 prose-lio" {!! $article->body() !!}
- @if($article->latestEditLog) -
+ @if($article->latestEdit) +
Last edit by - {{ $article->latestEditLog->author()->name() }} - on {{ $article->latestEditLog->edited_at->format('j M, Y') }} + {{ $article->latestEdit->author()->name() }} + on {{ $article->latestEdit->edited_at->format('j M, Y') }}.
@endif diff --git a/resources/views/components/threads/reply.blade.php b/resources/views/components/threads/reply.blade.php index ca65a30d7..b5c81d9b5 100644 --- a/resources/views/components/threads/reply.blade.php +++ b/resources/views/components/threads/reply.blade.php @@ -31,6 +31,14 @@ class="prose prose-lio max-w-none p-6 break-words" >
+ @if($reply->latestEdit) +
+ Last edit by + {{ $reply->latestEdit->author()->name() }} + on {{ $reply->latestEdit->edited_at->format('j M, Y') }}. +
+ @endif +
diff --git a/resources/views/components/threads/thread.blade.php b/resources/views/components/threads/thread.blade.php index d6bce4c27..f6daeac0b 100644 --- a/resources/views/components/threads/thread.blade.php +++ b/resources/views/components/threads/thread.blade.php @@ -57,6 +57,14 @@ class="prose prose-lio max-w-none p-6 break-words" >
+ @if($thread->latestEdit) +
+ Last edit by + {{ $thread->latestEdit->author()->name() }} + on {{ $thread->latestEdit->edited_at->format('j M, Y') }}. +
+ @endif +
From 2237db9a506a166efd67d120b0757ce5ade406ca Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 22:33:50 +0100 Subject: [PATCH 3/8] Add caching --- app/Concerns/HasEditLog.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Concerns/HasEditLog.php b/app/Concerns/HasEditLog.php index 0747f3dfb..48cfca817 100644 --- a/app/Concerns/HasEditLog.php +++ b/app/Concerns/HasEditLog.php @@ -38,8 +38,8 @@ public function getLatestEditAttribute() { $cacheKey = sprintf('%s-%s', Str::slug($this::class), $this->id); - //return Cache::rememberForever($cacheKey, function() { + return Cache::rememberForever($cacheKey, function() { return $this->edits()->latest('edited_at')->first(); - //}); + }); } } From 7c3b2336880b468291ce90eda37c744d018f03c3 Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 22:35:25 +0100 Subject: [PATCH 4/8] Fix styling --- app/Concerns/HasEditLog.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/app/Concerns/HasEditLog.php b/app/Concerns/HasEditLog.php index 48cfca817..1ca72537d 100644 --- a/app/Concerns/HasEditLog.php +++ b/app/Concerns/HasEditLog.php @@ -6,24 +6,21 @@ use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; -use function constant; -use function json_encode; -use function sprintf; trait HasEditLog { public static function bootHasEditLog() { - static::updating(function($model) { + static::updating(function ($model) { Edit::create([ 'author_id' => auth()->id(), 'editable_id' => $model->id, - 'editable_type' => constant($model::class . '::TABLE') ?? $model::class, + 'editable_type' => constant($model::class.'::TABLE') ?? $model::class, 'edited_at' => now(), ]); $cacheKey = sprintf('%s-%s', Str::slug($model::class), $model->id); - if(Cache::has($cacheKey)) { + if (Cache::has($cacheKey)) { Cache::forget($cacheKey); } }); @@ -38,7 +35,7 @@ public function getLatestEditAttribute() { $cacheKey = sprintf('%s-%s', Str::slug($this::class), $this->id); - return Cache::rememberForever($cacheKey, function() { + return Cache::rememberForever($cacheKey, function () { return $this->edits()->latest('edited_at')->first(); }); } From ad0b2ca8dc9f3681ad16e24490e246fd7c4c4522 Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 22:58:16 +0100 Subject: [PATCH 5/8] Fix failing tests --- app/Concerns/HasEditLog.php | 4 +-- database/factories/EditFactory.php | 45 ++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 database/factories/EditFactory.php diff --git a/app/Concerns/HasEditLog.php b/app/Concerns/HasEditLog.php index 1ca72537d..499ad6331 100644 --- a/app/Concerns/HasEditLog.php +++ b/app/Concerns/HasEditLog.php @@ -11,9 +11,9 @@ trait HasEditLog { public static function bootHasEditLog() { - static::updating(function ($model) { + static::updated(function ($model) { Edit::create([ - 'author_id' => auth()->id(), + 'author_id' => $model->author_id, 'editable_id' => $model->id, 'editable_type' => constant($model::class.'::TABLE') ?? $model::class, 'edited_at' => now(), diff --git a/database/factories/EditFactory.php b/database/factories/EditFactory.php new file mode 100644 index 000000000..1839e9c5e --- /dev/null +++ b/database/factories/EditFactory.php @@ -0,0 +1,45 @@ +faker->randomElement([Article::class, Reply::class, Thread::class]); + $editableFactory = call_user_func([$editableClass, 'factory']); + + return [ + 'author_id' => function() { + return User::factory()->create()->id; + }, + 'editable_id' => function() use($editableFactory) { + return $editableFactory->create()->id; + }, + 'editable_type' => function() use($editableClass) { + return constant($editableClass.'::TABLE') ?? $editableClass; + }, + 'edited_at' => $this->faker->dateTimeThisMonth(), + ]; + } +} From a4c680b5a440c4a07ff738b04ad2fc0d69741eee Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 22:59:14 +0100 Subject: [PATCH 6/8] Fix styling --- database/factories/EditFactory.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/database/factories/EditFactory.php b/database/factories/EditFactory.php index 1839e9c5e..b925ab0ba 100644 --- a/database/factories/EditFactory.php +++ b/database/factories/EditFactory.php @@ -8,7 +8,6 @@ use App\Models\Thread; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; -use function call_user_func; class EditFactory extends Factory { @@ -30,13 +29,13 @@ public function definition() $editableFactory = call_user_func([$editableClass, 'factory']); return [ - 'author_id' => function() { + 'author_id' => function () { return User::factory()->create()->id; }, - 'editable_id' => function() use($editableFactory) { + 'editable_id' => function () use ($editableFactory) { return $editableFactory->create()->id; }, - 'editable_type' => function() use($editableClass) { + 'editable_type' => function () use ($editableClass) { return constant($editableClass.'::TABLE') ?? $editableClass; }, 'edited_at' => $this->faker->dateTimeThisMonth(), From 99c0481e826f36c4a95bc6517f12f38e49916b05 Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 23:02:27 +0100 Subject: [PATCH 7/8] Remove unnecessary parameter from GithubController and add return type to HasEditLog trait --- app/Concerns/HasEditLog.php | 2 +- app/Http/Controllers/Auth/GithubController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Concerns/HasEditLog.php b/app/Concerns/HasEditLog.php index 499ad6331..8848b4ec1 100644 --- a/app/Concerns/HasEditLog.php +++ b/app/Concerns/HasEditLog.php @@ -31,7 +31,7 @@ public function edits(): MorphMany return $this->morphMany(Edit::class, 'editable'); } - public function getLatestEditAttribute() + public function getLatestEditAttribute(): ?Edit { $cacheKey = sprintf('%s-%s', Str::slug($this::class), $this->id); diff --git a/app/Http/Controllers/Auth/GithubController.php b/app/Http/Controllers/Auth/GithubController.php index 2eeb39084..721d3d269 100644 --- a/app/Http/Controllers/Auth/GithubController.php +++ b/app/Http/Controllers/Auth/GithubController.php @@ -19,7 +19,7 @@ class GithubController extends Controller /** * Redirect the user to the GitHub authentication page. */ - public function redirectToProvider(Request $request) + public function redirectToProvider() { return Socialite::driver('github')->redirect(); } From 07dd697e77c64091a230d1ab50f1aeff4748ec8f Mon Sep 17 00:00:00 2001 From: tomhatzer Date: Fri, 5 Nov 2021 23:03:05 +0100 Subject: [PATCH 8/8] Remove unnecessary import from GithubController --- app/Http/Controllers/Auth/GithubController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Http/Controllers/Auth/GithubController.php b/app/Http/Controllers/Auth/GithubController.php index 721d3d269..9ea29e49c 100644 --- a/app/Http/Controllers/Auth/GithubController.php +++ b/app/Http/Controllers/Auth/GithubController.php @@ -3,7 +3,6 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; -use App\Http\Requests\Request; use App\Jobs\UpdateProfile; use App\Models\User; use App\Social\GithubUser;