From d6dd6ff8974cb1fcc99cdbd119efb08fe675a719 Mon Sep 17 00:00:00 2001 From: sup39 Date: Tue, 16 Jan 2024 03:17:43 +0900 Subject: [PATCH] refactor: move misc/post.ts to native-utils Co-authored-by: naskya --- neko/index.js | 2 ++ packages/backend/native-utils/Cargo.toml | 4 +-- .../src/util/convert_to_hidden_post.rs | 25 +++++++++++++++++++ packages/backend/native-utils/src/util/mod.rs | 1 + packages/backend/src/misc/post.ts | 21 ---------------- .../processors/db/import-firefish-post.ts | 5 ++-- 6 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 packages/backend/native-utils/src/util/convert_to_hidden_post.rs delete mode 100644 packages/backend/src/misc/post.ts diff --git a/neko/index.js b/neko/index.js index 77894208f..54ceecb36 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 fa51f43e2..01e6f0644 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 000000000..3a713f682 --- /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 4c20f6778..441b0325c 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 0b107ed00..000000000 --- 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 658796015..5d33488d7 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,