Skip to content

Commit 031ebc1

Browse files
authored
feat: implement note archive library, root feed transition, and blog KaTeX rendering (#435)
* feat(notes): implement note library and archive with cascading filters * feat(feed): set feed as root and add intermediate compose modal * feat(blog): add KaTeX equation rendering and sanitize excerpts * fix(resources): simplify upload key and restore resource edit capabilities * feat(resources): require subject_id and validate chapter-subject consistency * fix(migrations): support sqlite update statement and remove unused /note route * refactor(resources): trust node subject_id when node is present and keep form request simple * fix(tests): import Node in ResourceController and align homepage cache/component tests with feed
1 parent 00538dc commit 031ebc1

27 files changed

Lines changed: 1625 additions & 387 deletions

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Illuminate\Http\Request;
99
use Illuminate\Support\Facades\Auth;
1010
use Illuminate\Support\Facades\DB;
11-
use Illuminate\Support\Facades\Storage;
1211
use Inertia\Inertia;
1312

1413
class ResourceController extends BaseResourceController
@@ -127,14 +126,6 @@ public function reject(Request $request)
127126

128127
public function destroy(Resource $resource)
129128
{
130-
// Clean up stored items in S3/R2
131-
foreach ($resource->items as $item) {
132-
$source = is_array($item) ? ($item['source'] ?? '') : ($item->source ?? '');
133-
if ($source && ! str_starts_with($source, 'http://') && ! str_starts_with($source, 'https://')) {
134-
Storage::disk('s3')->delete($source);
135-
}
136-
}
137-
138129
$resource->delete();
139130

140131
return redirect()->back()->with('success', 'Resource deleted successfully.');

‎app/Http/Controllers/HomeController.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function index(Request $request)
3333
}
3434

3535
$subjects = $subjectsQuery->orderBy('sort_order', 'asc')
36+
->with(['nodes' => function ($q) {
37+
$q->whereNull('parent_id')
38+
->select('id', 'subject_id', 'parent_id', 'name', 'slug')
39+
->orderBy('sort_order');
40+
}])
3641
->get(['id', 'name', 'slug', 'course', 'group']);
3742

3843
return Inertia::render('Feed', [
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Resource;
6+
use App\Models\Subject;
7+
use Illuminate\Http\Request;
8+
use Inertia\Inertia;
9+
use Inertia\Response;
10+
11+
class NoteController extends Controller
12+
{
13+
public function index(Request $request): Response
14+
{
15+
$course = $request->query('curriculum') ?? $request->query('course');
16+
if (! in_array($course, ['hsc', 'ssc'], true)) {
17+
$course = $request->cookie('preferred_course') === 'ssc' ? 'ssc' : 'hsc';
18+
}
19+
20+
$group = $request->query('group', 'science');
21+
if (! in_array($group, ['science', 'commerce', 'humanities'], true)) {
22+
$group = 'science';
23+
}
24+
25+
$sort = $request->query('sort', 'top');
26+
if (! in_array($sort, ['top', 'latest'], true)) {
27+
$sort = 'top';
28+
}
29+
30+
$type = $request->query('type', 'all');
31+
$subjectId = $request->query('subject_id');
32+
$nodeId = $request->query('node_id');
33+
$search = trim($request->query('search', ''));
34+
35+
$query = Resource::query()
36+
->approved()
37+
->with([
38+
'user:id,name,username,image_path,institution,is_verified',
39+
'subject:id,name,course,slug,group',
40+
'node:id,name,slug,subject_id',
41+
]);
42+
43+
// Filter by course and group (unless searching specific @username without explicit track)
44+
$isAuthorSearch = str_starts_with($search, '@');
45+
$hasExplicitTrack = $request->filled('curriculum') || $request->filled('course') || $request->filled('group');
46+
47+
if (! $isAuthorSearch || $hasExplicitTrack) {
48+
$query->where(function ($q) use ($course, $group) {
49+
$q->whereHas('subject', function ($s) use ($course, $group) {
50+
$s->where('course', $course)
51+
->whereIn('group', [$group, 'common']);
52+
})->orWhereHas('node.subject', function ($s) use ($course, $group) {
53+
$s->where('course', $course)
54+
->whereIn('group', [$group, 'common']);
55+
});
56+
});
57+
}
58+
59+
if ($subjectId) {
60+
$query->where(function ($q) use ($subjectId) {
61+
$q->where('subject_id', $subjectId)
62+
->orWhereHas('node', fn ($n) => $n->where('subject_id', $subjectId));
63+
});
64+
}
65+
66+
if ($nodeId === 'whole' || $nodeId === 'general') {
67+
$query->whereNull('node_id');
68+
} elseif (! empty($nodeId) && is_numeric($nodeId)) {
69+
$query->where('node_id', (int) $nodeId);
70+
}
71+
72+
if (in_array($type, ['note', 'video', 'practical', 'other'], true)) {
73+
$query->where('resource_type', $type);
74+
}
75+
76+
if (! empty($search)) {
77+
if ($isAuthorSearch) {
78+
$username = ltrim($search, '@');
79+
$query->whereHas('user', fn ($u) => $u->where('username', $username));
80+
} else {
81+
$query->where(function ($q) use ($search) {
82+
$q->where('title', 'like', "%{$search}%")
83+
->orWhere('description', 'like', "%{$search}%");
84+
});
85+
}
86+
}
87+
88+
if ($sort === 'latest') {
89+
$query->latest('id');
90+
} else {
91+
$query->orderByDesc('vote_score')->latest('id');
92+
}
93+
94+
$resources = $query->simplePaginate(16)->withQueryString();
95+
96+
// Query available subjects for filter dropdown (simple, no caching)
97+
$subjects = Subject::where('is_disabled', false)
98+
->where('course', $course)
99+
->whereIn('group', [$group, 'common'])
100+
->orderBy('sort_order')
101+
->with(['nodes' => function ($q) {
102+
$q->whereNull('parent_id')
103+
->select('id', 'subject_id', 'parent_id', 'name', 'slug')
104+
->orderBy('sort_order');
105+
}])
106+
->get(['id', 'name', 'slug', 'course', 'group'])
107+
->toArray();
108+
109+
return Inertia::render('Notes/Index', [
110+
'resources' => $resources,
111+
'subjects' => $subjects,
112+
'filters' => [
113+
'curriculum' => $course,
114+
'course' => $course,
115+
'group' => $group,
116+
'subject_id' => $subjectId ? (int) $subjectId : null,
117+
'node_id' => $nodeId,
118+
'type' => $type,
119+
'sort' => $sort,
120+
'search' => $search,
121+
],
122+
]);
123+
}
124+
}

‎app/Http/Controllers/ResourceController.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Http\Requests\Resource\StoreResourceRequest;
66
use App\Http\Requests\Resource\UpdateResourceRequest;
7+
use App\Models\Node;
78
use App\Models\Resource;
89
use App\Models\ResourceVote;
910
use App\Notifications\ResourceVoteNotification;
@@ -211,7 +212,7 @@ public function generateUploadUrl(Request $request)
211212
'image/webp' => 'webp',
212213
};
213214

214-
$key = "resources/notes/{$userId}/".(string) Str::uuid().'.'.$extension;
215+
$key = 'resources/notes/'.(string) Str::uuid().'.'.$extension;
215216
$signed = Storage::disk('s3')->temporaryUploadUrl(
216217
$key,
217218
now()->addMinutes(15),
@@ -262,8 +263,14 @@ public function store(StoreResourceRequest $request)
262263
$isModerator = $request->user()->can('moderate resources');
263264
$status = $isModerator ? 'approved' : 'pending';
264265

266+
$nodeId = ! empty($validated['node_id']) ? (int) $validated['node_id'] : null;
267+
$subjectId = $nodeId
268+
? (int) (Node::where('id', $nodeId)->value('subject_id') ?? $validated['subject_id'])
269+
: (int) $validated['subject_id'];
270+
265271
Resource::create([
266-
'node_id' => $validated['node_id'],
272+
'subject_id' => $subjectId,
273+
'node_id' => $nodeId,
267274
'user_id' => Auth::id(),
268275
'resource_type' => $validated['resource_type'],
269276
'title' => $validated['title'],

‎app/Http/Controllers/UserProfileController.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,9 @@ public function show(string $username)
147147

148148
$notes = Resource::where('user_id', $user->id)
149149
->when(! $isOwner, fn ($q) => $q->where('status', 'approved'))
150-
->with(['node:id,name,slug,subject_id', 'node.subject:id,name,course,slug'])
151-
->orderByDesc('vote_score')
150+
->with(['node:id,name,slug,subject_id', 'node.subject:id,name,course,slug', 'subject:id,name,course,slug'])
152151
->latest('id')
153-
->take(8)
152+
->take(5)
154153
->get();
155154

156155
$syllabusProgress = $this->getSyllabusProgress($user);

‎app/Http/Requests/Blog/StoreBlogRequest.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public function authorize(): bool
1515
protected function prepareForValidation(): void
1616
{
1717

18-
$excerpt = Str::limit(strip_tags($this->input('content')), 150);
18+
$content = (string) $this->input('content', '');
19+
$clean = preg_replace('/```[\s\S]*?```|\$\$[\s\S]*?\$\$/', ' [Equation] ', $content);
20+
$clean = preg_replace('/\\$[^$\n]+\\$/', ' [Equation] ', (string) $clean);
21+
$excerpt = Str::limit(trim(preg_replace('/\s+/', ' ', strip_tags((string) $clean))), 150);
1922

2023
$this->merge([
2124
'slug' => Str::slug($this->input('title')),

‎app/Http/Requests/Blog/UpdateBlogRequest.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public function authorize(): bool
1515

1616
protected function prepareForValidation(): void
1717
{
18-
$excerpt = Str::limit(strip_tags($this->input('content')), 150);
18+
$content = (string) $this->input('content', '');
19+
$clean = preg_replace('/```[\s\S]*?```|\$\$[\s\S]*?\$\$/', ' [Equation] ', $content);
20+
$clean = preg_replace('/\\$[^$\n]+\\$/', ' [Equation] ', (string) $clean);
21+
$excerpt = Str::limit(trim(preg_replace('/\s+/', ' ', strip_tags((string) $clean))), 150);
1922

2023
$this->merge([
2124
'slug' => Str::slug($this->input('title')),

‎app/Http/Requests/Resource/ResourceValidationRules.php‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ public static function validateItems(Validator $validator, array $items, ?string
5050
$validator->errors()->add("items.{$index}.source", 'PDF এবং ভিডিওর জন্য সঠিক URL লিঙ্ক প্রদান করতে হবে।');
5151
}
5252
} elseif ($itemFormat === 'image') {
53-
$isAllowedUpload = str_starts_with($source, 'resources/notes/')
54-
&& preg_match('#^resources/notes/([0-9a-zA-Z_-]+/)*[0-9a-zA-Z_.-]+\.(jpg|jpeg|png|webp)$#i', $source);
55-
$isExternalUrl = (str_starts_with($source, 'http://') || str_starts_with($source, 'https://')) && filter_var($source, FILTER_VALIDATE_URL);
56-
57-
if (! $isAllowedUpload && ! $isExternalUrl) {
58-
$validator->errors()->add("items.{$index}.source", 'ছবির জন্য একটি সঠিক আপলোড কী বা লিঙ্ক প্রদান করতে হবে।');
53+
if (! is_string($source) || ! str_starts_with($source, 'resources/')) {
54+
$validator->errors()->add("items.{$index}.source", 'ছবির জন্য একটি সঠিক আপলোড কী প্রদান করতে হবে।');
5955
}
6056
}
6157
}

‎app/Http/Requests/Resource/StoreResourceRequest.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ class StoreResourceRequest extends FormRequest
99
{
1010
public function authorize(): bool
1111
{
12-
$node = Node::find($this->input('node_id'));
12+
if ($this->filled('node_id')) {
13+
$node = Node::find($this->input('node_id'));
1314

14-
return ! ($node?->isEffectivelyFrozen());
15+
return ! ($node?->isEffectivelyFrozen());
16+
}
17+
18+
return true;
1519
}
1620

1721
public function rules(): array
1822
{
1923
return [
20-
'node_id' => ['required', 'integer', 'exists:nodes,id'],
24+
'subject_id' => ['required', 'integer', 'exists:subjects,id'],
25+
'node_id' => ['nullable', 'integer', 'exists:nodes,id'],
2126
'resource_type' => ['required', 'string', 'in:note,practical,marked_book,other,video'],
2227
'title' => ['required', 'string', 'max:255'],
2328
'description' => ['nullable', 'string'],

‎app/Models/Resource.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Resource extends Model
1616
use Prunable;
1717

1818
protected $fillable = [
19+
'subject_id',
1920
'node_id',
2021
'user_id',
2122
'resource_type',
@@ -86,6 +87,11 @@ public function node(): BelongsTo
8687
return $this->belongsTo(Node::class);
8788
}
8889

90+
public function subject(): BelongsTo
91+
{
92+
return $this->belongsTo(Subject::class);
93+
}
94+
8995
public function user(): BelongsTo
9096
{
9197
return $this->belongsTo(User::class);

0 commit comments

Comments
 (0)