From 657857e8d6cceb124fa6afc694b10b38f2f9dd73 Mon Sep 17 00:00:00 2001 From: April John Date: Sun, 14 May 2023 18:46:48 +0200 Subject: [PATCH] Feat: move post imports to new jobs --- .../processors/db/import-calckey-post.ts | 53 ++++++++++++++++ .../queue/processors/db/import-masto-post.ts | 61 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 packages/backend/src/queue/processors/db/import-calckey-post.ts create mode 100644 packages/backend/src/queue/processors/db/import-masto-post.ts diff --git a/packages/backend/src/queue/processors/db/import-calckey-post.ts b/packages/backend/src/queue/processors/db/import-calckey-post.ts new file mode 100644 index 000000000..707933ae5 --- /dev/null +++ b/packages/backend/src/queue/processors/db/import-calckey-post.ts @@ -0,0 +1,53 @@ +import * as Post from "@/misc/post.js"; +import create from "@/services/note/create.js"; +import { Users } from "@/models/index.js"; +import type { + DbUserImportMastoPostJobData, +} from "@/queue/types.js"; +import { queueLogger } from "../../logger.js"; +import type Bull from "bull"; + +const logger = queueLogger.createSubLogger("import-calckey-post"); + +export async function importCkPost( + job: Bull.Job, + done: any, +): Promise { + const user = await Users.findOneBy({ id: job.data.user.id }); + if (user == null) { + done(); + return; + } + const post = job.data.post; + if (post.replyId != null) { + done(); + return; + } + if (post.renoteId != null) { + done(); + return; + } + if (post.visibility !== "public") { + done(); + return; + } + const { text, cw, localOnly, createdAt } = Post.parse(post); + const note = await create(user, { + createdAt: createdAt, + files: undefined, + poll: undefined, + text: text || undefined, + reply: null, + renote: null, + cw: cw, + localOnly, + visibility: "hidden", + visibleUsers: [], + channel: null, + apMentions: new Array(0), + apHashtags: undefined, + apEmojis: undefined, + }); + logger.succ("Imported"); + done(); +} diff --git a/packages/backend/src/queue/processors/db/import-masto-post.ts b/packages/backend/src/queue/processors/db/import-masto-post.ts new file mode 100644 index 000000000..98823d109 --- /dev/null +++ b/packages/backend/src/queue/processors/db/import-masto-post.ts @@ -0,0 +1,61 @@ +import create from "@/services/note/create.js"; +import { Users } from "@/models/index.js"; +import type { + DbUserImportMastoPostJobData, +} from "@/queue/types.js"; +import { queueLogger } from "../../logger.js"; +import type Bull from "bull"; +import { htmlToMfm } from "@/remote/activitypub/misc/html-to-mfm.js"; +import { resolveNote } from "@/remote/activitypub/models/note.js"; +import { Note } from "@/models/entities/note.js"; + +const logger = queueLogger.createSubLogger("import-masto-post"); + +export async function importMastoPost( + job: Bull.Job, + done: any, +): Promise { + const user = await Users.findOneBy({ id: job.data.user.id }); + if (user == null) { + done(); + return; + } + const post = job.data.post; + let reply: Note | null = null; + if (post.object.inReplyTo != null) { + reply = await resolveNote(post.object.inReplyTo); + } + if (post.directMessage) { + return; + } + if (job.data.signatureCheck) { + if (!post.signature) { + return; + } + } + let text; + try { + text = htmlToMfm(post.object.content, post.object.tag); + } catch (e) { + return; + } + + const note = await create(user, { + createdAt: new Date(post.object.published), + files: undefined, + poll: undefined, + text: text || undefined, + reply, + renote: null, + cw: post.sensitive, + localOnly: false, + visibility: "hidden", + visibleUsers: [], + channel: null, + apMentions: new Array(0), + apHashtags: undefined, + apEmojis: undefined, + }); + logger.succ("Imported"); + done(); +}