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";
|
|
|
|
import type { DbUserImportJobData } from "@/queue/types.js";
|
|
|
|
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-03-28 23:48:27 +02:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger("import-posts");
|
|
|
|
|
|
|
|
export async function importPosts(
|
|
|
|
job: Bull.Job<DbUserImportJobData>,
|
|
|
|
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) {
|
|
|
|
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) {
|
2023-03-29 21:15:06 +02:00
|
|
|
try {
|
|
|
|
linenum++;
|
|
|
|
if (post.inReplyTo != null) {
|
|
|
|
logger.info(`Is reply, skip [${linenum}] ...`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (post.directMessage) {
|
|
|
|
logger.info(`Is dm, skip [${linenum}] ...`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const text = htmlToMfm(post.content, post.tag);
|
|
|
|
logger.info(`Posting[${linenum}] ...`);
|
|
|
|
|
|
|
|
const note = await create(user, {
|
|
|
|
createdAt: new Date(post.published),
|
|
|
|
files: undefined,
|
|
|
|
poll: undefined,
|
|
|
|
text: text || undefined,
|
|
|
|
reply: null,
|
|
|
|
renote: null,
|
|
|
|
cw: post.sensitive,
|
|
|
|
localOnly: false,
|
|
|
|
visibility: "public",
|
|
|
|
visibleUsers: [],
|
|
|
|
channel: null,
|
|
|
|
apMentions: null,
|
|
|
|
apHashtags: undefined,
|
|
|
|
apEmojis: undefined,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn(`Error in line:${linenum} ${e}`);
|
2023-03-29 19:23:37 +02:00
|
|
|
}
|
2023-03-29 21:15:06 +02:00
|
|
|
|
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();
|
|
|
|
}
|