feat: friend system
This commit is contained in:
@ -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}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user