Preview arbitrary urls within a document (#5598)

This commit is contained in:
Apoorv Mishra
2023-07-30 05:21:49 +05:30
committed by GitHub
parent 67691477a9
commit ddc883bfcd
8 changed files with 83 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
import { User } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { getTestServer } from "@server/test/support";
import { Iframely } from "@server/utils/unfurl";
import resolvers from "@server/utils/unfurl";
jest.mock("@server/utils/unfurl", () => ({
Iframely: {
@@ -146,7 +146,7 @@ describe("#urls.unfurl", () => {
});
it("should succeed with status 200 ok for a valid external url", async () => {
(Iframely.unfurl as jest.Mock).mockResolvedValue(
(resolvers.Iframely.unfurl as jest.Mock).mockResolvedValue(
Promise.resolve({
url: "https://www.flickr.com",
type: "rich",
@@ -167,7 +167,9 @@ describe("#urls.unfurl", () => {
const body = await res.json();
expect(Iframely.unfurl).toHaveBeenCalledWith("https://www.flickr.com");
expect(resolvers.Iframely.unfurl).toHaveBeenCalledWith(
"https://www.flickr.com"
);
expect(res.status).toEqual(200);
expect(body.url).toEqual("https://www.flickr.com");
expect(body.type).toEqual("rich");
@@ -181,7 +183,7 @@ describe("#urls.unfurl", () => {
});
it("should succeed with status 204 no content for a non-existing external url", async () => {
(Iframely.unfurl as jest.Mock).mockResolvedValue(
(resolvers.Iframely.unfurl as jest.Mock).mockResolvedValue(
Promise.resolve({
status: 404,
error:
@@ -196,7 +198,9 @@ describe("#urls.unfurl", () => {
},
});
expect(Iframely.unfurl).toHaveBeenCalledWith("https://random.url");
expect(resolvers.Iframely.unfurl).toHaveBeenCalledWith(
"https://random.url"
);
expect(res.status).toEqual(204);
});
});

View File

@@ -11,7 +11,7 @@ import { presentDocument, presentMention } from "@server/presenters/unfurls";
import presentUnfurl from "@server/presenters/unfurls/unfurl";
import { APIContext } from "@server/types";
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
import { Iframely } from "@server/utils/unfurl";
import resolvers from "@server/utils/unfurl";
import * as T from "./schema";
const router = new Router();
@@ -62,12 +62,14 @@ router.post(
return;
}
const data = await Iframely.unfurl(url);
if (data.error) {
ctx.response.status = 204;
return;
if (resolvers.Iframely) {
const data = await resolvers.Iframely.unfurl(url);
return data.error
? (ctx.response.status = 204)
: (ctx.body = presentUnfurl(data));
}
ctx.body = presentUnfurl(data);
return (ctx.response.status = 204);
}
);