Skip to content
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ In order to index your existing threads, run the following command:
php artisan scout:import App\\Models\\Thread
```

New threads will be automatically added to the index and threads which get edited will be automatically updated. If you need to flush your index and start again, you can run the following command:
New threads will be automatically added to the index and threads which get updated will be automatically synced. If you need to flush your index and start again, you can run the following command:

```bash
php artisan scout:flush App\\Models\\Thread
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ReplyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function update(UpdateReplyRequest $request, Reply $reply)
{
$this->authorize(ReplyPolicy::UPDATE, $reply);

$this->dispatchNow(new UpdateReply($reply, $request->body()));
$this->dispatchNow(new UpdateReply($reply, $request->user(), $request->body()));

$this->success('replies.updated');

Expand Down
6 changes: 0 additions & 6 deletions app/Http/Requests/ThreadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Requests;

use App\Models\User;
use App\Rules\DoesNotContainUrlRule;
use App\Rules\HttpImageRule;

Expand All @@ -18,11 +17,6 @@ public function rules()
];
}

public function author(): User
{
return $this->user();
}

public function subject(): string
{
return $this->get('subject');
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/CreateThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function fromRequest(ThreadRequest $request): self
return new static(
$request->subject(),
$request->body(),
$request->author(),
$request->user(),
$request->tags()
);
}
Expand Down
1 change: 1 addition & 0 deletions app/Jobs/UpdateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function handle(): Article
'slug' => $this->title,
'submitted_at' => $this->shouldUpdateSubmittedAt() ? now() : $this->article->submittedAt(),
]);

$this->article->syncTags($this->tags);

return $this->article;
Expand Down
15 changes: 12 additions & 3 deletions app/Jobs/UpdateReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
namespace App\Jobs;

use App\Models\Reply;
use App\Models\User;

final class UpdateReply
{
/**
* @var \App\Models\Reply
* @var Reply
*/
private $reply;

/**
* @var User
*/
private $updatedBy;

/**
* @var string
*/
private $body;

public function __construct(Reply $reply, string $body)
public function __construct(Reply $reply, User $updatedBy, string $body)
{
$this->reply = $reply;
$this->updatedBy = $updatedBy;
$this->body = $body;
}

public function handle()
{
$this->reply->update(['body' => $this->body]);
$this->reply->body = $this->body;
$this->reply->updatedByRelation()->associate($this->updatedBy);
$this->reply->save();
}
}
15 changes: 12 additions & 3 deletions app/Jobs/UpdateThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@

use App\Http\Requests\ThreadRequest;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Support\Arr;

