cae6ba0edb
### What does this PR do? Adding files fields in the export notes option, and corresponding import notes Current the mastodon import does not import any attachments, this pr will use the "upload from url" feature to include medias if its a valid URL. There are many way to convert the outbox.json file, can be simple as upload media_attachments to any web hosting and do string replace on the json file. I also create a tool that upload the tar.gz file with auto convert and host the media as simplify the process at https://tempfile.moegirl.live Detail example can be found at https://fedi.moegirl.live/notes/9h76gtqnp2gwl5dz https://r2temp.moegirl.live/2023/7/15/15356683-050f-423a-b331-c9a05561f52a/shana-settings-_-meng-zhai-le-yuan-xyou-yu-ou-xiang-de-luo-ke-ke-wu-yan-moe-otaku-elysian-x-gloomily-idol-s-rococo-luncheon----mozilla-firefox-private-browsing-2023-07-15-18-36-37.mp4 Co-authored-by: CGsama <CGsama@outlook.com> Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10496 Co-authored-by: コルセット姫@がんばらない <cgsama@noreply.codeberg.org> Co-committed-by: コルセット姫@がんばらない <cgsama@noreply.codeberg.org>
85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
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";
|
|
import { uploadFromUrl } from "@/services/drive/upload-from-url.js";
|
|
import type { DriveFile } from "@/models/entities/drive-file.js";
|
|
|
|
const logger = queueLogger.createSubLogger("import-masto-post");
|
|
|
|
export async function importMastoPost(
|
|
job: Bull.Job<DbUserImportMastoPostJobData>,
|
|
done: any,
|
|
): Promise<void> {
|
|
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;
|
|
job.progress(20);
|
|
if (post.object.inReplyTo != null) {
|
|
reply = await resolveNote(post.object.inReplyTo);
|
|
}
|
|
job.progress(40);
|
|
if (post.directMessage) {
|
|
done();
|
|
return;
|
|
}
|
|
if (job.data.signatureCheck) {
|
|
if (!post.signature) {
|
|
done();
|
|
return;
|
|
}
|
|
}
|
|
job.progress(60);
|
|
let text;
|
|
try {
|
|
text = htmlToMfm(post.object.content, post.object.tag);
|
|
} catch (e) {
|
|
throw e;
|
|
}
|
|
job.progress(80);
|
|
const urls = post.object.attachment
|
|
.map((x: any) => x.url)
|
|
.filter((x: String) => x.startsWith("http"));
|
|
const files: DriveFile[] = [];
|
|
for (const url of urls) {
|
|
try {
|
|
const file = await uploadFromUrl({
|
|
url: url,
|
|
user: user,
|
|
});
|
|
files.push(file);
|
|
} catch (e) {
|
|
logger.error(`Skipped adding file to drive: ${url}`);
|
|
}
|
|
}
|
|
|
|
const note = await create(user, {
|
|
createdAt: new Date(post.object.published),
|
|
files: files.length == 0 ? undefined : files,
|
|
poll: undefined,
|
|
text: text || undefined,
|
|
reply,
|
|
renote: null,
|
|
cw: post.object.sensitive ? post.object.summary : undefined,
|
|
localOnly: false,
|
|
visibility: "hidden",
|
|
visibleUsers: [],
|
|
channel: null,
|
|
apMentions: new Array(0),
|
|
apHashtags: undefined,
|
|
apEmojis: undefined,
|
|
});
|
|
job.progress(100);
|
|
done();
|
|
|
|
logger.succ("Imported");
|
|
}
|