refactor: move misc/post.ts to native-utils

Co-authored-by: naskya <m@naskya.net>
This commit is contained in:
sup39 2024-01-16 03:17:43 +09:00 committed by naskya
parent 0d8ee4387b
commit d6dd6ff897
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
6 changed files with 33 additions and 25 deletions

View file

@ -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;

View file

@ -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"

View file

@ -0,0 +1,25 @@
use napi;
#[cfg_attr(feature = "napi", napi_derive::napi(object))]
pub struct Post {
pub text: Option<String>,
pub cw: Option<String>,
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),
}
}

View file

@ -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;

View file

@ -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();
}

View file

@ -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,