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
13 changes: 13 additions & 0 deletions app/Http/Controllers/PeerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\User;
use App\Models\UserAppreciation;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
Expand Down Expand Up @@ -57,6 +58,18 @@ public function index(Request $request): Response
if ($sort === 'appreciated') {
$query->orderByDesc('appreciations_received_count')->latest('users.id');
} else {
// 1. Primary: Mutual connections (friends of friends) for authenticated users
if ($currentUser) {
$query->selectSub(
UserAppreciation::query()
->selectRaw('count(*)')
->whereIn('appreciator_id', $currentUser->appreciationsGiven()->select('user_id'))
->whereColumn('user_appreciations.user_id', 'users.id'),
'mutual_count'
)->orderByDesc('mutual_count');
}

// 2. Fallback: Institution / location matching
$targetLocation = $currentUser?->institution ? trim($currentUser->institution) : '';

// For guests or users without institution, infer location from Cloudflare headers
Expand Down
31 changes: 22 additions & 9 deletions resources/js/components/navigation/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1024,18 +1024,31 @@ export const SiteDrawer = defineComponent({
<AppLogo />
</div>
<div class="ml-auto flex items-center gap-1.5">
{isHome.value && (
{user.value ? (
<>
{isHome.value && (
<Link
href="/peers"
class="relative flex h-9 items-center gap-1.5 rounded-xl border border-slate-200/80 bg-slate-50 px-2.5 text-xs font-semibold text-slate-700 transition-all hover:bg-slate-100 hover:text-slate-900 active:scale-95 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300 dark:hover:bg-gray-800 dark:hover:text-gray-100"
aria-label="Find"
title="Find"
>
<Search class="h-3.5 w-3.5 text-slate-500 dark:text-gray-400" />
<span>Find</span>
</Link>
)}
<NotificationDropdown plain />
</>
) : !currentUrl.value.startsWith('/login') ? (
<Link
href="/peers"
class="relative flex h-9 items-center gap-1.5 rounded-xl border border-slate-200/80 bg-slate-50 px-2.5 text-xs font-semibold text-slate-700 transition-all hover:bg-slate-100 hover:text-slate-900 active:scale-95 dark:border-gray-800 dark:bg-gray-900 dark:text-gray-300 dark:hover:bg-gray-800 dark:hover:text-gray-100"
aria-label="Find"
title="Find"
href="/login"
class="flex h-9 items-center gap-1.5 rounded-xl bg-indigo-600 px-3 text-xs font-semibold text-white shadow-sm transition-all hover:bg-indigo-700 active:scale-95 dark:bg-indigo-500 dark:hover:bg-indigo-400"
aria-label="Login"
>
<Search class="h-3.5 w-3.5 text-slate-500 dark:text-gray-400" />
<span>Find</span>
<MaterialIcon name="login" size={18} />
<span>Login</span>
</Link>
)}
<NotificationDropdown plain />
) : null}
</div>
</div>

Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/PeerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,24 @@
->where('peers.data.0.is_appreciated', true)
);
});

test('peers in You May Know prioritizes mutual connections', function () {
$viewer = User::factory()->create(['name' => 'Current Viewer']);
$friend = User::factory()->create(['name' => 'Direct Friend']);
$mutualPeer = User::factory()->create(['name' => 'Mutual Friend Candidate']);
$randomPeer = User::factory()->create(['name' => 'Random Candidate']);

// Viewer appreciates Friend
$friend->appreciators()->attach($viewer->id);

// Friend appreciates Mutual Peer
$mutualPeer->appreciators()->attach($friend->id);

$response = $this->actingAs($viewer)->get(route('peers.index', ['sort' => 'relevant']));

$response->assertOk()
->assertInertia(fn ($page) => $page
->component('Peers/Index')
->where('peers.data.0.name', 'Mutual Friend Candidate')
);
});
Loading