Refactor unfurling related types (#6730)

* wip

* fix: refactor unfurl types
This commit is contained in:
Apoorv Mishra
2024-04-03 07:28:30 +05:30
committed by GitHub
parent e0ae044f4c
commit 6a4628afef
19 changed files with 399 additions and 457 deletions

View File

@@ -1,3 +1,4 @@
import { UnfurlResourceType } from "@shared/types";
import env from "@server/env";
import { User } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
@@ -18,7 +19,7 @@ jest.mock("dns", () => ({
}));
jest
.spyOn(Iframely, "fetch")
.spyOn(Iframely, "requestResource")
.mockImplementation(() => Promise.resolve(undefined));
const server = getTestServer();
@@ -133,9 +134,8 @@ describe("#urls.unfurl", () => {
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.type).toEqual("mention");
expect(body.title).toEqual(mentionedUser.name);
expect(body.meta.id).toEqual(mentionedUser.id);
expect(body.type).toEqual(UnfurlResourceType.Mention);
expect(body.name).toEqual(mentionedUser.name);
});
it("should succeed with status 200 ok when valid document url is supplied", async () => {
@@ -152,13 +152,13 @@ describe("#urls.unfurl", () => {
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.type).toEqual("document");
expect(body.type).toEqual(UnfurlResourceType.Document);
expect(body.title).toEqual(document.titleWithDefault);
expect(body.meta.id).toEqual(document.id);
expect(body.id).toEqual(document.id);
});
it("should succeed with status 200 ok for a valid external url", async () => {
(Iframely.fetch as jest.Mock).mockResolvedValue(
(Iframely.requestResource as jest.Mock).mockResolvedValue(
Promise.resolve({
url: "https://www.flickr.com",
type: "rich",
@@ -182,7 +182,7 @@ describe("#urls.unfurl", () => {
expect(res.status).toEqual(200);
expect(body.url).toEqual("https://www.flickr.com");
expect(body.type).toEqual("rich");
expect(body.type).toEqual(UnfurlResourceType.OEmbed);
expect(body.title).toEqual("Flickr");
expect(body.description).toEqual(
"The safest and most inclusive global community of photography enthusiasts. The best place for inspiration, connection, and sharing!"
@@ -193,7 +193,7 @@ describe("#urls.unfurl", () => {
});
it("should succeed with status 204 no content for a non-existing external url", async () => {
(Iframely.fetch as jest.Mock).mockResolvedValue(
(Iframely.requestResource as jest.Mock).mockResolvedValue(
Promise.resolve({
status: 404,
error:

View File

@@ -1,5 +1,6 @@
import dns from "dns";
import Router from "koa-router";
import { UnfurlResourceType } from "@shared/types";
import { getBaseDomain, parseDomain } from "@shared/utils/domains";
import parseDocumentSlug from "@shared/utils/parseDocumentSlug";
import parseMentionUrl from "@shared/utils/parseMentionUrl";
@@ -10,8 +11,7 @@ import { rateLimiter } from "@server/middlewares/rateLimiter";
import validate from "@server/middlewares/validate";
import { Document, Share, Team, User } from "@server/models";
import { authorize } from "@server/policies";
import { presentDocument, presentMention } from "@server/presenters/unfurls";
import presentUnfurl from "@server/presenters/unfurls/unfurl";
import presentUnfurl from "@server/presenters/unfurl";
import { APIContext } from "@server/types";
import { CacheHelper } from "@server/utils/CacheHelper";
import { Hook, PluginManager } from "@server/utils/PluginManager";
@@ -53,7 +53,11 @@ router.post(
authorize(actor, "read", user);
authorize(actor, "read", document);
ctx.body = await presentMention(user, document);
ctx.body = await presentUnfurl({
type: UnfurlResourceType.Mention,
user,
document,
});
return;
}
@@ -69,7 +73,11 @@ router.post(
}
authorize(actor, "read", document);
ctx.body = presentDocument(document, actor);
ctx.body = await presentUnfurl({
type: UnfurlResourceType.Document,
document,
viewer: actor,
});
return;
}
return (ctx.response.status = 204);
@@ -80,7 +88,7 @@ router.post(
CacheHelper.getUnfurlKey(actor.teamId, url)
);
if (cachedData) {
return (ctx.body = presentUnfurl(cachedData));
return (ctx.body = await presentUnfurl(cachedData));
}
for (const plugin of plugins) {
@@ -94,7 +102,7 @@ router.post(
data,
plugin.value.cacheExpiry
);
return (ctx.body = presentUnfurl(data));
return (ctx.body = await presentUnfurl(data));
}
}
}