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,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>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user