1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-02 23:19:57 +00:00
elk/pages/[...permalink].vue

48 lines
1.1 KiB
Vue
Raw Normal View History

2022-11-29 20:51:52 +00:00
<script setup lang="ts">
import { parseURL } from 'ufo'
import { HANDLED_MASTO_URLS } from '~/constants'
2022-11-29 20:51:52 +00:00
definePageMeta({
name: 'permalink',
2022-11-29 20:51:52 +00:00
middleware: async (to) => {
try {
let permalink = Array.isArray(to.params.permalink)
? to.params.permalink.join('/')
: to.params.permalink
2022-11-29 20:51:52 +00:00
if (!HANDLED_MASTO_URLS.test(permalink))
return '/home'
2022-11-29 20:51:52 +00:00
if (!permalink.startsWith('http'))
permalink = `https://${permalink}`
if (!currentUser.value) {
const { host, pathname } = parseURL(permalink)
await loginTo({ server: host! })
if (pathname.match(/^\/@[^/]+$/))
return `${pathname}@${host}`
return pathname
}
2022-11-29 20:51:52 +00:00
const { value } = await useMasto().search({ q: permalink, resolve: true, limit: 1 }).next()
2022-11-29 20:51:52 +00:00
const { accounts, statuses } = value
if (statuses[0])
return getStatusRoute(statuses[0])
if (accounts[0])
return getAccountRoute(accounts[0])
2022-11-29 20:51:52 +00:00
}
catch {}
2022-11-29 20:51:52 +00:00
return '/home'
},
})
</script>
<template>
<div />
</template>