From c307a67c4b171fee6893ce16890873d7851d9b3f Mon Sep 17 00:00:00 2001 From: TR Tajim Date: Thu, 22 Oct 2026 15:51:36 +0600 Subject: [PATCH 01/31] refactor: remove node voting and decouple folder taxonomy from user content --- app/Http/Controllers/NodeController.php | 59 +-- app/Http/Controllers/SubjectController.php | 6 +- .../Controllers/UserProfileController.php | 42 -- app/Models/Node.php | 17 - app/Models/NodeVote.php | 28 -- app/Models/User.php | 5 - app/Notifications/NodeVoteNotification.php | 80 ---- app/Observers/NodeVoteObserver.php | 38 -- app/Observers/ResourceObserver.php | 15 - app/Providers/AppServiceProvider.php | 3 - ...026_10_22_094417_drop_node_votes_table.php | 37 ++ resources/js/components/NodeRow.vue | 23 +- .../js/components/NotificationDropdown.vue | 3 +- resources/js/pages/Node.vue | 391 +-------------- resources/js/pages/User/Show.vue | 1 - routes/web.php | 1 - tests/Feature/NodeVoteNotificationTest.php | 235 --------- tests/Feature/NodeVoteTest.php | 447 ------------------ tests/Feature/UserProfileTest.php | 57 --- 19 files changed, 54 insertions(+), 1434 deletions(-) delete mode 100644 app/Models/NodeVote.php delete mode 100644 app/Notifications/NodeVoteNotification.php delete mode 100644 app/Observers/NodeVoteObserver.php create mode 100644 database/migrations/2026_10_22_094417_drop_node_votes_table.php delete mode 100644 tests/Feature/NodeVoteNotificationTest.php delete mode 100644 tests/Feature/NodeVoteTest.php diff --git a/app/Http/Controllers/NodeController.php b/app/Http/Controllers/NodeController.php index f5e5bea6..5bb9811b 100644 --- a/app/Http/Controllers/NodeController.php +++ b/app/Http/Controllers/NodeController.php @@ -4,8 +4,6 @@ use App\Models\Node; use App\Models\Subject; -use App\Notifications\NodeVoteNotification; -use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Inertia\Inertia; @@ -38,9 +36,9 @@ public function show(Subject $subject, $path) $nodes = Cache::remember("node_children_{$node->id}", now()->addDay(), function () use ($node) { return Node::where('parent_id', $node->id) - ->withCount(['children', 'resources', 'upvotes', 'downvotes']) - ->orderByRaw('(upvotes_count - downvotes_count) DESC') + ->withCount(['children', 'resources']) ->orderBy('sort_order') + ->orderBy('name') ->get(['id', 'name', 'slug'])->toArray(); }); @@ -48,21 +46,6 @@ public function show(Subject $subject, $path) return $node->resources()->get()->toArray(); }); - $upvotesCount = $node->upvotes()->count(); - $downvotesCount = $node->downvotes()->count(); - $userVote = auth()->check() - ? $node->votes()->where('user_id', auth()->id())->value('type') - : null; - - $upvoters = $node->upvotes() - ->with('user:id,name,username,image_path,institution,is_verified') - ->latest('id') - ->limit(50) - ->get() - ->pluck('user') - ->filter() - ->values(); - return Inertia::render('Node', [ 'subject' => $subject, 'currentNode' => [ @@ -74,44 +57,6 @@ public function show(Subject $subject, $path) 'nodes' => $nodes, 'breadcrumb' => $node->breadcrumb(), 'resources' => $resources, - 'upvotesCount' => $upvotesCount, - 'downvotesCount' => $downvotesCount, - 'userVote' => $userVote, - 'upvoters' => $upvoters, - ]); - } - - public function vote(Request $request, Node $node) - { - $validated = $request->validate([ - 'type' => ['required', 'in:up,down'], ]); - - $user = auth()->user(); - $existing = $node->votes()->where('user_id', $user->id)->first(); - $isFirstTimeUpvote = false; - - if ($existing) { - if ($existing->type === $validated['type']) { - $existing->delete(); - } else { - $existing->update(['type' => $validated['type']]); - } - } else { - $node->votes()->create([ - 'user_id' => $user->id, - 'type' => $validated['type'], - ]); - - if ($validated['type'] === 'up') { - $isFirstTimeUpvote = true; - } - } - - if ($isFirstTimeUpvote && $node->user_id && $node->user_id !== $user->id) { - NodeVoteNotification::sendTo($node->user, $node, $user); - } - - return back(); } } diff --git a/app/Http/Controllers/SubjectController.php b/app/Http/Controllers/SubjectController.php index a4a1cd84..bc395e85 100644 --- a/app/Http/Controllers/SubjectController.php +++ b/app/Http/Controllers/SubjectController.php @@ -104,7 +104,7 @@ public function show(Subject $subject) $nodes = Cache::rememberForever("subject_page_{$subject->id}", function () use ($subject) { return Node::where('subject_id', $subject->id) ->whereNull('parent_id') - ->withCount(['children', 'resources', 'upvotes', 'downvotes']) + ->withCount(['children', 'resources']) ->orderBy('sort_order') ->get(['id', 'name', 'slug'])->toArray(); }); @@ -115,10 +115,6 @@ public function show(Subject $subject) 'nodes' => $nodes, 'breadcrumb' => [], 'resources' => [], - 'upvotesCount' => 0, - 'downvotesCount' => 0, - 'userVote' => null, - 'upvoters' => [], ]); } } diff --git a/app/Http/Controllers/UserProfileController.php b/app/Http/Controllers/UserProfileController.php index e219951c..634eb387 100644 --- a/app/Http/Controllers/UserProfileController.php +++ b/app/Http/Controllers/UserProfileController.php @@ -9,7 +9,6 @@ use App\Models\DailyStudyLog; use App\Models\ForumAnswer; use App\Models\ForumPost; -use App\Models\Node; use App\Models\NodeCompletion; use App\Models\Resource; use App\Models\Subject; @@ -179,30 +178,6 @@ public function show(string $username) ]) ->filter(fn ($item) => $item['url'] !== null); - $recentFolders = Node::where('user_id', $user->id) - ->with([ - 'subject:id,name,slug', - 'parent:id,name,slug', - 'parent.parent:id,name,slug', - ]) - ->latest('id') - ->take(3) - ->get() - ->map(function ($node) { - $url = $this->buildNodeUrl($node); - - return [ - 'type' => 'folder', - 'title' => $node->name, - 'subtitle' => $node->subject?->name.($node->parent ? ' · '.$node->parent->name : ''), - 'url' => $url, - 'created_at' => $node->created_at?->diffForHumans(), - 'timestamp' => $node->created_at?->timestamp ?? 0, - ]; - }) - ->filter() - ->values(); - $recentUploads = Resource::where('user_id', $user->id) ->with(['node:id,name,subject_id', 'node.subject:id,name']) ->latest() @@ -281,7 +256,6 @@ public function show(string $username) 'recentActivities' => [ 'forum_posts' => $recentForumPosts->values(), 'forum_answers' => $recentForumAnswers->values(), - 'folders' => $recentFolders->values(), 'uploads' => $recentUploads->values(), 'reactions' => $recentReactions->values(), 'comments' => $recentComments->values(), @@ -373,22 +347,6 @@ public function toggleAppreciate(User $user) return back(); } - private function buildNodeUrl(Node $node): ?string - { - if (! $node->subject) { - return null; - } - - $slugs = []; - $curr = $node; - while ($curr) { - array_unshift($slugs, $curr->slug); - $curr = $curr->parent; - } - - return '/'.$node->subject->slug.'/'.implode('/', $slugs); - } - private function getSyllabusProgress(User $user): array { $course = $user->curriculum ?: 'hsc'; diff --git a/app/Models/Node.php b/app/Models/Node.php index 2310a684..f5eb2605 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -30,8 +30,6 @@ protected function casts(): array 'is_frozen' => 'boolean', 'children_count' => 'integer', 'resources_count' => 'integer', - 'upvotes_count' => 'integer', - 'downvotes_count' => 'integer', ]; } @@ -107,21 +105,6 @@ public function resources() return $this->hasMany(Resource::class); } - public function votes() - { - return $this->hasMany(NodeVote::class); - } - - public function upvotes() - { - return $this->hasMany(NodeVote::class)->where('type', 'up'); - } - - public function downvotes() - { - return $this->hasMany(NodeVote::class)->where('type', 'down'); - } - public function completions() { return $this->hasMany(NodeCompletion::class); diff --git a/app/Models/NodeVote.php b/app/Models/NodeVote.php deleted file mode 100644 index a385a4ee..00000000 --- a/app/Models/NodeVote.php +++ /dev/null @@ -1,28 +0,0 @@ -belongsTo(Node::class); - } - - public function user(): BelongsTo - { - return $this->belongsTo(User::class); - } -} diff --git a/app/Models/User.php b/app/Models/User.php index 99e9c875..8d0d2d87 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -170,11 +170,6 @@ public function completedResources() return $this->belongsToMany(Resource::class, 'resource_completions'); } - public function nodeVotes(): HasMany - { - return $this->hasMany(NodeVote::class); - } - public function nodes(): HasMany { return $this->hasMany(Node::class); diff --git a/app/Notifications/NodeVoteNotification.php b/app/Notifications/NodeVoteNotification.php deleted file mode 100644 index 896c150a..00000000 --- a/app/Notifications/NodeVoteNotification.php +++ /dev/null @@ -1,80 +0,0 @@ -id === $voter->id) { - return; - } - - $unread = $targetUser->unreadNotifications() - ->where('type', self::class) - ->get() - ->first(fn ($n) => ($n->data['node_id'] ?? null) === $node->id); - - if ($unread) { - $data = $unread->data; - $count = ($data['count'] ?? 1) + 1; - $others = $count - 1; - $othersText = $others === 1 ? '1 other person' : "{$others} other people"; - - $data['count'] = $count; - $data['title'] = "{$voter->name} and {$othersText} upvoted your folder"; - $data['message'] = "\"{$node->name}\""; - - $unread->update([ - 'data' => $data, - 'created_at' => now(), - ]); - - Cache::forever("user:{$targetUser->id}:has_unread_notifs", true); - } else { - $targetUser->notify(new self($node, $voter)); - } - } - - public function via(object $notifiable): array - { - return ['database']; - } - - public function toArray(object $notifiable): array - { - $subject = $this->node->subject; - if ($subject) { - $path = implode('/', array_column($this->node->breadcrumb(), 'slug')); - $url = url("/{$subject->slug}/{$path}"); - } else { - $url = url('/'); - } - - return [ - 'type' => 'node_vote', - 'title' => "{$this->voter->name} upvoted your folder", - 'message' => "\"{$this->node->name}\"", - 'url' => $url, - 'node_id' => $this->node->id, - 'count' => 1, - ]; - } -} diff --git a/app/Observers/NodeVoteObserver.php b/app/Observers/NodeVoteObserver.php deleted file mode 100644 index 843dfdef..00000000 --- a/app/Observers/NodeVoteObserver.php +++ /dev/null @@ -1,38 +0,0 @@ -clearVoteCache($nodeVote); - } - - public function updated(NodeVote $nodeVote): void - { - $this->clearVoteCache($nodeVote); - } - - public function deleted(NodeVote $nodeVote): void - { - $this->clearVoteCache($nodeVote); - } - - private function clearVoteCache(NodeVote $nodeVote): void - { - $node = $nodeVote->node; - if (! $node) { - return; - } - - if ($node->parent_id) { - Cache::forget("node_children_{$node->parent_id}"); - } else { - Cache::forget("subject_page_{$node->subject_id}"); - } - } -} diff --git a/app/Observers/ResourceObserver.php b/app/Observers/ResourceObserver.php index 88937b07..2d0f24cf 100644 --- a/app/Observers/ResourceObserver.php +++ b/app/Observers/ResourceObserver.php @@ -2,26 +2,11 @@ namespace App\Observers; -use App\Models\Node; use App\Models\Resource; use Illuminate\Support\Facades\Cache; class ResourceObserver { - public function creating(Resource $resource): void - { - if ($resource->node_id && $resource->user_id) { - $otherResourcesCount = Resource::where('node_id', $resource->node_id)->count(); - - 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 saved(Resource $resource): void { $this->clearCache($resource); diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 535c066c..cb361a22 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,7 +4,6 @@ use App\Models\Blog; use App\Models\Node; -use App\Models\NodeVote; use App\Models\Notice; use App\Models\Product; use App\Models\Resource; @@ -12,7 +11,6 @@ use App\Models\User; use App\Observers\BlogObserver; use App\Observers\NodeObserver; -use App\Observers\NodeVoteObserver; use App\Observers\NoticeObserver; use App\Observers\ProductObserver; use App\Observers\ResourceObserver; @@ -45,7 +43,6 @@ public function boot(): void Blog::observe(BlogObserver::class); Node::observe(NodeObserver::class); - NodeVote::observe(NodeVoteObserver::class); Notice::observe(NoticeObserver::class); Product::observe(ProductObserver::class); Resource::observe(ResourceObserver::class); diff --git a/database/migrations/2026_10_22_094417_drop_node_votes_table.php b/database/migrations/2026_10_22_094417_drop_node_votes_table.php new file mode 100644 index 00000000..44c07c79 --- /dev/null +++ b/database/migrations/2026_10_22_094417_drop_node_votes_table.php @@ -0,0 +1,37 @@ +where('type', 'App\Notifications\NodeVoteNotification') + ->delete(); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::create('node_votes', function (Blueprint $table) { + $table->id(); + $table->foreignId('node_id')->constrained()->cascadeOnDelete(); + $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->enum('type', ['up', 'down']); + $table->timestamps(); + + $table->unique(['node_id', 'user_id']); + }); + } +}; diff --git a/resources/js/components/NodeRow.vue b/resources/js/components/NodeRow.vue index cbcb1fd1..360484cc 100644 --- a/resources/js/components/NodeRow.vue +++ b/resources/js/components/NodeRow.vue @@ -1,6 +1,6 @@ diff --git a/resources/js/pages/User/Show.vue b/resources/js/pages/User/Show.vue index 9c84da6e..69056f48 100644 --- a/resources/js/pages/User/Show.vue +++ b/resources/js/pages/User/Show.vue @@ -299,7 +299,6 @@ const sortedActivities = computed(() => { const list: ActivityItem[] = [ ...((props.recentActivities.forum_posts || []) as ActivityItem[]), ...((props.recentActivities.forum_answers || []) as ActivityItem[]), - ...((props.recentActivities.folders || []) as ActivityItem[]), ...((props.recentActivities.uploads || []) as ActivityItem[]), ...((props.recentActivities.reactions || []) as ActivityItem[]), ...((props.recentActivities.comments || []) as ActivityItem[]), diff --git a/routes/web.php b/routes/web.php index 64ddefc7..fd930bbc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -82,7 +82,6 @@ Route::post('/tracker/log-time', [StudyTrackerController::class, 'logTime'])->name('tracker.log-time'); Route::post('/tracker/reset-today', [StudyTrackerController::class, 'resetToday'])->name('tracker.reset-today'); Route::post('/tracker/curriculum', [StudyTrackerController::class, 'updateCurriculum'])->name('tracker.curriculum'); - Route::post('/nodes/{node}/vote', [NodeController::class, 'vote'])->name('nodes.vote'); Route::post('/u/{user}/appreciate', [UserProfileController::class, 'toggleAppreciate'])->name('user.appreciate'); Route::post('/u/{user}/poke', [UserProfileController::class, 'poke'])->name('user.poke'); Route::get('/support/my-tickets', [SupportTicketController::class, 'myTickets'])->name('support.my-tickets'); diff --git a/tests/Feature/NodeVoteNotificationTest.php b/tests/Feature/NodeVoteNotificationTest.php deleted file mode 100644 index 0d6d8c2e..00000000 --- a/tests/Feature/NodeVoteNotificationTest.php +++ /dev/null @@ -1,235 +0,0 @@ -seed(RolePermissionSeeder::class); -}); - -test('upvoting a study node sends NodeVoteNotification to node contributor', function () { - Notification::fake(); - - $author = User::factory()->create(); - $voter = User::factory()->create(['name' => 'Diligent Student']); - $subject = Subject::create([ - 'name' => 'Physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'slug' => 'physics-hsc-node', - 'icon' => 'atom', - 'sort_order' => 1, - ]); - - $node = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Vectors and Kinematics', - 'slug' => 'vectors-and-kinematics', - 'sort_order' => 1, - ]); - - $this->actingAs($voter)->post(route('nodes.vote', $node->id), [ - 'type' => 'up', - ]); - - Notification::assertSentTo($author, NodeVoteNotification::class, function ($notification) use ($node, $voter) { - return $notification->node->id === $node->id - && $notification->voter->id === $voter->id; - }); -}); - -test('downvoting a study node does not trigger NodeVoteNotification', function () { - Notification::fake(); - - $author = User::factory()->create(); - $voter = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'slug' => 'physics-hsc-node-down', - 'icon' => 'atom', - 'sort_order' => 1, - ]); - - $node = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Vectors and Kinematics', - 'slug' => 'vectors-and-kinematics', - 'sort_order' => 1, - ]); - - $this->actingAs($voter)->post(route('nodes.vote', $node->id), [ - 'type' => 'down', - ]); - - Notification::assertNothingSent(); -}); - -test('author upvoting own study node does not trigger NodeVoteNotification', function () { - Notification::fake(); - - $author = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'slug' => 'physics-hsc-node-self', - 'icon' => 'atom', - 'sort_order' => 1, - ]); - - $node = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Vectors and Kinematics', - 'slug' => 'vectors-and-kinematics', - 'sort_order' => 1, - ]); - - $this->actingAs($author)->post(route('nodes.vote', $node->id), [ - 'type' => 'up', - ]); - - Notification::assertNothingSent(); -}); - -test('repeatedly toggling node vote does not trigger spam notifications', function () { - Notification::fake(); - - $author = User::factory()->create(); - $voter = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'slug' => 'physics-hsc-node-spam', - 'icon' => 'atom', - 'sort_order' => 1, - ]); - - $node = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Vectors and Kinematics', - 'slug' => 'vectors-and-kinematics', - 'sort_order' => 1, - ]); - - // 1st initial upvote -> sends notification - $this->actingAs($voter)->post(route('nodes.vote', $node->id), ['type' => 'up']); - Notification::assertSentToTimes($author, NodeVoteNotification::class, 1); - - // 2nd toggle to downvote -> NO notification - $this->actingAs($voter)->post(route('nodes.vote', $node->id), ['type' => 'down']); - Notification::assertSentToTimes($author, NodeVoteNotification::class, 1); - - // 3rd toggle back to upvote -> NO notification - $this->actingAs($voter)->post(route('nodes.vote', $node->id), ['type' => 'up']); - Notification::assertSentToTimes($author, NodeVoteNotification::class, 1); -}); - -test('NodeVoteNotification is strictly database only', function () { - $author = User::factory()->create(['receive_emails' => true]); - $voter = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'slug' => 'physics-hsc-node-db', - 'icon' => 'atom', - 'sort_order' => 1, - ]); - - $node = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Vectors and Kinematics', - 'slug' => 'vectors-and-kinematics', - 'sort_order' => 1, - ]); - - $notif = new NodeVoteNotification($node, $voter); - expect($notif->via($author))->toBe(['database']); -}); - -test('NodeVoteNotification constructs full nested path for child folders', function () { - $author = User::factory()->create(); - $voter = User::factory()->create(['name' => 'Diligent Student']); - $subject = Subject::create([ - 'name' => 'Higher Math', - 'course' => 'hsc', - 'tailwind_format' => 'bg-emerald-500', - 'slug' => 'hsc-hmath-1st', - 'icon' => 'calculator', - 'sort_order' => 1, - ]); - - $parent = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Circles', - 'slug' => 'circles', - 'sort_order' => 1, - ]); - - $child = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'parent_id' => $parent->id, - 'name' => 'Class', - 'slug' => 'class', - 'sort_order' => 1, - ]); - - $notif = new NodeVoteNotification($child, $voter); - $data = $notif->toArray($author); - - expect($data['url'])->toBe(url('/hsc-hmath-1st/circles/class')) - ->and($data['title'])->toBe('Diligent Student upvoted your folder') - ->and($data['message'])->toBe('"Class"'); -}); - -test('multiple upvotes on a node aggregate into a single unread notification with rolled up titles', function () { - $author = User::factory()->create(); - $voter1 = User::factory()->create(['name' => 'Alice']); - $voter2 = User::factory()->create(['name' => 'Bob']); - $subject = Subject::create([ - 'name' => 'Physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'slug' => 'physics-rollup', - 'icon' => 'atom', - 'sort_order' => 1, - ]); - - $node = Node::create([ - 'user_id' => $author->id, - 'subject_id' => $subject->id, - 'name' => 'Electromagnetism', - 'slug' => 'electromagnetism', - 'sort_order' => 1, - ]); - - // 1st upvote - $this->actingAs($voter1)->post(route('nodes.vote', $node->id), ['type' => 'up']); - expect($author->unreadNotifications()->count())->toBe(1); - - $notif = $author->unreadNotifications()->first(); - expect($notif->data['title'])->toBe('Alice upvoted your folder') - ->and($notif->data['count'])->toBe(1); - - // 2nd upvote - $this->actingAs($voter2)->post(route('nodes.vote', $node->id), ['type' => 'up']); - expect($author->unreadNotifications()->count())->toBe(1); - - $notif->refresh(); - expect($notif->data['title'])->toBe('Bob and 1 other person upvoted your folder') - ->and($notif->data['count'])->toBe(2); -}); diff --git a/tests/Feature/NodeVoteTest.php b/tests/Feature/NodeVoteTest.php deleted file mode 100644 index 16be6092..00000000 --- a/tests/Feature/NodeVoteTest.php +++ /dev/null @@ -1,447 +0,0 @@ - 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - $node = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - ]); - - $this->post("/nodes/{$node->id}/vote", ['type' => 'up']) - ->assertRedirect('/login'); - - $this->assertDatabaseMissing('node_votes', [ - 'node_id' => $node->id, - ]); -}); - -test('authenticated user can upvote a folder', function () { - $user = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - $node = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - ]); - - $this->actingAs($user) - ->post("/nodes/{$node->id}/vote", ['type' => 'up']) - ->assertRedirect(); - - $this->assertDatabaseHas('node_votes', [ - 'node_id' => $node->id, - 'user_id' => $user->id, - 'type' => 'up', - ]); -}); - -test('upvoting an already upvoted folder removes the vote (toggle off)', function () { - $user = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - $node = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - ]); - - NodeVote::create([ - 'node_id' => $node->id, - 'user_id' => $user->id, - 'type' => 'up', - ]); - - $this->actingAs($user) - ->post("/nodes/{$node->id}/vote", ['type' => 'up']) - ->assertRedirect(); - - $this->assertDatabaseMissing('node_votes', [ - 'node_id' => $node->id, - 'user_id' => $user->id, - ]); -}); - -test('authenticated user can downvote a folder and toggle it off', function () { - $user = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - $node = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - ]); - - // Downvote - $this->actingAs($user) - ->post("/nodes/{$node->id}/vote", ['type' => 'down']) - ->assertRedirect(); - - $this->assertDatabaseHas('node_votes', [ - 'node_id' => $node->id, - 'user_id' => $user->id, - 'type' => 'down', - ]); - - // Toggle off - $this->actingAs($user) - ->post("/nodes/{$node->id}/vote", ['type' => 'down']) - ->assertRedirect(); - - $this->assertDatabaseMissing('node_votes', [ - 'node_id' => $node->id, - 'user_id' => $user->id, - ]); -}); - -test('authenticated user switching vote between up and down updates type', function () { - $user = User::factory()->create(); - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - $node = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - ]); - - // Start with upvote - $this->actingAs($user) - ->post("/nodes/{$node->id}/vote", ['type' => 'up']) - ->assertRedirect(); - - $this->assertDatabaseHas('node_votes', [ - 'node_id' => $node->id, - 'user_id' => $user->id, - 'type' => 'up', - ]); - - // Switch to downvote - $this->actingAs($user) - ->post("/nodes/{$node->id}/vote", ['type' => 'down']) - ->assertRedirect(); - - $this->assertDatabaseHas('node_votes', [ - 'node_id' => $node->id, - 'user_id' => $user->id, - 'type' => 'down', - ]); - $this->assertEquals(1, NodeVote::where('node_id', $node->id)->where('user_id', $user->id)->count()); -}); - -test('top level chapters under subject are sorted strictly by sort_order', function () { - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - - $chapter1 = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1: Vectors', - 'slug' => 'chapter-1', - 'sort_order' => 1, - ]); - - $chapter2 = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 2: Dynamics', - 'slug' => 'chapter-2', - 'sort_order' => 2, - ]); - - $chapter3 = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 3: Energy', - 'slug' => 'chapter-3', - 'sort_order' => 3, - ]); - - $user1 = User::factory()->create(); - $user2 = User::factory()->create(); - - // Chapter 3 has 2 upvotes, Chapter 1 has 0 upvotes - NodeVote::create(['node_id' => $chapter3->id, 'user_id' => $user1->id, 'type' => 'up']); - NodeVote::create(['node_id' => $chapter3->id, 'user_id' => $user2->id, 'type' => 'up']); - - // Subject page must still maintain chronological syllabus sequence by sort_order - $response = $this->get('/physics'); - $response->assertInertia(fn (Assert $page) => $page - ->component('Node') - ->has('nodes', 3) - ->where('nodes.0.id', $chapter1->id) - ->where('nodes.1.id', $chapter2->id) - ->where('nodes.2.id', $chapter3->id) - ); -}); - -test('subfolders inside a chapter are sorted by net votes first then sort_order', function () { - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - - $chapter = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - 'sort_order' => 1, - ]); - - // Subfolders uploaded by different contributors inside Chapter 1 - $subA = Node::create([ - 'subject_id' => $subject->id, - 'parent_id' => $chapter->id, - 'name' => 'Notes by Rahim (score +1)', - 'slug' => 'notes-by-rahim', - 'sort_order' => 0, - ]); - - $subB = Node::create([ - 'subject_id' => $subject->id, - 'parent_id' => $chapter->id, - 'name' => 'Notes by Karim (score +3)', - 'slug' => 'notes-by-karim', - 'sort_order' => 0, - ]); - - $subC = Node::create([ - 'subject_id' => $subject->id, - 'parent_id' => $chapter->id, - 'name' => 'Notes by Teacher (score -1)', - 'slug' => 'notes-by-teacher', - 'sort_order' => 0, - ]); - - $user1 = User::factory()->create(); - $user2 = User::factory()->create(); - $user3 = User::factory()->create(); - - // SubB gets 3 upvotes - NodeVote::create(['node_id' => $subB->id, 'user_id' => $user1->id, 'type' => 'up']); - NodeVote::create(['node_id' => $subB->id, 'user_id' => $user2->id, 'type' => 'up']); - NodeVote::create(['node_id' => $subB->id, 'user_id' => $user3->id, 'type' => 'up']); - - // SubA gets 1 upvote - NodeVote::create(['node_id' => $subA->id, 'user_id' => $user1->id, 'type' => 'up']); - - // SubC gets 1 downvote - NodeVote::create(['node_id' => $subC->id, 'user_id' => $user1->id, 'type' => 'down']); - - // Inside Chapter 1, subfolders rank by net votes: SubB (+3) -> SubA (+1) -> SubC (-1) - $response = $this->get('/physics/chapter-1'); - $response->assertInertia(fn (Assert $page) => $page - ->component('Node') - ->has('nodes', 3) - ->where('nodes.0.id', $subB->id) - ->where('nodes.1.id', $subA->id) - ->where('nodes.2.id', $subC->id) - ); -}); - -test('node page shows upvote list and counts while downvote list is not exposed', function () { - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - $node = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Chapter 1', - 'slug' => 'chapter-1', - ]); - - $upvoter = User::factory()->create(['name' => 'Upvoter User']); - $downvoter = User::factory()->create(['name' => 'Downvoter Secret User']); - - NodeVote::create(['node_id' => $node->id, 'user_id' => $upvoter->id, 'type' => 'up']); - NodeVote::create(['node_id' => $node->id, 'user_id' => $downvoter->id, 'type' => 'down']); - - $response = $this->actingAs($upvoter)->get('/physics/chapter-1'); - - $response->assertInertia(fn (Assert $page) => $page - ->component('Node') - ->where('upvotesCount', 1) - ->where('downvotesCount', 1) - ->where('userVote', 'up') - ->has('upvoters', 1) - ->where('upvoters.0.id', $upvoter->id) - ->where('upvoters.0.name', 'Upvoter User') - ->missing('downvoters') - ); -}); - -test('voting on a folder properly clears and refreshes parent node and subject page caches', function () { - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - - $folderA = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Folder A', - 'slug' => 'folder-a', - 'sort_order' => 1, - ]); - - $folderB = Node::create([ - 'subject_id' => $subject->id, - 'name' => 'Folder B', - 'slug' => 'folder-b', - 'sort_order' => 2, - ]); - - $user = User::factory()->create(); - - // 1. Visit subject page to warm the cache - $this->get('/physics') - ->assertInertia(fn (Assert $page) => $page - ->where('nodes.0.upvotes_count', 0) - ->where('nodes.1.upvotes_count', 0) - ); - - expect(Cache::has("subject_page_{$subject->id}"))->toBeTrue(); - - // 2. User votes on Folder B - $this->actingAs($user) - ->post("/nodes/{$folderB->id}/vote", ['type' => 'up']) - ->assertRedirect(); - - // Cache should have been invalidated - expect(Cache::has("subject_page_{$subject->id}"))->toBeFalse(); - - // 3. Next visit to subject page reflects Folder B with updated upvote count - $this->get('/physics') - ->assertInertia(fn (Assert $page) => $page - ->where('nodes.1.id', $folderB->id) - ->where('nodes.1.upvotes_count', 1) - ); - - // 4. Test nested child node caching - $subfolderA = Node::create([ - 'subject_id' => $subject->id, - 'parent_id' => $folderA->id, - 'name' => 'Subfolder A', - 'slug' => 'subfolder-a', - 'sort_order' => 0, - ]); - - $subfolderB = Node::create([ - 'subject_id' => $subject->id, - 'parent_id' => $folderA->id, - 'name' => 'Subfolder B', - 'slug' => 'subfolder-b', - 'sort_order' => 0, - ]); - - // Warm child cache - $this->get("/physics/{$folderA->slug}") - ->assertInertia(fn (Assert $page) => $page - ->where('nodes.0.upvotes_count', 0) - ->where('nodes.1.upvotes_count', 0) - ); - - expect(Cache::has("node_children_{$folderA->id}"))->toBeTrue(); - - // Vote on Subfolder B - $this->actingAs($user) - ->post("/nodes/{$subfolderB->id}/vote", ['type' => 'up']) - ->assertRedirect(); - - // Cache should be cleared - expect(Cache::has("node_children_{$folderA->id}"))->toBeFalse(); - - // Next visit reflects new order and count - $this->get("/physics/{$folderA->slug}") - ->assertInertia(fn (Assert $page) => $page - ->where('nodes.0.id', $subfolderB->id) - ->where('nodes.0.upvotes_count', 1) - ); -}); - -test('first resource upload in an empty node automatically updates node user_id to the contributor', function () { - $admin = User::factory()->create([ - 'name' => 'Admin User', - 'email' => 'admin@example.com', - ]); - - $contributor = User::factory()->create([ - 'name' => 'Contributor Rahim', - 'email' => 'rahim@example.com', - 'receive_emails' => true, - ]); - - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-indigo-500', - 'icon' => 'atom', - ]); - - // Admin pre-creates an empty folder skeleton - $folder = Node::create([ - 'user_id' => $admin->id, - 'subject_id' => $subject->id, - 'name' => 'Chapter 1 Skeleton', - 'slug' => 'chapter-1-skeleton', - ]); - - expect($folder->user_id)->toBe($admin->id); - - // Contributor uploads the first resource - App\Models\Resource::create([ - 'node_id' => $folder->id, - 'user_id' => $contributor->id, - 'title' => 'Lecture Note PDF', - 'resource_type' => 'note', - ]); - - // Node owner should now be automatically updated to the contributor - $folder->refresh(); - expect($folder->user_id)->toBe($contributor->id); -}); diff --git a/tests/Feature/UserProfileTest.php b/tests/Feature/UserProfileTest.php index adb9d939..416573ee 100644 --- a/tests/Feature/UserProfileTest.php +++ b/tests/Feature/UserProfileTest.php @@ -169,33 +169,6 @@ ); }); -test('public profile displays created folders in recent activity', function () { - $user = User::factory()->create(['username' => 'voter_student']); - $subject = Subject::create([ - 'name' => 'Higher Math', - 'slug' => 'higher-math', - 'course' => 'hsc', - 'tailwind_format' => 'bg-emerald-500', - 'icon' => 'calculator', - ]); - - $parentFolder = Node::create([ - 'user_id' => $user->id, - 'subject_id' => $subject->id, - 'name' => 'Calculus', - 'slug' => 'calculus', - ]); - - $response = $this->get('/u/voter_student'); - - $response->assertOk(); - $response->assertInertia(fn ($page) => $page - ->component('User/Show') - ->has('recentActivities.folders', 1) - ->where('recentActivities.folders.0.title', 'Calculus') - ); -}); - test('user model has is_verified boolean column and is single source of truth', function () { $regularUser = User::factory()->create(['is_verified' => false]); expect($regularUser->is_verified)->toBeFalse(); @@ -205,36 +178,6 @@ expect($verifiedUser->toArray())->toHaveKey('is_verified', true); }); -test('public profile returns created folders in recent activities', function () { - $user = User::factory()->create(['username' => 'folder_creator']); - - $subject = Subject::create([ - 'name' => 'Physics', - 'slug' => 'physics', - 'course' => 'hsc', - 'tailwind_format' => 'bg-blue-500', - 'icon' => 'atom', - ]); - - Node::create([ - 'user_id' => $user->id, - 'subject_id' => $subject->id, - 'name' => 'Thermodynamics Chapter', - 'slug' => 'thermodynamics-chapter', - ]); - - $response = $this->get('/u/folder_creator'); - - $response->assertOk(); - $response->assertInertia(fn ($page) => $page - ->component('User/Show') - ->has('recentActivities.folders', 1) - ->where('recentActivities.folders.0.title', 'Thermodynamics Chapter') - ->where('recentActivities.folders.0.subtitle', 'Physics') - ->where('recentActivities.folders.0.url', '/physics/thermodynamics-chapter') - ); -}); - test('profile update rejects inappropriate words in name, username, about, or institution', function () { AppSetting::set('global_chat_banned_words', 'badword, offensive'); From 5fd34d02fe43c1c66ae440eef9137a2d0462e1ab Mon Sep 17 00:00:00 2001 From: TR Tajim Date: Thu, 22 Oct 2026 16:01:10 +0600 Subject: [PATCH 02/31] feat: enforce mutual exclusivity of folders and resources and restrict uploads to leaf nodes --- app/Http/Controllers/NodeController.php | 10 +++++++--- resources/js/pages/Node.vue | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/NodeController.php b/app/Http/Controllers/NodeController.php index 5bb9811b..70a65768 100644 --- a/app/Http/Controllers/NodeController.php +++ b/app/Http/Controllers/NodeController.php @@ -42,9 +42,13 @@ public function show(Subject $subject, $path) ->get(['id', 'name', 'slug'])->toArray(); }); - $resources = Cache::remember("node_resources_{$node->id}", now()->addDay(), function () use ($node) { - return $node->resources()->get()->toArray(); - }); + $resources = []; + + if (empty($nodes)) { + $resources = Cache::remember("node_resources_{$node->id}", now()->addDay(), function () use ($node) { + return $node->resources()->get()->toArray(); + }); + } return Inertia::render('Node', [ 'subject' => $subject, diff --git a/resources/js/pages/Node.vue b/resources/js/pages/Node.vue index bd4b4a31..f734dce8 100644 --- a/resources/js/pages/Node.vue +++ b/resources/js/pages/Node.vue @@ -97,19 +97,21 @@ const totalItemsCount = computed( > Name
-