From 9959f5bd04fc5cd5d3b8f66bf0e790ca6064f8be Mon Sep 17 00:00:00 2001 From: okayurisotto Date: Thu, 6 Jul 2023 08:47:47 +0900 Subject: [PATCH] =?UTF-8?q?refactor(`ApDbResolverService.ts`):=20URL?= =?UTF-8?q?=E3=82=92=E6=89=B1=E3=81=86=E8=A4=87=E9=9B=91=E3=81=AA=E6=AD=A3?= =?UTF-8?q?=E8=A6=8F=E8=A1=A8=E7=8F=BE=E3=82=92URL=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=83=BC=E3=83=95=E3=82=A7=E3=82=A4=E3=82=B9=E3=81=A7?= =?UTF-8?q?=E7=BD=AE=E3=81=8D=E6=8F=9B=E3=81=88=20(#11123)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え * fixup! refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え --- .../core/activitypub/ApDbResolverService.ts | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/backend/src/core/activitypub/ApDbResolverService.ts b/packages/backend/src/core/activitypub/ApDbResolverService.ts index 2d9e7a14e..ca148916d 100644 --- a/packages/backend/src/core/activitypub/ApDbResolverService.ts +++ b/packages/backend/src/core/activitypub/ApDbResolverService.ts @@ -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); - - // 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); - - if (matchLocal) { - return { - local: true, - type: matchLocal[1], - id: matchLocal[2], - rest: matchLocal[3], - }; - } else { - return { - local: false, - uri, - }; - } + const separator = '/'; + + const uri = new URL(getApId(value)); + if (uri.origin !== this.config.url) return { local: false, uri: uri.href }; + + const [, type, id, ...rest] = uri.pathname.split(separator); + return { + local: true, + type, + id, + rest: rest.length === 0 ? undefined : rest.join(separator), + }; } /**