Reimplemented outdated code, added encryption keys for each room, added support for profile images, added friend requests via url/qr-code
This commit is contained in:
1 parent
508f17315e
commit
32e4bf8b32
58 files changed
+3270
-362
No files matched your search
@@ -0,0 +1,42 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { sendFriendRequest, acceptFriend } from "@/utils/friends/friends";
|
||||
import { UUID_REGEX } from "@/utils/validation";
|
||||
|
||||
export type AddFriendState = {
|
||||
success?: boolean;
|
||||
error?: string;
|
||||
} | null;
|
||||
|
||||
export async function addFriendAction(
|
||||
_prevState: AddFriendState,
|
||||
formData: FormData,
|
||||
): Promise<AddFriendState> {
|
||||
const userId = formData.get("userId");
|
||||
if (typeof userId !== "string" || !userId.trim()) {
|
||||
return { error: "User ID is required." };
|
||||
}
|
||||
|
||||
const trimmed = userId.trim();
|
||||
|
||||
if (!UUID_REGEX.test(trimmed)) {
|
||||
return { error: "Please enter a valid user ID (UUID format)." };
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await sendFriendRequest(trimmed);
|
||||
if (result === false) {
|
||||
return { error: "Could not send friend request. It may already exist." };
|
||||
}
|
||||
revalidatePath("/friends");
|
||||
return { success: true };
|
||||
} catch {
|
||||
return { error: "Something went wrong. Please try again." };
|
||||
}
|
||||
}
|
||||
|
||||
export async function acceptRequestAction(senderId: string) {
|
||||
await acceptFriend(senderId);
|
||||
revalidatePath("/friends");
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
"use client";
|
||||
|
||||
import { useActionState } from "react";
|
||||
import { addFriendAction, type AddFriendState } from "./actions";
|
||||
|
||||
const initialState: AddFriendState = null;
|
||||
|
||||
export function AddFriendForm() {
|
||||
const [state, action, pending] = useActionState(addFriendAction, initialState);
|
||||
|
||||
return (
|
||||
<form action={action} className="space-y-3">
|
||||
<label htmlFor="userId" className="block text-sm font-medium text-neutral-700">
|
||||
Add by User ID
|
||||
</label>
|
||||
<div className="flex gap-2">
|
||||
<input
|
||||
id="userId"
|
||||
name="userId"
|
||||
type="text"
|
||||
placeholder="e.g. 3b241101-e2bb-..."
|
||||
required
|
||||
className="flex-1 rounded-lg border border-neutral-300 bg-white px-3 py-2 text-sm text-neutral-900 shadow-sm placeholder:text-neutral-400 focus:border-neutral-900 focus:outline-none"
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={pending}
|
||||
className="rounded-lg bg-neutral-900 px-4 py-2 text-sm font-medium text-white shadow-sm transition hover:bg-neutral-800 disabled:cursor-not-allowed disabled:bg-neutral-400"
|
||||
>
|
||||
{pending ? "Sending..." : "Add"}
|
||||
</button>
|
||||
</div>
|
||||
{state?.error && (
|
||||
<p className="text-sm text-red-600">{state.error}</p>
|
||||
)}
|
||||
{state?.success && (
|
||||
<p className="text-sm text-emerald-600">Friend request sent!</p>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { redirect } from "next/navigation";
|
||||
import { sendFriendRequest } from "@/utils/friends/friends";
|
||||
import { UUID_REGEX } from "@/utils/validation";
|
||||
import Link from "next/link";
|
||||
|
||||
type ResultStatus = "success" | "already_friends" | "error";
|
||||
|
||||
export default async function AddFriendPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ id?: string }>;
|
||||
}) {
|
||||
const { id } = await searchParams;
|
||||
|
||||
// Validate the ID parameter
|
||||
if (!id || !UUID_REGEX.test(id)) {
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
<ResultCard
|
||||
status="error"
|
||||
message="Invalid or missing user ID in the link."
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
// Must be authenticated — redirect to login with return URL
|
||||
if (!user) {
|
||||
redirect(`/login?next=%2Ffriends%2Fadd%3Fid%3D${id}`);
|
||||
}
|
||||
|
||||
// Can't add yourself
|
||||
if (user.id === id) {
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
<ResultCard
|
||||
status="error"
|
||||
message="You can't send yourself a friend request."
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Look up the target user
|
||||
const { data: targetUser } = await supabase
|
||||
.from("users")
|
||||
.select("id, name")
|
||||
.eq("id", id)
|
||||
.single();
|
||||
|
||||
if (!targetUser) {
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
<ResultCard status="error" message="User not found." />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Check if already friends
|
||||
const { data: existing } = await supabase
|
||||
.from("friends")
|
||||
.select("user_id")
|
||||
.eq("user_id", user.id)
|
||||
.eq("friend_id", id)
|
||||
.maybeSingle();
|
||||
|
||||
if (existing) {
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
<ResultCard
|
||||
status="already_friends"
|
||||
message={`You are already friends with ${targetUser.name}.`}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Send the friend request
|
||||
let status: ResultStatus;
|
||||
let message: string;
|
||||
|
||||
try {
|
||||
const sent = await sendFriendRequest(id);
|
||||
if (sent === false) {
|
||||
status = "error";
|
||||
message =
|
||||
"Could not send friend request. It may already exist.";
|
||||
} else {
|
||||
status = "success";
|
||||
message = `Friend request sent to ${targetUser.name}!`;
|
||||
}
|
||||
} catch {
|
||||
status = "error";
|
||||
message = "Something went wrong. Please try again.";
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
<ResultCard status={status} message={message} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ResultCard({
|
||||
status,
|
||||
message,
|
||||
}: {
|
||||
status: ResultStatus;
|
||||
message: string;
|
||||
}) {
|
||||
const isSuccess = status === "success";
|
||||
const isAlreadyFriends = status === "already_friends";
|
||||
|
||||
return (
|
||||
<div className="rounded-xl border border-neutral-200 bg-white p-8 shadow-sm">
|
||||
<div className="flex flex-col items-center text-center">
|
||||
<div
|
||||
className={`flex size-12 items-center justify-center rounded-full ${
|
||||
isSuccess
|
||||
? "bg-emerald-100"
|
||||
: isAlreadyFriends
|
||||
? "bg-blue-100"
|
||||
: "bg-red-100"
|
||||
}`}
|
||||
>
|
||||
{isSuccess ? (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
className="size-6 text-emerald-600"
|
||||
>
|
||||
<path d="M10 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6ZM3.465 14.493a1.23 1.23 0 0 0 .41 1.412A9.957 9.957 0 0 0 10 18c2.31 0 4.438-.784 6.131-2.1.43-.333.604-.903.408-1.41a7.002 7.002 0 0 0-13.074.003Z" />
|
||||
</svg>
|
||||
) : isAlreadyFriends ? (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
className="size-6 text-blue-600"
|
||||
>
|
||||
<path d="M10 9a3 3 0 1 0 0-6 3 3 0 0 0 0 6ZM6 8a2 2 0 1 1-4 0 2 2 0 0 1 4 0ZM1.49 15.326a.75.75 0 0 0 .41.18 9.957 9.957 0 0 0 2.1.196 9.957 9.957 0 0 0 2.1-.196.75.75 0 0 0 .41-.18 7.002 7.002 0 0 0-5.02 0ZM16.9 15.326a7.002 7.002 0 0 1-5.02 0 .75.75 0 0 1 .41.18 9.957 9.957 0 0 0 4.2 0 .75.75 0 0 1 .41-.18ZM18 8a2 2 0 1 1-4 0 2 2 0 0 1 4 0ZM5.304 16.255A7.025 7.025 0 0 0 10 17.5c1.792 0 3.44-.672 4.696-1.783A5.98 5.98 0 0 0 10 15a5.98 5.98 0 0 0-4.696 1.255Z" />
|
||||
</svg>
|
||||
) : (
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
className="size-6 text-red-600"
|
||||
>
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M18 10a8 8 0 1 1-16 0 8 8 0 0 1 16 0Zm-8-5a.75.75 0 0 1 .75.75v4.5a.75.75 0 0 1-1.5 0v-4.5A.75.75 0 0 1 10 5Zm0 10a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<h1 className="mt-4 text-lg font-semibold text-neutral-900">
|
||||
{isSuccess
|
||||
? "Request Sent!"
|
||||
: isAlreadyFriends
|
||||
? "Already Friends"
|
||||
: "Something went wrong"}
|
||||
</h1>
|
||||
<p className="mt-2 text-sm text-neutral-600">{message}</p>
|
||||
|
||||
<div className="mt-6 flex gap-3">
|
||||
<Link
|
||||
href="/friends"
|
||||
className="rounded-lg bg-neutral-900 px-4 py-2 text-sm font-medium text-white shadow-sm transition hover:bg-neutral-800"
|
||||
>
|
||||
Back to Friends
|
||||
</Link>
|
||||
<Link
|
||||
href="/rooms"
|
||||
className="rounded-lg border border-neutral-300 px-4 py-2 text-sm font-medium text-neutral-700 transition hover:bg-neutral-50"
|
||||
>
|
||||
Go to Rooms
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import Image from "next/image";
|
||||
|
||||
export interface FriendEntry {
|
||||
friend_id: string;
|
||||
friend_alias: string;
|
||||
avatar_url: string | null;
|
||||
}
|
||||
|
||||
export function FriendsList({ friends }: { friends: FriendEntry[] }) {
|
||||
if (friends.length === 0) {
|
||||
return (
|
||||
<div className="flex flex-col items-center rounded-xl border border-dashed border-neutral-300 py-12">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
strokeWidth={1}
|
||||
className="size-10 text-neutral-300"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
d="M15 19.128a9.38 9.38 0 0 0 2.625.372 9.337 9.337 0 0 0 4.121-.952 4.125 4.125 0 0 0-7.533-2.493M15 19.128v-.003c0-1.113-.285-2.16-.786-3.07M15 19.128v.106A12.318 12.318 0 0 1 8.624 21c-2.331 0-4.512-.645-6.374-1.766l-.001-.109a6.375 6.375 0 0 1 11.964-3.07M12 6.375a3.375 3.375 0 1 1-6.75 0 3.375 3.375 0 0 1 6.75 0Zm8.25 2.25a2.625 2.625 0 1 1-5.25 0 2.625 2.625 0 0 1 5.25 0Z"
|
||||
/>
|
||||
</svg>
|
||||
<p className="mt-3 text-sm text-neutral-400">No friends yet</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-sm font-semibold text-neutral-900">
|
||||
Friends
|
||||
<span className="ml-2 text-xs font-normal text-neutral-500">
|
||||
{friends.length}
|
||||
</span>
|
||||
</h2>
|
||||
<ul className="mt-3 divide-y divide-neutral-100 rounded-xl border border-neutral-200 bg-white">
|
||||
{friends.map((friend) => (
|
||||
<li
|
||||
key={friend.friend_id}
|
||||
className="flex items-center gap-3 px-4 py-3"
|
||||
>
|
||||
{friend.avatar_url ? (
|
||||
<Image
|
||||
src={friend.avatar_url}
|
||||
alt=""
|
||||
width={200}
|
||||
height={200}
|
||||
className="size-16 rounded-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<div className="flex size-8 items-center justify-center rounded-full bg-neutral-100 text-xs font-medium text-neutral-600">
|
||||
{friend.friend_alias.charAt(0).toUpperCase()}
|
||||
</div>
|
||||
)}
|
||||
<span className="text-sm font-medium text-neutral-900">
|
||||
{friend.friend_alias} - {friend.friend_id}
|
||||
</span>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { listFriends, listRequests } from "@/utils/friends/friends";
|
||||
import { AddFriendForm } from "./add-friend-form";
|
||||
import { RequestsList, type FriendRequest } from "./requests-list";
|
||||
import { FriendsList, type FriendEntry } from "./friends-list";
|
||||
|
||||
export default async function FriendsPage() {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Fetch incoming friend requests with sender names
|
||||
const rawRequests = await listRequests();
|
||||
let requests: FriendRequest[] = [];
|
||||
|
||||
if (rawRequests && rawRequests.length > 0) {
|
||||
const senderIds = rawRequests.map((r) => r.sender_id);
|
||||
const { data: senders } = await supabase
|
||||
.from("users")
|
||||
.select("id, name")
|
||||
.in("id", senderIds);
|
||||
|
||||
const senderMap = new Map(senders?.map((s) => [s.id, s.name]));
|
||||
|
||||
requests = rawRequests.map((r) => ({
|
||||
id: r.id,
|
||||
sender_id: r.sender_id,
|
||||
sender_name: senderMap.get(r.sender_id) ?? "Unknown",
|
||||
}));
|
||||
}
|
||||
|
||||
// Fetch friends list
|
||||
const rawFriends = await listFriends();
|
||||
const friends: FriendEntry[] = rawFriends ?? [];
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-3xl px-6 py-10">
|
||||
<h1 className="text-2xl font-semibold text-neutral-900">Friends</h1>
|
||||
<p className="mt-1 text-sm text-neutral-500">
|
||||
Manage your friends and requests.
|
||||
</p>
|
||||
|
||||
<div className="mt-8 space-y-8">
|
||||
{/* Add friend */}
|
||||
<div className="rounded-xl border border-neutral-200 bg-white p-5 shadow-sm">
|
||||
<AddFriendForm />
|
||||
</div>
|
||||
|
||||
{/* Incoming requests */}
|
||||
<RequestsList requests={requests} />
|
||||
|
||||
{/* Friends list */}
|
||||
<FriendsList friends={friends} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { useTransition } from "react";
|
||||
import { acceptRequestAction } from "./actions";
|
||||
|
||||
export interface FriendRequest {
|
||||
id: string;
|
||||
sender_id: string;
|
||||
sender_name: string;
|
||||
}
|
||||
|
||||
export function RequestsList({ requests }: { requests: FriendRequest[] }) {
|
||||
if (requests.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2 className="text-sm font-semibold text-neutral-900">
|
||||
Incoming Requests
|
||||
<span className="ml-2 inline-flex size-5 items-center justify-center rounded-full bg-neutral-900 text-xs text-white">
|
||||
{requests.length}
|
||||
</span>
|
||||
</h2>
|
||||
<ul className="mt-3 divide-y divide-neutral-100 rounded-xl border border-neutral-200 bg-white">
|
||||
{requests.map((req) => (
|
||||
<RequestItem key={req.id} req={req} />
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function RequestItem({ req }: { req: FriendRequest }) {
|
||||
const [pending, startTransition] = useTransition();
|
||||
|
||||
return (
|
||||
<li className="flex items-center justify-between px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex size-8 items-center justify-center rounded-full bg-neutral-100 text-xs font-medium text-neutral-600">
|
||||
{req.sender_name.charAt(0).toUpperCase()}
|
||||
</div>
|
||||
<span className="text-sm font-medium text-neutral-900">
|
||||
{req.sender_name}
|
||||
</span>
|
||||
</div>
|
||||
<button
|
||||
disabled={pending}
|
||||
onClick={() => startTransition(() => acceptRequestAction(req.sender_id))}
|
||||
className="rounded-lg bg-neutral-900 px-3 py-1.5 text-xs font-medium text-white transition hover:bg-neutral-800 disabled:cursor-not-allowed disabled:bg-neutral-400"
|
||||
>
|
||||
{pending ? "Accepting..." : "Accept"}
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user