refactor (backend): port convert-host to backend-rs

Co-authored-by: sup39 <dev@sup39.dev>
This commit is contained in:
naskya 2024-04-12 17:13:57 +09:00
parent 3d6031dca3
commit 148c3736ce
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
44 changed files with 150 additions and 123 deletions

2
Cargo.lock generated
View file

@ -195,6 +195,7 @@ dependencies = [
"cfg-if",
"chrono",
"cuid2",
"idna",
"jsonschema",
"macro_rs",
"napi",
@ -212,6 +213,7 @@ dependencies = [
"serde_yaml",
"thiserror",
"tokio",
"url",
"urlencoding",
]

View file

@ -15,6 +15,7 @@ cfg-if = "1.0.0"
chrono = "0.4.37"
convert_case = "0.6.0"
cuid2 = "0.1.2"
idna = "0.5.0"
jsonschema = "0.17.1"
once_cell = "1.19.0"
parse-display = "0.9.0"
@ -31,6 +32,7 @@ serde_yaml = "0.9.34"
syn = "2.0.58"
thiserror = "1.0.58"
tokio = "1.37.0"
url = "2.5.0"
urlencoding = "2.1.3"
[profile.release]

View file

@ -22,6 +22,7 @@ basen = { workspace = true }
cfg-if = { workspace = true }
chrono = { workspace = true }
cuid2 = { workspace = true }
idna = { workspace = true }
jsonschema = { workspace = true }
once_cell = { workspace = true }
parse-display = { workspace = true }
@ -34,6 +35,7 @@ serde_json = { workspace = true }
serde_yaml = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
url = { workspace = true }
urlencoding = { workspace = true }
[dev-dependencies]

View file

@ -127,6 +127,11 @@ export interface NoteLike {
replyId: string | null
}
export function checkWordMute(note: NoteLike, mutedWordLists: Array<Array<string>>, mutedPatterns: Array<string>): Promise<boolean>
export function getFullApAccount(username: string, host?: string | undefined | null): string
export function isSelfHost(host?: string | undefined | null): boolean
export function isSameOrigin(uri: string): boolean
export function extractHost(uri: string): string
export function toPuny(host: string): string
export function nyaify(text: string, lang?: string | undefined | null): string
export interface AbuseUserReport {
id: string

View file

@ -310,12 +310,17 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}
const { readServerConfig, stringToAcct, acctToString, checkWordMute, nyaify, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, initIdGenerator, getTimestamp, genId, secureRndstr, IdConvertType, convertId } = nativeBinding
const { readServerConfig, stringToAcct, acctToString, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, nyaify, AntennaSrcEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, initIdGenerator, getTimestamp, genId, secureRndstr, IdConvertType, convertId } = nativeBinding
module.exports.readServerConfig = readServerConfig
module.exports.stringToAcct = stringToAcct
module.exports.acctToString = acctToString
module.exports.checkWordMute = checkWordMute
module.exports.getFullApAccount = getFullApAccount
module.exports.isSelfHost = isSelfHost
module.exports.isSameOrigin = isSameOrigin
module.exports.extractHost = extractHost
module.exports.toPuny = toPuny
module.exports.nyaify = nyaify
module.exports.AntennaSrcEnum = AntennaSrcEnum
module.exports.MutedNoteReasonEnum = MutedNoteReasonEnum

View file

@ -170,7 +170,12 @@ pub fn read_server_config() -> ServerConfig {
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")
let mut data: ServerConfig = serde_yaml::from_reader(yml).expect("Failed to parse yaml");
data.url = url::Url::parse(&data.url)
.expect("Config url is invalid")
.origin()
.ascii_serialization();
data
}
pub static SERVER_CONFIG: Lazy<ServerConfig> = Lazy::new(read_server_config);

View file

@ -0,0 +1,67 @@
use crate::config::server::SERVER_CONFIG;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Idna error: {0}")]
IdnaError(#[from] idna::Errors),
#[error("Url parse error: {0}")]
UrlParseError(#[from] url::ParseError),
#[error("Hostname is missing")]
NoHostname,
}
#[crate::export]
pub fn get_full_ap_account(username: &str, host: Option<&str>) -> Result<String, Error> {
Ok(match host {
Some(host) => format!("{}@{}", username, to_puny(host)?),
None => format!("{}@{}", username, extract_host(&SERVER_CONFIG.url)?),
})
}
#[crate::export]
pub fn is_self_host(host: Option<&str>) -> Result<bool, Error> {
Ok(match host {
Some(host) => extract_host(&SERVER_CONFIG.url)? == to_puny(host)?,
None => true,
})
}
#[crate::export]
pub fn is_same_origin(uri: &str) -> Result<bool, Error> {
Ok(url::Url::parse(uri)?.origin().ascii_serialization() == SERVER_CONFIG.url)
}
#[crate::export]
pub fn extract_host(uri: &str) -> Result<String, Error> {
url::Url::parse(uri)?
.host_str()
.ok_or(Error::NoHostname)
.and_then(|v| Ok(to_puny(v)?))
}
#[crate::export]
pub fn to_puny(host: &str) -> Result<String, idna::Errors> {
idna::domain_to_ascii(host)
}
#[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://firefish.dev/firefish/firefish.git").unwrap(),
"firefish.dev"
);
}
#[test]
fn to_puny_test() {
assert_eq!(
to_puny("何もかも.owari.shop").unwrap(),
"xn--u8jyfb5762a.owari.shop"
);
}
}

View file

@ -1,3 +1,4 @@
pub mod acct;
pub mod check_word_mute;
pub mod convert_host;
pub mod nyaify;

View file

@ -3,8 +3,7 @@ import type { Note } from "@/models/entities/note.js";
import type { User } from "@/models/entities/user.js";
import type { UserProfile } from "@/models/entities/user-profile.js";
import { Blockings, Followings, UserProfiles } from "@/models/index.js";
import { getFullApAccount } from "@/misc/convert-host.js";
import { checkWordMute, stringToAcct } from "backend-rs";
import { checkWordMute, getFullApAccount, stringToAcct } from "backend-rs";
import type { Packed } from "@/misc/schema.js";
import { Cache } from "@/misc/cache.js";

View file

@ -1,46 +0,0 @@
import { URL } from "node:url";
import config from "@/config/index.js";
import { toASCII } from "punycode";
import Logger from "@/services/logger.js";
import { inspect } from "node:util";
const logger = new Logger("convert-host");
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 isSameOrigin(src: unknown): boolean | null {
if (typeof src !== "string") {
logger.debug(`unknown origin: ${inspect(src)}`);
return null;
}
try {
const u = new URL(src);
return u.origin === config.url;
} catch (e) {
logger.debug(inspect(e));
return false;
}
}
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());
}

View file

@ -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, toPuny } from "backend-rs";
import { decodeReaction } from "./reaction-lib.js";
import config from "@/config/index.js";
import { query } from "@/prelude/url.js";
@ -27,7 +27,7 @@ function normalizeHost(
noteUserHost: string | null,
): string | null {
// クエリに使うホスト
let host =
const host =
src === "."
? null // .はローカルホスト (ここがマッチするのはリアクションのみ)
: src === undefined
@ -36,9 +36,7 @@ function normalizeHost(
? null // 自ホスト指定
: src || noteUserHost; // 指定されたホスト || ノートなどの所有者のホスト (こっちがリアクションにマッチすることはない)
host = toPunyNullable(host);
return host;
return host == null ? null : toPuny(host);
}
function parseEmojiStr(emojiName: string, noteUserHost: string | null) {
@ -46,11 +44,7 @@ function parseEmojiStr(emojiName: string, noteUserHost: string | null) {
if (!match) return { name: null, host: null };
const name = match[1];
// ホスト正規化
const host = toPunyNullable(normalizeHost(match[2], noteUserHost));
return { name, host };
return { name, host: normalizeHost(match[2], noteUserHost) };
}
/**

View file

@ -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 { toPuny } from "backend-rs";
import { IsNull } from "typeorm";
export function convertReactions(reactions: Record<string, number>) {
@ -23,7 +23,7 @@ export async function toDbReaction(
): Promise<string> {
if (!reaction) return (await fetchMeta()).defaultReaction;
reacterHost = toPunyNullable(reacterHost);
reacterHost = reacterHost == null ? null : toPuny(reacterHost);
if (reaction.includes("❤") || reaction.includes("♥️")) return "❤️";

View file

@ -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 "backend-rs";
import { awaitAll } from "@/prelude/await-all.js";
import type { Packed } from "@/misc/schema.js";
import config from "@/config/index.js";

View file

@ -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 "backend-rs";
import { createTemp } from "@/misc/create-temp.js";
import { Users, Blockings } from "@/models/index.js";
import { MoreThan } from "typeorm";

View file

@ -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 "backend-rs";
import { createTemp } from "@/misc/create-temp.js";
import { Users, Followings, Mutings } from "@/models/index.js";
import { In, MoreThan, Not } from "typeorm";

View file

@ -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 "backend-rs";
import { createTemp } from "@/misc/create-temp.js";
import { Users, Mutings } from "@/models/index.js";
import { IsNull, MoreThan } from "typeorm";

View file

@ -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 "backend-rs";
import { createTemp } from "@/misc/create-temp.js";
import { Users, UserLists, UserListJoinings } from "@/models/index.js";
import { In } from "typeorm";

View file

@ -1,10 +1,9 @@
import type Bull from "bull";
import { queueLogger } from "../../logger.js";
import { stringToAcct } from "backend-rs";
import { isSelfHost, stringToAcct, toPuny } from "backend-rs";
import { resolveUser } from "@/remote/resolve-user.js";
import { downloadTextFile } from "@/misc/download-text-file.js";
import { isSelfHost, toPuny } from "@/misc/convert-host.js";
import { Users, DriveFiles } from "@/models/index.js";
import type { DbUserImportJobData } from "@/queue/types.js";
import block from "@/services/blocking/create.js";

View file

@ -1,10 +1,9 @@
import { IsNull } from "typeorm";
import follow from "@/services/following/create.js";
import { stringToAcct } from "backend-rs";
import { isSelfHost, stringToAcct, toPuny } from "backend-rs";
import { resolveUser } from "@/remote/resolve-user.js";
import { downloadTextFile } from "@/misc/download-text-file.js";
import { isSelfHost, toPuny } from "@/misc/convert-host.js";
import { Users, DriveFiles } from "@/models/index.js";
import type { DbUserImportJobData } from "@/queue/types.js";
import { queueLogger } from "../../logger.js";

View file

@ -3,11 +3,10 @@ import type Bull from "bull";
import { queueLogger } from "../../logger.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 { Users, DriveFiles, Mutings } from "@/models/index.js";
import type { DbUserImportJobData } from "@/queue/types.js";
import type { User } from "@/models/entities/user.js";
import { genId, stringToAcct } from "backend-rs";
import { genId, isSelfHost, stringToAcct, toPuny } from "backend-rs";
import { IsNull } from "typeorm";
import { inspect } from "node:util";

View file

@ -4,14 +4,13 @@ import { queueLogger } from "../../logger.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 {
DriveFiles,
Users,
UserLists,
UserListJoinings,
} from "@/models/index.js";
import { genId, stringToAcct } from "backend-rs";
import { genId, isSelfHost, stringToAcct, toPuny } from "backend-rs";
import type { DbUserImportJobData } from "@/queue/types.js";
import { IsNull } from "typeorm";
import { inspect } from "node:util";

View file

@ -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 "backend-rs";
import { StatusError } from "@/misc/fetch.js";
import { shouldSkipInstance } from "@/misc/skipped-instances.js";
import type { DeliverJobData } from "@/queue/types.js";

View file

@ -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 "backend-rs";
import { getApId } from "@/remote/activitypub/type.js";
import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js";
import type { InboxJobData } from "../types.js";
@ -157,7 +157,7 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
}
// ブロックしてたら中断
const ldHost = extractDbHost(authUser.user.uri);
const ldHost = extractHost(authUser.user.uri);
if (await shouldBlockInstance(ldHost, meta)) {
return `Blocked request: ${ldHost}`;
}
@ -168,8 +168,8 @@ export default async (job: Bull.Job<InboxJobData>): Promise<string> => {
// 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}`;
}

View file

@ -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 "backend-rs";
import DbResolver from "@/remote/activitypub/db-resolver.js";
import { getApId } from "@/remote/activitypub/type.js";
import { shouldBlockInstance } from "@/misc/should-block-instance.js";

View file

@ -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 "backend-rs";
import { getApLock } from "@/misc/app-lock.js";
import { parseAudience } from "../../audience.js";
import { StatusError } from "@/misc/fetch.js";
@ -31,7 +31,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);

View file

@ -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 "backend-rs";
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";
}
}

View file

@ -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 "backend-rs";
import { shouldBlockInstance } from "@/misc/should-block-instance.js";
import { inspect } from "node:util";
@ -70,7 +70,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;
}

View file

@ -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 "backend-rs";
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<string> => {
const id = await getApId(activity.object);
if (!isSelfHost(extractDbHost(id))) {
if (!isSelfHost(extractHost(id))) {
return `skip: Read to foreign host (${id})`;
}

View file

@ -13,7 +13,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, isSameOrigin, toPuny } from "@/misc/convert-host.js";
import { extractHost, isSameOrigin, toPuny } from "backend-rs";
import {
Emojis,
Polls,
@ -54,7 +54,7 @@ import { inspect } from "node:util";
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,
)}`,
);
@ -420,11 +420,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);

View file

@ -1,7 +1,6 @@
import { URL } from "node:url";
import promiseLimit from "promise-limit";
import config from "@/config/index.js";
import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js";
import type { Note } from "@/models/entities/note.js";
import { updateUsertags } from "@/services/update-hashtag.js";
@ -19,7 +18,7 @@ import { UserNotePining } from "@/models/entities/user-note-pining.js";
import { genId } from "backend-rs";
import { UserPublickey } from "@/models/entities/user-publickey.js";
import { isDuplicateKeyValueError } from "@/misc/is-duplicate-key-value-error.js";
import { isSameOrigin, toPuny } from "@/misc/convert-host.js";
import { isSameOrigin, toPuny } from "backend-rs";
import { UserProfile } from "@/models/entities/user-profile.js";
import { toArray } from "@/prelude/array.js";
import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js";
@ -164,8 +163,6 @@ export async function createPerson(
uri: string,
resolver?: Resolver,
): Promise<User> {
if (typeof uri !== "string") throw new Error("uri is not string");
if (isSameOrigin(uri)) {
throw new StatusError(
"cannot resolve local user",

View file

@ -4,7 +4,7 @@ import { getApId, isQuestion } from "../type.js";
import { apLogger } from "../logger.js";
import { Notes, Polls } from "@/models/index.js";
import type { IPoll } from "@/models/entities/poll.js";
import { isSameOrigin } from "@/misc/convert-host.js";
import { isSameOrigin } from "backend-rs";
export async function extractPollFromQuestion(
source: string | IObject,

View file

@ -2,7 +2,7 @@ import config from "@/config/index.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 "backend-rs";
import { apGet } from "./request.js";
import type { IObject, ICollection, IOrderedCollection } from "./type.js";
import { isCollectionOrOrderedCollection, getApId } from "./type.js";
@ -68,7 +68,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");
}
@ -95,7 +95,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);
}

View file

@ -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 "backend-rs";
import webFinger from "./webfinger.js";
import { createPerson, updatePerson } from "./activitypub/models/person.js";
import { remoteLogger } from "./logger.js";

View file

@ -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 "backend-rs";
import {
Notes,
Users,

View file

@ -4,8 +4,7 @@ import { User } from "@/models/entities/user.js";
import { Users, UsedUsernames } from "@/models/index.js";
import { UserProfile } from "@/models/entities/user-profile.js";
import { IsNull } from "typeorm";
import { genId } from "backend-rs";
import { toPunyNullable } from "@/misc/convert-host.js";
import { genId, toPuny } from "backend-rs";
import { UserKeypair } from "@/models/entities/user-keypair.js";
import { UsedUsername } from "@/models/entities/used-username.js";
import { db } from "@/db/postgre.js";
@ -100,7 +99,7 @@ export async function signup(opts: {
createdAt: new Date(),
username: username,
usernameLower: username.toLowerCase(),
host: toPunyNullable(host),
host: host == null ? null : toPuny(host),
token: secret,
isAdmin:
(await Users.countBy({

View file

@ -1,6 +1,6 @@
import define from "@/server/api/define.js";
import { Emojis } from "@/models/index.js";
import { toPuny } from "@/misc/convert-host.js";
import { toPuny } from "backend-rs";
import { makePaginationQuery } from "@/server/api/common/make-pagination-query.js";
import { sqlLikeEscape } from "@/misc/sql-like-escape.js";
import { ApiError } from "@/server/api/error.js";

View file

@ -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 "backend-rs";
import { fetchInstanceMetadata } from "@/services/fetch-instance-metadata.js";
export const meta = {

View file

@ -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 "backend-rs";
export const meta = {
tags: ["admin"],

View file

@ -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 "backend-rs";
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<SchemaType<(typeof meta)["res"]> | null> {
// Wait if blocked.
if (await shouldBlockInstance(extractDbHost(uri))) return null;
if (await shouldBlockInstance(extractHost(uri))) return null;
const dbResolver = new DbResolver();

View file

@ -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 "backend-rs";
export const meta = {
tags: ["federation"],

View file

@ -1,6 +1,6 @@
import { IsNull } from "typeorm";
import { Users, Followings, UserProfiles } from "@/models/index.js";
import { toPunyNullable } from "@/misc/convert-host.js";
import { toPuny } from "backend-rs";
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: ps.host == null ? IsNull() : toPuny(ps.host),
},
);

View file

@ -1,6 +1,6 @@
import { IsNull } from "typeorm";
import { Users, Followings, UserProfiles } from "@/models/index.js";
import { toPunyNullable } from "@/misc/convert-host.js";
import { toPuny } from "backend-rs";
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: ps.host == null ? IsNull() : toPuny(ps.host),
},
);

View file

@ -7,7 +7,7 @@ import {
Mutings,
Users,
} from "@/models/index.js";
import { genId } from "backend-rs";
import { genId, toPuny } from "backend-rs";
import type { MessagingMessage } from "@/models/entities/messaging-message.js";
import {
publishMessagingStream,
@ -22,7 +22,6 @@ 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 { Instances } from "@/models/index.js";
export async function createMessage(

View file

@ -4,7 +4,7 @@ import {
} from "@/models/entities/instance.js";
import { Instances } from "@/models/index.js";
import { genId } from "backend-rs";
import { toPuny } from "@/misc/convert-host.js";
import { toPuny } from "backend-rs";
import { Cache } from "@/misc/cache.js";
import Logger from "@/services/logger.js";