Repository navigation
feat: implement note archive library, root feed transition, and blog KaTeX rendering #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
05aed26
ebe5658
4a98f73
193920c
a613abd
4a73fed
e5dd4f0
5ce8179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| <?php | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Models\Resource; | ||
| use App\Models\Subject; | ||
| use Illuminate\Http\Request; | ||
| use Inertia\Inertia; | ||
| use Inertia\Response; | ||
|
|
||
| class NoteController extends Controller | ||
| { | ||
| public function index(Request $request): Response | ||
| { | ||
| $course = $request->query('curriculum') ?? $request->query('course'); | ||
| if (! in_array($course, ['hsc', 'ssc'], true)) { | ||
| $course = $request->cookie('preferred_course') === 'ssc' ? 'ssc' : 'hsc'; | ||
| } | ||
|
|
||
| $group = $request->query('group', 'science'); | ||
| if (! in_array($group, ['science', 'commerce', 'humanities'], true)) { | ||
| $group = 'science'; | ||
| } | ||
|
|
||
| $sort = $request->query('sort', 'top'); | ||
| if (! in_array($sort, ['top', 'latest'], true)) { | ||
| $sort = 'top'; | ||
| } | ||
|
|
||
| $type = $request->query('type', 'all'); | ||
| $subjectId = $request->query('subject_id'); | ||
| $nodeId = $request->query('node_id'); | ||
| $search = trim($request->query('search', '')); | ||
|
|
||
| $query = Resource::query() | ||
| ->approved() | ||
| ->with([ | ||
| 'user:id,name,username,image_path,institution,is_verified', | ||
| 'subject:id,name,course,slug,group', | ||
| 'node:id,name,slug,subject_id', | ||
| ]); | ||
|
|
||
| // Filter by course and group (unless searching specific @username without explicit track) | ||
| $isAuthorSearch = str_starts_with($search, '@'); | ||
| $hasExplicitTrack = $request->filled('curriculum') || $request->filled('course') || $request->filled('group'); | ||
|
|
||
| if (! $isAuthorSearch || $hasExplicitTrack) { | ||
| $query->where(function ($q) use ($course, $group) { | ||
| $q->whereHas('subject', function ($s) use ($course, $group) { | ||
| $s->where('course', $course) | ||
| ->whereIn('group', [$group, 'common']); | ||
| })->orWhereHas('node.subject', function ($s) use ($course, $group) { | ||
| $s->where('course', $course) | ||
| ->whereIn('group', [$group, 'common']); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| if ($subjectId) { | ||
| $query->where(function ($q) use ($subjectId) { | ||
| $q->where('subject_id', $subjectId) | ||
| ->orWhereHas('node', fn ($n) => $n->where('subject_id', $subjectId)); | ||
| }); | ||
| } | ||
|
|
||
| if ($nodeId === 'whole' || $nodeId === 'general') { | ||
| $query->whereNull('node_id'); | ||
| } elseif (! empty($nodeId) && is_numeric($nodeId)) { | ||
| $query->where('node_id', (int) $nodeId); | ||
| } | ||
|
|
||
| if (in_array($type, ['note', 'video', 'practical', 'other'], true)) { | ||
| $query->where('resource_type', $type); | ||
|
Comment on lines
+72
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply the marked-book type filter.
|
||
| } | ||
|
|
||
| if (! empty($search)) { | ||
| if ($isAuthorSearch) { | ||
| $username = ltrim($search, '@'); | ||
| $query->whereHas('user', fn ($u) => $u->where('username', $username)); | ||
| } else { | ||
| $query->where(function ($q) use ($search) { | ||
| $q->where('title', 'like', "%{$search}%") | ||
| ->orWhere('description', 'like', "%{$search}%"); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if ($sort === 'latest') { | ||
| $query->latest('id'); | ||
| } else { | ||
| $query->orderByDesc('vote_score')->latest('id'); | ||
| } | ||
|
|
||
| $resources = $query->simplePaginate(16)->withQueryString(); | ||
|
|
||
| // Query available subjects for filter dropdown (simple, no caching) | ||
| $subjects = Subject::where('is_disabled', false) | ||
| ->where('course', $course) | ||
| ->whereIn('group', [$group, 'common']) | ||
| ->orderBy('sort_order') | ||
| ->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']) | ||
| ->toArray(); | ||
|
|
||
| return Inertia::render('Notes/Index', [ | ||
| 'resources' => $resources, | ||
| 'subjects' => $subjects, | ||
| 'filters' => [ | ||
| 'curriculum' => $course, | ||
| 'course' => $course, | ||
| 'group' => $group, | ||
| 'subject_id' => $subjectId ? (int) $subjectId : null, | ||
| 'node_id' => $nodeId, | ||
| 'type' => $type, | ||
| 'sort' => $sort, | ||
| 'search' => $search, | ||
| ], | ||
| ]); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,10 @@ public function authorize(): bool | |
| protected function prepareForValidation(): void | ||
| { | ||
|
|
||
| $excerpt = Str::limit(strip_tags($this->input('content')), 150); | ||
| $content = (string) $this->input('content', ''); | ||
| $clean = preg_replace('/```[\s\S]*?```|\$\$[\s\S]*?\$\$/', ' [Equation] ', $content); | ||
| $clean = preg_replace('/\\$[^$\n]+\\$/', ' [Equation] ', (string) $clean); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not treat two currency amounts as an equation. For
|
||
| $excerpt = Str::limit(trim(preg_replace('/\s+/', ' ', strip_tags((string) $clean))), 150); | ||
|
|
||
| $this->merge([ | ||
| 'slug' => Str::slug($this->input('title')), | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -50,12 +50,8 @@ public static function validateItems(Validator $validator, array $items, ?string | |||||||||
| $validator->errors()->add("items.{$index}.source", 'PDF এবং ভিডিওর জন্য সঠিক URL লিঙ্ক প্রদান করতে হবে।'); | ||||||||||
| } | ||||||||||
| } elseif ($itemFormat === 'image') { | ||||||||||
| $isAllowedUpload = str_starts_with($source, 'resources/notes/') | ||||||||||
| && preg_match('#^resources/notes/([0-9a-zA-Z_-]+/)*[0-9a-zA-Z_.-]+\.(jpg|jpeg|png|webp)$#i', $source); | ||||||||||
| $isExternalUrl = (str_starts_with($source, 'http://') || str_starts_with($source, 'https://')) && filter_var($source, FILTER_VALIDATE_URL); | ||||||||||
|
|
||||||||||
| if (! $isAllowedUpload && ! $isExternalUrl) { | ||||||||||
| $validator->errors()->add("items.{$index}.source", 'ছবির জন্য একটি সঠিক আপলোড কী বা লিঙ্ক প্রদান করতে হবে।'); | ||||||||||
| if (! is_string($source) || ! str_starts_with($source, 'resources/')) { | ||||||||||
| $validator->errors()->add("items.{$index}.source", 'ছবির জন্য একটি সঠিক আপলোড কী প্রদান করতে হবে।'); | ||||||||||
|
Comment on lines
+53
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restrict image keys to the upload namespace. The check now accepts any string that starts with Require the 🔒 Proposed fix
Committable suggestion
|
||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('resources', function (Blueprint $table) { | ||
| $table->foreignId('subject_id')->nullable()->after('user_id')->constrained('subjects')->cascadeOnDelete(); | ||
| $table->unsignedBigInteger('node_id')->nullable()->change(); | ||
| }); | ||
|
|
||
| // Backfill subject_id from nodes | ||
| if (DB::getDriverName() === 'sqlite') { | ||
| DB::statement(' | ||
| UPDATE resources | ||
| SET subject_id = (SELECT subject_id FROM nodes WHERE nodes.id = resources.node_id) | ||
| WHERE subject_id IS NULL AND node_id IS NOT NULL | ||
| '); | ||
| } else { | ||
| DB::statement(' | ||
| UPDATE resources r | ||
| JOIN nodes n ON r.node_id = n.id | ||
| SET r.subject_id = n.subject_id | ||
| WHERE r.subject_id IS NULL AND r.node_id IS NOT NULL | ||
| '); | ||
| } | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::table('resources', function (Blueprint $table) { | ||
| $table->dropForeign(['subject_id']); | ||
| $table->dropColumn('subject_id'); | ||
| $table->unsignedBigInteger('node_id')->nullable(false)->change(); | ||
| }); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Load the node’s subject for archive cards.
If a resource has a node but no direct
subject_id,TrendingNoteCard.vuecannot show its subject. This query loadsnodebut notnode.subject, and the card’s direct-subject fallback is null. Eager-loadnode.subjectalongsidenodeso these resources retain their subject label.