fix: early return on invalid permalinks

resolves #255
pull/257/head
Daniel Roe 2022-11-30 13:31:33 +00:00
rodzic 71fbe6acf9
commit 24bbe9135b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 22D5008E4F5D9B55
1 zmienionych plików z 28 dodań i 21 usunięć

Wyświetl plik

@ -3,32 +3,39 @@ import { parseURL } from 'ufo'
definePageMeta({
middleware: async (to) => {
let permalink = Array.isArray(to.params.permalink)
? to.params.permalink.join('/')
: to.params.permalink
const HANDLED_MASTO_URL = /^(https?:\/\/)?(\w+\.)+\w+\/(@[@\w\d\.]+)(\/\d+)?$/
try {
let permalink = Array.isArray(to.params.permalink)
? to.params.permalink.join('/')
: to.params.permalink
if (!permalink.startsWith('http'))
permalink = `https://${permalink}`
if (!HANDLED_MASTO_URL.test(permalink))
return '/home'
if (!currentUser.value) {
const { host, pathname } = parseURL(permalink)
await loginTo({ server: host! })
return pathname
}
if (!permalink.startsWith('http'))
permalink = `https://${permalink}`
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 (!currentUser.value) {
const { host, pathname } = parseURL(permalink)
await loginTo({ server: host! })
return pathname
}
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])
return getAccountPath(accounts[0])
catch {}
return '/home'
},