hippofish/packages/backend/src/queue/processors/db/import-posts.ts

135 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-03-28 23:48:27 +02:00
import { IsNull } from "typeorm";
import follow from "@/services/following/create.js";
2023-03-28 23:48:27 +02:00
import * as Post from "@/misc/post.js";
2023-03-28 23:48:27 +02:00
import create from "@/services/note/create.js";
import { downloadTextFile } from "@/misc/download-text-file.js";
import { Users, DriveFiles } from "@/models/index.js";
2023-03-30 17:31:29 +02:00
import type { DbUserImportPostsJobData } from "@/queue/types.js";
2023-03-28 23:48:27 +02:00
import { queueLogger } from "../../logger.js";
import type Bull from "bull";
2023-03-29 21:10:01 +02:00
import { htmlToMfm } from "@/remote/activitypub/misc/html-to-mfm.js";
2023-05-08 22:06:30 +02:00
import { resolveNote } from "@/remote/activitypub/models/note.js";
2023-05-08 22:08:29 +02:00
import { Note } from "@/models/entities/note.js";
2023-03-28 23:48:27 +02:00
const logger = queueLogger.createSubLogger("import-posts");
export async function importPosts(
2023-03-30 17:31:29 +02:00
job: Bull.Job<DbUserImportPostsJobData>,
2023-03-28 23:48:27 +02:00
done: any,
): Promise<void> {
2023-03-29 19:23:37 +02:00
logger.info(`Importing posts of ${job.data.user.id} ...`);
2023-03-28 23:48:27 +02:00
const user = await Users.findOneBy({ id: job.data.user.id });
if (user == null) {
done();
return;
}
const file = await DriveFiles.findOneBy({
id: job.data.fileId,
});
if (file == null) {
done();
return;
}
2023-03-29 19:36:23 +02:00
const json = await downloadTextFile(file.url);
2023-03-28 23:48:27 +02:00
let linenum = 0;
2023-03-29 19:36:23 +02:00
try {
2023-03-29 21:10:01 +02:00
const parsed = JSON.parse(json);
if (parsed instanceof Array) {
2023-03-29 21:16:45 +02:00
logger.info("Parsing key style posts");
2023-03-29 21:10:01 +02:00
for (const post of JSON.parse(json)) {
try {
linenum++;
if (post.replyId != null) {
continue;
}
if (post.renoteId != null) {
continue;
}
if (post.visibility !== "public") {
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: "hidden",
2023-03-29 21:10:01 +02:00
visibleUsers: [],
channel: null,
2023-03-30 10:40:20 +02:00
apMentions: new Array(0),
2023-03-29 21:10:01 +02:00
apHashtags: undefined,
apEmojis: undefined,
});
} catch (e) {
logger.warn(`Error in line:${linenum} ${e}`);
}
}
2023-03-29 21:16:45 +02:00
} else if (parsed instanceof Object) {
logger.info("Parsing animal style posts");
2023-03-29 21:10:01 +02:00
for (const post of parsed.orderedItems) {
try {
linenum++;
let reply: Note | null = null;
if (post.object.inReplyTo != null) {
reply = await resolveNote(post.object.inReplyTo);
}
if (post.directMessage) {
continue;
}
if (job.data.signatureCheck) {
if (!post.signature) {
continue;
2023-05-08 22:30:40 +02:00
}
}
let text;
try {
text = htmlToMfm(post.object.content, post.object.tag);
2023-03-29 21:25:28 +02:00
} catch (e) {
continue;
2023-03-29 21:25:28 +02:00
}
logger.info(`Posting[${linenum}] ...`);
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,
});
} catch (e) {
logger.warn(`Error in line:${linenum} ${e}`);
}
2023-03-28 23:48:27 +02:00
}
}
2023-03-29 19:36:23 +02:00
} catch (e) {
// handle error
logger.warn(`Error reading: ${e}`);
2023-03-28 23:48:27 +02:00
}
logger.succ("Imported");
done();
}