Skip to content

Implement Verified Author Logic for Publishing Without Review (#1276) #1303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions app/Console/Commands/SyncArticleImages.php

This file was deleted.

24 changes: 24 additions & 0 deletions app/Http/Controllers/Admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use App\Jobs\DeleteUser;
use App\Jobs\DeleteUserThreads;
use App\Jobs\UnbanUser;
use App\Jobs\UnVerifyAuthor;
use App\Jobs\VerifyAuthor;
use App\Models\User;
use App\Policies\UserPolicy;
use App\Queries\SearchUsers;
Expand Down Expand Up @@ -60,6 +62,28 @@ public function unban(User $user): RedirectResponse
return redirect()->route('profile', $user->username());
}

public function verifyAuthor(User $user)
{
$this->authorize(UserPolicy::ADMIN, $user);

$this->dispatchSync(new VerifyAuthor($user));

$this->success($user->name() . ' was verified!');

return redirect()->route('admin.users');
}

public function unverifyAuthor(User $user)
{
$this->authorize(UserPolicy::ADMIN, $user);

$this->dispatchSync(new UnverifyAuthor($user));

$this->success($user->name() . ' was unverified!');

return redirect()->route('admin.users');
}

public function delete(User $user): RedirectResponse
{
$this->authorize(UserPolicy::DELETE, $user);
Expand Down
12 changes: 7 additions & 5 deletions app/Http/Controllers/Articles/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ public function store(ArticleRequest $request)

$article = Article::findByUuidOrFail($uuid);

$this->success(
$request->shouldBeSubmitted()
? 'Thank you for submitting, unfortunately we can\'t accept every submission. You\'ll only hear back from us when we accept your article.'
: 'Article successfully created!'
);
if ($article->isNotApproved()) {
$this->success(
$request->shouldBeSubmitted()
? 'Thank you for submitting, unfortunately we can\'t accept every submission. You\'ll only hear back from us when we accept your article.'
: 'Article successfully created!'
);
}

return $request->wantsJson()
? ArticleResource::make($article)
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Requests/ArticleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\User;
use App\Rules\HttpImageRule;
use Illuminate\Http\Concerns\InteractsWithInput;
use Illuminate\Validation\Rules\RequiredIf;

class ArticleRequest extends Request
{
Expand All @@ -14,6 +15,7 @@ public function rules(): array
{
return [
'title' => ['required', 'max:100'],
'hero_image_id' => ['nullable', new RequiredIf(auth()->user()->isVerifiedAuthor())],
'body' => ['required', new HttpImageRule],
'tags' => 'array|nullable',
'tags.*' => 'exists:tags,id',
Expand Down Expand Up @@ -58,4 +60,9 @@ public function shouldBeSubmitted(): bool
{
return $this->boolean('submitted');
}

public function heroImageId(): ?string
{
return $this->get('hero_image_id');
}
}
13 changes: 13 additions & 0 deletions app/Jobs/CreateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
private string $body,
private User $author,
private bool $shouldBeSubmitted,
private ?string $heroImageId = null,
array $options = []
) {
$this->originalUrl = $options['original_url'] ?? null;
Expand All @@ -34,6 +35,7 @@ public static function fromRequest(ArticleRequest $request, UuidInterface $uuid)
$request->body(),
$request->author(),
$request->shouldBeSubmitted(),
$request->heroImageId(),
[
'original_url' => $request->originalUrl(),
'tags' => $request->tags(),
Expand All @@ -46,16 +48,27 @@ public function handle(): void
$article = new Article([
'uuid' => $this->uuid->toString(),
'title' => $this->title,
'hero_image_id' => $this->heroImageId,
'body' => $this->body,
'original_url' => $this->originalUrl,
'slug' => $this->title,
'submitted_at' => $this->shouldBeSubmitted ? now() : null,
'approved_at' => $this->canBeAutoApproved() ? now() : null,
]);
$article->authoredBy($this->author);
$article->syncTags($this->tags);

if ($article->hero_image_id) {
SyncArticleImage::dispatch($article);
}

if ($article->isAwaitingApproval()) {
event(new ArticleWasSubmittedForApproval($article));
}
}

private function canBeAutoApproved(): bool
{
return $this->shouldBeSubmitted && $this->author->canVerifiedAuthorPublishMoreArticleToday();
}
}
60 changes: 60 additions & 0 deletions app/Jobs/SyncArticleImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Jobs;

use App\Models\Article;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;

final class SyncArticleImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(public Article $article)
{
//
}

public function handle(): void
{
$imageData = $this->fetchUnsplashImageDataFromId($this->article);

if (! is_null($imageData)) {
$this->article->hero_image_url = $imageData['image_url'];
$this->article->hero_image_author_name = $imageData['author_name'];
$this->article->hero_image_author_url = $imageData['author_url'];
$this->article->save();
}
}

protected function fetchUnsplashImageDataFromId(Article $article): ?array
{
$response = Http::retry(3, 100, throw: false)
->withToken(config('services.unsplash.access_key'), 'Client-ID')
->get("https://api.unsplash.com/photos/{$article->hero_image_id}");

if ($response->failed()) {
$article->hero_image_id = null;
$article->save();

return null;
}

$response = $response->json();

// Trigger as Unsplash download...
Http::retry(3, 100, throw: false)
->withToken(config('services.unsplash.access_key'), 'Client-ID')
->get($response['links']['download_location']);

return [
'image_url' => $response['urls']['raw'],
'author_name' => $response['user']['name'],
'author_url' => $response['user']['links']['html'],
];
}
}
29 changes: 29 additions & 0 deletions app/Jobs/UnVerifyAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class UnVerifyAuthor implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(private User $user)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$this->user->author_verified_at = null;
$this->user->save();
}
}
9 changes: 9 additions & 0 deletions app/Jobs/UpdateArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(
private string $title,
private string $body,
private bool $shouldBeSubmitted,
private ?string $heroImageId = null,
array $options = []
) {
$this->originalUrl = $options['original_url'] ?? null;
Expand All @@ -30,6 +31,7 @@ public static function fromRequest(Article $article, ArticleRequest $request): s
$request->title(),
$request->body(),
$request->shouldBeSubmitted(),
$request->heroImageId(),
[
'original_url' => $request->originalUrl(),
'tags' => $request->tags(),
Expand All @@ -39,9 +41,12 @@ public static function fromRequest(Article $article, ArticleRequest $request): s

public function handle(): void
{
$originalImage = $this->article->hero_image_id;

$this->article->update([
'title' => $this->title,
'body' => $this->body,
'hero_image_id' => $this->heroImageId,
'original_url' => $this->originalUrl,
'slug' => $this->title,
]);
Expand All @@ -54,6 +59,10 @@ public function handle(): void
}

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

if ($this->article->hero_image_id !== $originalImage) {
SyncArticleImage::dispatch($this->article);
}
}

private function shouldUpdateSubmittedAt(): bool
Expand Down
29 changes: 29 additions & 0 deletions app/Jobs/VerifyAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class VerifyAuthor implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(private User $user)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$this->user->author_verified_at = now();
$this->user->save();
}
}
2 changes: 1 addition & 1 deletion app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function isShared(): bool

public function isAwaitingApproval(): bool
{
return $this->isSubmitted() && $this->isNotApproved() && $this->isNotDeclined();
return $this->isSubmitted() && $this->isNotApproved() && $this->isNotDeclined() && ! $this->author()->canVerifiedAuthorPublishMoreArticleToday();
}

public function isNotAwaitingApproval(): bool
Expand Down
Loading