|
| 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 | +} |
0 commit comments