mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor: move url check to a separate function
This commit is contained in:
parent
3c699fd23f
commit
9df50a167a
2 changed files with 30 additions and 18 deletions
28
src/utils/accounts.ts
Normal file
28
src/utils/accounts.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
function sanitizeWebsite(website: string | undefined): string | undefined {
|
||||
if (!website) return undefined;
|
||||
|
||||
if (
|
||||
'https://'.includes(website) ||
|
||||
'http://'.includes(website)
|
||||
) return undefined;
|
||||
|
||||
try {
|
||||
// See if 'website' is a valid url
|
||||
new URL(website);
|
||||
return website;
|
||||
} catch (_) {
|
||||
try {
|
||||
const websiteWithPrefix = 'https://' + website;
|
||||
// 'website' could still be a valid url
|
||||
// try adding a 'https' prefix to 'website'
|
||||
new URL(websiteWithPrefix);
|
||||
|
||||
return websiteWithPrefix;
|
||||
} catch (_) {
|
||||
// 'website' is not a valid url even with 'https' prefix
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { sanitizeWebsite };
|
||||
|
|
@ -8,6 +8,7 @@ import { getLnurl } from '@/utils/lnurl.ts';
|
|||
import { nip05Cache } from '@/utils/nip05.ts';
|
||||
import { Nip05, nostrDate, nostrNow, parseNip05 } from '@/utils.ts';
|
||||
import { renderEmojis } from '@/views/mastodon/emojis.ts';
|
||||
import { sanitizeWebsite } from '@/utils/accounts.ts';
|
||||
|
||||
interface ToAccountOpts {
|
||||
withSource?: boolean;
|
||||
|
|
@ -31,23 +32,6 @@ async function renderAccount(
|
|||
website,
|
||||
} = n.json().pipe(n.metadata()).catch({}).parse(event.content);
|
||||
|
||||
let websiteUrl;
|
||||
try {
|
||||
// See if 'website' is a valid url
|
||||
new URL(website as string);
|
||||
websiteUrl = website;
|
||||
} catch (_) {
|
||||
try {
|
||||
// 'website' could still be a valid url
|
||||
// try adding a 'https' prefix to 'website'
|
||||
new URL('https' + website);
|
||||
websiteUrl = 'https' + website;
|
||||
} catch (_) {
|
||||
// 'website' is not a valid url even with 'https' prefix
|
||||
websiteUrl = '';
|
||||
}
|
||||
}
|
||||
|
||||
const npub = nip19.npubEncode(pubkey);
|
||||
const parsed05 = await parseAndVerifyNip05(nip05, pubkey);
|
||||
const role = event.user?.tags.find(([name]) => name === 'role')?.[1] ?? 'user';
|
||||
|
|
@ -103,7 +87,7 @@ async function renderAccount(
|
|||
pubkey,
|
||||
lud16,
|
||||
},
|
||||
website: websiteUrl,
|
||||
website: sanitizeWebsite(website) ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue