aethex-forge/client/lib/aethex-social-service.ts
Builder.io 3ce65bfd7b Switch follow/unfollow/endorse to server APIs so notifications fire
cgen-295023c025ae4bbd9f2937b51bd579cb
2025-10-18 03:00:09 +00:00

155 lines
4.6 KiB
TypeScript

import { supabase } from "@/lib/supabase";
export const aethexSocialService = {
async listRecommended(userId: string, limit = 10) {
try {
const { data, error } = await supabase
.from("user_profiles")
.select("id, username, full_name, avatar_url, bio")
.neq("id", userId)
.order("updated_at", { ascending: false })
.limit(limit);
if (error) {
console.error(
"Failed to load recommended profiles:",
(error as any)?.message || error,
);
return [];
}
return (data || []) as any[];
} catch (error) {
console.error(
"Unexpected error loading recommended profiles:",
(error as any)?.message || error,
);
return [];
}
},
async getFollowing(userId: string): Promise<string[]> {
try {
const { data, error } = await supabase
.from("user_follows")
.select("following_id")
.eq("follower_id", userId);
if (error) {
console.error(
"Failed to load following list:",
(error as any)?.message || error,
);
return [];
}
return (data as any[]).map((r: any) => r.following_id);
} catch (error) {
console.error(
"Unexpected error loading following list:",
(error as any)?.message || error,
);
return [];
}
},
async followUser(followerId: string, followingId: string): Promise<void> {
const resp = await fetch("/api/social/follow", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ follower_id: followerId, following_id: followingId }),
});
if (!resp.ok) throw new Error(await resp.text());
},
async unfollowUser(followerId: string, followingId: string): Promise<void> {
const resp = await fetch("/api/social/unfollow", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ follower_id: followerId, following_id: followingId }),
});
if (!resp.ok) throw new Error(await resp.text());
},
async sendInvite(inviterId: string, email: string, message?: string | null) {
const resp = await fetch("/api/invites", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
inviter_id: inviterId,
invitee_email: email,
message,
}),
});
if (!resp.ok) {
const err = await resp.text();
throw new Error(err || "Failed to send invite");
}
return (await resp.json()) as {
ok: boolean;
inviteUrl: string;
token: string;
};
},
async listInvites(inviterId: string) {
const resp = await fetch(
`/api/invites?inviter_id=${encodeURIComponent(inviterId)}`,
);
if (!resp.ok) return [];
return await resp.json();
},
async acceptInvite(token: string, acceptorId: string) {
const resp = await fetch("/api/invites/accept", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token, acceptor_id: acceptorId }),
});
if (!resp.ok) {
const err = await resp.text();
throw new Error(err || "Failed to accept invite");
}
return await resp.json();
},
async applyReward(userId: string, action: string, amount?: number) {
const resp = await fetch("/api/rewards/apply", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ user_id: userId, action, amount }),
});
if (!resp.ok) return false;
return true;
},
async getConnections(userId: string) {
const { data, error } = await supabase
.from("user_connections")
.select(
`connection_id, created_at, user_profiles:connection_id ( id, full_name, username, avatar_url, bio )`,
)
.eq("user_id", userId)
.order("created_at", { ascending: false });
if (error) return [] as any[];
return (data || []) as any[];
},
async getEndorsements(userId: string) {
const { data, error } = await supabase
.from("endorsements")
.select("endorser_id, skill, created_at")
.eq("endorsed_id", userId)
.order("created_at", { ascending: false });
if (error) return [] as any[];
return (data || []) as any[];
},
async endorseSkill(endorserId: string, endorsedId: string, skill: string) {
const resp = await fetch("/api/social/endorse", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ endorser_id: endorserId, endorsed_id: endorsedId, skill }),
});
if (!resp.ok) throw new Error(await resp.text());
},
};