chore: Plugin registration (#6623)
* first pass * test * test * priority * Reduce boilerplate further * Update server/utils/PluginManager.ts Co-authored-by: Apoorv Mishra <apoorvmishra101092@gmail.com> * fix: matchesNode error in destroyed editor transaction * fix: Individual imported files do not display source correctly in 'Insights' * chore: Add sleep before Slack notification * docs * fix: Error logged about missing plugin.json * Remove email template glob --------- Co-authored-by: Apoorv Mishra <apoorvmishra101092@gmail.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import env from "@server/env";
|
||||
import { User } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
import resolvers from "@server/utils/unfurl";
|
||||
import Iframely from "plugins/iframely/server/iframely";
|
||||
|
||||
jest.mock("dns", () => ({
|
||||
resolveCname: (
|
||||
@@ -17,9 +17,7 @@ jest.mock("dns", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
jest
|
||||
.spyOn(resolvers.Iframely, "unfurl")
|
||||
.mockImplementation(async (_: string) => false);
|
||||
jest.spyOn(Iframely, "fetch").mockImplementation(() => Promise.resolve(false));
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
@@ -158,7 +156,7 @@ describe("#urls.unfurl", () => {
|
||||
});
|
||||
|
||||
it("should succeed with status 200 ok for a valid external url", async () => {
|
||||
(resolvers.Iframely.unfurl as jest.Mock).mockResolvedValue(
|
||||
(Iframely.fetch as jest.Mock).mockResolvedValue(
|
||||
Promise.resolve({
|
||||
url: "https://www.flickr.com",
|
||||
type: "rich",
|
||||
@@ -180,9 +178,6 @@ describe("#urls.unfurl", () => {
|
||||
expect(res.status).toEqual(200);
|
||||
const body = await res.json();
|
||||
|
||||
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");
|
||||
@@ -196,7 +191,7 @@ describe("#urls.unfurl", () => {
|
||||
});
|
||||
|
||||
it("should succeed with status 204 no content for a non-existing external url", async () => {
|
||||
(resolvers.Iframely.unfurl as jest.Mock).mockResolvedValue(
|
||||
(Iframely.fetch as jest.Mock).mockResolvedValue(
|
||||
Promise.resolve({
|
||||
status: 404,
|
||||
error:
|
||||
@@ -211,9 +206,6 @@ describe("#urls.unfurl", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(resolvers.Iframely.unfurl).toHaveBeenCalledWith(
|
||||
"https://random.url"
|
||||
);
|
||||
expect(res.status).toEqual(204);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,11 +13,12 @@ import { authorize } from "@server/policies";
|
||||
import { presentDocument, presentMention } from "@server/presenters/unfurls";
|
||||
import presentUnfurl from "@server/presenters/unfurls/unfurl";
|
||||
import { APIContext } from "@server/types";
|
||||
import { PluginManager, PluginType } from "@server/utils/PluginManager";
|
||||
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
|
||||
import resolvers from "@server/utils/unfurl";
|
||||
import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
const plugins = PluginManager.getEnabledPlugins(PluginType.UnfurlProvider);
|
||||
|
||||
router.post(
|
||||
"urls.unfurl",
|
||||
@@ -74,11 +75,13 @@ router.post(
|
||||
}
|
||||
|
||||
// External resources
|
||||
if (resolvers.Iframely) {
|
||||
const data = await resolvers.Iframely.unfurl(url);
|
||||
return data.error
|
||||
? (ctx.response.status = 204)
|
||||
: (ctx.body = presentUnfurl(data));
|
||||
for (const plugin of plugins) {
|
||||
const data = await plugin.value(url);
|
||||
if (data) {
|
||||
return "error" in data
|
||||
? (ctx.response.status = 204)
|
||||
: (ctx.body = presentUnfurl(data));
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.response.status = 204);
|
||||
|
||||
Reference in New Issue
Block a user