添加html页面

This commit is contained in:
浪子
2026-05-16 09:57:35 +08:00
parent 6c104b9db3
commit d39940cd59
5 changed files with 740 additions and 186 deletions
+3 -2
View File
@@ -44,7 +44,8 @@ import {
id,
mediaUrl,
objectUrl,
profileUrl
profileUrl,
statusUrl
} from "./util";
export async function webFinger(request: Request, env: Env): Promise<Response> {
@@ -746,7 +747,7 @@ export function noteObject(env: Env, user: User, status: Status, opts: { to?: st
attributedTo: actorUrl(env, user),
content: status.content,
published: status.created_at,
url: status.url,
url: statusUrl(env, user, status.id),
to: opts.to ?? audience.to,
cc: opts.cc ?? audience.cc,
attachment: opts.attachments ?? [],
+17 -5
View File
@@ -88,7 +88,7 @@ import {
verifyCredentials
} from "./mastodon";
import { processOutgoingDeliveries } from "./federation";
import { profilePage } from "./profile";
import { homePage, objectPage, profilePage, statusPage } from "./profile";
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
@@ -117,7 +117,10 @@ async function route(request: Request, env: Env): Promise<Response> {
const path = url.pathname.replace(/\/+$/, "") || "/";
const method = request.method.toUpperCase();
if (method === "GET" && path === "/") return nodeInfo(env);
if (method === "GET" && path === "/") {
if (wantsHtmlDocument(request)) return homePage(env);
return nodeInfo(env);
}
if (method === "GET" && path === "/avatar.png") return svgResponse(AVATAR_SVG);
if (method === "GET" && path === "/header.png") return svgResponse(HEADER_SVG);
@@ -213,13 +216,18 @@ async function route(request: Request, env: Env): Promise<Response> {
if (method === "GET" && (m = path.match(/^\/media\/(.+)$/))) return serveMedia(env, m[1]);
if (method === "GET" && (m = path.match(/^\/@([^/]+)\/([^/]+)$/))) return statusPage(env, decodeURIComponent(m[1]), decodeURIComponent(m[2]));
if (method === "GET" && (m = path.match(/^\/@([^/]+)$/))) return profilePage(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/web\/@([^/]+)\/([^/]+)$/))) return statusPage(env, decodeURIComponent(m[1]), decodeURIComponent(m[2]));
if (method === "GET" && (m = path.match(/^\/web\/@([^/]+)$/))) return profilePage(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/web\/statuses\/([^/]+)$/))) return objectPage(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/statuses\/([^/]+)$/))) return objectPage(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/web\/accounts\/([^/]+)$/))) return profilePage(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/users\/([^/]+)\/statuses\/([^/]+)$/))) return statusPage(env, decodeURIComponent(m[1]), decodeURIComponent(m[2]));
if (method === "GET" && (m = path.match(/^\/users\/([^/]+)$/))) {
const username = decodeURIComponent(m[1]);
if (wantsProfileHtml(request)) return profilePage(env, username);
if (wantsHtmlDocument(request)) return profilePage(env, username);
return actor(env, username);
}
if (method === "GET" && (m = path.match(/^\/users\/([^/]+)\/outbox$/))) return outbox(request, env, decodeURIComponent(m[1]));
@@ -227,12 +235,16 @@ async function route(request: Request, env: Env): Promise<Response> {
if (method === "POST" && path === "/inbox") return inboxHandler(request, env, null);
if (method === "GET" && (m = path.match(/^\/users\/([^/]+)\/followers$/))) return followersCollection(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/users\/([^/]+)\/following$/))) return followingCollection(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/objects\/([^/]+)$/))) return activityObject(env, decodeURIComponent(m[1]));
if (method === "GET" && (m = path.match(/^\/objects\/([^/]+)$/))) {
const objectId = decodeURIComponent(m[1]);
if (wantsHtmlDocument(request)) return objectPage(env, objectId);
return activityObject(env, objectId);
}
return json({ error: "not_found" }, 404);
}
function wantsProfileHtml(request: Request): boolean {
function wantsHtmlDocument(request: Request): boolean {
const accept = request.headers.get("accept") ?? "";
return /\btext\/html\b/i.test(accept) && !/(application\/activity\+json|application\/ld\+json)/i.test(accept);
}
+4 -3
View File
@@ -96,6 +96,7 @@ import {
objectUrl,
profileUrl,
safeFileName,
statusUrl,
tokenString
} from "./util";
@@ -626,7 +627,7 @@ async function publishStatus(env: Env, user: User, input: StatusCreateInput): Pr
activityId,
objectId,
now,
objectId
statusUrl(env, user, statusId)
)
.run();
@@ -959,7 +960,7 @@ export async function deleteStatusEndpoint(request: Request, env: Env, statusId:
await env.DB.prepare(
"INSERT INTO deleted_statuses (id, user_id, object_id, url, deleted_at) VALUES (?, ?, ?, ?, ?)"
).bind(status.id, user.id, status.object_id, status.url, new Date().toISOString()).run();
).bind(status.id, user.id, status.object_id, statusUrl(env, user, status.id), new Date().toISOString()).run();
await env.DB.prepare("DELETE FROM statuses WHERE id = ?").bind(status.id).run();
await env.DB.prepare("DELETE FROM media WHERE status_id = ?").bind(status.id).run();
await env.DB.prepare("DELETE FROM mentions WHERE status_id = ?").bind(status.id).run();
@@ -1885,7 +1886,7 @@ function statusRecord(env: Env, status: Status, user: User, context: StatusSeria
return {
id: status.id,
uri: status.object_id,
url: status.url,
url: statusUrl(env, user, status.id),
account: context.accountByUserId.get(user.id) ?? {
id: user.id,
username: user.username,
+712 -176
View File
File diff suppressed because it is too large Load Diff
+4
View File
@@ -99,6 +99,10 @@ export function profileUrl(env: Env, user: User): string {
return `${baseUrl(env)}/@${encodeURIComponent(user.username)}`;
}
export function statusUrl(env: Env, user: User, statusId: string): string {
return `${profileUrl(env, user)}/${encodeURIComponent(statusId)}`;
}
export function objectUrl(env: Env, statusId: string): string {
return `${baseUrl(env)}/objects/${statusId}`;
}