diff --git a/app/Http/Controllers/LeaderboardController.php b/app/Http/Controllers/LeaderboardController.php index e8655d50..cd9a7b7e 100644 --- a/app/Http/Controllers/LeaderboardController.php +++ b/app/Http/Controllers/LeaderboardController.php @@ -59,14 +59,13 @@ public function index(Request $request): Response 'appreciating' => UserAppreciation::where('appreciator_id', $user->id)->pluck('user_id')->push($user->id)->unique()->toArray(), 'appreciators' => UserAppreciation::where('user_id', $user->id)->pluck('appreciator_id')->push($user->id)->unique()->toArray(), 'mutual' => UserAppreciation::where('appreciator_id', $user->id) - ->whereIn('user_id', function ($query) use ($user) { - $query->select('appreciator_id') - ->from('user_appreciations') - ->where('user_id', $user->id); - }) ->pluck('user_id') + ->merge( + UserAppreciation::where('user_id', $user->id)->pluck('appreciator_id') + ) ->push($user->id) ->unique() + ->values() ->toArray(), }; } diff --git a/resources/js/layouts/AppLayout.vue b/resources/js/layouts/AppLayout.vue index af6843d5..9390f84a 100644 --- a/resources/js/layouts/AppLayout.vue +++ b/resources/js/layouts/AppLayout.vue @@ -36,6 +36,7 @@ const showBottomNav = computed(() => // Whitelisted pages that show the marketing footer (static and discovery pages) const STATIC_FOOTER_COMPONENTS = new Set([ 'Blog/Index', + 'Leaderboard/Index', 'Donate', 'Projects', 'Tracker/Index', diff --git a/resources/js/pages/Resource.vue b/resources/js/pages/Resource.vue index 7725ce84..b2c2cd49 100644 --- a/resources/js/pages/Resource.vue +++ b/resources/js/pages/Resource.vue @@ -224,6 +224,68 @@ const backHref = computed(() => { return `/notes?curriculum=${curriculum}&group=${group}`; }); +interface DescriptionSegment { + isUrl: boolean; + text: string; + url?: string; +} + +const descriptionSegments = computed(() => { + const text = props.resource?.description; + + if (!text) { + return []; + } + + const segments: DescriptionSegment[] = []; + const urlRegex = /https:\/\/[^\s<>"'()]+/g; + let lastIndex = 0; + let match: RegExpExecArray | null; + + while ((match = urlRegex.exec(text)) !== null) { + if (match.index > lastIndex) { + segments.push({ + isUrl: false, + text: text.slice(lastIndex, match.index), + }); + } + + let rawUrl = match[0]; + let trailingPunctuation = ''; + + const punctMatch = rawUrl.match(/[.,!?;:]+$/); + + if (punctMatch) { + trailingPunctuation = punctMatch[0]; + rawUrl = rawUrl.slice(0, -trailingPunctuation.length); + } + + segments.push({ + isUrl: true, + text: rawUrl, + url: rawUrl, + }); + + if (trailingPunctuation) { + segments.push({ + isUrl: false, + text: trailingPunctuation, + }); + } + + lastIndex = match.index + match[0].length; + } + + if (lastIndex < text.length) { + segments.push({ + isUrl: false, + text: text.slice(lastIndex), + }); + } + + return segments; +}); + const scrollToComments = () => { if (typeof document === 'undefined') { return; @@ -631,8 +693,21 @@ onUnmounted(() => { v-if="resource.description" class="mt-4 rounded-2xl border border-slate-200/80 bg-slate-50/70 p-4 text-sm text-slate-700 dark:border-gray-800 dark:bg-gray-900/60 dark:text-gray-300" > -

- {{ resource.description }} +

+

diff --git a/tests/Feature/UserAppreciationTest.php b/tests/Feature/UserAppreciationTest.php index ef396b85..733cc3de 100644 --- a/tests/Feature/UserAppreciationTest.php +++ b/tests/Feature/UserAppreciationTest.php @@ -1,5 +1,6 @@ where('isAppreciated', false) ); }); + +test('leaderboard mutual scope includes users being appreciated or appreciating current user', function () { + $this->withoutVite(); + $me = User::factory()->create(['username' => 'me']); + $myIdol = User::factory()->create(['username' => 'myidol']); + $myFan = User::factory()->create(['username' => 'myfan']); + $stranger = User::factory()->create(['username' => 'stranger']); + + // $me appreciates $myIdol + UserAppreciation::create(['user_id' => $myIdol->id, 'appreciator_id' => $me->id]); + + // $myFan appreciates $me + UserAppreciation::create(['user_id' => $me->id, 'appreciator_id' => $myFan->id]); + + DailyStudyLog::create([ + 'user_id' => $myIdol->id, + 'study_date' => now()->toDateString(), + 'total_seconds' => 3600, + ]); + + DailyStudyLog::create([ + 'user_id' => $myFan->id, + 'study_date' => now()->toDateString(), + 'total_seconds' => 1800, + ]); + + DailyStudyLog::create([ + 'user_id' => $stranger->id, + 'study_date' => now()->toDateString(), + 'total_seconds' => 7200, + ]); + + $this->actingAs($me) + ->get('/leaderboard?tab=students&timeframe=all_time&scope=mutual') + ->assertOk() + ->assertInertia(fn (Assert $page) => $page + ->component('Leaderboard/Index') + ->where('scope', 'mutual') + ->has('leaderboard', 2) + ->where('leaderboard.0.username', 'myidol') + ->where('leaderboard.1.username', 'myfan') + ); +});