feat: 后台增加开放消息开关
关闭后前台隐藏私聊/群聊入口并由后端拦截;同步去掉公告页重复计数胶囊。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -334,28 +334,7 @@ export default function AnnouncementAdmin({
|
||||
}
|
||||
/>
|
||||
|
||||
{/* 计数条 */}
|
||||
<div className="flex items-center gap-3 mb-4 flex-wrap">
|
||||
<div
|
||||
className="inline-flex items-center gap-4 rounded-lg px-3.5 py-2 text-[13px]"
|
||||
style={{ background: "var(--panel-2)", color: "var(--ink-2)" }}
|
||||
>
|
||||
<span>
|
||||
草稿{" "}
|
||||
<strong className="tabular-nums" style={{ color: "var(--ink)" }}>
|
||||
{draftCount}
|
||||
</strong>
|
||||
</span>
|
||||
<span aria-hidden style={{ color: "var(--line)" }}>
|
||||
|
|
||||
</span>
|
||||
<span>
|
||||
已发布{" "}
|
||||
<strong className="tabular-nums" style={{ color: "var(--ink)" }}>
|
||||
{publishedCount}
|
||||
</strong>
|
||||
</span>
|
||||
</div>
|
||||
<div className="mb-4">
|
||||
<AdminSegmented<StatusFilter>
|
||||
ariaLabel="公告状态筛选"
|
||||
size="sm"
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
MessageSquare,
|
||||
Save,
|
||||
UserPlus,
|
||||
MessagesSquare,
|
||||
ExternalLink,
|
||||
Shield,
|
||||
Mail,
|
||||
@@ -107,12 +108,14 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
|
||||
const [siteDescription, setSiteDescription] = useState(initial.site_description);
|
||||
const [allowRegister, setAllowRegister] = useState(initial.allow_register);
|
||||
const [allowComments, setAllowComments] = useState(initial.allow_comments !== false);
|
||||
const [allowMessages, setAllowMessages] = useState(initial.allow_messages !== false);
|
||||
const [postLinkNewTab, setPostLinkNewTab] = useState(initial.post_link_new_tab !== false);
|
||||
const [savedName, setSavedName] = useState(initial.site_name);
|
||||
const [savedDesc, setSavedDesc] = useState(initial.site_description);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [togglingReg, setTogglingReg] = useState(false);
|
||||
const [togglingComments, setTogglingComments] = useState(false);
|
||||
const [togglingMessages, setTogglingMessages] = useState(false);
|
||||
const [togglingLink, setTogglingLink] = useState(false);
|
||||
const [active, setActive] = useState<SectionId>("basic");
|
||||
|
||||
@@ -199,6 +202,21 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
|
||||
}
|
||||
};
|
||||
|
||||
const toggleMessages = async () => {
|
||||
if (togglingMessages) return;
|
||||
const next = !allowMessages;
|
||||
setTogglingMessages(true);
|
||||
try {
|
||||
const res = await apiUpdateSiteSettings({ allow_messages: next });
|
||||
setAllowMessages(res.allow_messages);
|
||||
toast(res.allow_messages ? "已开放消息" : "已关闭消息", "ok");
|
||||
} catch (e) {
|
||||
toast(e instanceof Error ? e.message : "保存失败");
|
||||
} finally {
|
||||
setTogglingMessages(false);
|
||||
}
|
||||
};
|
||||
|
||||
const togglePostLinkNewTab = async () => {
|
||||
if (togglingLink) return;
|
||||
const next = !postLinkNewTab;
|
||||
@@ -293,7 +311,7 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
|
||||
</div>
|
||||
|
||||
<div id="settings-content" className="scroll-mt-4">
|
||||
<AdminSection title="内容与互动" description="控制注册、评论与链接打开方式">
|
||||
<AdminSection title="内容与互动" description="控制注册、评论、消息与链接打开方式">
|
||||
<SwitchRow
|
||||
bordered
|
||||
title={
|
||||
@@ -328,6 +346,23 @@ export default function SettingsAdmin({ initial }: { initial: PublicSettings })
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SwitchRow
|
||||
bordered
|
||||
title={
|
||||
<>
|
||||
<MessagesSquare size={15} /> 开放消息
|
||||
</>
|
||||
}
|
||||
description="关闭后前台不展示私聊与群聊入口;已有会话由后台消息管理继续监管。"
|
||||
control={
|
||||
<AdminSwitch
|
||||
checked={allowMessages}
|
||||
disabled={togglingMessages}
|
||||
onChange={() => void toggleMessages()}
|
||||
label="开放消息"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
<SwitchRow
|
||||
title={
|
||||
<>
|
||||
|
||||
@@ -2,10 +2,11 @@ import type { Metadata } from "next";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { cookies } from "next/headers";
|
||||
import { authCookieHeader } from "@/lib/cookies";
|
||||
import { getMeCached } from "@/lib/serverData";
|
||||
import { getMeCached, getPublicSettingsCached } from "@/lib/serverData";
|
||||
import { fetchChatRoom, fetchChatMessages, fetchChatRooms } from "@/lib/api";
|
||||
import ChatShell from "../ChatShell";
|
||||
import ChatRoomClient from "./ChatRoomClient";
|
||||
import MessagesUnsupported from "@/components/MessagesUnsupported";
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>;
|
||||
@@ -13,6 +14,9 @@ interface PageProps {
|
||||
|
||||
export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
|
||||
const { id } = await params;
|
||||
if ((await getPublicSettingsCached()).allow_messages === false) {
|
||||
return { title: "本站不支持消息" };
|
||||
}
|
||||
try {
|
||||
const { room } = await fetchChatRoom(id, authCookieHeader(await cookies()));
|
||||
const title =
|
||||
@@ -27,6 +31,9 @@ export async function generateMetadata({ params }: PageProps): Promise<Metadata>
|
||||
|
||||
export default async function ChatRoomPage({ params }: PageProps) {
|
||||
const { id } = await params;
|
||||
if ((await getPublicSettingsCached()).allow_messages === false) {
|
||||
return <MessagesUnsupported />;
|
||||
}
|
||||
const cookie = authCookieHeader(await cookies());
|
||||
const me = await getMeCached(cookie || undefined);
|
||||
if (!me.user) {
|
||||
|
||||
@@ -2,9 +2,10 @@ import { redirect } from "next/navigation";
|
||||
import { cookies } from "next/headers";
|
||||
import type { Metadata } from "next";
|
||||
import { authCookieHeader } from "@/lib/cookies";
|
||||
import { getMeCached } from "@/lib/serverData";
|
||||
import { getMeCached, getPublicSettingsCached } from "@/lib/serverData";
|
||||
import { fetchChatRooms } from "@/lib/api";
|
||||
import ChatShell from "./ChatShell";
|
||||
import MessagesUnsupported from "@/components/MessagesUnsupported";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "消息",
|
||||
@@ -12,6 +13,9 @@ export const metadata: Metadata = {
|
||||
};
|
||||
|
||||
export default async function ChatListPage() {
|
||||
if ((await getPublicSettingsCached()).allow_messages === false) {
|
||||
return <MessagesUnsupported />;
|
||||
}
|
||||
const cookie = authCookieHeader(await cookies());
|
||||
const me = await getMeCached(cookie || undefined);
|
||||
if (!me.user) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import PostLinkProvider from "@/components/PostLinkProvider";
|
||||
import CodeFoldProvider from "@/components/markdown/CodeFoldProvider";
|
||||
import MotionProvider from "@/components/MotionProvider";
|
||||
import CommentsPolicyProvider from "@/components/CommentsPolicyProvider";
|
||||
import MessagesPolicyProvider from "@/components/MessagesPolicyProvider";
|
||||
import type { User } from "@/lib/api";
|
||||
import { getMeCached, getPublicSettingsCached } from "@/lib/serverData";
|
||||
import { TOKEN_COOKIE, authCookieHeader } from "@/lib/cookies";
|
||||
@@ -121,24 +122,26 @@ export default async function RootLayout({
|
||||
</a>
|
||||
<MotionProvider initial={motionCfg}>
|
||||
<CommentsPolicyProvider allowComments={settings.allow_comments !== false}>
|
||||
<PostLinkProvider openInNewTab={settings.post_link_new_tab !== false}>
|
||||
<CodeFoldProvider
|
||||
initialEnabled={settings.code_block_auto_fold}
|
||||
initialFoldLines={settings.code_block_fold_lines}
|
||||
>
|
||||
<SiteChrome
|
||||
initialIsAdmin={isAdmin}
|
||||
user={user}
|
||||
unread={unread}
|
||||
chatUnread={chatUnread}
|
||||
theme={theme}
|
||||
siteName={settings.site_name}
|
||||
allowRegister={settings.allow_register}
|
||||
<MessagesPolicyProvider allowMessages={settings.allow_messages !== false}>
|
||||
<PostLinkProvider openInNewTab={settings.post_link_new_tab !== false}>
|
||||
<CodeFoldProvider
|
||||
initialEnabled={settings.code_block_auto_fold}
|
||||
initialFoldLines={settings.code_block_fold_lines}
|
||||
>
|
||||
{children}
|
||||
</SiteChrome>
|
||||
</CodeFoldProvider>
|
||||
</PostLinkProvider>
|
||||
<SiteChrome
|
||||
initialIsAdmin={isAdmin}
|
||||
user={user}
|
||||
unread={unread}
|
||||
chatUnread={settings.allow_messages !== false ? chatUnread : 0}
|
||||
theme={theme}
|
||||
siteName={settings.site_name}
|
||||
allowRegister={settings.allow_register}
|
||||
>
|
||||
{children}
|
||||
</SiteChrome>
|
||||
</CodeFoldProvider>
|
||||
</PostLinkProvider>
|
||||
</MessagesPolicyProvider>
|
||||
</CommentsPolicyProvider>
|
||||
</MotionProvider>
|
||||
<Toaster />
|
||||
|
||||
@@ -7,6 +7,7 @@ import { MessagesSquare } from "lucide-react";
|
||||
import { apiLogin, apiMe, apiGetSettings } from "@/lib/api";
|
||||
import { hasAuthCookieHint } from "@/lib/cookies";
|
||||
import { useAllowComments } from "@/components/CommentsPolicyProvider";
|
||||
import { useAllowMessages } from "@/components/MessagesPolicyProvider";
|
||||
|
||||
// 仅允许同源相对路径跳转,拦截 //host、/\\host 与绝对 URL 形式的开放重定向
|
||||
function safeRedirect(to: string | null): string {
|
||||
@@ -42,6 +43,7 @@ export default function LoginPage() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const allowComments = useAllowComments();
|
||||
const allowMessages = useAllowMessages();
|
||||
const redirect = safeRedirect(searchParams.get("redirect"));
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
@@ -93,7 +95,7 @@ export default function LoginPage() {
|
||||
欢迎回来
|
||||
</h1>
|
||||
<p className="text-sm mt-2" style={{ color: "var(--ink-2)" }}>
|
||||
登录后发帖{allowComments ? "、评论" : ""}与点赞
|
||||
登录后发帖{allowComments ? "、评论" : ""}{allowMessages ? "、发消息" : ""}与点赞
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -5,12 +5,16 @@ import { useRouter } from "next/navigation";
|
||||
import { MessageSquare } from "lucide-react";
|
||||
import { apiOpenDirectChat } from "@/lib/api";
|
||||
import { toast } from "@/lib/toast";
|
||||
import { useAllowMessages } from "@/components/MessagesPolicyProvider";
|
||||
|
||||
/** 用户主页:发起私聊 */
|
||||
export default function StartDMButton({ userId }: { userId: number }) {
|
||||
const router = useRouter();
|
||||
const allowMessages = useAllowMessages();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
if (!allowMessages) return null;
|
||||
|
||||
const start = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
|
||||
@@ -56,7 +56,9 @@ export default async function UserPage({ params, searchParams }: UserPageProps)
|
||||
|
||||
const cookie = authCookieHeader(await cookies());
|
||||
const me = await getMeCached(cookie || undefined);
|
||||
const allowComments = (await getPublicSettingsCached()).allow_comments !== false;
|
||||
const settings = await getPublicSettingsCached();
|
||||
const allowComments = settings.allow_comments !== false;
|
||||
const allowMessages = settings.allow_messages !== false;
|
||||
|
||||
let data: UserProfile;
|
||||
try {
|
||||
@@ -172,7 +174,7 @@ export default async function UserPage({ params, searchParams }: UserPageProps)
|
||||
banned: u.banned,
|
||||
}}
|
||||
/>
|
||||
<StartDMButton userId={data.user.id} />
|
||||
{allowMessages ? <StartDMButton userId={data.user.id} /> : null}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user