Repository navigation
refactor(resources): overhaul resource architecture and moderation pipeline #430
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
Changes from all commits
c307a67
5fd34d0
67eee3e
e3d2cc0
5195583
3caf156
7fe8b05
d952b76
b7b44bf
7424e2c
cbb61ee
a16ebf0
394b50d
570d4f0
4e272b2
250f54e
7040f46
e347068
2c7ba9c
5604d8e
f5d955a
b916aa0
f85fefc
9730d59
9f181c2
939c5ba
2997747
b293830
7f35174
bea04a4
85d01eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,6 @@ | |
| use App\Models\ForumPost; | ||
| use App\Models\Notice; | ||
| use App\Models\Resource; | ||
| use App\Models\ResourceChangeRequest; | ||
| use App\Models\SupportTicket; | ||
| use App\Models\User; | ||
| use Illuminate\Console\Command; | ||
|
|
@@ -46,20 +45,25 @@ public function handle(): void | |
| ->toArray() | ||
| ); | ||
|
|
||
| // Resource files (notes, images, videos — all stored under resources/) | ||
| // Also protects pending change requests awaiting moderation review | ||
| $activeResourceFiles = Resource::whereNotNull('file_path')->pluck('file_path')->toArray(); | ||
| $pendingChangeRequestFiles = ResourceChangeRequest::where('status', 'pending') | ||
| ->whereNotNull('payload') | ||
| ->get() | ||
| ->pluck('payload.file_path') | ||
| ->filter() | ||
| ->values() | ||
| ->toArray(); | ||
| // Resource files (items stored under resources/) | ||
| $rawItems = DB::table('resources') | ||
| ->whereNotNull('items') | ||
| ->pluck('items'); | ||
|
|
||
| $activeResourceFiles = []; | ||
| foreach ($rawItems as $json) { | ||
| $items = is_array($json) ? $json : (json_decode($json, true) ?: []); | ||
| foreach ($items as $item) { | ||
| $source = $item['source'] ?? null; | ||
| if ($source && ! str_starts_with($source, 'http://') && ! str_starts_with($source, 'https://')) { | ||
| $activeResourceFiles[] = $source; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $this->cleanDirectory( | ||
| 'resources', | ||
| array_values(array_unique(array_merge($activeResourceFiles, $pendingChangeRequestFiles))) | ||
| array_values(array_unique($activeResourceFiles)) | ||
| ); | ||
|
|
||
| // Forum post images | ||
|
|
@@ -94,9 +98,19 @@ protected function cleanDirectory(string $directory, array $usedFiles): void | |
| $files = Storage::allFiles($directory); | ||
|
|
||
| $deleted = 0; | ||
| $oneDayAgo = now()->subDay()->timestamp; | ||
|
|
||
| foreach ($files as $file) { | ||
| if (! in_array($file, $usedFiles, true)) { | ||
| // Skip newly uploaded files (e.g. uploaded within last 24h) to avoid race conditions with unsubmitted forms | ||
| try { | ||
| if (Storage::lastModified($file) > $oneDayAgo) { | ||
| continue; | ||
| } | ||
| } catch (\Throwable) { | ||
| // ignore if storage does not support lastModified | ||
| } | ||
|
Comment on lines
+110
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Do not delete a file when its age is unknown. If 🤖 Prompt for AI Agents
|
||
|
|
||
| $this->line("Unused: {$file}"); | ||
|
|
||
| if (! $this->option('dry-run')) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,92 +3,140 @@ | |||||||||||||||||
| namespace App\Http\Controllers\Admin; | ||||||||||||||||||
|
|
||||||||||||||||||
| use App\Http\Controllers\ResourceController as BaseResourceController; | ||||||||||||||||||
| use App\Http\Requests\Resource\UpdateResourceRequest; | ||||||||||||||||||
| use App\Models\Node; | ||||||||||||||||||
| use App\Models\Resource; | ||||||||||||||||||
| use App\Models\ResourceChangeRequest; | ||||||||||||||||||
| use App\Notifications\ResourceModerationNotification; | ||||||||||||||||||
| use Illuminate\Http\Request; | ||||||||||||||||||
| use Illuminate\Support\Facades\Auth; | ||||||||||||||||||
| use Illuminate\Support\Facades\DB; | ||||||||||||||||||
| use Illuminate\Support\Facades\Storage; | ||||||||||||||||||
| use Inertia\Inertia; | ||||||||||||||||||
|
|
||||||||||||||||||
| class ResourceController extends BaseResourceController | ||||||||||||||||||
| { | ||||||||||||||||||
| public function update(UpdateResourceRequest $request, Resource $resource) | ||||||||||||||||||
| public function moderationIndex(Request $request) | ||||||||||||||||||
| { | ||||||||||||||||||
| $validated = $request->validated(); | ||||||||||||||||||
|
|
||||||||||||||||||
| $isModerator = $request->user()->can('moderate resources'); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (! $isModerator) { | ||||||||||||||||||
| $this->ensureCanSubmit(1); | ||||||||||||||||||
| $status = $request->query('status', 'pending'); | ||||||||||||||||||
| if (! in_array($status, ['pending', 'approved', 'rejected', 'all'])) { | ||||||||||||||||||
| $status = 'pending'; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if ($request->hasFile('file')) { | ||||||||||||||||||
| if ($isModerator && $resource->file_path) { | ||||||||||||||||||
| Storage::delete($resource->file_path); | ||||||||||||||||||
| } | ||||||||||||||||||
| $validated['file_path'] = $request->file('file')->store("resources/{$validated['resource_type']}s"); | ||||||||||||||||||
| } | ||||||||||||||||||
| $counts = Resource::selectRaw('status, count(*) as total') | ||||||||||||||||||
| ->groupBy('status') | ||||||||||||||||||
| ->pluck('total', 'status'); | ||||||||||||||||||
|
|
||||||||||||||||||
| if ($isModerator) { | ||||||||||||||||||
| $resource->update($validated); | ||||||||||||||||||
| $query = Resource::query() | ||||||||||||||||||
| ->with([ | ||||||||||||||||||
| 'user:id,name,username,image_path', | ||||||||||||||||||
| 'node.subject', | ||||||||||||||||||
| ]) | ||||||||||||||||||
| ->latest('id'); | ||||||||||||||||||
|
|
||||||||||||||||||
| return back()->with('success', 'Resource updated successfully.'); | ||||||||||||||||||
| if ($status !== 'all') { | ||||||||||||||||||
| $query->where('status', $status); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| ResourceChangeRequest::recordUpdate( | ||||||||||||||||||
| Auth::id(), | ||||||||||||||||||
| $resource, | ||||||||||||||||||
| $validated | ||||||||||||||||||
| ); | ||||||||||||||||||
| $resources = $query->paginate(20)->withQueryString(); | ||||||||||||||||||
|
|
||||||||||||||||||
| return back()->with('success', 'Resource update submitted for moderation.'); | ||||||||||||||||||
| $resources->getCollection()->each(function ($res) { | ||||||||||||||||||
| $res->node?->append('breadcrumb'); | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| return Inertia::render('admin/moderation/Resources', [ | ||||||||||||||||||
| 'resources' => $resources, | ||||||||||||||||||
| 'currentStatus' => $status, | ||||||||||||||||||
| 'counts' => [ | ||||||||||||||||||
| 'all' => (int) $counts->sum(), | ||||||||||||||||||
| 'pending' => (int) $counts->get('pending', 0), | ||||||||||||||||||
| 'approved' => (int) $counts->get('approved', 0), | ||||||||||||||||||
| 'rejected' => (int) $counts->get('rejected', 0), | ||||||||||||||||||
| ], | ||||||||||||||||||
| ]); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function destroy(Resource $resource) | ||||||||||||||||||
| public function approve(Request $request) | ||||||||||||||||||
| { | ||||||||||||||||||
| if (Auth::user()?->can('moderate resources')) { | ||||||||||||||||||
| if ($resource->file_path) { | ||||||||||||||||||
| Storage::delete($resource->file_path); | ||||||||||||||||||
| } | ||||||||||||||||||
| $resource->delete(); | ||||||||||||||||||
|
|
||||||||||||||||||
| return redirect()->back()->with('success', 'Resource deleted successfully.'); | ||||||||||||||||||
| } | ||||||||||||||||||
| $validated = $request->validate([ | ||||||||||||||||||
| 'ids' => ['required', 'array', 'min:1'], | ||||||||||||||||||
| 'ids.*' => ['integer', 'exists:resources,id'], | ||||||||||||||||||
| ]); | ||||||||||||||||||
|
|
||||||||||||||||||
| $this->ensureCanSubmit(1); | ||||||||||||||||||
| $resources = Resource::whereIn('id', $validated['ids']) | ||||||||||||||||||
| ->where('status', '!=', 'approved') | ||||||||||||||||||
| ->with('user') | ||||||||||||||||||
| ->get(); | ||||||||||||||||||
|
|
||||||||||||||||||
| DB::transaction(function () use ($resources) { | ||||||||||||||||||
| foreach ($resources as $res) { | ||||||||||||||||||
| $res->update([ | ||||||||||||||||||
| 'status' => 'approved', | ||||||||||||||||||
| 'reviewed_by' => Auth::id(), | ||||||||||||||||||
| 'reviewed_at' => now(), | ||||||||||||||||||
| ]); | ||||||||||||||||||
|
|
||||||||||||||||||
| if ($res->user) { | ||||||||||||||||||
| ResourceModerationNotification::sendTo($res->user, $res, 'approved'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis
Roll up batch moderation notifications into one per author.
Send one notification per author after the loop, passing the number of resources. Add a count parameter to the notification class: Proposed fix (notification class; the controller change follows)
In 🤖 Prompt for AI Agents
|
||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+68
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis
Invalidate the trending-notes cache in
🐛 Suggested fix
🤖 Prompt for AI Agents
|
||||||||||||||||||
|
|
||||||||||||||||||
| ResourceChangeRequest::recordDelete(Auth::id(), $resource); | ||||||||||||||||||
| $count = $resources->count(); | ||||||||||||||||||
| $message = $count === 1 | ||||||||||||||||||
| ? 'Resource approved and published successfully.' | ||||||||||||||||||
| : "{$count} resources approved and published successfully."; | ||||||||||||||||||
|
|
||||||||||||||||||
| return redirect()->back()->with('success', 'Resource deletion request submitted for moderation.'); | ||||||||||||||||||
| return back()->with('success', $message); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function bulkRename(Request $request, Node $node) | ||||||||||||||||||
| public function reject(Request $request) | ||||||||||||||||||
| { | ||||||||||||||||||
| if ($node->isEffectivelyFrozen()) { | ||||||||||||||||||
| abort(403, 'This folder is frozen and cannot be modified.'); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| $validated = $request->validate([ | ||||||||||||||||||
| 'prefix' => ['required', 'string', 'max:100'], | ||||||||||||||||||
| 'start_number' => ['nullable', 'integer', 'min:0'], | ||||||||||||||||||
| 'ids' => ['required', 'array', 'min:1'], | ||||||||||||||||||
| 'ids.*' => ['integer', 'exists:resources,id'], | ||||||||||||||||||
| 'admin_note' => ['nullable', 'string', 'max:500'], | ||||||||||||||||||
| ]); | ||||||||||||||||||
|
|
||||||||||||||||||
| $rawPrefix = trim($validated['prefix']); | ||||||||||||||||||
| $cleanPrefix = rtrim($rawPrefix, ' -'); | ||||||||||||||||||
| $startNumber = (int) ($validated['start_number'] ?? 1); | ||||||||||||||||||
| $resources = Resource::whereIn('id', $validated['ids']) | ||||||||||||||||||
| ->where('status', '!=', 'rejected') | ||||||||||||||||||
| ->with('user') | ||||||||||||||||||
| ->get(); | ||||||||||||||||||
|
|
||||||||||||||||||
| $adminNote = $validated['admin_note'] ?? null; | ||||||||||||||||||
|
|
||||||||||||||||||
| DB::transaction(function () use ($resources, $adminNote) { | ||||||||||||||||||
| foreach ($resources as $res) { | ||||||||||||||||||
| $res->update([ | ||||||||||||||||||
| 'status' => 'rejected', | ||||||||||||||||||
| 'admin_note' => $adminNote, | ||||||||||||||||||
| 'reviewed_by' => Auth::id(), | ||||||||||||||||||
| 'reviewed_at' => now(), | ||||||||||||||||||
| ]); | ||||||||||||||||||
|
|
||||||||||||||||||
| if ($res->user) { | ||||||||||||||||||
| ResourceModerationNotification::sendTo($res->user, $res, 'rejected', $adminNote); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| $resources = $node->resources()->orderBy('id')->get(); | ||||||||||||||||||
| $count = $resources->count(); | ||||||||||||||||||
| $message = $count === 1 | ||||||||||||||||||
| ? 'Resource rejected.' | ||||||||||||||||||
| : "{$count} resources rejected."; | ||||||||||||||||||
|
|
||||||||||||||||||
| DB::transaction(function () use ($resources, $cleanPrefix, $startNumber) { | ||||||||||||||||||
| foreach ($resources as $index => $resource) { | ||||||||||||||||||
| $number = str_pad((string) ($startNumber + $index), 2, '0', STR_PAD_LEFT); | ||||||||||||||||||
| $title = $cleanPrefix !== '' ? "{$cleanPrefix} - {$number}" : $number; | ||||||||||||||||||
| $resource->update(['title' => $title]); | ||||||||||||||||||
| return back()->with('success', $message); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public function destroy(Resource $resource) | ||||||||||||||||||
| { | ||||||||||||||||||
| // Clean up stored items in S3/R2 | ||||||||||||||||||
| foreach ($resource->items as $item) { | ||||||||||||||||||
| $source = is_array($item) ? ($item['source'] ?? '') : ($item->source ?? ''); | ||||||||||||||||||
| if ($source && ! str_starts_with($source, 'http://') && ! str_starts_with($source, 'https://')) { | ||||||||||||||||||
| Storage::disk('s3')->delete($source); | ||||||||||||||||||
|
Comment on lines
+131
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis
Use the configured default disk for deletion. When Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents
|
||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| $resource->delete(); | ||||||||||||||||||
|
|
||||||||||||||||||
| return back()->with('success', "Renamed {$resources->count()} resources successfully."); | ||||||||||||||||||
| return redirect()->back()->with('success', 'Resource deleted successfully.'); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Exclude skipped files from the dry-run count.
When this guard skips a recent file, the count at Line 124 still includes it in “would delete.” The documented dry-run preview can therefore report files that a real run would keep. Count only files that pass the age check.
🤖 Prompt for AI Agents