Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/Http/Controllers/LeaderboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
}
Expand Down
1 change: 1 addition & 0 deletions resources/js/layouts/AppLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
79 changes: 77 additions & 2 deletions resources/js/pages/Resource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,68 @@ const backHref = computed(() => {
return `/notes?curriculum=${curriculum}&group=${group}`;
});

interface DescriptionSegment {
isUrl: boolean;
text: string;
url?: string;
}

const descriptionSegments = computed<DescriptionSegment[]>(() => {
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;
Expand Down Expand Up @@ -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"
>
<p class="leading-relaxed whitespace-pre-line">
{{ resource.description }}
<p class="leading-relaxed break-words whitespace-pre-line">
<template
v-for="(segment, idx) in descriptionSegments"
:key="idx"
>
<a
v-if="segment.isUrl && segment.url"
:href="segment.url"
target="_blank"
rel="noopener noreferrer"
class="font-medium text-indigo-600 underline underline-offset-2 hover:text-indigo-700 dark:text-indigo-400 dark:hover:text-indigo-300"
>{{ segment.text }}</a
>
<template v-else>{{ segment.text }}</template>
</template>
</p>
</div>

Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/UserAppreciationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Models\DailyStudyLog;
use App\Models\User;
use App\Models\UserAppreciation;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -93,3 +94,46 @@
->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')
);
});
Loading