mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): ♻️ Use URL literal instead of strings
This commit is contained in:
parent
99fac323c8
commit
76d1ccc859
50 changed files with 343 additions and 256 deletions
|
|
@ -69,7 +69,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Relationships" WHERE "Relationships"."subjectId" = ${otherUser.id} AND "Relationships"."ownerId" = ${Users.id} AND "Relationships"."following" = true)`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Relationships" WHERE "Relationships"."subjectId" = ${Users.id} AND "Relationships"."ownerId" = ${otherUser.id} AND "Relationships"."following" = true)`,
|
||||
),
|
||||
context.req.valid("query").limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ beforeAll(async () => {
|
|||
priority: 2,
|
||||
description: "test",
|
||||
visible: true,
|
||||
icon: "test",
|
||||
icon: "https://test.com",
|
||||
});
|
||||
|
||||
expect(role).toBeDefined();
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ export default apiRoute((app) =>
|
|||
exclude_replies ? isNull(Notes.replyId) : undefined,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
user?.id,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -60,11 +60,11 @@ describe("/api/v1/accounts/update_credentials", () => {
|
|||
const object = (await response.json()) as APIAccount;
|
||||
// Proxy url is base_url/media/proxy/<base64url encoded url>
|
||||
expect(object.note).toBe(
|
||||
`<p><img src="${config.http.base_url}/media/proxy/${Buffer.from(
|
||||
`<p><img src="${config.http.base_url}media/proxy/${Buffer.from(
|
||||
"https://example.com/image.jpg",
|
||||
).toString("base64url")}"> <video src="${
|
||||
config.http.base_url
|
||||
}/media/proxy/${Buffer.from(
|
||||
}media/proxy/${Buffer.from(
|
||||
"https://example.com/video.mp4",
|
||||
).toString("base64url")}"> Test!</p>\n`,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Relationships" WHERE "Relationships"."subjectId" = ${Users.id} AND "Relationships"."ownerId" = ${user.id} AND "Relationships"."blocking" = true)`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Likes" WHERE "Likes"."likedId" = ${Notes.id} AND "Likes"."likerId" = ${user.id})`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
user?.id,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Relationships" WHERE "Relationships"."subjectId" = ${user.id} AND "Relationships"."ownerId" = ${Users.id} AND "Relationships"."requested" = true)`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -98,8 +98,12 @@ export default apiRoute((app) =>
|
|||
status_count: statusCount,
|
||||
user_count: userCount,
|
||||
},
|
||||
thumbnail: proxyUrl(config.instance.logo),
|
||||
banner: proxyUrl(config.instance.banner),
|
||||
thumbnail: config.instance.logo
|
||||
? proxyUrl(config.instance.logo).toString()
|
||||
: null,
|
||||
banner: config.instance.banner
|
||||
? proxyUrl(config.instance.banner).toString()
|
||||
: null,
|
||||
title: config.instance.name,
|
||||
uri: config.http.base_url,
|
||||
urls: {
|
||||
|
|
@ -113,7 +117,9 @@ export default apiRoute((app) =>
|
|||
providers:
|
||||
oidcConfig?.providers?.map((p) => ({
|
||||
name: p.name,
|
||||
icon: proxyUrl(p.icon) || undefined,
|
||||
icon: p.icon
|
||||
? proxyUrl(new URL(p.icon)).toString()
|
||||
: undefined,
|
||||
id: p.id,
|
||||
})) ?? [],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Relationships" WHERE "Relationships"."subjectId" = ${Users.id} AND "Relationships"."ownerId" = ${user.id} AND "Relationships"."muting" = true)`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ export default apiRoute((app) =>
|
|||
)`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
user.id,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ beforeAll(async () => {
|
|||
priority: 2,
|
||||
description: "test",
|
||||
visible: true,
|
||||
icon: "test",
|
||||
icon: "https://test.com",
|
||||
});
|
||||
|
||||
expect(role).toBeDefined();
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ beforeAll(async () => {
|
|||
priority: 10,
|
||||
description: "test",
|
||||
visible: true,
|
||||
icon: "test",
|
||||
icon: "https://test.com",
|
||||
});
|
||||
|
||||
expect(role).toBeDefined();
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Likes" WHERE "Likes"."likedId" = ${note.id} AND "Likes"."likerId" = ${Users.id})`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ export default apiRoute((app) =>
|
|||
sql`EXISTS (SELECT 1 FROM "Notes" WHERE "Notes"."reblogId" = ${note.id} AND "Notes"."authorId" = ${Users.id})`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
);
|
||||
|
||||
return context.json(
|
||||
|
|
|
|||
|
|
@ -429,11 +429,11 @@ describe("/api/v1/statuses", () => {
|
|||
const object = (await response.json()) as ApiStatus;
|
||||
// Proxy url is base_url/media/proxy/<base64url encoded url>
|
||||
expect(object.content).toBe(
|
||||
`<p><img src="${config.http.base_url}/media/proxy/${Buffer.from(
|
||||
`<p><img src="${config.http.base_url}media/proxy/${Buffer.from(
|
||||
"https://example.com/image.jpg",
|
||||
).toString("base64url")}"> <video src="${
|
||||
config.http.base_url
|
||||
}/media/proxy/${Buffer.from(
|
||||
}media/proxy/${Buffer.from(
|
||||
"https://example.com/video.mp4",
|
||||
).toString("base64url")}"> Test!</p>`,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ describe("/api/v1/timelines/home", () => {
|
|||
);
|
||||
|
||||
expect(response.headers.get("link")).toBe(
|
||||
`<${config.http.base_url}/api/v1/timelines/home?limit=20&max_id=${timeline[19].id}>; rel="next"`,
|
||||
`<${config.http.base_url}api/v1/timelines/home?limit=20&max_id=${timeline[19].id}>; rel="next"`,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -112,7 +112,7 @@ describe("/api/v1/timelines/home", () => {
|
|||
);
|
||||
|
||||
expect(response.headers.get("link")).toInclude(
|
||||
`${config.http.base_url}/api/v1/timelines/home?limit=20&min_id=${timeline[20].id}>; rel="prev"`,
|
||||
`${config.http.base_url}api/v1/timelines/home?limit=20&min_id=${timeline[20].id}>; rel="prev"`,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ export default apiRoute((app) =>
|
|||
sql`NOT EXISTS (SELECT 1 FROM "Filters" WHERE "Filters"."userId" = ${user.id} AND "Filters"."filter_action" = 'hide' AND EXISTS (SELECT 1 FROM "FilterKeywords" WHERE "FilterKeywords"."filterId" = "Filters"."id" AND "Notes"."content" LIKE '%' || "FilterKeywords"."keyword" || '%') AND "Filters"."context" @> ARRAY['home'])`,
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
user.id,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ describe("/api/v1/timelines/public", () => {
|
|||
);
|
||||
|
||||
expect(response.headers.get("link")).toBe(
|
||||
`<${config.http.base_url}/api/v1/timelines/public?limit=20&max_id=${timeline[19].id}>; rel="next"`,
|
||||
`<${config.http.base_url}api/v1/timelines/public?limit=20&max_id=${timeline[19].id}>; rel="next"`,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -153,7 +153,7 @@ describe("/api/v1/timelines/public", () => {
|
|||
);
|
||||
|
||||
expect(response.headers.get("link")).toInclude(
|
||||
`${config.http.base_url}/api/v1/timelines/public?limit=20&min_id=${timeline[20].id}>; rel="prev"`,
|
||||
`${config.http.base_url}api/v1/timelines/public?limit=20&min_id=${timeline[20].id}>; rel="prev"`,
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ export default apiRoute((app) =>
|
|||
: eq(Notes.visibility, "public"),
|
||||
),
|
||||
limit,
|
||||
context.req.url,
|
||||
new URL(context.req.url),
|
||||
user?.id,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -147,10 +147,14 @@ export default apiRoute((app) =>
|
|||
},
|
||||
},
|
||||
thumbnail: {
|
||||
url: proxyUrl(config.instance.logo),
|
||||
url: config.instance.logo
|
||||
? proxyUrl(config.instance.logo)
|
||||
: null,
|
||||
},
|
||||
banner: {
|
||||
url: proxyUrl(config.instance.banner),
|
||||
url: config.instance.banner
|
||||
? proxyUrl(config.instance.banner)
|
||||
: null,
|
||||
},
|
||||
languages: ["en"],
|
||||
configuration: {
|
||||
|
|
@ -227,7 +231,7 @@ export default apiRoute((app) =>
|
|||
providers:
|
||||
oidcConfig?.providers?.map((p) => ({
|
||||
name: p.name,
|
||||
icon: proxyUrl(p.icon) ?? "",
|
||||
icon: p.icon ? proxyUrl(new URL(p.icon)) : "",
|
||||
id: p.id,
|
||||
})) ?? [],
|
||||
},
|
||||
|
|
|
|||
|
|
@ -72,7 +72,11 @@ export default apiRoute((app) =>
|
|||
|
||||
const userJson = user.toVersia();
|
||||
|
||||
const { headers } = await user.sign(userJson, context.req.url, "GET");
|
||||
const { headers } = await user.sign(
|
||||
userJson,
|
||||
new URL(context.req.url),
|
||||
"GET",
|
||||
);
|
||||
|
||||
return context.json(userJson, 200, headers.toJSON());
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ export default apiRoute((app) =>
|
|||
config.http.base_url,
|
||||
).toString(),
|
||||
total: totalNotes,
|
||||
author: author.getUri(),
|
||||
author: author.getUri().toString(),
|
||||
next:
|
||||
notes.length === NOTES_PER_PAGE
|
||||
? new URL(
|
||||
|
|
@ -125,7 +125,11 @@ export default apiRoute((app) =>
|
|||
items: notes.map((note) => note.toVersia()),
|
||||
};
|
||||
|
||||
const { headers } = await author.sign(json, context.req.url, "GET");
|
||||
const { headers } = await author.sign(
|
||||
json,
|
||||
new URL(context.req.url),
|
||||
"GET",
|
||||
);
|
||||
|
||||
return context.json(json, 200, headers.toJSON());
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ export default apiRoute((app) =>
|
|||
user.data.username,
|
||||
new URL(config.http.base_url).host,
|
||||
"application/activity+json",
|
||||
config.federation.bridge.url,
|
||||
config.federation.bridge.url?.toString(),
|
||||
);
|
||||
} catch (e) {
|
||||
const error = e as ResponseError;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue