Repository navigation
feat(resources): implement resource approval moderation system for non-contributor uploads #394
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
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 |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ class ResourceObserver | |
| { | ||
| public function creating(Resource $resource): void | ||
| { | ||
| if ($resource->node_id && $resource->user_id) { | ||
| if ($resource->node_id && $resource->user_id && $resource->status === 'approved') { | ||
|
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🏁 Script executed: cat app/Observers/ResourceObserver.php
sed -n '225,300p' app/Http/Controllers/Admin/ResourceController.php
rg -n 'ResourceObserver|owner_id|user_id' app/Models/Node.php app/ProvidersRepository: hscstack/platform Length of output: 3727 🏁 Script executed: set -o pipefail
printf '%s\n' '--- observer registration ---'
nl -ba app/Providers/AppServiceProvider.php | sed -n '1,75p'
printf '%s\n' '--- Resource model ---'
nl -ba app/Models/Resource.php | sed -n '1,240p'
printf '%s\n' '--- Node model ---'
nl -ba app/Models/Node.php | sed -n '1,180p'
printf '%s\n' '--- resource writes and status references ---'
rg -n -F --glob '*.php' -- 'Resource::create' app tests database || test "$?" -eq 1
rg -n --glob '*.php' -- '->(create|save|update|updateQuietly|forceCreate)\s*\(' app/Http app/Models app/Services app/Observers | rg 'resource|Resource|resources' || true
rg -n --glob '*.php' -- 'status.?[[:space:]]*=>|status.?[[:space:]]*===|where\([^)]*status|whereStatus|approved|pending|rejected' app/Http app/Models app/Services app/Observers database tests | head -300 || true
printf '%s\n' '--- schema and tests ---'
rg -n --glob '*.php' -- 'create\(['"'"'"]resources|Schema::table\(['"'"'"]resources|owner|user_id' database/migrations tests app/Http/Controllers/ResourceController.php app/Http/Controllers/Admin/ResourceController.php 2>/dev/null | head -300 || true
printf '%s\n' '--- relevant controller declarations ---'
rg -n --glob '*.php' -F -- 'function ' app/Http/Controllers/ResourceController.php app/Http/Controllers/Admin/ResourceController.php 2>/dev/null || trueRepository: hscstack/platform Length of output: 41747 🏁 Script executed: nl -ba app/Providers/AppServiceProvider.php | sed -n '1,75p'
nl -ba app/Models/Resource.php | sed -n '1,220p'
nl -ba app/Models/Node.php | sed -n '1,160p'
rg -n --glob '*.php' 'Resource::create|resources->create|new Resource|status.?=>|status.?===|whereStatus|where\([^)]*status' app database tests || test "$?" -eq 1
rg -n --glob '*.php' 'function ' app/Http/Controllers/ResourceController.php app/Http/Controllers/Admin/ResourceController.php 2>/dev/null || trueRepository: hscstack/platform Length of output: 17180 🏁 Script executed: printf '%s\n' '--- current observer ---'
nl -ba app/Observers/ResourceObserver.php | sed -n '1,100p'
printf '%s\n' '--- admin resource creation and update paths ---'
nl -ba app/Http/Controllers/Admin/ResourceController.php | sed -n '1,225p'
nl -ba app/Http/Controllers/Admin/ResourceController.php | sed -n '234,300p'
printf '%s\n' '--- ownership test ---'
nl -ba tests/Feature/NodeVoteTest.php | sed -n '400,455p'
printf '%s\n' '--- resource schema ---'
nl -ba database/migrations/2026_06_12_091830_create_resources_table.php | sed -n '1,80p'
nl -ba database/migrations/2026_10_08_230000_add_moderation_to_resources_table.php | sed -n '1,60p'Repository: hscstack/platform Length of output: 17656 Preserve first-approved ownership across creation and approval.
Handle the effective default status, count only other approved resources, and run the ownership check when an update changes a resource to approved. Suggested fix--- "a/app/Observers/ResourceObserver.php"
+++ "b/app/Observers/ResourceObserver.php"
@@ -8,19 +8,40 @@
class ResourceObserver
{
public function creating(Resource $resource): void
{
- if ($resource->node_id && $resource->user_id && $resource->status === 'approved') {
- $otherResourcesCount = Resource::where('node_id', $resource->node_id)->count();
+ $this->assignNodeOwnerIfFirstApproved($resource);
+ }
- if ($otherResourcesCount === 0) {
- $node = Node::find($resource->node_id);
- if ($node && $node->user_id !== $resource->user_id) {
- $node->updateQuietly(['user_id' => $resource->user_id]);
- }
+ public function updated(Resource $resource): void
+ {
+ if ($resource->wasChanged('status') && $resource->status === 'approved') {
+ $this->assignNodeOwnerIfFirstApproved($resource);
+ }
+ }
+
+ private function assignNodeOwnerIfFirstApproved(Resource $resource): void
+ {
+ $status = $resource->status ?? 'approved';
+
+ if (!$resource->node_id || !$resource->user_id || $status !== 'approved') {
+ return;
+ }
+
+ $approvedResources = Resource::where('node_id', $resource->node_id)
+ ->where('status', 'approved');
+
+ if ($resource->exists) {
+ $approvedResources->where($resource->getKeyName(), '<>', $resource->getKey());
+ }
+
+ if (!$approvedResources->exists()) {
+ $node = Node::find($resource->node_id);
+ if ($node && $node->user_id !== $resource->user_id) {
+ $node->updateQuietly(['user_id' => $resource->user_id]);
}
}
}
public function saved(Resource $resource): void
{🤖 Prompt for AI Agents |
||
| $otherResourcesCount = Resource::where('node_id', $resource->node_id)->count(); | ||
|
|
||
| if ($otherResourcesCount === 0) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| /** | ||
| * Run the migrations. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| Schema::table('resources', function (Blueprint $table) { | ||
| $table->string('status', 20)->default('approved')->index()->after('user_id'); | ||
| $table->foreignId('reviewed_by')->nullable()->after('status')->constrained('users')->nullOnDelete(); | ||
| $table->timestamp('reviewed_at')->nullable()->after('reviewed_by'); | ||
| $table->text('rejection_reason')->nullable()->after('reviewed_at'); | ||
|
|
||
| $table->index(['node_id', 'status']); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| */ | ||
| public function down(): void | ||
| { | ||
| Schema::table('resources', function (Blueprint $table) { | ||
| $table->dropIndex(['node_id', 'status']); | ||
| $table->dropIndex(['status']); | ||
| $table->dropForeign(['reviewed_by']); | ||
| $table->dropColumn(['status', 'reviewed_by', 'reviewed_at', 'rejection_reason']); | ||
| }); | ||
| } | ||
| }; |
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 | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: hscstack/platform
Length of output: 6305
🏁 Script executed:
Repository: hscstack/platform
Length of output: 4659
Restrict rejection to pending resources.
The route accepts any route-bound
Resource, butrejectdoes not check its status. A reviewer withapprove resourcescan therefore reject an approved resource. Public resource queries include onlystatus = approved, so this removes the resource from public listings. Rejecting should remain a pending-review action.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents