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:
@@ -1,14 +1,12 @@
|
||||
import path from "path";
|
||||
import glob from "glob";
|
||||
import Koa, { BaseContext } from "koa";
|
||||
import bodyParser from "koa-body";
|
||||
import Router from "koa-router";
|
||||
import userAgent, { UserAgentContext } from "koa-useragent";
|
||||
import env from "@server/env";
|
||||
import { NotFoundError } from "@server/errors";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import coalesceBody from "@server/middlewares/coaleseBody";
|
||||
import { AppState, AppContext } from "@server/types";
|
||||
import { PluginManager, PluginType } from "@server/utils/PluginManager";
|
||||
import apiKeys from "./apiKeys";
|
||||
import attachments from "./attachments";
|
||||
import auth from "./auth";
|
||||
@@ -60,19 +58,13 @@ api.use(apiTracer());
|
||||
api.use(apiResponse());
|
||||
api.use(editor());
|
||||
|
||||
// register package API routes before others to allow for overrides
|
||||
const rootDir = env.ENVIRONMENT === "test" ? "" : "build";
|
||||
glob
|
||||
.sync(path.join(rootDir, "plugins/*/server/api/!(*.test).[jt]s"))
|
||||
.forEach((filePath: string) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const pkg: Router = require(path.join(process.cwd(), filePath)).default;
|
||||
|
||||
if (pkg && "routes" in pkg) {
|
||||
router.use("/", pkg.routes());
|
||||
Logger.debug("lifecycle", `Registered API routes for ${filePath}`);
|
||||
}
|
||||
});
|
||||
// Register plugin API routes before others to allow for overrides
|
||||
const plugins = PluginManager.getEnabledPlugins(PluginType.API).map(
|
||||
(plugin) => plugin.value
|
||||
);
|
||||
for (const router of plugins) {
|
||||
router.use("/", router.routes());
|
||||
}
|
||||
|
||||
// routes
|
||||
router.use("/", auth.routes());
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -17,7 +17,7 @@ router.use(passport.initialize());
|
||||
|
||||
// dynamically load available authentication provider routes
|
||||
AuthenticationHelper.providers.forEach((provider) => {
|
||||
router.use("/", provider.router.routes());
|
||||
router.use("/", provider.value.routes());
|
||||
});
|
||||
|
||||
router.get("/redirect", auth(), async (ctx: APIContext) => {
|
||||
|
||||
Reference in New Issue
Block a user