refactor: use dont federate initially flag instead of visibility hack

Co-authored-by: naskya <m@naskya.net>
This commit is contained in:
laozhoubuluo 2024-07-04 02:56:16 +09:00 committed by naskya
parent c0f7939a93
commit ee8679e239
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
4 changed files with 98 additions and 39 deletions

View file

@ -1,3 +1,5 @@
import { noteVisibilities } from "@/types.js";
export type Post = {
text: string | undefined;
cw: string | null;
@ -12,7 +14,9 @@ export function parse(acct: any): Post {
cw: acct.cw,
localOnly: acct.localOnly,
createdAt: new Date(acct.createdAt),
visibility: `hidden${acct.visibility || ""}`,
visibility: noteVisibilities.includes(acct.visibility)
? acct.visibility
: "specified",
};
}

View file

@ -92,22 +92,29 @@ export async function importCkPost(
logger.info("Post updated");
}
if (note == null) {
note = await create(user, {
createdAt: createdAt,
files: files.length === 0 ? undefined : files,
poll: undefined,
text: text || undefined,
reply: post.replyId ? job.data.parent : null,
renote: post.renoteId ? job.data.parent : null,
cw: cw,
localOnly,
visibility: visibility,
visibleUsers: [],
channel: null,
apMentions: new Array(0),
apHashtags: undefined,
apEmojis: undefined,
});
note = await create(
user,
{
createdAt: createdAt,
scheduledAt: undefined,
files: files.length === 0 ? undefined : files,
poll: undefined,
text: text || undefined,
reply: post.replyId ? job.data.parent : null,
renote: post.renoteId ? job.data.parent : null,
cw: cw,
localOnly,
visibility: visibility,
visibleUsers: [],
channel: null,
apMentions: new Array(0),
apHashtags: undefined,
apEmojis: undefined,
},
false,
undefined,
true,
);
logger.debug("New post has been created");
} else {
logger.info("This post already exists");

View file

@ -10,6 +10,10 @@ import type { DriveFile } from "@/models/entities/drive-file.js";
import { Notes, NoteEdits } from "@/models/index.js";
import type { Note } from "@/models/entities/note.js";
import { genId } from "backend-rs";
import promiseLimit from "promise-limit";
import { unique, concat } from "@/prelude/array.js";
import type { CacheableUser } from "@/models/entities/user.js";
import { resolvePerson } from "@/remote/activitypub/models/person.js";
const logger = queueLogger.createSubLogger("import-masto-post");
@ -118,24 +122,57 @@ export async function importMastoPost(
logger.info("Post updated");
}
if (note == null) {
note = await create(user, {
createdAt: isRenote
? new Date(post.published)
: new Date(post.object.published),
files: files.length === 0 ? undefined : files,
poll: undefined,
text: text || undefined,
reply,
renote,
cw: !isRenote && post.object.sensitive ? post.object.summary : undefined,
localOnly: false,
visibility: "hiddenpublic",
visibleUsers: [],
channel: null,
apMentions: new Array(0),
apHashtags: undefined,
apEmojis: undefined,
});
let visibility = "specified";
let visibleUsers: CacheableUser[] = [];
if (isPublic(post.to)) {
visibility = "public";
} else if (isPublic(post.cc)) {
visibility = "home";
} else if (isFollowers(post.cc)) {
visibility = "followers";
} else {
try {
const visibleUsersList = unique(concat([post.to, post.cc]));
const limit = promiseLimit<CacheableUser | null>(2);
visibleUsers = (
await Promise.all(
visibleUsersList.map((id) =>
limit(() => resolvePerson(id).catch(() => null)),
),
)
).filter((x): x is CacheableUser => x != null);
} catch {
// nothing need to do.
}
}
note = await create(
user,
{
createdAt: isRenote
? new Date(post.published)
: new Date(post.object.published),
scheduledAt: undefined,
files: files.length === 0 ? undefined : files,
poll: undefined,
text: text || undefined,
reply,
renote,
cw:
!isRenote && post.object.sensitive ? post.object.summary : undefined,
localOnly: false,
visibility,
visibleUsers,
channel: null,
apMentions: new Array(0),
apHashtags: undefined,
apEmojis: undefined,
},
false,
undefined,
true,
);
logger.debug("New post has been created");
} else {
logger.info("This post already exists");
@ -145,3 +182,15 @@ export async function importMastoPost(
logger.info("Imported");
}
function isPublic(id: string) {
return [
"https://www.w3.org/ns/activitystreams#Public",
"as:Public",
"Public",
].includes(id);
}
function isFollowers(id: string) {
return id.endsWith("/followers");
}

View file

@ -162,11 +162,12 @@ export default async (
data: NoteLike,
silent = false,
waitToPublish?: (note: Note) => Promise<void>,
dontFederateInitially = false,
) =>
// biome-ignore lint/suspicious/noAsyncPromiseExecutor: FIXME
new Promise<Note>(async (res, rej) => {
const dontFederateInitially =
data.visibility?.startsWith("hidden") === true;
dontFederateInitially =
dontFederateInitially || data.visibility?.startsWith("hidden");
// Whether this is a scheduled "draft" post (yet to be published)
const isDraft = data.scheduledAt != null;
@ -204,8 +205,6 @@ export default async (
if (data.channel != null) data.visibility = "public";
if (data.channel != null) data.visibleUsers = [];
if (data.channel != null) data.localOnly = true;
if (data.visibility.startsWith("hidden") && data.visibility !== "hidden")
data.visibility = data.visibility.slice(6);
// enforce silent clients on server
if (