From 21c7f93d7a69eb7dd8bb1b81a7563ca304ad98df Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Wed, 29 Mar 2023 21:10:01 +0200 Subject: [PATCH] handle mastodon style posts --- .../src/queue/processors/db/import-posts.ts | 67 ++++++++++++++----- .../src/remote/activitypub/models/note.ts | 4 +- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/packages/backend/src/queue/processors/db/import-posts.ts b/packages/backend/src/queue/processors/db/import-posts.ts index 36e183c1ea..a1cfab7353 100644 --- a/packages/backend/src/queue/processors/db/import-posts.ts +++ b/packages/backend/src/queue/processors/db/import-posts.ts @@ -8,6 +8,7 @@ import { Users, DriveFiles } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import { queueLogger } from "../../logger.js"; import type Bull from "bull"; +import { htmlToMfm } from "@/remote/activitypub/misc/html-to-mfm.js"; const logger = queueLogger.createSubLogger("import-posts"); @@ -36,34 +37,70 @@ export async function importPosts( let linenum = 0; try { - for (const post of JSON.parse(json)) { - try { + const parsed = JSON.parse(json); + if (parsed instanceof Array) { + for (const post of JSON.parse(json)) { + try { + linenum++; + if (post.replyId != null) { + logger.info(`Is reply, skip [${linenum}] ...`); + continue; + } + if (post.renoteId != null) { + logger.info(`Is boost, skip [${linenum}] ...`); + continue; + } + if (post.visibility !== "public") { + logger.info(`Is non-public, skip [${linenum}] ...`); + continue; + } + const { text, cw, localOnly, createdAt } = Post.parse(post); + + logger.info(`Posting[${linenum}] ...`); + + const note = await create(user, { + createdAt: createdAt, + files: undefined, + poll: undefined, + text: text || undefined, + reply: null, + renote: null, + cw: cw, + localOnly, + visibility: "public", + visibleUsers: [], + channel: null, + apMentions: null, + apHashtags: undefined, + apEmojis: undefined, + }); + } catch (e) { + logger.warn(`Error in line:${linenum} ${e}`); + } + } + } else { + for (const post of parsed.orderedItems) { linenum++; - if (post.replyId != null) { + if (post.inReplyTo != null) { logger.info(`Is reply, skip [${linenum}] ...`); continue; } - if (post.renoteId != null) { - logger.info(`Is boost, skip [${linenum}] ...`); + if (post.directMessage) { + logger.info(`Is dm, skip [${linenum}] ...`); continue; } - if (post.visibility !== "public") { - logger.info(`Is non-public, skip [${linenum}] ...`); - continue; - } - const { text, cw, localOnly, createdAt } = Post.parse(post); - + const text = htmlToMfm(post.content, post.tag); logger.info(`Posting[${linenum}] ...`); const note = await create(user, { - createdAt: createdAt, + createdAt: new Date(post.published), files: undefined, poll: undefined, text: text || undefined, reply: null, renote: null, - cw: cw, - localOnly, + cw: post.sensitive, + localOnly: false, visibility: "public", visibleUsers: [], channel: null, @@ -71,8 +108,6 @@ export async function importPosts( apHashtags: undefined, apEmojis: undefined, }); - } catch (e) { - logger.warn(`Error in line:${linenum} ${e}`); } } } catch (e) { diff --git a/packages/backend/src/remote/activitypub/models/note.ts b/packages/backend/src/remote/activitypub/models/note.ts index a0945ae7b1..033157b081 100644 --- a/packages/backend/src/remote/activitypub/models/note.ts +++ b/packages/backend/src/remote/activitypub/models/note.ts @@ -112,13 +112,13 @@ export async function createNote( const note: IPost = object; if (note.id && !note.id.startsWith("https://")) { - throw new Error(`unexpected shcema of note.id: ${note.id}`); + throw new Error(`unexpected schema of note.id: ${note.id}`); } const url = getOneApHrefNullable(note.url); if (url && !url.startsWith("https://")) { - throw new Error(`unexpected shcema of note url: ${url}`); + throw new Error(`unexpected schema of note url: ${url}`); } logger.debug(`Note fetched: ${JSON.stringify(note, null, 2)}`);