Internationalised the ~44 public pages with next-intl (the shell was already translated). Each page now pulls its copy from a "pages.<slug>" namespace via getTranslations (server) / useTranslations (client); the EN + IT catalogs were authored by parallel agents and merged centrally, with natural Italian (ICU plurals) and it backfilled from en for any gap. request.ts gained getMessageFallback/onError so a missing key degrades to the English value, never a raw key. Behaviour unchanged (only display text moved to t() calls; queries, actions, fields, ContentCard structure preserved). Verified on the prod server: with NEXT_LOCALE=it, home/community/staff/news/shop/rankings all render Italian copy, no raw-key leakage; English unchanged. The nav language switcher toggles EN/IT live. tsc 0, vitest 49/49, next build 0. Admin pages intentionally left in English (staff tooling).
213 lines
7.3 KiB
TypeScript
213 lines
7.3 KiB
TypeScript
import { getTranslations } from "next-intl/server";
|
|
import Link from "next/link";
|
|
import { redirect } from "next/navigation";
|
|
import { ContentCard, EmptyState } from "@/components/public/ui";
|
|
import { auth } from "@/lib/auth";
|
|
import { avatarImageUrl } from "@/lib/format";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { siteSettings } from "@/lib/services/site-settings";
|
|
import { acceptFriend } from "@/actions/messenger";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
function formatTimestamp(seconds: number): string {
|
|
if (!seconds) return "";
|
|
// sended_on is a unix timestamp in seconds (emulator convention).
|
|
return new Date(seconds * 1000).toISOString().slice(0, 16).replace("T", " ");
|
|
}
|
|
|
|
export default async function MessagesPage() {
|
|
const t = await getTranslations("pages.messages");
|
|
const session = await auth();
|
|
if (!session?.user?.id) redirect("/login");
|
|
|
|
const userId = Number(session.user.id);
|
|
|
|
// Pending friend requests addressed to the session user, the most recent
|
|
// offline messages they've received, and the avatar-imager base URL.
|
|
let requests: { id: number; userFromId: number }[] = [];
|
|
let messages: { id: number; userFromId: number; message: string; sendedOn: number }[] = [];
|
|
let imagerBase =
|
|
"https://www.habbo.com/habbo-imaging/avatarimage";
|
|
|
|
try {
|
|
const [reqRows, msgRows, base] = await Promise.all([
|
|
prisma.messengerFriendrequests.findMany({
|
|
where: { userToId: userId },
|
|
select: { id: true, userFromId: true },
|
|
orderBy: { id: "desc" },
|
|
take: 100,
|
|
}),
|
|
prisma.messengerOffline.findMany({
|
|
where: { userId },
|
|
select: { id: true, userFromId: true, message: true, sendedOn: true },
|
|
orderBy: { sendedOn: "desc" },
|
|
take: 50,
|
|
}),
|
|
siteSettings.get(
|
|
"habbo_imaging_url",
|
|
"https://www.habbo.com/habbo-imaging/avatarimage",
|
|
),
|
|
]);
|
|
requests = reqRows;
|
|
messages = msgRows;
|
|
if (base) imagerBase = base;
|
|
} catch {
|
|
requests = [];
|
|
messages = [];
|
|
}
|
|
|
|
// Resolve usernames/looks for every counterparty referenced above.
|
|
const otherIds = Array.from(
|
|
new Set([
|
|
...requests.map((r) => r.userFromId),
|
|
...messages.map((m) => m.userFromId),
|
|
]),
|
|
).filter((id) => id && id > 0);
|
|
|
|
let userById = new Map<
|
|
number,
|
|
{ id: number; username: string; look: string }
|
|
>();
|
|
if (otherIds.length > 0) {
|
|
try {
|
|
const users = await prisma.user.findMany({
|
|
where: { id: { in: otherIds } },
|
|
select: { id: true, username: true, look: true },
|
|
});
|
|
userById = new Map(users.map((u) => [u.id, u]));
|
|
} catch {
|
|
userById = new Map();
|
|
}
|
|
}
|
|
|
|
return (
|
|
<main style={{ display: "grid", gap: "1.5rem" }}>
|
|
<ContentCard
|
|
icon="✉️"
|
|
title={t("title")}
|
|
subtitle={t("subtitle")}
|
|
/>
|
|
|
|
{/* ── Pending friend requests ───────────────────────────── */}
|
|
<ContentCard
|
|
icon="🙋"
|
|
title={t("requestsTitle")}
|
|
subtitle={
|
|
requests.length === 0
|
|
? t("requestsEmptySubtitle")
|
|
: t("requestsSubtitle", { count: requests.length })
|
|
}
|
|
padded={requests.length === 0}
|
|
>
|
|
{requests.length === 0 ? (
|
|
<EmptyState icon="🙋">{t("requestsEmptyState")}</EmptyState>
|
|
) : (
|
|
<div className="grid" style={{ padding: "1rem", gap: "0.75rem" }}>
|
|
{requests.map((req) => {
|
|
const from = userById.get(req.userFromId);
|
|
const avatar = avatarImageUrl(imagerBase, from?.look ?? "", {
|
|
size: "s",
|
|
headOnly: true,
|
|
});
|
|
return (
|
|
<div
|
|
key={req.id}
|
|
className="card"
|
|
style={{ display: "flex", gap: "0.85rem", alignItems: "center" }}
|
|
>
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
className="avatar"
|
|
src={avatar}
|
|
alt={`${from?.username ?? "User"} avatar`}
|
|
width={50}
|
|
height={50}
|
|
/>
|
|
<div style={{ minWidth: 0, flex: 1 }}>
|
|
<p style={{ margin: 0 }}>
|
|
{from ? (
|
|
<Link href={`/u/${from.username}`}>
|
|
<strong>{from.username}</strong>
|
|
</Link>
|
|
) : (
|
|
<strong>{t("unknownUser", { id: req.userFromId })}</strong>
|
|
)}
|
|
</p>
|
|
<p className="muted" style={{ margin: 0 }}>
|
|
{t("wantsToBeFriend")}
|
|
</p>
|
|
</div>
|
|
<form action={acceptFriend}>
|
|
<input type="hidden" name="requestId" value={String(req.id)} />
|
|
<button type="submit" className="btn btn-secondary">
|
|
{t("accept")}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</ContentCard>
|
|
|
|
{/* ── Offline messages ──────────────────────────────────── */}
|
|
<ContentCard
|
|
icon="💬"
|
|
title={t("offlineTitle")}
|
|
subtitle={
|
|
messages.length === 0
|
|
? t("offlineEmptySubtitle")
|
|
: t("offlineSubtitle", { count: messages.length })
|
|
}
|
|
padded={messages.length === 0}
|
|
>
|
|
{messages.length === 0 ? (
|
|
<EmptyState icon="💬">{t("offlineEmptyState")}</EmptyState>
|
|
) : (
|
|
<div className="grid" style={{ padding: "1rem", gap: "0.75rem" }}>
|
|
{messages.map((msg) => {
|
|
const from = userById.get(msg.userFromId);
|
|
const avatar = avatarImageUrl(imagerBase, from?.look ?? "", {
|
|
size: "s",
|
|
headOnly: true,
|
|
});
|
|
const when = formatTimestamp(msg.sendedOn);
|
|
return (
|
|
<article
|
|
key={msg.id}
|
|
className="card"
|
|
style={{ display: "flex", gap: "0.85rem", alignItems: "flex-start" }}
|
|
>
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
className="avatar"
|
|
src={avatar}
|
|
alt={`${from?.username ?? "User"} avatar`}
|
|
width={50}
|
|
height={50}
|
|
/>
|
|
<div style={{ minWidth: 0, flex: 1 }}>
|
|
<p
|
|
className="muted"
|
|
style={{ marginTop: 0, marginBottom: "0.35rem" }}
|
|
>
|
|
{from ? (
|
|
<Link href={`/u/${from.username}`}>{from.username}</Link>
|
|
) : (
|
|
<span>{t("unknownUser", { id: msg.userFromId })}</span>
|
|
)}
|
|
{when ? <span> · {when}</span> : null}
|
|
</p>
|
|
<p style={{ margin: 0, whiteSpace: "pre-wrap" }}>{msg.message}</p>
|
|
</div>
|
|
</article>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</ContentCard>
|
|
</main>
|
|
);
|
|
}
|