final class UpdateThread
{
/**
* @var \App\Models\Thread
* @var Thread
*/
private $thread;

/**
* @var User
*/
private $updatedBy;

/**
* @var array
*/
private $attributes;

public function __construct(Thread $thread, array $attributes = [])
public function __construct(Thread $thread, User $updatedBy, array $attributes = [])
{
$this->thread = $thread;
$this->updatedBy = $updatedBy;
$this->attributes = Arr::only($attributes, ['subject', 'body', 'slug', 'tags']);
}

public static function fromRequest(Thread $thread, ThreadRequest $request): self
{
return new static($thread, [
return new static($thread, $request->user(), [
'subject' => $request->subject(),
'body' => $request->body(),
'slug' => $request->subject(),
Expand All @@ -42,6 +49,8 @@ public function handle(): Thread
$this->thread->syncTags($this->attributes['tags']);
}

$this->thread->updatedByRelation()->associate($this->updatedBy);

$this->thread->save();

return $this->thread;
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public function readTime()
return $minutes == 0 ? 1 : $minutes;
}

public function isUpdated(): bool
{
return $this->updated_at->gt($this->created_at);
}

public function scopeSubmitted(Builder $query): Builder
{
return $query->whereNotNull('submitted_at');
Expand Down
17 changes: 17 additions & 0 deletions app/Models/Reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -38,6 +39,7 @@ final class Reply extends Model
*/
protected $with = [
'likesRelation',
'updatedByRelation',
];

public function solutionTo(): HasOne
Expand Down Expand Up @@ -70,6 +72,21 @@ public function replyAble(): ReplyAble
return $this->replyAbleRelation;
}

public function updatedBy(): ?User
{
return $this->updatedByRelation;
}

public function updatedByRelation(): BelongsTo
{
return $this->belongsTo(User::class, 'updated_by');
}

public function isUpdated(): bool
{
return $this->updated_at->gt($this->created_at);
}

/**
* It's important to name the relationship the same as the method because otherwise
* eager loading of the polymorphic relationship will fail on queued jobs.
Expand Down
16 changes: 16 additions & 0 deletions app/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ final class Thread extends Model implements ReplyAble, SubscriptionAble, Feedabl
'likesRelation',
'repliesRelation',
'tagsRelation',
'updatedByRelation',
];

public function id(): int
Expand All @@ -87,6 +88,21 @@ public function excerpt(int $limit = 100): string
return Str::limit(strip_tags(md_to_html($this->body())), $limit);
}

public function updatedBy(): ?User
{
return $this->updatedByRelation;
}

public function updatedByRelation(): BelongsTo
{
return $this->belongsTo(User::class, 'updated_by');
}

public function isUpdated(): bool
{
return $this->updated_at->gt($this->created_at);
}

public function solutionReply(): ?Reply
{
return $this->solutionReplyRelation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUpdatedByToThreadsAndRepliesTable extends Migration
{
public function up()
{
Schema::table('threads', function (Blueprint $table) {
$table->foreignId('updated_by')->nullable();
});

Schema::table('replies', function (Blueprint $table) {
$table->foreignId('updated_by')->nullable();
});
}
}
6 changes: 6 additions & 0 deletions resources/views/articles/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class="prose prose-lg text-gray-800 prose-lio"
<x-buk-markdown>{!! $article->body() !!}</x-buk-markdown>
</div>

@if ($article->isUpdated())
<div class="text-sm text-gray-900 py-6">
Last updated on {{ $article->updated_at->format('j M, Y') }}.
</div>
@endif

<div class="flex items-center gap-x-6 pt-6 pb-10">
<livewire:like-article :article="$article" :isSidebar="false" />

Expand Down
14 changes: 14 additions & 0 deletions resources/views/components/threads/reply.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class="prose prose-lio max-w-none p-6 break-words"
>
</div>

@if ($reply->isUpdated())
<div class="text-sm text-gray-900 p-6">
Last updated

@if ($updatedBy = $reply->updatedBy())
by <a href="{{ route('profile', $updatedBy->username()) }}" class="text-lio-500 border-b-2 pb-0.5 border-lio-100 hover:text-lio-600">
{{ '@'.$reply->updatedBy()->username() }}
</a>
@endif

on {{ $reply->updated_at->format('j M, Y') }}.
</div>
@endif

<div class="flex justify-between">
<div class="px-6 pb-6">
<livewire:like-reply :reply="$reply"/>
Expand Down
14 changes: 14 additions & 0 deletions resources/views/components/threads/thread.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ class="prose prose-lio max-w-none p-6 break-words"
>
</div>

@if ($thread->isUpdated())
<div class="text-sm text-gray-900 p-6">
Last updated

@if ($updatedBy = $thread->updatedBy())
by <a href="{{ route('profile', $updatedBy->username()) }}" class="text-lio-500 border-b-2 pb-0.5 border-lio-100 hover:text-lio-600">
{{ '@'.$thread->updatedBy()->username() }}
</a>
@endif

on {{ $thread->updated_at->format('j M, Y') }}.
</div>
@endif

<div class="px-6 pb-6">
<livewire:like-thread :thread="$thread"/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ReplyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
$this->loginAs($user);

$this->put('/replies/1', [
'body' => 'The edited reply',
'body' => 'The updated reply',
])
->assertRedirectedTo('/forum/the-first-thread')
->assertSessionHas('success', 'Reply successfully updated!');
Expand Down