feat: friend system
Some checks failed
TrafficCue CI / check (push) Failing after 1m30s
TrafficCue CI / build (push) Successful in 9m50s
TrafficCue CI / build-android (push) Successful in 24m44s

This commit is contained in:
2025-10-03 14:13:43 +02:00
parent 3b28779b69
commit 46bf44a324
2 changed files with 64 additions and 1 deletions

View File

@ -5,7 +5,7 @@
import { getAuthURL, getOIDCUser } from "$lib/services/oidc";
import * as Avatar from "$lib/components/ui/avatar";
import { m } from "$lang/messages";
import { refreshToken, uploadID } from "$lib/services/lnv";
import { fetchMyUser, followUser, refreshToken, unfollowUser, uploadID } from "$lib/services/lnv";
interface OIDCUser {
sub: string;
@ -25,6 +25,8 @@
);
}
});
let testInput = "";
</script>
{#if !user}
@ -74,6 +76,22 @@
refreshToken();
}}>refresh</button
>
{#await fetchMyUser() then u}
<span><b>Followers:</b> {u.followers}</span>
<span><b>Following:</b> {u.following}</span>
<span><b>Reviews:</b> {u.reviewsCount}</span>
<span><b>Hazards:</b> {u.hazardsCount}</span>
{/await}
<input type="text" bind:value={testInput}>
<button onclick={async () => {
alert(await followUser(testInput).catch(alert));
}}>Follow</button>
<button onclick={async () => {
alert(await unfollowUser(testInput).catch(alert));
}}>Unfollow</button>
<pre>{user.sub}</pre>
{JSON.stringify(user, null, 2)}
{/if}

View File

@ -280,3 +280,48 @@ export async function ping() {
const res = await fetch(LNV_SERVER + "/ping").catch(() => ({ ok: false }));
return res.ok;
}
export interface UserInfo {
username: string;
createdAt: string;
reviewsCount: number;
hazardsCount: number;
followers: number;
following: number;
}
export async function fetchMyUser(): Promise<UserInfo> {
const res = await authFetch(LNV_SERVER + "/user/me");
if (!res.ok) {
throw new Error(`Failed to fetch user: ${res.statusText}`);
}
return await res.json();
}
export async function followUser(username: string) {
const res = await authFetch(LNV_SERVER + "/user/follow", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username }),
});
if (!res.ok) {
throw new Error(`Failed to follow user: ${res.statusText}`);
}
return await res.json();
}
export async function unfollowUser(username: string) {
const res = await authFetch(LNV_SERVER + "/user/unfollow", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username }),
});
if (!res.ok) {
throw new Error(`Failed to unfollow user: ${res.statusText}`);
}
return await res.json();
}