refactor: bring all internal functions into misc/native-utils.ts
Co-authored-by: sup39 <dev@sup39.dev>
This commit is contained in:
parent
93b7396990
commit
0ac115874c
13 changed files with 57 additions and 30 deletions
|
@ -1,20 +1,24 @@
|
|||
use crate::config::server::read_server_config;
|
||||
use crate::config::server::ServerConfig;
|
||||
use idna;
|
||||
use url::Url;
|
||||
|
||||
#[napi_derive::napi]
|
||||
pub fn get_full_ap_account(username: String, host: Option<String>) -> String {
|
||||
pub fn get_full_ap_account(
|
||||
server_config: ServerConfig,
|
||||
username: String,
|
||||
host: Option<String>,
|
||||
) -> String {
|
||||
if host.is_none() {
|
||||
format!("{}@{}", username, extract_host(read_server_config().url))
|
||||
format!("{}@{}", username, extract_host(server_config.url))
|
||||
} else {
|
||||
format!("{}@{}", username, to_puny(host.unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
#[napi_derive::napi]
|
||||
pub fn is_self_host(host: Option<String>) -> bool {
|
||||
pub fn is_self_host(server_config: ServerConfig, host: Option<String>) -> bool {
|
||||
if let Some(x) = host {
|
||||
extract_host(read_server_config().url) == to_puny(x)
|
||||
extract_host(server_config.url) == to_puny(x)
|
||||
} else {
|
||||
true
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ import chalk from "chalk";
|
|||
import Xev from "xev";
|
||||
|
||||
import Logger from "@/services/logger.js";
|
||||
import { envOption } from "@/config/index.js";
|
||||
import { envConfig } from "@/misc/native-utils.js";
|
||||
|
||||
// for typeorm
|
||||
import "reflect-metadata";
|
||||
|
@ -26,14 +26,14 @@ export default async function () {
|
|||
const type = cluster.isPrimary ? "(master)" : "(worker)";
|
||||
process.title = `Firefish ${mode} ${type}`;
|
||||
|
||||
if (cluster.isPrimary || envOption.disableClustering) {
|
||||
if (cluster.isPrimary || envConfig.disableClustering) {
|
||||
await masterMain();
|
||||
if (cluster.isPrimary) {
|
||||
ev.mount();
|
||||
}
|
||||
}
|
||||
|
||||
if (cluster.isWorker || envOption.disableClustering) {
|
||||
if (cluster.isWorker || envConfig.disableClustering) {
|
||||
await workerMain();
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ cluster.on("exit", (worker) => {
|
|||
});
|
||||
|
||||
// Display detail of unhandled promise rejection
|
||||
if (!envOption.quiet) {
|
||||
if (!envConfig.quiet) {
|
||||
process.on("unhandledRejection", console.dir);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ import semver from "semver";
|
|||
import Logger from "@/services/logger.js";
|
||||
import loadConfig from "@/config/load.js";
|
||||
import type { Config } from "@/config/types.js";
|
||||
import { envOption } from "@/config/index.js";
|
||||
import { envConfig } from "@/misc/native-utils.js";
|
||||
import { showMachineInfo } from "@/misc/show-machine-info.js";
|
||||
import { db, initDb } from "@/db/postgre.js";
|
||||
|
||||
|
@ -27,7 +27,7 @@ const bootLogger = logger.createSubLogger("boot", "magenta", false);
|
|||
const themeColor = chalk.hex("#31748f");
|
||||
|
||||
function greet() {
|
||||
if (!envOption.quiet) {
|
||||
if (!envConfig.quiet) {
|
||||
//#region Firefish logo
|
||||
console.log(
|
||||
themeColor(
|
||||
|
@ -109,7 +109,7 @@ export async function masterMain() {
|
|||
|
||||
bootLogger.succ("Firefish initialized");
|
||||
|
||||
if (!envOption.disableClustering) {
|
||||
if (!envConfig.disableClustering) {
|
||||
await spawnWorkers(config.clusterLimits);
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,7 @@ export async function masterMain() {
|
|||
);
|
||||
|
||||
if (
|
||||
!envOption.noDaemons &&
|
||||
!envConfig.noDaemons &&
|
||||
config.clusterLimits?.web &&
|
||||
config.clusterLimits?.web >= 1
|
||||
) {
|
||||
|
|
|
@ -1,5 +1,2 @@
|
|||
import load from "./load.js";
|
||||
import { readEnvironmentConfig } from "native-utils/built/index.js";
|
||||
|
||||
export default load();
|
||||
export const envOption = readEnvironmentConfig();
|
||||
|
|
|
@ -2,7 +2,8 @@ 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, stringToAcct } from "native-utils/built/index.js";
|
||||
import { getFullApAccount } from "@/misc/native-utils.js";
|
||||
import { 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";
|
||||
|
@ -25,7 +26,7 @@ export async function checkHitAntenna(
|
|||
if (antenna.src === "users") {
|
||||
const accts = antenna.users.map((x) => {
|
||||
const { username, host } = stringToAcct(x);
|
||||
return getFullApAccount(username, host ?? null).toLowerCase();
|
||||
return getFullApAccount(username, host).toLowerCase();
|
||||
});
|
||||
if (
|
||||
!accts.includes(
|
||||
|
|
22
packages/backend/src/misc/native-utils.ts
Normal file
22
packages/backend/src/misc/native-utils.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import {
|
||||
readServerConfig,
|
||||
readEnvironmentConfig,
|
||||
getFullApAccount as getFullApAccountImpl,
|
||||
isSelfHost as isSelfHostImpl,
|
||||
} from "native-utils/built/index.js";
|
||||
|
||||
export const serverConfig = readServerConfig();
|
||||
export const envConfig = readEnvironmentConfig();
|
||||
|
||||
type Option<T> = T | null | undefined;
|
||||
|
||||
export function getFullApAccount(
|
||||
username: string,
|
||||
host: Option<string>,
|
||||
): string {
|
||||
return getFullApAccountImpl(serverConfig, username, host);
|
||||
}
|
||||
|
||||
export function isSelfHost(host: Option<string>): boolean {
|
||||
return isSelfHostImpl(serverConfig, host);
|
||||
}
|
|
@ -3,7 +3,8 @@ 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, toPunyOptional } from "native-utils/built/index.js";
|
||||
import { toPunyOptional } from "native-utils/built/index.js";
|
||||
import { isSelfHost } from "@/misc/native-utils.js";
|
||||
import { decodeReaction } from "./reaction-lib.js";
|
||||
import config from "@/config/index.js";
|
||||
import { query } from "@/prelude/url.js";
|
||||
|
|
|
@ -5,7 +5,7 @@ import config from "@/config/index.js";
|
|||
import type { DriveFile } from "@/models/entities/drive-file.js";
|
||||
import type { IActivity } from "@/remote/activitypub/type.js";
|
||||
import type { Webhook, webhookEventTypes } from "@/models/entities/webhook.js";
|
||||
import { envOption } from "@/config/index.js";
|
||||
import { envConfig } from "@/misc/native-utils.js";
|
||||
|
||||
import processDeliver from "./processors/deliver.js";
|
||||
import processInbox from "./processors/inbox.js";
|
||||
|
@ -518,7 +518,7 @@ export function webhookDeliver(
|
|||
}
|
||||
|
||||
export default function () {
|
||||
if (envOption.onlyServer) return;
|
||||
if (envConfig.onlyServer) return;
|
||||
|
||||
deliverQueue.process(config.deliverJobConcurrency || 128, processDeliver);
|
||||
inboxQueue.process(config.inboxJobConcurrency || 16, processInbox);
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import type { CacheableRemoteUser } from "@/models/entities/user.js";
|
||||
import type { IRead } from "../type.js";
|
||||
import { getApId } from "../type.js";
|
||||
import { isSelfHost, extractHost } from "native-utils/built/index.js";
|
||||
import { extractHost } from "native-utils/built/index.js";
|
||||
import { isSelfHost } from "@/misc/native-utils.js";
|
||||
import { MessagingMessages } from "@/models/index.js";
|
||||
import { readUserMessagingMessage } from "@/server/api/common/read-messaging-message.js";
|
||||
|
||||
|
|
|
@ -3,7 +3,8 @@ 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 { extractHost, isSelfHost } from "native-utils/built/index.js";
|
||||
import { extractHost } from "native-utils/built/index.js";
|
||||
import { isSelfHost } from "@/misc/native-utils.js";
|
||||
import { signedGet } from "./request.js";
|
||||
import type { IObject, ICollection, IOrderedCollection } from "./type.js";
|
||||
import { isCollectionOrOrderedCollection, getApId } from "./type.js";
|
||||
|
|
|
@ -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 "native-utils/built/index.js";
|
||||
import { isSelfHost } from "@/misc/native-utils.js";
|
||||
import {
|
||||
Notes,
|
||||
Users,
|
||||
|
|
|
@ -20,7 +20,7 @@ import { fetchMeta } from "@/misc/fetch-meta.js";
|
|||
import { genIdenticon } from "@/misc/gen-identicon.js";
|
||||
import { createTemp } from "@/misc/create-temp.js";
|
||||
import { stringToAcct } from "native-utils/built/index.js";
|
||||
import { envOption } from "@/config/index.js";
|
||||
import { envConfig } from "@/misc/native-utils.js";
|
||||
import megalodon, { MegalodonInterface } from "megalodon";
|
||||
import activityPub from "./activitypub.js";
|
||||
import nodeinfo from "./nodeinfo.js";
|
||||
|
@ -57,7 +57,7 @@ if (!["production", "test"].includes(process.env.NODE_ENV || "")) {
|
|||
);
|
||||
|
||||
// Delay
|
||||
if (envOption.slow) {
|
||||
if (envConfig.slow) {
|
||||
app.use(
|
||||
slow({
|
||||
delay: 3000,
|
||||
|
|
|
@ -2,7 +2,7 @@ import cluster from "node:cluster";
|
|||
import chalk from "chalk";
|
||||
import { default as convertColor } from "color-convert";
|
||||
import { format as dateFormat } from "date-fns";
|
||||
import { envOption } from "@/config/index.js";
|
||||
import { envConfig } from "@/misc/native-utils.js";
|
||||
import config from "@/config/index.js";
|
||||
|
||||
import * as SyslogPro from "syslog-pro";
|
||||
|
@ -56,7 +56,7 @@ export default class Logger {
|
|||
subDomains: Domain[] = [],
|
||||
store = true,
|
||||
): void {
|
||||
if (envOption.quiet) return;
|
||||
if (envConfig.quiet) return;
|
||||
if (!this.store) store = false;
|
||||
if (level === "debug") store = false;
|
||||
|
||||
|
@ -111,7 +111,7 @@ export default class Logger {
|
|||
: null;
|
||||
|
||||
let log = `${l} ${worker}\t[${domains.join(" ")}]\t${m}`;
|
||||
if (envOption.withLogTime) log = `${chalk.gray(time)} ${log}`;
|
||||
if (envConfig.withLogTime) log = `${chalk.gray(time)} ${log}`;
|
||||
|
||||
console.log(important ? chalk.bold(log) : log);
|
||||
|
||||
|
@ -187,7 +187,7 @@ export default class Logger {
|
|||
important = false,
|
||||
): void {
|
||||
// デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
||||
if (process.env.NODE_ENV !== "production" || envOption.verbose) {
|
||||
if (process.env.NODE_ENV !== "production" || envConfig.verbose) {
|
||||
this.log("debug", message, data, important);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue