This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: discover random profiles #2558 * fix: discover tests
- Loading branch information
1 parent
b264849
commit ab1d8ad
Showing
7 changed files
with
150 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
import connectMongo from "../../../config/mongo"; | ||
import Profile from "../../../models/Profile"; | ||
|
||
export default async function handler(req, res) { | ||
await connectMongo(); | ||
|
||
// get popular profiles | ||
const popularProfiles = await Profile.find({}).sort({ views: -1 }).limit(50); | ||
|
||
if (popularProfiles.length === 0) { | ||
return res.status(404).json([]); | ||
} | ||
|
||
const directoryPath = path.join(process.cwd(), "data"); | ||
|
||
const fullPopularProfiles = popularProfiles.flatMap((profile) => { | ||
const filePath = path.join(directoryPath, `${profile.username}.json`); | ||
try { | ||
const user = JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
|
||
if (user.displayStatsPublic) { | ||
return { | ||
...user, | ||
...profile._doc, | ||
}; | ||
} | ||
|
||
return []; | ||
} catch (e) { | ||
console.log(`ERROR loading profile "${filePath}"`); | ||
return []; | ||
} | ||
}); | ||
|
||
const selectedPopularProfiles = fullPopularProfiles.slice(0, 10); | ||
|
||
// get random profiles | ||
const randomProfiles = await Profile.aggregate([{ $sample: { size: 5 } }]); | ||
|
||
const fullRandomProfiles = randomProfiles.flatMap((profile) => { | ||
const filePath = path.join(directoryPath, `${profile.username}.json`); | ||
try { | ||
const user = JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
|
||
if (user.displayStatsPublic) { | ||
return { | ||
...user, | ||
...profile._doc, | ||
}; | ||
} | ||
|
||
return user; | ||
} catch (e) { | ||
console.log(`ERROR loading profile "${filePath}"`); | ||
return []; | ||
} | ||
}); | ||
|
||
const profileSets = { | ||
popular: selectedPopularProfiles, | ||
random: fullRandomProfiles, | ||
}; | ||
|
||
res.status(200).json(profileSets); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Head from "next/head"; | ||
import UserCard from "../components/user/UserCard"; | ||
import Page from "../components/Page"; | ||
|
||
export async function getServerSideProps(context) { | ||
let data = []; | ||
try { | ||
const res = await fetch( | ||
`${process.env.NEXT_PUBLIC_BASE_URL}/api/users/discover` | ||
); | ||
data = await res.json(); | ||
} catch (e) { | ||
console.log("ERROR loading popular profiles", e); | ||
} | ||
|
||
return { | ||
props: { data }, | ||
}; | ||
} | ||
|
||
export default function Popular({ data }) { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Discover LinkFree Profiles</title> | ||
<meta | ||
name="description" | ||
content="Discover more people in your LinkFree community" | ||
/> | ||
<link rel="icon" href="/favicon.ico" /> | ||
</Head> | ||
<Page> | ||
<h1 className="text-4xl mb-4 font-bold">Discover LinkFree Profiles</h1> | ||
|
||
<div className="mb-12"> | ||
<h2 className="text-xl font-bold mb-4">Random LinkFree Profiles</h2> | ||
<ul className="flex flex-wrap gap-3 justify-center"> | ||
{data.random.map((profile) => ( | ||
<li key={profile.username}> | ||
<UserCard profile={profile} /> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
|
||
<div className="mb-12"> | ||
<h2 className="text-xl font-bold mb-4">Popular LinkFree Profiles</h2> | ||
<ul className="flex flex-wrap gap-3 justify-center"> | ||
{data.popular.map((profile) => ( | ||
<li key={profile.username}> | ||
<UserCard profile={profile} /> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
</Page> | ||
</> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @ts-check | ||
import { test, expect } from "@playwright/test"; | ||
|
||
test("Click on discover profile in navbar navigates to discover page", async ({ | ||
page, | ||
}) => { | ||
await page.goto("/"); | ||
await page.getByRole("link", { name: "Discover" }).click(); | ||
await expect(page).toHaveURL("/discover"); | ||
}); | ||
|
||
test.fixme("Random profiles listed", async ({ page }) => { | ||
// 1. navigate to discover profile page | ||
// 2. check random profiles are displayed | ||
}); | ||
|
||
test.fixme("Discover profiles listed", async ({ page }) => { | ||
// 1. navigate to discover profile page | ||
// 2. check the most popular profile are listed | ||
}); |
This file was deleted.
Oops, something went wrong.