try to fix oembed
This commit is contained in:
parent
cb9ed1931f
commit
c544741ef0
2 changed files with 34 additions and 8 deletions
|
@ -70,7 +70,7 @@ interface Instance {
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
type instace = {
|
export type instace = {
|
||||||
name: string;
|
name: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
descriptionLong?: string;
|
descriptionLong?: string;
|
||||||
|
@ -146,7 +146,7 @@ app.use("/getupdates", async (_req: Request, res: Response) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use("/services/oembed", (req: Request, res: Response) => {
|
app.use("/services/oembed", (req: Request, res: Response) => {
|
||||||
inviteResponse(req, res);
|
inviteResponse(req, res, instances);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use("/uptime", (req: Request, res: Response) => {
|
app.use("/uptime", (req: Request, res: Response) => {
|
||||||
|
@ -159,7 +159,7 @@ app.use("/", async (req: Request, res: Response) => {
|
||||||
const host = `${scheme}://${req.get("Host")}`;
|
const host = `${scheme}://${req.get("Host")}`;
|
||||||
const ref = host + req.originalUrl;
|
const ref = host + req.originalUrl;
|
||||||
|
|
||||||
if (host && ref && false) {
|
if (host && ref) {
|
||||||
const link = `${host}/services/oembed?url=${encodeURIComponent(ref)}`;
|
const link = `${host}/services/oembed?url=${encodeURIComponent(ref)}`;
|
||||||
res.set(
|
res.set(
|
||||||
"Link",
|
"Link",
|
||||||
|
|
36
src/utils.ts
36
src/utils.ts
|
@ -1,5 +1,5 @@
|
||||||
import {Request, Response} from "express";
|
import {Request, Response} from "express";
|
||||||
|
import {instace} from "./index.js";
|
||||||
interface ApiUrls {
|
interface ApiUrls {
|
||||||
api: string;
|
api: string;
|
||||||
gateway: string;
|
gateway: string;
|
||||||
|
@ -19,10 +19,32 @@ interface Invite {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getApiUrls(url: string): Promise<ApiUrls | null> {
|
export async function getApiUrls(
|
||||||
|
url: string,
|
||||||
|
instances: instace[],
|
||||||
|
check = true,
|
||||||
|
): Promise<ApiUrls | null> {
|
||||||
if (!url.endsWith("/")) {
|
if (!url.endsWith("/")) {
|
||||||
url += "/";
|
url += "/";
|
||||||
}
|
}
|
||||||
|
if (check) {
|
||||||
|
let valid = false;
|
||||||
|
for (const instace of instances) {
|
||||||
|
const urlstr = instace.url || instace.urls?.api;
|
||||||
|
if (!urlstr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (new URL(urlstr).host === new URL(url).host) {
|
||||||
|
valid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
if (!valid) {
|
||||||
|
throw new Error("Invalid instance");
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const info: ApiUrls = await fetch(`${url}.well-known/spacebar`).then((res) => res.json());
|
const info: ApiUrls = await fetch(`${url}.well-known/spacebar`).then((res) => res.json());
|
||||||
const api = info.api;
|
const api = info.api;
|
||||||
|
@ -42,7 +64,11 @@ export async function getApiUrls(url: string): Promise<ApiUrls | null> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function inviteResponse(req: Request, res: Response): Promise<void> {
|
export async function inviteResponse(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
instances: instace[],
|
||||||
|
): Promise<void> {
|
||||||
let url: URL;
|
let url: URL;
|
||||||
try {
|
try {
|
||||||
url = new URL(req.query.url as string);
|
url = new URL(req.query.url as string);
|
||||||
|
@ -53,7 +79,7 @@ export async function inviteResponse(req: Request, res: Response): Promise<void>
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (url.pathname.startsWith("invite")) {
|
if (!url.pathname.startsWith("invite") && !url.pathname.startsWith("/invite")) {
|
||||||
throw new Error("Invalid invite URL");
|
throw new Error("Invalid invite URL");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +89,7 @@ export async function inviteResponse(req: Request, res: Response): Promise<void>
|
||||||
throw new Error("Instance not specified");
|
throw new Error("Instance not specified");
|
||||||
}
|
}
|
||||||
|
|
||||||
const urls = await getApiUrls(instance);
|
const urls = await getApiUrls(instance, instances);
|
||||||
if (!urls) {
|
if (!urls) {
|
||||||
throw new Error("Failed to get API URLs");
|
throw new Error("Failed to get API URLs");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue