From 4b401e189fcf19812c5da8599dffcf7bbc924a05 Mon Sep 17 00:00:00 2001 From: naskya Date: Mon, 15 Jan 2024 15:42:55 +0900 Subject: [PATCH] refactor: move misc/convert-host.ts to native-utils --- neko/index.js | 12 ++++ packages/backend/native-utils/Cargo.lock | 3 + packages/backend/native-utils/Cargo.toml | 3 + .../backend/native-utils/src/util/config.rs | 20 ++++++ .../native-utils/src/util/convert_host.rs | 64 +++++++++++++++++++ packages/backend/native-utils/src/util/mod.rs | 2 + .../backend/src/misc/check-hit-antenna.ts | 3 +- packages/backend/src/misc/convert-host.ts | 28 -------- packages/backend/src/misc/populate-emojis.ts | 6 +- packages/backend/src/misc/reaction-lib.ts | 4 +- .../src/models/repositories/drive-file.ts | 2 +- .../queue/processors/db/export-blocking.ts | 2 +- .../queue/processors/db/export-following.ts | 2 +- .../src/queue/processors/db/export-mute.ts | 2 +- .../queue/processors/db/export-user-lists.ts | 2 +- .../queue/processors/db/import-blocking.ts | 3 +- .../queue/processors/db/import-following.ts | 3 +- .../src/queue/processors/db/import-muting.ts | 3 +- .../queue/processors/db/import-user-lists.ts | 3 +- .../backend/src/queue/processors/deliver.ts | 2 +- .../backend/src/queue/processors/inbox.ts | 8 +-- .../src/remote/activitypub/check-fetch.ts | 2 +- .../activitypub/kernel/announce/note.ts | 4 +- .../remote/activitypub/kernel/create/note.ts | 4 +- .../src/remote/activitypub/kernel/index.ts | 4 +- .../src/remote/activitypub/kernel/read.ts | 4 +- .../src/remote/activitypub/models/note.ts | 16 ++--- .../src/remote/activitypub/models/person.ts | 2 +- .../src/remote/activitypub/resolver.ts | 6 +- packages/backend/src/remote/resolve-user.ts | 2 +- packages/backend/src/server/activitypub.ts | 2 +- .../backend/src/server/api/common/signup.ts | 4 +- .../api/endpoints/admin/emoji/list-remote.ts | 3 +- .../refresh-remote-instance-metadata.ts | 2 +- .../admin/federation/update-instance.ts | 2 +- .../src/server/api/endpoints/ap/show.ts | 4 +- .../api/endpoints/federation/show-instance.ts | 2 +- .../server/api/endpoints/users/followers.ts | 4 +- .../server/api/endpoints/users/following.ts | 4 +- .../backend/src/services/messages/create.ts | 2 +- .../register-or-fetch-instance-doc.ts | 2 +- 41 files changed, 161 insertions(+), 91 deletions(-) create mode 100644 packages/backend/native-utils/src/util/config.rs create mode 100644 packages/backend/native-utils/src/util/convert_host.rs delete mode 100644 packages/backend/src/misc/convert-host.ts diff --git a/neko/index.js b/neko/index.js index 57b993977..77894208f 100644 --- a/neko/index.js +++ b/neko/index.js @@ -268,6 +268,12 @@ if (!nativeBinding) { const { stringToAcct, acctToString, + config, + getFullApAccount, + isSelfHost, + extractHost, + toPuny, + toPunyOptional, sqlLikeEscape, safeForSql, formatMilliseconds, @@ -281,6 +287,12 @@ const { module.exports.stringToAcct = stringToAcct; module.exports.acctToString = acctToString; +module.exports.config = config; +module.exports.getFullApAccount = getFullApAccount; +module.exports.isSelfHost = isSelfHost; +module.exports.extractHost = extractHost; +module.exports.toPuny = toPuny; +module.exports.toPunyOptional = toPunyOptional; module.exports.sqlLikeEscape = sqlLikeEscape; module.exports.safeForSql = safeForSql; module.exports.formatMilliseconds = formatMilliseconds; diff --git a/packages/backend/native-utils/Cargo.lock b/packages/backend/native-utils/Cargo.lock index c2fc047a5..e855b3fd3 100644 --- a/packages/backend/native-utils/Cargo.lock +++ b/packages/backend/native-utils/Cargo.lock @@ -1436,6 +1436,7 @@ dependencies = [ "chrono", "cuid2", "derive_more", + "idna", "jsonschema", "napi", "napi-build", @@ -1448,8 +1449,10 @@ dependencies = [ "sea-orm", "serde", "serde_json", + "serde_yaml", "thiserror", "tokio", + "url", "utoipa", ] diff --git a/packages/backend/native-utils/Cargo.toml b/packages/backend/native-utils/Cargo.toml index c797692be..fa51f43e2 100644 --- a/packages/backend/native-utils/Cargo.toml +++ b/packages/backend/native-utils/Cargo.toml @@ -19,6 +19,7 @@ cfg-if = "1.0.0" chrono = "0.4.31" cuid2 = "0.1.2" derive_more = "0.99.17" +idna = "0.5.0" jsonschema = "0.17.1" once_cell = "1.19.0" parse-display = "0.8.2" @@ -27,8 +28,10 @@ schemars = { version = "0.8.16", features = ["chrono"] } sea-orm = { version = "0.12.10", features = ["sqlx-postgres", "runtime-tokio-rustls"] } serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" +serde_yaml = "0.9.29" thiserror = "1.0.52" tokio = { version = "1.35.1", features = ["full"] } +url = "2.5.0" utoipa = "4.1.0" # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix diff --git a/packages/backend/native-utils/src/util/config.rs b/packages/backend/native-utils/src/util/config.rs new file mode 100644 index 000000000..84a226c85 --- /dev/null +++ b/packages/backend/native-utils/src/util/config.rs @@ -0,0 +1,20 @@ +use serde::Deserialize; +use serde_yaml; +use std::env; +use std::fs; + +/// TODO: add more fields +#[derive(Debug, PartialEq, Deserialize)] +#[serde(rename_all = "camelCase")] +#[cfg_attr(feature = "napi", napi_derive::napi(object))] +pub struct Config { + pub url: String, +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn config() -> Config { + let cwd = env::current_dir().unwrap(); + let yml = fs::File::open(cwd.join("../../.config/default.yml")) + .expect("Failed to open '.config/default.yml'"); + serde_yaml::from_reader(yml).expect("Failed to parse yaml") +} diff --git a/packages/backend/native-utils/src/util/convert_host.rs b/packages/backend/native-utils/src/util/convert_host.rs new file mode 100644 index 000000000..030228971 --- /dev/null +++ b/packages/backend/native-utils/src/util/convert_host.rs @@ -0,0 +1,64 @@ +use crate::util::config::config; +use idna; +use url::Url; + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn get_full_ap_account(username: String, host: Option) -> String { + if host.is_none() { + format!("{}@{}", username, extract_host(config().url)) + } else { + format!("{}@{}", username, to_puny(host.unwrap())) + } +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn is_self_host(host: Option) -> bool { + if let Some(x) = host { + extract_host(config().url) == to_puny(x) + } else { + true + } +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn extract_host(uri: String) -> String { + to_puny( + Url::parse(uri.as_str()) + .expect("Invalid uri") + .host_str() + .unwrap() + .to_string(), + ) +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn to_puny(host: String) -> String { + idna::domain_to_ascii(&host).expect("Failed to encode the host to punycode") +} + +#[cfg_attr(feature = "napi", napi_derive::napi)] +pub fn to_puny_optional(host: Option) -> Option { + host.map(to_puny) +} + +#[cfg(test)] +mod unit_test { + use super::{extract_host, to_puny}; + use pretty_assertions::assert_eq; + + #[test] + fn extract_host_test() { + assert_eq!( + extract_host("https://git.joinfirefish.org/firefish/firefish.git".to_string()), + "git.joinfirefish.org".to_string() + ); + } + + #[test] + fn to_puny_test() { + assert_eq!( + to_puny("何もかも.owari.shop".to_string()), + "xn--u8jyfb5762a.owari.shop".to_string() + ); + } +} diff --git a/packages/backend/native-utils/src/util/mod.rs b/packages/backend/native-utils/src/util/mod.rs index 0c4e1160c..4c20f6778 100644 --- a/packages/backend/native-utils/src/util/mod.rs +++ b/packages/backend/native-utils/src/util/mod.rs @@ -1,4 +1,6 @@ pub mod acct; +pub mod config; +pub mod convert_host; pub mod escape_sql; pub mod format_milliseconds; pub mod id; diff --git a/packages/backend/src/misc/check-hit-antenna.ts b/packages/backend/src/misc/check-hit-antenna.ts index 23e409d19..5e8f39601 100644 --- a/packages/backend/src/misc/check-hit-antenna.ts +++ b/packages/backend/src/misc/check-hit-antenna.ts @@ -2,8 +2,7 @@ import type { Antenna } from "@/models/entities/antenna.js"; import type { Note } from "@/models/entities/note.js"; import type { User } from "@/models/entities/user.js"; import { Blockings, Followings, UserProfiles } from "@/models/index.js"; -import { getFullApAccount } from "@/misc/convert-host.js"; -import { stringToAcct } from "native-utils/built/index.js"; +import { getFullApAccount, stringToAcct } from "native-utils/built/index.js"; import type { Packed } from "@/misc/schema.js"; import { Cache } from "@/misc/cache.js"; import { getWordHardMute } from "@/misc/check-word-mute.js"; diff --git a/packages/backend/src/misc/convert-host.ts b/packages/backend/src/misc/convert-host.ts deleted file mode 100644 index 856ce3c12..000000000 --- a/packages/backend/src/misc/convert-host.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { URL } from "node:url"; -import config from "@/config/index.js"; -import { toASCII } from "punycode"; - -export function getFullApAccount(username: string, host: string | null) { - return host - ? `${username}@${toPuny(host)}` - : `${username}@${toPuny(config.host)}`; -} - -export function isSelfHost(host: string) { - if (host == null) return true; - return toPuny(config.host) === toPuny(host); -} - -export function extractDbHost(uri: string) { - const url = new URL(uri); - return toPuny(url.hostname); -} - -export function toPuny(host: string) { - return toASCII(host.toLowerCase()); -} - -export function toPunyNullable(host: string | null | undefined): string | null { - if (host == null) return null; - return toASCII(host.toLowerCase()); -} diff --git a/packages/backend/src/misc/populate-emojis.ts b/packages/backend/src/misc/populate-emojis.ts index 5bd74f190..ec533d549 100644 --- a/packages/backend/src/misc/populate-emojis.ts +++ b/packages/backend/src/misc/populate-emojis.ts @@ -3,7 +3,7 @@ import { Emojis } from "@/models/index.js"; import type { Emoji } from "@/models/entities/emoji.js"; import type { Note } from "@/models/entities/note.js"; import { Cache } from "./cache.js"; -import { isSelfHost, toPunyNullable } from "./convert-host.js"; +import { isSelfHost, toPunyOptional } from "native-utils/built/index.js"; import { decodeReaction } from "./reaction-lib.js"; import config from "@/config/index.js"; import { query } from "@/prelude/url.js"; @@ -35,7 +35,7 @@ function normalizeHost( ? null // 自ホスト指定 : src || noteUserHost; // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない) - host = toPunyNullable(host); + host = toPunyOptional(host); return host; } @@ -47,7 +47,7 @@ function parseEmojiStr(emojiName: string, noteUserHost: string | null) { const name = match[1]; // ホスト正規化 - const host = toPunyNullable(normalizeHost(match[2], noteUserHost)); + const host = toPunyOptional(normalizeHost(match[2], noteUserHost)); return { name, host }; } diff --git a/packages/backend/src/misc/reaction-lib.ts b/packages/backend/src/misc/reaction-lib.ts index db88b0590..7c4bdd84a 100644 --- a/packages/backend/src/misc/reaction-lib.ts +++ b/packages/backend/src/misc/reaction-lib.ts @@ -1,7 +1,7 @@ import { emojiRegex } from "./emoji-regex.js"; import { fetchMeta } from "./fetch-meta.js"; import { Emojis } from "@/models/index.js"; -import { toPunyNullable } from "./convert-host.js"; +import { toPunyOptional } from "native-utils/built/index.js"; import { IsNull } from "typeorm"; export function convertReactions(reactions: Record) { @@ -23,7 +23,7 @@ export async function toDbReaction( ): Promise { if (!reaction) return (await fetchMeta()).defaultReaction; - reacterHost = toPunyNullable(reacterHost); + reacterHost = toPunyOptional(reacterHost); if (reaction.includes("❤") || reaction.includes("♥️")) return "❤️"; diff --git a/packages/backend/src/models/repositories/drive-file.ts b/packages/backend/src/models/repositories/drive-file.ts index 1b4f477d3..5b2ea3a03 100644 --- a/packages/backend/src/models/repositories/drive-file.ts +++ b/packages/backend/src/models/repositories/drive-file.ts @@ -1,7 +1,7 @@ import { db } from "@/db/postgre.js"; import { DriveFile } from "@/models/entities/drive-file.js"; import type { User } from "@/models/entities/user.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import { awaitAll } from "@/prelude/await-all.js"; import type { Packed } from "@/misc/schema.js"; import config from "@/config/index.js"; diff --git a/packages/backend/src/queue/processors/db/export-blocking.ts b/packages/backend/src/queue/processors/db/export-blocking.ts index b3e7b0b66..521d18f02 100644 --- a/packages/backend/src/queue/processors/db/export-blocking.ts +++ b/packages/backend/src/queue/processors/db/export-blocking.ts @@ -4,7 +4,7 @@ import * as fs from "node:fs"; import { queueLogger } from "../../logger.js"; import { addFile } from "@/services/drive/add-file.js"; import { format as dateFormat } from "date-fns"; -import { getFullApAccount } from "@/misc/convert-host.js"; +import { getFullApAccount } from "native-utils/built/index.js"; import { createTemp } from "@/misc/create-temp.js"; import { Users, Blockings } from "@/models/index.js"; import { MoreThan } from "typeorm"; diff --git a/packages/backend/src/queue/processors/db/export-following.ts b/packages/backend/src/queue/processors/db/export-following.ts index f3483406c..e3020bbf4 100644 --- a/packages/backend/src/queue/processors/db/export-following.ts +++ b/packages/backend/src/queue/processors/db/export-following.ts @@ -4,7 +4,7 @@ import * as fs from "node:fs"; import { queueLogger } from "../../logger.js"; import { addFile } from "@/services/drive/add-file.js"; import { format as dateFormat } from "date-fns"; -import { getFullApAccount } from "@/misc/convert-host.js"; +import { getFullApAccount } from "native-utils/built/index.js"; import { createTemp } from "@/misc/create-temp.js"; import { Users, Followings, Mutings } from "@/models/index.js"; import { In, MoreThan, Not } from "typeorm"; diff --git a/packages/backend/src/queue/processors/db/export-mute.ts b/packages/backend/src/queue/processors/db/export-mute.ts index c35c602a1..39ce3771f 100644 --- a/packages/backend/src/queue/processors/db/export-mute.ts +++ b/packages/backend/src/queue/processors/db/export-mute.ts @@ -4,7 +4,7 @@ import * as fs from "node:fs"; import { queueLogger } from "../../logger.js"; import { addFile } from "@/services/drive/add-file.js"; import { format as dateFormat } from "date-fns"; -import { getFullApAccount } from "@/misc/convert-host.js"; +import { getFullApAccount } from "native-utils/built/index.js"; import { createTemp } from "@/misc/create-temp.js"; import { Users, Mutings } from "@/models/index.js"; import { IsNull, MoreThan } from "typeorm"; diff --git a/packages/backend/src/queue/processors/db/export-user-lists.ts b/packages/backend/src/queue/processors/db/export-user-lists.ts index 310bd90b3..6c32c803a 100644 --- a/packages/backend/src/queue/processors/db/export-user-lists.ts +++ b/packages/backend/src/queue/processors/db/export-user-lists.ts @@ -4,7 +4,7 @@ import * as fs from "node:fs"; import { queueLogger } from "../../logger.js"; import { addFile } from "@/services/drive/add-file.js"; import { format as dateFormat } from "date-fns"; -import { getFullApAccount } from "@/misc/convert-host.js"; +import { getFullApAccount } from "native-utils/built/index.js"; import { createTemp } from "@/misc/create-temp.js"; import { Users, UserLists, UserListJoinings } from "@/models/index.js"; import { In } from "typeorm"; diff --git a/packages/backend/src/queue/processors/db/import-blocking.ts b/packages/backend/src/queue/processors/db/import-blocking.ts index cac91af7f..a0bed08a8 100644 --- a/packages/backend/src/queue/processors/db/import-blocking.ts +++ b/packages/backend/src/queue/processors/db/import-blocking.ts @@ -1,10 +1,9 @@ import type Bull from "bull"; import { queueLogger } from "../../logger.js"; -import { stringToAcct } from "native-utils/built/index.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, toPuny } from "@/misc/convert-host.js"; +import { isSelfHost, stringToAcct, toPuny } from "native-utils/built/index.js"; import { Users, DriveFiles } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import block from "@/services/blocking/create.js"; diff --git a/packages/backend/src/queue/processors/db/import-following.ts b/packages/backend/src/queue/processors/db/import-following.ts index c2e597399..09ee01fa8 100644 --- a/packages/backend/src/queue/processors/db/import-following.ts +++ b/packages/backend/src/queue/processors/db/import-following.ts @@ -1,10 +1,9 @@ import { IsNull } from "typeorm"; import follow from "@/services/following/create.js"; -import { stringToAcct } from "native-utils/built/index.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, toPuny } from "@/misc/convert-host.js"; +import { isSelfHost, stringToAcct, toPuny } from "native-utils/built/index.js"; import { Users, DriveFiles } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import { queueLogger } from "../../logger.js"; diff --git a/packages/backend/src/queue/processors/db/import-muting.ts b/packages/backend/src/queue/processors/db/import-muting.ts index 83c9f5d49..fc76bbd9b 100644 --- a/packages/backend/src/queue/processors/db/import-muting.ts +++ b/packages/backend/src/queue/processors/db/import-muting.ts @@ -1,10 +1,9 @@ import type Bull from "bull"; import { queueLogger } from "../../logger.js"; -import { stringToAcct } from "native-utils/built/index.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, toPuny } from "@/misc/convert-host.js"; +import { isSelfHost, stringToAcct, toPuny } from "native-utils/built/index.js"; import { Users, DriveFiles, Mutings } from "@/models/index.js"; import type { DbUserImportJobData } from "@/queue/types.js"; import type { User } from "@/models/entities/user.js"; diff --git a/packages/backend/src/queue/processors/db/import-user-lists.ts b/packages/backend/src/queue/processors/db/import-user-lists.ts index 6661b3155..a9a96a381 100644 --- a/packages/backend/src/queue/processors/db/import-user-lists.ts +++ b/packages/backend/src/queue/processors/db/import-user-lists.ts @@ -1,11 +1,10 @@ import type Bull from "bull"; import { queueLogger } from "../../logger.js"; -import { stringToAcct } from "native-utils/built/index.js"; import { resolveUser } from "@/remote/resolve-user.js"; import { pushUserToUserList } from "@/services/user-list/push.js"; import { downloadTextFile } from "@/misc/download-text-file.js"; -import { isSelfHost, toPuny } from "@/misc/convert-host.js"; +import { isSelfHost, stringToAcct, toPuny } from "native-utils/built/index.js"; import { DriveFiles, Users, diff --git a/packages/backend/src/queue/processors/deliver.ts b/packages/backend/src/queue/processors/deliver.ts index 45c2d3dc9..067f1558c 100644 --- a/packages/backend/src/queue/processors/deliver.ts +++ b/packages/backend/src/queue/processors/deliver.ts @@ -4,7 +4,7 @@ import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instanc import Logger from "@/services/logger.js"; import { Instances } from "@/models/index.js"; import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import { StatusError } from "@/misc/fetch.js"; import { shouldSkipInstance } from "@/misc/skipped-instances.js"; import type { DeliverJobData } from "@/queue/types.js"; diff --git a/packages/backend/src/queue/processors/inbox.ts b/packages/backend/src/queue/processors/inbox.ts index e8bd3bac9..671d3afc7 100644 --- a/packages/backend/src/queue/processors/inbox.ts +++ b/packages/backend/src/queue/processors/inbox.ts @@ -6,7 +6,7 @@ import Logger from "@/services/logger.js"; import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js"; import { Instances } from "@/models/index.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; -import { toPuny, extractDbHost } from "@/misc/convert-host.js"; +import { toPuny, extractHost } from "native-utils/built/index.js"; import { getApId } from "@/remote/activitypub/type.js"; import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js"; import type { InboxJobData } from "../types.js"; @@ -158,7 +158,7 @@ export default async (job: Bull.Job): Promise => { } // ブロックしてたら中断 - const ldHost = extractDbHost(authUser.user.uri); + const ldHost = extractHost(authUser.user.uri); if (await shouldBlockInstance(ldHost, meta)) { return `Blocked request: ${ldHost}`; } @@ -169,8 +169,8 @@ export default async (job: Bull.Job): Promise => { // activity.idがあればホストが署名者のホストであることを確認する if (typeof activity.id === "string") { - const signerHost = extractDbHost(authUser.user.uri!); - const activityIdHost = extractDbHost(activity.id); + const signerHost = extractHost(authUser.user.uri!); + const activityIdHost = extractHost(activity.id); if (signerHost !== activityIdHost) { return `skip: signerHost(${signerHost}) !== activity.id host(${activityIdHost}`; } diff --git a/packages/backend/src/remote/activitypub/check-fetch.ts b/packages/backend/src/remote/activitypub/check-fetch.ts index a170e3eb3..a058f4d8c 100644 --- a/packages/backend/src/remote/activitypub/check-fetch.ts +++ b/packages/backend/src/remote/activitypub/check-fetch.ts @@ -2,7 +2,7 @@ import { URL } from "url"; import httpSignature, { IParsedSignature } from "@peertube/http-signature"; import config from "@/config/index.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import DbResolver from "@/remote/activitypub/db-resolver.js"; import { getApId } from "@/remote/activitypub/type.js"; import { shouldBlockInstance } from "@/misc/should-block-instance.js"; diff --git a/packages/backend/src/remote/activitypub/kernel/announce/note.ts b/packages/backend/src/remote/activitypub/kernel/announce/note.ts index 50b59932c..591b1d048 100644 --- a/packages/backend/src/remote/activitypub/kernel/announce/note.ts +++ b/packages/backend/src/remote/activitypub/kernel/announce/note.ts @@ -5,7 +5,7 @@ import type { IAnnounce } from "../../type.js"; import { getApId } from "../../type.js"; import { fetchNote, resolveNote } from "../../models/note.js"; import { apLogger } from "../../logger.js"; -import { extractDbHost } from "@/misc/convert-host.js"; +import { extractHost } from "native-utils/built/index.js"; import { getApLock } from "@/misc/app-lock.js"; import { parseAudience } from "../../audience.js"; import { StatusError } from "@/misc/fetch.js"; @@ -30,7 +30,7 @@ export default async function ( } // Interrupt if you block the announcement destination - if (await shouldBlockInstance(extractDbHost(uri))) return; + if (await shouldBlockInstance(extractHost(uri))) return; const lock = await getApLock(uri); diff --git a/packages/backend/src/remote/activitypub/kernel/create/note.ts b/packages/backend/src/remote/activitypub/kernel/create/note.ts index 92b0ffb1e..5453ca051 100644 --- a/packages/backend/src/remote/activitypub/kernel/create/note.ts +++ b/packages/backend/src/remote/activitypub/kernel/create/note.ts @@ -4,7 +4,7 @@ import { createNote, fetchNote } from "../../models/note.js"; import type { IObject, ICreate } from "../../type.js"; import { getApId } from "../../type.js"; import { getApLock } from "@/misc/app-lock.js"; -import { extractDbHost } from "@/misc/convert-host.js"; +import { extractHost } from "native-utils/built/index.js"; import { StatusError } from "@/misc/fetch.js"; /** @@ -25,7 +25,7 @@ export default async function ( } if (typeof note.id === "string") { - if (extractDbHost(actor.uri) !== extractDbHost(note.id)) { + if (extractHost(actor.uri) !== extractHost(note.id)) { return "skip: host in actor.uri !== note.id"; } } diff --git a/packages/backend/src/remote/activitypub/kernel/index.ts b/packages/backend/src/remote/activitypub/kernel/index.ts index a90ac0234..f4e61f07f 100644 --- a/packages/backend/src/remote/activitypub/kernel/index.ts +++ b/packages/backend/src/remote/activitypub/kernel/index.ts @@ -38,7 +38,7 @@ import block from "./block/index.js"; import flag from "./flag/index.js"; import move from "./move/index.js"; import type { IObject, IActivity } from "../type.js"; -import { extractDbHost } from "@/misc/convert-host.js"; +import { extractHost } from "native-utils/built/index.js"; import { shouldBlockInstance } from "@/misc/should-block-instance.js"; export async function performActivity( @@ -71,7 +71,7 @@ async function performOneActivity( if (actor.isSuspended) return; if (typeof activity.id !== "undefined") { - const host = extractDbHost(getApId(activity)); + const host = extractHost(getApId(activity)); if (await shouldBlockInstance(host)) return; } diff --git a/packages/backend/src/remote/activitypub/kernel/read.ts b/packages/backend/src/remote/activitypub/kernel/read.ts index 53fa7fe63..20f5d3128 100644 --- a/packages/backend/src/remote/activitypub/kernel/read.ts +++ b/packages/backend/src/remote/activitypub/kernel/read.ts @@ -1,7 +1,7 @@ import type { CacheableRemoteUser } from "@/models/entities/user.js"; import type { IRead } from "../type.js"; import { getApId } from "../type.js"; -import { isSelfHost, extractDbHost } from "@/misc/convert-host.js"; +import { isSelfHost, extractHost } from "native-utils/built/index.js"; import { MessagingMessages } from "@/models/index.js"; import { readUserMessagingMessage } from "@/server/api/common/read-messaging-message.js"; @@ -11,7 +11,7 @@ export const performReadActivity = async ( ): Promise => { const id = await getApId(activity.object); - if (!isSelfHost(extractDbHost(id))) { + if (!isSelfHost(extractHost(id))) { return `skip: Read to foreign host (${id})`; } diff --git a/packages/backend/src/remote/activitypub/models/note.ts b/packages/backend/src/remote/activitypub/models/note.ts index 1a36b8bfa..3279c1c1c 100644 --- a/packages/backend/src/remote/activitypub/models/note.ts +++ b/packages/backend/src/remote/activitypub/models/note.ts @@ -14,7 +14,7 @@ import { extractPollFromQuestion } from "./question.js"; import vote from "@/services/note/polls/vote.js"; import { apLogger } from "../logger.js"; import { DriveFile } from "@/models/entities/drive-file.js"; -import { extractDbHost, toPuny } from "@/misc/convert-host.js"; +import { extractHost, toPuny } from "native-utils/built/index.js"; import { Emojis, Polls, @@ -54,7 +54,7 @@ import { langmap } from "@/misc/langmap.js"; const logger = apLogger; export function validateNote(object: any, uri: string) { - const expectHost = extractDbHost(uri); + const expectHost = extractHost(uri); if (object == null) { return new Error("invalid Note: object is null"); @@ -64,9 +64,9 @@ export function validateNote(object: any, uri: string) { return new Error(`invalid Note: invalid object type ${getApType(object)}`); } - if (object.id && extractDbHost(object.id) !== expectHost) { + if (object.id && extractHost(object.id) !== expectHost) { return new Error( - `invalid Note: id has different host. expected: ${expectHost}, actual: ${extractDbHost( + `invalid Note: id has different host. expected: ${expectHost}, actual: ${extractHost( object.id, )}`, ); @@ -74,10 +74,10 @@ export function validateNote(object: any, uri: string) { if ( object.attributedTo && - extractDbHost(getOneApId(object.attributedTo)) !== expectHost + extractHost(getOneApId(object.attributedTo)) !== expectHost ) { return new Error( - `invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${extractDbHost( + `invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${extractHost( object.attributedTo, )}`, ); @@ -422,11 +422,11 @@ export async function resolveNote( if (uri == null) throw new Error("missing uri"); // Abort if origin host is blocked - if (await shouldBlockInstance(extractDbHost(uri))) + if (await shouldBlockInstance(extractHost(uri))) throw new StatusError( "host blocked", 451, - `host ${extractDbHost(uri)} is blocked`, + `host ${extractHost(uri)} is blocked`, ); const lock = await getApLock(uri); diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index e0eb72072..5be29aad1 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -19,7 +19,7 @@ import { UserNotePining } from "@/models/entities/user-note-pining.js"; import { genId } from "@/misc/gen-id.js"; import { UserPublickey } from "@/models/entities/user-publickey.js"; import { isDuplicateKeyValueError } from "@/misc/is-duplicate-key-value-error.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import { UserProfile } from "@/models/entities/user-profile.js"; import { toArray } from "@/prelude/array.js"; import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js"; diff --git a/packages/backend/src/remote/activitypub/resolver.ts b/packages/backend/src/remote/activitypub/resolver.ts index c4bee109d..18f88d538 100644 --- a/packages/backend/src/remote/activitypub/resolver.ts +++ b/packages/backend/src/remote/activitypub/resolver.ts @@ -3,7 +3,7 @@ import { getJson } from "@/misc/fetch.js"; import type { ILocalUser } from "@/models/entities/user.js"; import { getInstanceActor } from "@/services/instance-actor.js"; import { fetchMeta } from "@/misc/fetch-meta.js"; -import { extractDbHost, isSelfHost } from "@/misc/convert-host.js"; +import { extractHost, isSelfHost } from "native-utils/built/index.js"; import { signedGet } from "./request.js"; import type { IObject, ICollection, IOrderedCollection } from "./type.js"; import { isCollectionOrOrderedCollection, getApId } from "./type.js"; @@ -62,7 +62,7 @@ export default class Resolver { if (typeof value !== "string") { apLogger.debug("Object to resolve is not a string"); if (typeof value.id !== "undefined") { - const host = extractDbHost(getApId(value)); + const host = extractHost(getApId(value)); if (await shouldBlockInstance(host)) { throw new Error("instance is blocked"); } @@ -89,7 +89,7 @@ export default class Resolver { } this.history.add(value); - const host = extractDbHost(value); + const host = extractHost(value); if (isSelfHost(host)) { return await this.resolveLocal(value); } diff --git a/packages/backend/src/remote/resolve-user.ts b/packages/backend/src/remote/resolve-user.ts index a6c1e399a..5c6d641e4 100644 --- a/packages/backend/src/remote/resolve-user.ts +++ b/packages/backend/src/remote/resolve-user.ts @@ -4,7 +4,7 @@ import { IsNull } from "typeorm"; import config from "@/config/index.js"; import type { User, IRemoteUser } from "@/models/entities/user.js"; import { Users } from "@/models/index.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import webFinger from "./webfinger.js"; import { createPerson, updatePerson } from "./activitypub/models/person.js"; import { remoteLogger } from "./logger.js"; diff --git a/packages/backend/src/server/activitypub.ts b/packages/backend/src/server/activitypub.ts index 68ccec48c..e21040a3e 100644 --- a/packages/backend/src/server/activitypub.ts +++ b/packages/backend/src/server/activitypub.ts @@ -9,7 +9,7 @@ import renderKey from "@/remote/activitypub/renderer/key.js"; import { renderPerson } from "@/remote/activitypub/renderer/person.js"; import renderEmoji from "@/remote/activitypub/renderer/emoji.js"; import { inbox as processInbox } from "@/queue/index.js"; -import { isSelfHost } from "@/misc/convert-host.js"; +import { isSelfHost } from "native-utils/built/index.js"; import { Notes, Users, diff --git a/packages/backend/src/server/api/common/signup.ts b/packages/backend/src/server/api/common/signup.ts index dc76c264a..92889caa5 100644 --- a/packages/backend/src/server/api/common/signup.ts +++ b/packages/backend/src/server/api/common/signup.ts @@ -5,7 +5,7 @@ import { Users, UsedUsernames } from "@/models/index.js"; import { UserProfile } from "@/models/entities/user-profile.js"; import { IsNull } from "typeorm"; import { genId } from "@/misc/gen-id.js"; -import { toPunyNullable } from "@/misc/convert-host.js"; +import { toPunyOptional } from "native-utils/built/index.js"; import { UserKeypair } from "@/models/entities/user-keypair.js"; import { UsedUsername } from "@/models/entities/used-username.js"; import { db } from "@/db/postgre.js"; @@ -100,7 +100,7 @@ export async function signup(opts: { createdAt: new Date(), username: username, usernameLower: username.toLowerCase(), - host: toPunyNullable(host), + host: toPunyOptional(host), token: secret, isAdmin: (await Users.countBy({ diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts b/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts index 879ab56c5..ec21abd2a 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/list-remote.ts @@ -1,9 +1,8 @@ import define from "@/server/api/define.js"; import { ApiError } from "@/server/api/error.js"; import { Emojis } from "@/models/index.js"; -import { toPuny } from "@/misc/convert-host.js"; import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; -import { sqlLikeEscape } from "native-utils/built/index.js"; +import { sqlLikeEscape, toPuny } from "native-utils/built/index.js"; export const meta = { tags: ["admin", "emoji"], diff --git a/packages/backend/src/server/api/endpoints/admin/federation/refresh-remote-instance-metadata.ts b/packages/backend/src/server/api/endpoints/admin/federation/refresh-remote-instance-metadata.ts index bf71cd250..0d1111d8f 100644 --- a/packages/backend/src/server/api/endpoints/admin/federation/refresh-remote-instance-metadata.ts +++ b/packages/backend/src/server/api/endpoints/admin/federation/refresh-remote-instance-metadata.ts @@ -1,6 +1,6 @@ import define from "@/server/api/define.js"; import { Instances } from "@/models/index.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js"; export const meta = { diff --git a/packages/backend/src/server/api/endpoints/admin/federation/update-instance.ts b/packages/backend/src/server/api/endpoints/admin/federation/update-instance.ts index 653d187c2..0cfeacfb6 100644 --- a/packages/backend/src/server/api/endpoints/admin/federation/update-instance.ts +++ b/packages/backend/src/server/api/endpoints/admin/federation/update-instance.ts @@ -1,6 +1,6 @@ import define from "@/server/api/define.js"; import { Instances } from "@/models/index.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; export const meta = { tags: ["admin"], diff --git a/packages/backend/src/server/api/endpoints/ap/show.ts b/packages/backend/src/server/api/endpoints/ap/show.ts index ec9209ecd..db3df9a3c 100644 --- a/packages/backend/src/server/api/endpoints/ap/show.ts +++ b/packages/backend/src/server/api/endpoints/ap/show.ts @@ -4,7 +4,7 @@ import { createNote } from "@/remote/activitypub/models/note.js"; import DbResolver from "@/remote/activitypub/db-resolver.js"; import Resolver from "@/remote/activitypub/resolver.js"; import { ApiError } from "@/server/api/error.js"; -import { extractDbHost } from "@/misc/convert-host.js"; +import { extractHost } from "native-utils/built/index.js"; import { Users, Notes } from "@/models/index.js"; import type { Note } from "@/models/entities/note.js"; import type { CacheableLocalUser, User } from "@/models/entities/user.js"; @@ -101,7 +101,7 @@ async function fetchAny( me: CacheableLocalUser | null | undefined, ): Promise | null> { // Wait if blocked. - if (await shouldBlockInstance(extractDbHost(uri))) return null; + if (await shouldBlockInstance(extractHost(uri))) return null; const dbResolver = new DbResolver(); diff --git a/packages/backend/src/server/api/endpoints/federation/show-instance.ts b/packages/backend/src/server/api/endpoints/federation/show-instance.ts index c4a6304d0..ac31acac8 100644 --- a/packages/backend/src/server/api/endpoints/federation/show-instance.ts +++ b/packages/backend/src/server/api/endpoints/federation/show-instance.ts @@ -1,6 +1,6 @@ import define from "@/server/api/define.js"; import { Instances } from "@/models/index.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; export const meta = { tags: ["federation"], diff --git a/packages/backend/src/server/api/endpoints/users/followers.ts b/packages/backend/src/server/api/endpoints/users/followers.ts index 541bb6bb2..9e2a343b2 100644 --- a/packages/backend/src/server/api/endpoints/users/followers.ts +++ b/packages/backend/src/server/api/endpoints/users/followers.ts @@ -1,6 +1,6 @@ import { IsNull } from "typeorm"; import { Users, Followings, UserProfiles } from "@/models/index.js"; -import { toPunyNullable } from "@/misc/convert-host.js"; +import { toPunyOptional } from "native-utils/built/index.js"; import define from "@/server/api/define.js"; import { ApiError } from "@/server/api/error.js"; import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; @@ -80,7 +80,7 @@ export default define(meta, paramDef, async (ps, me) => { ? { id: ps.userId } : { usernameLower: ps.username?.toLowerCase(), - host: toPunyNullable(ps.host) ?? IsNull(), + host: toPunyOptional(ps.host) ?? IsNull(), }, ); diff --git a/packages/backend/src/server/api/endpoints/users/following.ts b/packages/backend/src/server/api/endpoints/users/following.ts index 42d54b1ca..0c56ab40a 100644 --- a/packages/backend/src/server/api/endpoints/users/following.ts +++ b/packages/backend/src/server/api/endpoints/users/following.ts @@ -1,6 +1,6 @@ import { IsNull } from "typeorm"; import { Users, Followings, UserProfiles } from "@/models/index.js"; -import { toPunyNullable } from "@/misc/convert-host.js"; +import { toPunyOptional } from "native-utils/built/index.js"; import define from "@/server/api/define.js"; import { ApiError } from "@/server/api/error.js"; import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js"; @@ -79,7 +79,7 @@ export default define(meta, paramDef, async (ps, me) => { ? { id: ps.userId } : { usernameLower: ps.username?.toLowerCase(), - host: toPunyNullable(ps.host) ?? IsNull(), + host: toPunyOptional(ps.host) ?? IsNull(), }, ); diff --git a/packages/backend/src/services/messages/create.ts b/packages/backend/src/services/messages/create.ts index 0b3f8eded..3381bdea1 100644 --- a/packages/backend/src/services/messages/create.ts +++ b/packages/backend/src/services/messages/create.ts @@ -22,7 +22,7 @@ import renderNote from "@/remote/activitypub/renderer/note.js"; import renderCreate from "@/remote/activitypub/renderer/create.js"; import { renderActivity } from "@/remote/activitypub/renderer/index.js"; import { deliver } from "@/queue/index.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import { Instances } from "@/models/index.js"; export async function createMessage( diff --git a/packages/backend/src/services/register-or-fetch-instance-doc.ts b/packages/backend/src/services/register-or-fetch-instance-doc.ts index c0ead0819..17c8d9d1b 100644 --- a/packages/backend/src/services/register-or-fetch-instance-doc.ts +++ b/packages/backend/src/services/register-or-fetch-instance-doc.ts @@ -1,7 +1,7 @@ import type { Instance } from "@/models/entities/instance.js"; import { Instances } from "@/models/index.js"; import { genId } from "@/misc/gen-id.js"; -import { toPuny } from "@/misc/convert-host.js"; +import { toPuny } from "native-utils/built/index.js"; import { Cache } from "@/misc/cache.js"; const cache = new Cache("registerOrFetchInstanceDoc", 60 * 60);