2022-12-04 22:56:33 +03:00
|
|
|
export default defineNuxtRouteMiddleware(async (to, from) => {
|
2024-02-24 19:46:14 +03:00
|
|
|
if (import.meta.server)
|
2022-12-20 11:20:40 +03:00
|
|
|
return
|
|
|
|
|
2022-12-04 22:56:33 +03:00
|
|
|
if (!('server' in to.params))
|
|
|
|
return
|
|
|
|
|
2023-02-05 15:10:19 +03:00
|
|
|
const server = to.params.server as string || currentServer.value
|
2022-12-12 02:30:26 +03:00
|
|
|
const user = currentUser.value
|
2023-01-15 11:38:02 +03:00
|
|
|
const masto = useMasto()
|
2022-12-12 02:30:26 +03:00
|
|
|
if (!user) {
|
2023-02-05 15:10:19 +03:00
|
|
|
const fromServer = from.params.server || currentServer.value
|
|
|
|
if (fromServer !== server)
|
|
|
|
loginTo(masto, { server })
|
2022-12-04 22:56:33 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to additionally resolve an id if we're already logged in
|
2023-02-05 15:10:19 +03:00
|
|
|
if (user.server === server)
|
2022-12-04 22:56:33 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
// Tags don't need to be redirected to a local id
|
|
|
|
if (to.params.tag)
|
|
|
|
return
|
|
|
|
|
|
|
|
// Handle redirecting to new permalink structure for users with old links
|
2023-02-07 15:10:43 +03:00
|
|
|
if (!useRuntimeConfig().public.singleInstance && !to.params.server) {
|
2022-12-04 22:56:33 +03:00
|
|
|
return {
|
|
|
|
...to,
|
|
|
|
params: {
|
|
|
|
...to.params,
|
2022-12-12 02:30:26 +03:00
|
|
|
server: user.server,
|
2022-12-04 22:56:33 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-12 02:30:26 +03:00
|
|
|
// If we're already on an account page, we can search for this on the new instance
|
2022-12-14 23:57:15 +03:00
|
|
|
if (to.params.account && to.name !== 'status' && to.params.account.includes('@')) {
|
2022-12-12 02:30:26 +03:00
|
|
|
const account = await fetchAccountByHandle(to.params.account as string)
|
|
|
|
if (account)
|
|
|
|
return getAccountRoute(account)
|
|
|
|
}
|
|
|
|
|
2022-12-04 22:56:33 +03:00
|
|
|
// If we're logged in, search for the local id the account or status corresponds to
|
2024-01-09 11:56:15 +03:00
|
|
|
const paginator = masto.client.value.v2.search.list({ q: `https:/${to.fullPath}`, resolve: true, limit: 1 })
|
|
|
|
const { accounts, statuses } = (await paginator.next()).value ?? { accounts: [], statuses: [] }
|
|
|
|
|
2022-12-04 22:56:33 +03:00
|
|
|
if (statuses[0])
|
|
|
|
return getStatusRoute(statuses[0])
|
|
|
|
|
|
|
|
if (accounts[0])
|
|
|
|
return getAccountRoute(accounts[0])
|
|
|
|
}
|
2023-01-12 08:39:22 +03:00
|
|
|
catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2022-12-04 22:56:33 +03:00
|
|
|
|
|
|
|
return '/home'
|
|
|
|
})
|