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