From 9af2121efed275b8d12adeebba512fba53170a8b Mon Sep 17 00:00:00 2001 From: codytseng Date: Thu, 6 Feb 2025 15:28:06 +0800 Subject: [PATCH] fix: normalize user's website urls --- src/lib/event.ts | 6 +++--- src/lib/url.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/event.ts b/src/lib/event.ts index cdfa925..e0247c4 100644 --- a/src/lib/event.ts +++ b/src/lib/event.ts @@ -2,9 +2,9 @@ import { BIG_RELAY_URLS, COMMENT_EVENT_KIND, PICTURE_EVENT_KIND } from '@/consta import client from '@/services/client.service' import { TImageInfo, TRelayList } from '@/types' import { Event, kinds, nip19 } from 'nostr-tools' -import { extractImageInfoFromTag, isReplyETag, isRootETag, tagNameEquals } from './tag' -import { isWebsocketUrl, normalizeUrl } from './url' import { formatPubkey } from './pubkey' +import { extractImageInfoFromTag, isReplyETag, isRootETag, tagNameEquals } from './tag' +import { isWebsocketUrl, normalizeHttpUrl, normalizeUrl } from './url' export function isNsfwEvent(event: Event) { return event.tags.some( @@ -123,7 +123,7 @@ export function getProfileFromProfileEvent(event: Event) { original_username: username, nip05: profileObj.nip05, about: profileObj.about, - website: profileObj.website, + website: profileObj.website ? normalizeHttpUrl(profileObj.website) : undefined, created_at: event.created_at } } catch (err) { diff --git a/src/lib/url.ts b/src/lib/url.ts index 399339b..cb8e269 100644 --- a/src/lib/url.ts +++ b/src/lib/url.ts @@ -15,6 +15,18 @@ export function normalizeUrl(url: string): string { return p.toString() } +export function normalizeHttpUrl(url: string): string { + if (url.indexOf('://') === -1) url = 'https://' + url + const p = new URL(url) + p.pathname = p.pathname.replace(/\/+/g, '/') + if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1) + if ((p.port === '80' && p.protocol === 'http:') || (p.port === '443' && p.protocol === 'https:')) + p.port = '' + p.searchParams.sort() + p.hash = '' + return p.toString() +} + export function simplifyUrl(url: string): string { return url.replace('wss://', '').replace('ws://', '').replace(/\/$/, '') }