Add Conf.url() function for producing local absolute URLs

This commit is contained in:
Alex Gleason 2023-07-09 14:23:02 -05:00
parent a3323a2618
commit 215ff85b6d
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 14 additions and 10 deletions

View File

@ -21,6 +21,17 @@ const Conf = {
get publishRelays() { get publishRelays() {
return ['wss://relay.mostr.pub']; return ['wss://relay.mostr.pub'];
}, },
/** Merges the path with the localDomain. */
url(path: string): string {
if (path.startsWith('/')) {
// Path is a path.
return new URL(path, Conf.localDomain).toString();
} else {
// Path is possibly a full URL. Replace the domain.
const { pathname } = new URL(path);
return new URL(pathname, Conf.localDomain).toString();
}
},
}; };
export { Conf }; export { Conf };

View File

@ -28,7 +28,7 @@ function auth98(opts: Auth98Opts = {}): AppMiddleware {
.refine((event) => { .refine((event) => {
const url = findTag(event.tags, 'u')?.[1]; const url = findTag(event.tags, 'u')?.[1];
try { try {
return url === localUrl(c.req.url); return url === Conf.url(c.req.url);
} catch (_e) { } catch (_e) {
return false; return false;
} }
@ -51,11 +51,6 @@ function auth98(opts: Auth98Opts = {}): AppMiddleware {
}; };
} }
function localUrl(url: string): string {
const { pathname } = new URL(url);
return new URL(pathname, Conf.localDomain).toString();
}
const requireProof: AppMiddleware = async (c, next) => { const requireProof: AppMiddleware = async (c, next) => {
const pubkey = c.get('pubkey'); const pubkey = c.get('pubkey');
const proof = c.get('proof'); const proof = c.get('proof');

View File

@ -4,13 +4,11 @@ import { linkify, linkifyStr, mime, nip19, nip21 } from '@/deps.ts';
linkify.registerCustomProtocol('nostr', true); linkify.registerCustomProtocol('nostr', true);
linkify.registerCustomProtocol('wss'); linkify.registerCustomProtocol('wss');
const url = (path: string) => new URL(path, Conf.localDomain).toString();
const linkifyOpts: linkify.Opts = { const linkifyOpts: linkify.Opts = {
render: { render: {
hashtag: ({ content }) => { hashtag: ({ content }) => {
const tag = content.replace(/^#/, ''); const tag = content.replace(/^#/, '');
const href = url(`/tags/${tag}`); const href = Conf.url(`/tags/${tag}`);
return `<a class=\"mention hashtag\" href=\"${href}\" rel=\"tag\"><span>#</span>${tag}</a>`; return `<a class=\"mention hashtag\" href=\"${href}\" rel=\"tag\"><span>#</span>${tag}</a>`;
}, },
url: ({ content }) => { url: ({ content }) => {
@ -19,7 +17,7 @@ const linkifyOpts: linkify.Opts = {
const pubkey = getDecodedPubkey(decoded); const pubkey = getDecodedPubkey(decoded);
if (pubkey) { if (pubkey) {
const name = pubkey.substring(0, 8); const name = pubkey.substring(0, 8);
const href = url(`/users/${pubkey}`); const href = Conf.url(`/users/${pubkey}`);
return `<span class="h-card"><a class="u-url mention" href="${href}" rel="ugc">@<span>${name}</span></a></span>`; return `<span class="h-card"><a class="u-url mention" href="${href}" rel="ugc">@<span>${name}</span></a></span>`;
} else { } else {
return ''; return '';