diff --git a/neko/index.js b/neko/index.js index 77894208..54ceecb3 100644 --- a/neko/index.js +++ b/neko/index.js @@ -274,6 +274,7 @@ const { extractHost, toPuny, toPunyOptional, + convertToHiddenPost, sqlLikeEscape, safeForSql, formatMilliseconds, @@ -293,6 +294,7 @@ module.exports.isSelfHost = isSelfHost; module.exports.extractHost = extractHost; module.exports.toPuny = toPuny; module.exports.toPunyOptional = toPunyOptional; +module.exports.convertToHiddenPost = convertToHiddenPost; module.exports.sqlLikeEscape = sqlLikeEscape; module.exports.safeForSql = safeForSql; module.exports.formatMilliseconds = formatMilliseconds; diff --git a/packages/backend/native-utils/Cargo.toml b/packages/backend/native-utils/Cargo.toml index fa51f43e..01e6f064 100644 --- a/packages/backend/native-utils/Cargo.toml +++ b/packages/backend/native-utils/Cargo.toml @@ -8,7 +8,7 @@ members = ["migration"] [features] default = [] -napi = ["dep:napi", "dep:napi-derive"] +napi = ["dep:napi-derive"] [lib] crate-type = ["cdylib", "lib"] @@ -35,7 +35,7 @@ url = "2.5.0" utoipa = "4.1.0" # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix -napi = { version = "2.14.1", default-features = false, features = ["napi9", "tokio_rt"], optional = true } +napi = { version = "2.14.1", default-features = false, features = ["napi9", "tokio_rt"] } napi-derive = { version = "2.14.5", optional = true } basen = "0.1.0" diff --git a/packages/backend/native-utils/src/util/convert_to_hidden_post.rs b/packages/backend/native-utils/src/util/convert_to_hidden_post.rs new file mode 100644 index 00000000..3a713f68 --- /dev/null +++ b/packages/backend/native-utils/src/util/convert_to_hidden_post.rs @@ -0,0 +1,25 @@ +use napi; + +#[cfg_attr(feature = "napi", napi_derive::napi(object))] +pub struct Post { + pub text: Option, + pub cw: Option, + pub local_only: bool, + pub created_at: napi::JsDate, + /// FIXME: introduce enum type (and remove "hiddensomething" visibility if possible) + pub visibility: String, +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn convert_to_hidden_post(original_post: Post) -> Post { + Post { + text: match original_post.text { + Some(s) if !s.is_empty() => Some(s), + _ => None, + }, + cw: original_post.cw, + local_only: original_post.local_only, + created_at: original_post.created_at, + visibility: format!("hidden{}", original_post.visibility), + } +} diff --git a/packages/backend/native-utils/src/util/mod.rs b/packages/backend/native-utils/src/util/mod.rs index 4c20f677..441b0325 100644 --- a/packages/backend/native-utils/src/util/mod.rs +++ b/packages/backend/native-utils/src/util/mod.rs @@ -1,6 +1,7 @@ pub mod acct; pub mod config; pub mod convert_host; +pub mod convert_to_hidden_post; pub mod escape_sql; pub mod format_milliseconds; pub mod id; diff --git a/packages/backend/src/misc/post.ts b/packages/backend/src/misc/post.ts deleted file mode 100644 index 0b107ed0..00000000 --- a/packages/backend/src/misc/post.ts +++ /dev/null @@ -1,21 +0,0 @@ -export type Post = { - text: string | undefined; - cw: string | null; - localOnly: boolean; - createdAt: Date; - visibility: string; -}; - -export function parse(acct: any): Post { - return { - text: acct.text || undefined, - cw: acct.cw, - localOnly: acct.localOnly, - createdAt: new Date(acct.createdAt), - visibility: `hidden${acct.visibility || ""}`, - }; -} - -export function toJson(acct: Post): string { - return { text: acct.text, cw: acct.cw, localOnly: acct.localOnly }.toString(); -} diff --git a/packages/backend/src/queue/processors/db/import-firefish-post.ts b/packages/backend/src/queue/processors/db/import-firefish-post.ts index 65879601..5d33488d 100644 --- a/packages/backend/src/queue/processors/db/import-firefish-post.ts +++ b/packages/backend/src/queue/processors/db/import-firefish-post.ts @@ -1,4 +1,4 @@ -import * as Post from "@/misc/post.js"; +import { convertToHiddenPost } from "native-utils/built/index.js"; import create from "@/services/note/create.js"; import { Users } from "@/models/index.js"; import type { DbUserImportMastoPostJobData } from "@/queue/types.js"; @@ -52,7 +52,8 @@ export async function importCkPost( logger.error(`Skipped adding file to drive: ${url}`); } } - const { text, cw, localOnly, createdAt, visibility } = Post.parse(post); + const { text, cw, localOnly, createdAt, visibility } = + convertToHiddenPost(post); let note = await Notes.findOneBy({ createdAt: createdAt, text: text,