Skip to content

Commit a16c60a

Browse files
authored
feat(feed): implement endless feed, filter modal, and forum refinement(#432)
* feat(feed): implement unified endless feed, filter modal, and peer search integration * feat(feed): set feed as first nav item, remove people nav, and update forum test suite for body-first questions * fix(feed): address reviewer feedback, backfill title to body in migration, add snippet helper, and hydrate user votes * refactor(forum): generate slug directly in controller and remove static creating hook * test(forum): supply explicit slugs in test fixtures and import Str in Admin ForumController
1 parent fe2f0bb commit a16c60a

37 files changed

Lines changed: 2685 additions & 397 deletions

‎GEMINI.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ npm run format && composer lint && npm run lint
3737
## Strict Code Modification & Execution Guardrail
3838
- **Explicit Instruction Required**: NEVER modify files, apply code edits, or execute code refactors unless the user explicitly gives direct instruction or confirmation to make the change (e.g., "do it", "apply this", "fix it", "proceed").
3939
- **Exploratory / Question Turns**: When diagnosing bugs, answering questions, explaining behavior, or exploring solutions, provide explanations and code snippets in the response text ONLY. Do not apply file changes until confirmed.
40+
- **Clarification Over Assumptions**: If you are confused, uncertain, or need clarification on requirements or behavior, ask the user directly instead of attempting to discover or guess it on your own.

‎app/Console/Commands/GenerateSitemap.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ public function handle(): int
121121

122122
// 6. Latest 100 Answered Forum Questions
123123
ForumPost::approved()
124-
->where(function ($query) {
125-
$query->where('answers_count', '>', 0)
126-
->orWhere('is_answered', true);
127-
})
124+
->where('answers_count', '>', 0)
128125
->orderByDesc('updated_at')
129126
->take(100)
130127
->get(['slug', 'updated_at'])

‎app/Http/Controllers/Admin/ForumController.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Database\Eloquent\Relations\MorphTo;
1212
use Illuminate\Http\Request;
1313
use Illuminate\Support\Facades\Storage;
14+
use Illuminate\Support\Str;
1415
use Inertia\Inertia;
1516

1617
class ForumController extends Controller
@@ -31,8 +32,7 @@ public function index(Request $request)
3132
})
3233
->when($filters['search'] ?? null, function ($q, $search) {
3334
$q->where(function ($sub) use ($search) {
34-
$sub->where('title', 'like', "%{$search}%")
35-
->orWhere('body', 'like', "%{$search}%")
35+
$sub->where('body', 'like', "%{$search}%")
3636
->orWhereHas('user', function ($u) use ($search) {
3737
$u->where('name', 'like', "%{$search}%")
3838
->orWhere('username', 'like', "%{$search}%");
@@ -112,7 +112,7 @@ public function reports()
112112
'reportedUser:id,name,username,banned_until',
113113
'reportable' => function (MorphTo $morphTo) {
114114
$morphTo->morphWith([
115-
ForumAnswer::class => ['post:id,title,slug'],
115+
ForumAnswer::class => ['post:id,body,slug'],
116116
]);
117117
},
118118
])
@@ -126,10 +126,10 @@ public function reports()
126126

127127
if ($report->reportable instanceof ForumPost) {
128128
$postSlug = $report->reportable->slug;
129-
$postTitle = $report->reportable->title;
129+
$postTitle = Str::words(strip_tags($report->reportable->body ?? ''), 8, '...');
130130
} elseif ($report->reportable instanceof ForumAnswer) {
131131
$postSlug = $report->reportable->post?->slug;
132-
$postTitle = $report->reportable->post?->title;
132+
$postTitle = Str::words(strip_tags($report->reportable->post?->body ?? ''), 8, '...');
133133
}
134134

135135
return [

‎app/Http/Controllers/ForumController.php‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Illuminate\Support\Facades\DB;
2121
use Illuminate\Support\Facades\Notification;
2222
use Illuminate\Support\Facades\Storage;
23+
use Illuminate\Support\Str;
2324
use Inertia\Inertia;
2425
use Inertia\Response;
2526

@@ -214,8 +215,7 @@ public function store(Request $request): RedirectResponse
214215
}
215216

216217
$validated = $request->validate([
217-
'title' => ['required', 'string', 'min:5', 'max:255', new CleanText],
218-
'body' => ['nullable', 'string', 'max:30000', new CleanText],
218+
'body' => ['required', 'string', 'min:5', 'max:30000', new CleanText],
219219
'curriculum' => ['required', 'in:hsc,ssc'],
220220
'subject_id' => ['nullable', 'exists:subjects,id'],
221221
'node_id' => ['nullable', 'exists:nodes,id'],
@@ -248,13 +248,16 @@ public function store(Request $request): RedirectResponse
248248
$approvalMode = AppSetting::get('forum_approval_mode', 'auto');
249249
$moderationStatus = $approvalMode === 'auto' ? 'approved' : 'pending';
250250

251+
$baseSlug = Str::limit(Str::slug(Str::words(strip_tags($validated['body']), 8, '')) ?: 'post', 200, '');
252+
$slug = $baseSlug.'-'.Str::lower(Str::random(6));
253+
251254
$post = ForumPost::create([
252255
'user_id' => auth()->id(),
253256
'subject_id' => $subjectId,
254257
'node_id' => $nodeId,
255258
'curriculum' => $validated['curriculum'],
256-
'title' => $validated['title'],
257-
'body' => $validated['body'] ?? null,
259+
'slug' => $slug,
260+
'body' => $validated['body'],
258261
'image_path' => $imagePath,
259262
'is_locked' => false,
260263
'moderation_status' => $moderationStatus,
@@ -295,17 +298,6 @@ public function destroy(ForumPost $post): RedirectResponse
295298
return redirect()->route('forum.index')->with('success', 'Question deleted successfully.');
296299
}
297300

298-
public function toggleAnswered(Request $request, ForumPost $post): RedirectResponse
299-
{
300-
abort_unless(auth()->id() === $post->user_id || auth()->user()?->can('manage forums'), 403);
301-
302-
$post->update([
303-
'is_answered' => ! $post->is_answered,
304-
]);
305-
306-
return back();
307-
}
308-
309301
public function report(Request $request, ForumPost $post): JsonResponse
310302
{
311303
$user = auth()->user();
@@ -329,7 +321,7 @@ public function report(Request $request, ForumPost $post): JsonResponse
329321
'reported_user_id' => $author?->id,
330322
'reported_user_name' => $author?->name,
331323
'reported_user_username' => $author?->username,
332-
'content_snapshot' => $post->title."\n\n".$post->body,
324+
'content_snapshot' => $post->body,
333325
'reason' => $validated['reason'],
334326
'status' => 'pending',
335327
]);
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Blog;
6+
use App\Models\ForumPost;
7+
use App\Models\ForumVote;
8+
use App\Models\Notice;
9+
use App\Models\Resource;
10+
use App\Models\ResourceVote;
11+
use App\Models\Subject;
12+
use App\Models\UserAppreciation;
13+
use Illuminate\Http\Request;
14+
use Illuminate\Support\Facades\Auth;
15+
use Inertia\Inertia;
16+
17+
class HomeController extends Controller
18+
{
19+
public function index(Request $request)
20+
{
21+
$course = in_array($request->query('course'), ['hsc', 'ssc'], true) ? $request->query('course') : 'hsc';
22+
$group = in_array($request->query('group'), ['science', 'commerce', 'humanities'], true) ? $request->query('group') : null;
23+
$sort = in_array($request->query('sort'), ['trending', 'recent'], true) ? $request->query('sort') : 'trending';
24+
$page = max(1, (int) $request->query('page', 1));
25+
26+
$feed = $this->getFeed($course, $group, $sort, $page);
27+
28+
$subjectsQuery = Subject::where('is_disabled', false)
29+
->where('course', $course);
30+
31+
if ($group) {
32+
$subjectsQuery->whereIn('group', [$group, 'common']);
33+
}
34+
35+
$subjects = $subjectsQuery->orderBy('sort_order', 'asc')
36+
->get(['id', 'name', 'slug', 'course', 'group']);
37+
38+
return Inertia::render('Feed', [
39+
'feed' => $feed,
40+
'subjects' => $subjects,
41+
'course' => $course,
42+
'group' => $group,
43+
'sort' => $sort,
44+
'notice' => Notice::activeForDisplay(),
45+
]);
46+
}
47+
48+
public function feed(Request $request)
49+
{
50+
$course = in_array($request->query('course'), ['hsc', 'ssc'], true) ? $request->query('course') : 'hsc';
51+
$group = in_array($request->query('group'), ['science', 'commerce', 'humanities'], true) ? $request->query('group') : null;
52+
$sort = in_array($request->query('sort'), ['trending', 'recent'], true) ? $request->query('sort') : 'trending';
53+
$page = max(1, (int) $request->query('page', 1));
54+
55+
return response()->json($this->getFeed($course, $group, $sort, $page));
56+
}
57+
58+
private function getFeed(string $course, ?string $group, string $sort, int $page = 1): array
59+
{
60+
$page = max(1, $page);
61+
$currentUserId = Auth::id();
62+
63+
// 1. Forum Posts (6 items)
64+
$postsQuery = ForumPost::approved()
65+
->where('curriculum', $course)
66+
->when($group, function ($q) use ($group) {
67+
$q->where(function ($subQ) use ($group) {
68+
$subQ->whereNull('subject_id')
69+
->orWhereHas('subject', fn ($s) => $s->whereIn('group', [$group, 'common']));
70+
});
71+
})
72+
->when($currentUserId, fn ($q) => $q->where('user_id', '!=', $currentUserId))
73+
->with(['user:id,name,username,image_path,institution,is_verified', 'subject:id,name,slug,group', 'node:id,name,slug'])
74+
->withCount('answers');
75+
76+
// 2. Resources (4 items)
77+
$resourcesQuery = Resource::approved()
78+
->whereHas('node.subject', function ($s) use ($course, $group) {
79+
$s->where('course', $course);
80+
if ($group) {
81+
$s->whereIn('group', [$group, 'common']);
82+
}
83+
})
84+
->when($currentUserId, fn ($q) => $q->where('user_id', '!=', $currentUserId))
85+
->with(['user:id,name,username,image_path,institution,is_verified', 'node:id,name,slug,subject_id', 'node.subject:id,name,course,slug,group']);
86+
87+
// 3. Blogs (2 items)
88+
$blogsQuery = Blog::where('is_published', true)
89+
->when($currentUserId, fn ($q) => $q->where('user_id', '!=', $currentUserId))
90+
->with(['user:id,name,username,image_path,institution,is_verified'])
91+
->withCount(['reactions', 'comments']);
92+
93+
if ($currentUserId) {
94+
$blogsQuery->withExists([
95+
'reactions as is_reacted' => fn ($q) => $q->where('user_id', $currentUserId),
96+
]);
97+
}
98+
99+
if ($sort === 'recent') {
100+
$postsQuery->latest('id');
101+
$resourcesQuery->latest('id');
102+
$blogsQuery->latest('id');
103+
} else {
104+
// Trending: sort by vote_score / reactions/views DESC
105+
$postsQuery->orderByDesc('vote_score')->latest('id');
106+
$resourcesQuery->orderByDesc('vote_score')->latest('id');
107+
$blogsQuery->orderByDesc('views')->latest('id');
108+
}
109+
110+
$posts = $postsQuery->forPage($page, 6)->get()->map(function ($p) {
111+
$p->feed_type = 'forum_post';
112+
113+
return $p;
114+
});
115+
116+
$resources = $resourcesQuery->forPage($page, 4)->get()->map(function ($r) {
117+
$r->feed_type = 'resource';
118+
119+
return $r;
120+
});
121+
122+
$blogs = $blogsQuery->forPage($page, 2)->get()->map(function ($b) {
123+
$b->feed_type = 'blog';
124+
125+
return $b;
126+
});
127+
128+
// Compute is_appreciated and user_vote if logged in
129+
$userId = Auth::id();
130+
if ($userId) {
131+
$authorIds = array_unique(array_filter(array_merge(
132+
$posts->pluck('user_id')->all(),
133+
$resources->pluck('user_id')->all(),
134+
$blogs->pluck('user_id')->all()
135+
)));
136+
137+
$appreciatedAuthorIds = UserAppreciation::where('appreciator_id', $userId)
138+
->whereIn('user_id', $authorIds)
139+
->pluck('user_id')
140+
->flip()
141+
->all();
142+
143+
$forumPostVotes = ForumVote::where('user_id', $userId)
144+
->where('voteable_type', ForumPost::class)
145+
->whereIn('voteable_id', $posts->pluck('id'))
146+
->pluck('value', 'voteable_id')
147+
->all();
148+
149+
$resourceVotes = ResourceVote::where('user_id', $userId)
150+
->whereIn('resource_id', $resources->pluck('id'))
151+
->pluck('value', 'resource_id')
152+
->all();
153+
154+
foreach ($posts as $p) {
155+
if ($p->user) {
156+
$p->user->is_appreciated = isset($appreciatedAuthorIds[$p->user->id]);
157+
}
158+
$p->user_vote = $forumPostVotes[$p->id] ?? null;
159+
}
160+
foreach ($resources as $r) {
161+
if ($r->user) {
162+
$r->user->is_appreciated = isset($appreciatedAuthorIds[$r->user->id]);
163+
}
164+
$r->user_vote = $resourceVotes[$r->id] ?? null;
165+
}
166+
foreach ($blogs as $b) {
167+
if ($b->user) {
168+
$b->user->is_appreciated = isset($appreciatedAuthorIds[$b->user->id]);
169+
}
170+
}
171+
} else {
172+
foreach ($posts as $p) {
173+
if ($p->user) {
174+
$p->user->is_appreciated = false;
175+
}
176+
$p->user_vote = null;
177+
}
178+
foreach ($resources as $r) {
179+
if ($r->user) {
180+
$r->user->is_appreciated = false;
181+
}
182+
$r->user_vote = null;
183+
}
184+
foreach ($blogs as $b) {
185+
if ($b->user) {
186+
$b->user->is_appreciated = false;
187+
}
188+
}
189+
}
190+
191+
// Naturally interleave 6 posts, 4 resources, 2 blogs
192+
$pArr = $posts->values()->all();
193+
$rArr = $resources->values()->all();
194+
$bArr = $blogs->values()->all();
195+
196+
$merged = [];
197+
$pIndex = 0;
198+
$rIndex = 0;
199+
$bIndex = 0;
200+
201+
while ($pIndex < count($pArr) || $rIndex < count($rArr)) {
202+
// Post
203+
if ($pIndex < count($pArr)) {
204+
$merged[] = $pArr[$pIndex++];
205+
}
206+
// Resource
207+
if ($rIndex < count($rArr)) {
208+
$merged[] = $rArr[$rIndex++];
209+
}
210+
// Blog (interleaved every other cycle)
211+
if ($bIndex < count($bArr) && count($merged) % 4 === 2) {
212+
$merged[] = $bArr[$bIndex++];
213+
}
214+
// Another Post to maintain the 6:4:2 ratio
215+
if ($pIndex < count($pArr)) {
216+
$merged[] = $pArr[$pIndex++];
217+
}
218+
// Remaining blog if any
219+
if ($bIndex < count($bArr) && count($merged) % 5 === 0) {
220+
$merged[] = $bArr[$bIndex++];
221+
}
222+
}
223+
224+
// Catch any remaining blogs
225+
while ($bIndex < count($bArr)) {
226+
$merged[] = $bArr[$bIndex++];
227+
}
228+
229+
return [
230+
'data' => $merged,
231+
'has_more' => ($posts->count() === 6 || $resources->count() === 4 || $blogs->count() === 2),
232+
'page' => $page,
233+
];
234+
}
235+
}

‎app/Http/Controllers/UserProfileController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function show(string $username)
133133
->get();
134134
$forumAnswers = ForumAnswer::where('user_id', $user->id)
135135
->whereHas('post', fn ($q) => $q->approved())
136-
->with(['post:id,title,slug,curriculum,is_answered'])
136+
->with(['post:id,body,slug,curriculum'])
137137
->latest()
138138
->take(5)
139139
->get();

0 commit comments

Comments
 (0)