refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え (#11123)
* refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え * fixup! refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え
This commit is contained in:
parent
be143f91b2
commit
9959f5bd04
|
@ -1,5 +1,4 @@
|
||||||
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
||||||
import escapeRegexp from 'escape-regexp';
|
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js';
|
import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
|
@ -56,25 +55,18 @@ export class ApDbResolverService implements OnApplicationShutdown {
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public parseUri(value: string | IObject): UriParseResult {
|
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 uri = new URL(getApId(value));
|
||||||
const localRegex = new RegExp('^' + escapeRegexp(this.config.url) + '/(\\w+)/(\\w+)(?:\/(.+))?', 'i');
|
if (uri.origin !== this.config.url) return { local: false, uri: uri.href };
|
||||||
const matchLocal = uri.match(localRegex);
|
|
||||||
|
|
||||||
if (matchLocal) {
|
const [, type, id, ...rest] = uri.pathname.split(separator);
|
||||||
return {
|
return {
|
||||||
local: true,
|
local: true,
|
||||||
type: matchLocal[1],
|
type,
|
||||||
id: matchLocal[2],
|
id,
|
||||||
rest: matchLocal[3],
|
rest: rest.length === 0 ? undefined : rest.join(separator),
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
local: false,
|
|
||||||
uri,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue