refactor: migrate to nuxt compatibilityVersion: 4 (#3298)
This commit is contained in:
parent
46e4433e1c
commit
a3fbc056a9
342 changed files with 1200 additions and 2932 deletions
60
app/middleware/1.permalink.global.ts
Normal file
60
app/middleware/1.permalink.global.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||
if (import.meta.server)
|
||||
return
|
||||
|
||||
if (!('server' in to.params))
|
||||
return
|
||||
|
||||
const server = to.params.server as string || currentServer.value
|
||||
const user = currentUser.value
|
||||
const masto = useMasto()
|
||||
if (!user) {
|
||||
const fromServer = from.params.server || currentServer.value
|
||||
if (fromServer !== server)
|
||||
loginTo(masto, { server })
|
||||
return
|
||||
}
|
||||
|
||||
// No need to additionally resolve an id if we're already logged in
|
||||
if (user.server === server)
|
||||
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
|
||||
if (!useRuntimeConfig().public.singleInstance && !to.params.server) {
|
||||
return {
|
||||
...to,
|
||||
params: {
|
||||
...to.params,
|
||||
server: user.server,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
// If we're already on an account page, we can search for this on the new instance
|
||||
if (to.params.account && to.name !== 'status' && to.params.account.includes('@')) {
|
||||
const account = await fetchAccountByHandle(to.params.account as string)
|
||||
if (account)
|
||||
return getAccountRoute(account)
|
||||
}
|
||||
|
||||
// If we're logged in, search for the local id the account or status corresponds to
|
||||
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: [] }
|
||||
|
||||
if (statuses[0])
|
||||
return getStatusRoute(statuses[0])
|
||||
|
||||
if (accounts[0])
|
||||
return getAccountRoute(accounts[0])
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
return '/home'
|
||||
})
|
10
app/middleware/2.single-instance.global.ts
Normal file
10
app/middleware/2.single-instance.global.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
export default defineNuxtRouteMiddleware(async (to) => {
|
||||
if (import.meta.server || !useRuntimeConfig().public.singleInstance)
|
||||
return
|
||||
|
||||
if (to.params.server) {
|
||||
const newTo = { ...to }
|
||||
delete newTo.params.server
|
||||
return newTo
|
||||
}
|
||||
})
|
40
app/middleware/auth.ts
Normal file
40
app/middleware/auth.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import type { RouteLocationNormalized } from 'vue-router'
|
||||
|
||||
export default defineNuxtRouteMiddleware((to) => {
|
||||
if (import.meta.server)
|
||||
return
|
||||
|
||||
if (to.path === '/signin/callback')
|
||||
return
|
||||
|
||||
if (isHydrated.value)
|
||||
return handleAuth(to)
|
||||
|
||||
onHydrated(() => handleAuth(to))
|
||||
})
|
||||
|
||||
function handleAuth(to: RouteLocationNormalized) {
|
||||
if (to.path === '/') {
|
||||
// Installed PWA shortcut to notifications
|
||||
if (to.query['notifications-pwa-shortcut'] !== undefined) {
|
||||
if (currentUser.value)
|
||||
return navigateTo('/notifications')
|
||||
else
|
||||
return navigateTo(`/${currentServer.value}/public/local`)
|
||||
}
|
||||
|
||||
// Installed PWA shortcut to local
|
||||
if (to.query['local-pwa-shortcut'] !== undefined)
|
||||
return navigateTo(`/${currentServer.value}/public/local`)
|
||||
}
|
||||
|
||||
if (!currentUser.value) {
|
||||
if (to.path === '/home' && to.query['share-target'] !== undefined)
|
||||
return navigateTo('/share-target')
|
||||
else
|
||||
return navigateTo(`/${currentServer.value}/public/local`)
|
||||
}
|
||||
|
||||
if (to.path === '/')
|
||||
return navigateTo('/home')
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue