refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え (#11123)

* refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え

* fixup! refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え
This commit is contained in:
okayurisotto 2023-07-06 08:47:47 +09:00 committed by GitHub
parent be143f91b2
commit 9959f5bd04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 20 deletions

View File

@ -1,5 +1,4 @@
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import escapeRegexp from 'escape-regexp';
import { DI } from '@/di-symbols.js';
import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js';
import type { Config } from '@/config.js';
@ -56,25 +55,18 @@ export class ApDbResolverService implements OnApplicationShutdown {
@bindThis
public parseUri(value: string | IObject): UriParseResult {
const uri = getApId(value);
const separator = '/';
// the host part of a URL is case insensitive, so use the 'i' flag.
const localRegex = new RegExp('^' + escapeRegexp(this.config.url) + '/(\\w+)/(\\w+)(?:\/(.+))?', 'i');
const matchLocal = uri.match(localRegex);
const uri = new URL(getApId(value));
if (uri.origin !== this.config.url) return { local: false, uri: uri.href };
if (matchLocal) {
const [, type, id, ...rest] = uri.pathname.split(separator);
return {
local: true,
type: matchLocal[1],
id: matchLocal[2],
rest: matchLocal[3],
type,
id,
rest: rest.length === 0 ? undefined : rest.join(separator),
};
} else {
return {
local: false,
uri,
};
}
}
/**