fix: early return on invalid permalinks

resolves #255
This commit is contained in:
Daniel Roe 2022-11-30 13:31:33 +00:00
parent 71fbe6acf9
commit 24bbe9135b
No known key found for this signature in database
GPG key ID: 22D5008E4F5D9B55

View file

@ -3,32 +3,39 @@ import { parseURL } from 'ufo'
definePageMeta({ definePageMeta({
middleware: async (to) => { middleware: async (to) => {
let permalink = Array.isArray(to.params.permalink) const HANDLED_MASTO_URL = /^(https?:\/\/)?(\w+\.)+\w+\/(@[@\w\d\.]+)(\/\d+)?$/
? to.params.permalink.join('/') try {
: to.params.permalink let permalink = Array.isArray(to.params.permalink)
? to.params.permalink.join('/')
: to.params.permalink
if (!permalink.startsWith('http')) if (!HANDLED_MASTO_URL.test(permalink))
permalink = `https://${permalink}` return '/home'
if (!currentUser.value) { if (!permalink.startsWith('http'))
const { host, pathname } = parseURL(permalink) permalink = `https://${permalink}`
await loginTo({ server: host! })
return pathname
}
const { value } = await useMasto().search({ q: permalink, resolve: true, limit: 1 }).next() if (!currentUser.value) {
const { host, pathname } = parseURL(permalink)
const { accounts, statuses } = value await loginTo({ server: host! })
if (statuses[0]) { return pathname
return {
path: getStatusPath(statuses[0]),
state: {
status: statuses[0] as any,
},
} }
const { value } = await useMasto().search({ q: permalink, resolve: true, limit: 1 }).next()
const { accounts, statuses } = value
if (statuses[0]) {
return {
path: getStatusPath(statuses[0]),
state: {
status: statuses[0] as any,
},
}
}
if (accounts[0])
return getAccountPath(accounts[0])
} }
if (accounts[0]) catch {}
return getAccountPath(accounts[0])
return '/home' return '/home'
}, },