feat: allow running elk with a single server (#1606)
This commit is contained in:
parent
61428cd9cd
commit
53d0812efd
22 changed files with 232 additions and 79 deletions
|
@ -59,7 +59,8 @@ export function fetchAccountById(id?: string | null): Promise<mastodon.v1.Accoun
|
|||
export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Account> {
|
||||
const server = currentServer.value
|
||||
const userId = currentUser.value?.account.id
|
||||
const key = `${server}:${userId}:account:${acct}`
|
||||
const userAcct = acct.endsWith(`@${server}`) ? acct.slice(0, -server.length - 1) : acct
|
||||
const key = `${server}:${userId}:account:${userAcct}`
|
||||
const cached = cache.get(key)
|
||||
if (cached)
|
||||
return cached
|
||||
|
@ -69,9 +70,9 @@ export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Ac
|
|||
const client = useMastoClient()
|
||||
let account: mastodon.v1.Account
|
||||
if (!isGotoSocial.value)
|
||||
account = await client.v1.accounts.lookup({ acct })
|
||||
account = await client.v1.accounts.lookup({ acct: userAcct })
|
||||
else
|
||||
account = (await client.v1.search({ q: `@${acct}`, type: 'accounts' })).accounts[0]
|
||||
account = (await client.v1.search({ q: `@${userAcct}`, type: 'accounts' })).accounts[0]
|
||||
|
||||
if (account.acct && !account.acct.includes('@') && domain)
|
||||
account.acct = `${account.acct}@${domain}`
|
||||
|
@ -107,6 +108,7 @@ export function removeCachedStatus(id: string, server = currentServer.value) {
|
|||
|
||||
export function cacheAccount(account: mastodon.v1.Account, server = currentServer.value, override?: boolean) {
|
||||
const userId = currentUser.value?.account.id
|
||||
const userAcct = account.acct.endsWith(`@${server}`) ? account.acct.slice(0, -server.length - 1) : account.acct
|
||||
setCached(`${server}:${userId}:account:${account.id}`, account, override)
|
||||
setCached(`${server}:${userId}:account:${account.acct}`, account, override)
|
||||
setCached(`${server}:${userId}:account:${userAcct}`, account, override)
|
||||
}
|
||||
|
|
|
@ -245,6 +245,7 @@ export const provideGlobalCommands = () => {
|
|||
const masto = useMasto()
|
||||
const colorMode = useColorMode()
|
||||
const userSettings = useUserSettings()
|
||||
const { singleInstanceServer, oauth } = useSignIn()
|
||||
|
||||
useCommand({
|
||||
scope: 'Navigation',
|
||||
|
@ -310,7 +311,10 @@ export const provideGlobalCommands = () => {
|
|||
icon: 'i-ri:user-add-line',
|
||||
|
||||
onActivate() {
|
||||
openSigninDialog()
|
||||
if (singleInstanceServer)
|
||||
oauth()
|
||||
else
|
||||
openSigninDialog()
|
||||
},
|
||||
})
|
||||
useCommand({
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import type { mastodon } from 'masto'
|
||||
import type { ConfirmDialogChoice, ConfirmDialogLabel, Draft } from '~/types'
|
||||
import type { ConfirmDialogChoice, ConfirmDialogLabel, Draft, ErrorDialogData } from '~/types'
|
||||
import { STORAGE_KEY_FIRST_VISIT } from '~/constants'
|
||||
|
||||
export const confirmDialogChoice = ref<ConfirmDialogChoice>()
|
||||
export const confirmDialogLabel = ref<ConfirmDialogLabel>()
|
||||
export const errorDialogData = ref<ErrorDialogData>()
|
||||
|
||||
export const mediaPreviewList = ref<mastodon.v1.MediaAttachment[]>([])
|
||||
export const mediaPreviewIndex = ref(0)
|
||||
|
@ -22,6 +23,7 @@ export const isEditHistoryDialogOpen = ref(false)
|
|||
export const isPreviewHelpOpen = ref(isFirstVisit.value)
|
||||
export const isCommandPanelOpen = ref(false)
|
||||
export const isConfirmDialogOpen = ref(false)
|
||||
export const isErrorDialogOpen = ref(false)
|
||||
export const isFavouritedBoostedByDialogOpen = ref(false)
|
||||
|
||||
export const lastPublishDialogStatus = ref<mastodon.v1.Status | null>(null)
|
||||
|
@ -101,6 +103,17 @@ export function openMediaPreview(attachments: mastodon.v1.MediaAttachment[], ind
|
|||
}, '')
|
||||
}
|
||||
|
||||
export async function openErrorDialog(data: ErrorDialogData) {
|
||||
errorDialogData.value = data
|
||||
isErrorDialogOpen.value = true
|
||||
|
||||
await until(isErrorDialogOpen).toBe(false)
|
||||
}
|
||||
|
||||
export function closeErrorDialog() {
|
||||
isErrorDialogOpen.value = false
|
||||
}
|
||||
|
||||
export function closeMediaPreview() {
|
||||
history.back()
|
||||
}
|
||||
|
|
77
composables/sign-in.ts
Normal file
77
composables/sign-in.ts
Normal file
|
@ -0,0 +1,77 @@
|
|||
import type { Ref } from 'vue'
|
||||
|
||||
export const useSignIn = (input?: Ref<HTMLInputElement | undefined>) => {
|
||||
const singleInstanceServer = useAppConfig().singleInstanceServer
|
||||
const userSettings = useUserSettings()
|
||||
const users = useUsers()
|
||||
const { t } = useI18n()
|
||||
|
||||
const busy = ref(false)
|
||||
const error = ref(false)
|
||||
const server = ref('')
|
||||
const displayError = ref(false)
|
||||
|
||||
async function oauth() {
|
||||
if (busy.value)
|
||||
return
|
||||
|
||||
busy.value = true
|
||||
error.value = false
|
||||
displayError.value = false
|
||||
|
||||
await nextTick()
|
||||
|
||||
if (!singleInstanceServer && server.value)
|
||||
server.value = server.value.split('/')[0]
|
||||
|
||||
try {
|
||||
let href: string
|
||||
if (singleInstanceServer) {
|
||||
href = await (globalThis.$fetch as any)(`/api/${publicServer.value}/login`, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
force_login: users.value.length > 0,
|
||||
origin: location.origin,
|
||||
lang: userSettings.value.language,
|
||||
},
|
||||
})
|
||||
busy.value = false
|
||||
}
|
||||
else {
|
||||
href = await (globalThis.$fetch as any)(`/api/${server.value || publicServer.value}/login`, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
force_login: users.value.some(u => u.server === server.value),
|
||||
origin: location.origin,
|
||||
lang: userSettings.value.language,
|
||||
},
|
||||
})
|
||||
}
|
||||
location.href = href
|
||||
}
|
||||
catch (err) {
|
||||
if (singleInstanceServer) {
|
||||
console.error(err)
|
||||
busy.value = false
|
||||
await openErrorDialog({
|
||||
title: t('common.error'),
|
||||
messages: [t('error.sign_in_error')],
|
||||
close: t('action.close'),
|
||||
})
|
||||
}
|
||||
else {
|
||||
displayError.value = true
|
||||
error.value = true
|
||||
await nextTick()
|
||||
input?.value?.focus()
|
||||
await nextTick()
|
||||
setTimeout(() => {
|
||||
busy.value = false
|
||||
error.value = false
|
||||
}, 512)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { busy, displayError, error, server, singleInstanceServer, oauth }
|
||||
}
|
|
@ -263,7 +263,7 @@ export async function signOut() {
|
|||
if (!currentUserHandle.value)
|
||||
await useRouter().push('/')
|
||||
|
||||
loginTo(masto, currentUser.value)
|
||||
loginTo(masto, currentUser.value || { server: publicServer.value })
|
||||
}
|
||||
|
||||
export function checkLogin() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue