From afa15e17ef267385735a673522de3b5fa8cb4bd3 Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Thu, 25 May 2023 22:05:43 -0700 Subject: [PATCH] refactor: make post imports an experiment --- .../src/server/api/endpoints/admin/meta.ts | 3 +++ .../server/api/endpoints/admin/update-meta.ts | 1 + .../src/server/api/endpoints/i/import-posts.ts | 11 +++++++++++ .../backend/src/server/api/endpoints/meta.ts | 1 + .../src/server/api/endpoints/notes/edit.ts | 11 +++++++++++ .../client/src/pages/admin/experiments.vue | 18 +++++++++++++++++- 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/server/api/endpoints/admin/meta.ts b/packages/backend/src/server/api/endpoints/admin/meta.ts index dd04b8cad9..24c895cbb3 100644 --- a/packages/backend/src/server/api/endpoints/admin/meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/meta.ts @@ -479,6 +479,9 @@ export const meta = { postEditing: { type: "boolean", }, + postImports: { + type: "boolean", + }, }, }, }, diff --git a/packages/backend/src/server/api/endpoints/admin/update-meta.ts b/packages/backend/src/server/api/endpoints/admin/update-meta.ts index c5d534879e..b967112a5c 100644 --- a/packages/backend/src/server/api/endpoints/admin/update-meta.ts +++ b/packages/backend/src/server/api/endpoints/admin/update-meta.ts @@ -175,6 +175,7 @@ export const paramDef = { nullable: true, properties: { postEditing: { type: "boolean" }, + postImports: { type: "boolean" }, }, }, }, diff --git a/packages/backend/src/server/api/endpoints/i/import-posts.ts b/packages/backend/src/server/api/endpoints/i/import-posts.ts index 5f4b99f029..3adba0514e 100644 --- a/packages/backend/src/server/api/endpoints/i/import-posts.ts +++ b/packages/backend/src/server/api/endpoints/i/import-posts.ts @@ -3,6 +3,7 @@ import { createImportPostsJob } from "@/queue/index.js"; import { ApiError } from "../../error.js"; import { DriveFiles } from "@/models/index.js"; import { DAY } from "@/const.js"; +import { fetchMeta } from "@/misc/fetch-meta.js"; export const meta = { secure: true, @@ -23,6 +24,12 @@ export const meta = { code: "EMPTY_FILE", id: "d2f12af1-e7b4-feac-86a3-519548f2728e", }, + + importsDisabled: { + message: "Post imports are disabled.", + code: "IMPORTS_DISABLED", + id: " bc9227e4-fb82-11ed-be56-0242ac120002", + }, }, } as const; @@ -38,6 +45,10 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { const file = await DriveFiles.findOneBy({ id: ps.fileId }); + const instanceMeta = await fetchMeta(); + if (instanceMeta.experimentalFeatures?.postImports === false) + throw new ApiError(meta.errors.importsDisabled); + if (file == null) throw new ApiError(meta.errors.noSuchFile); if (file.size === 0) throw new ApiError(meta.errors.emptyFile); createImportPostsJob(user, file.id, ps.signatureCheck); diff --git a/packages/backend/src/server/api/endpoints/meta.ts b/packages/backend/src/server/api/endpoints/meta.ts index 2f409eb190..139afc88ba 100644 --- a/packages/backend/src/server/api/endpoints/meta.ts +++ b/packages/backend/src/server/api/endpoints/meta.ts @@ -524,6 +524,7 @@ export default define(meta, paramDef, async (ps, me) => { discord: instance.enableDiscordIntegration, serviceWorker: instance.enableServiceWorker, postEditing: instance.experimentalFeatures?.postEditing || false, + postImports: instance.experimentalFeatures?.postImports || false, miauth: true, }; } diff --git a/packages/backend/src/server/api/endpoints/notes/edit.ts b/packages/backend/src/server/api/endpoints/notes/edit.ts index 7cddb7d217..66db6f644f 100644 --- a/packages/backend/src/server/api/endpoints/notes/edit.ts +++ b/packages/backend/src/server/api/endpoints/notes/edit.ts @@ -33,6 +33,7 @@ import { renderActivity } from "@/remote/activitypub/renderer/index.js"; import renderNote from "@/remote/activitypub/renderer/note.js"; import renderUpdate from "@/remote/activitypub/renderer/update.js"; import { deliverToRelays } from "@/services/relay.js"; +import { fetchMeta } from "@/misc/fetch-meta.js"; export const meta = { tags: ["notes"], @@ -138,6 +139,12 @@ export const meta = { code: "NOT_LOCAL_USER", id: "b907f407-2aa0-4283-800b-a2c56290b822", }, + + editsDisabled: { + message: "Post edits are disabled.", + code: "EDITS_DISABLED", + id: "99306f00-fb81-11ed-be56-0242ac120002", + }, }, } as const; @@ -236,6 +243,10 @@ export const paramDef = { export default define(meta, paramDef, async (ps, user) => { if (user.movedToUri != null) throw new ApiError(meta.errors.accountLocked); + const instanceMeta = await fetchMeta(); + if (instanceMeta.experimentalFeatures?.postImports === false) + throw new ApiError(meta.errors.editsDisabled); + if (!Users.isLocalUser(user)) { throw new ApiError(meta.errors.notLocalUser); } diff --git a/packages/client/src/pages/admin/experiments.vue b/packages/client/src/pages/admin/experiments.vue index 7e37c65769..d655c93796 100644 --- a/packages/client/src/pages/admin/experiments.vue +++ b/packages/client/src/pages/admin/experiments.vue @@ -16,12 +16,24 @@ + + + + @@ -38,11 +50,13 @@ import { i18n } from "@/i18n"; import { definePageMetadata } from "@/scripts/page-metadata"; let enablePostEditing = $ref(false); +let enablePostImports = $ref(false); let meta = $ref(null); type MetaExperiments = { experimentalFeatures?: { postEditing?: boolean; + postImports?: boolean; }; }; @@ -51,12 +65,14 @@ async function init() { if (!meta) return; enablePostEditing = meta.experimentalFeatures?.postEditing ?? false; + enablePostImports = meta.experimentalFeatures?.postImports ?? false; } function save() { const experiments: MetaExperiments = { experimentalFeatures: { postEditing: enablePostEditing, + postImports: enablePostImports, }, }; os.apiWithDialog("admin/update-meta", experiments).then(() => {