Skip to content

Commit

Permalink
fix: user acct not respecting domain (elk-zone#2088)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored May 10, 2023
1 parent f28c904 commit 29f6a73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
6 changes: 3 additions & 3 deletions composables/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function fetchAccountById(id?: string | null): Promise<mastodon.v1.Accoun
const cached = cache.get(key)
if (cached)
return cached
const domain = currentInstance.value ? getInstanceDomain(currentInstance.value) : null
const domain = getInstanceDomainFromServer(server)
const promise = useMastoClient().v1.accounts.fetch(id)
.then((r) => {
if (r.acct && !r.acct.includes('@') && domain)
Expand All @@ -59,12 +59,12 @@ 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 userAcct = acct.endsWith(`@${server}`) ? acct.slice(0, -server.length - 1) : acct
const domain = getInstanceDomainFromServer(server)
const userAcct = (domain && acct.endsWith(`@${domain}`)) ? acct.slice(0, -domain.length - 1) : acct
const key = `${server}:${userId}:account:${userAcct}`
const cached = cache.get(key)
if (cached)
return cached
const domain = currentInstance.value ? getInstanceDomain(currentInstance.value) : undefined

async function lookupAccount() {
const client = useMastoClient()
Expand Down
30 changes: 28 additions & 2 deletions composables/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ export async function fetchAccountInfo(client: mastodon.Client, server: string)
fetchPrefs(),
])

if (!account.acct.includes('@'))
account.acct = `${account.acct}@${server}`
if (!account.acct.includes('@')) {
const webDomain = getInstanceDomainFromServer(server)
account.acct = `${account.acct}@${webDomain}`
}

// TODO: lazy load preferences
accountPreferencesMap.set(account.acct, preferences)
Expand All @@ -219,6 +221,12 @@ export async function fetchAccountInfo(client: mastodon.Client, server: string)
return account
}

export function getInstanceDomainFromServer(server: string) {
const instance = getInstanceCache(server)
const webDomain = instance ? getInstanceDomain(instance) : server
return webDomain
}

export async function refreshAccountInfo() {
const account = await fetchAccountInfo(useMastoClient(), currentServer.value)
currentUser.value!.account = account
Expand Down Expand Up @@ -340,10 +348,28 @@ export function useUserLocalStorage<T extends object>(key: string, initial: () =
const scope = effectScope(true)
const value = scope.run(() => {
const all = useLocalStorage<Record<string, T>>(key, {}, { deep: true })

return computed(() => {
const id = currentUser.value?.account.id
? currentUser.value.account.acct
: '[anonymous]'

// Backward compatibility, respect webDomain in acct
// In previous versions, acct was username@server instead of username@webDomain
// for example: elk@m.webtoo.ls instead of [email protected]
if (!all.value[id]) {
const [username, webDomain] = id.split('@')
const server = currentServer.value
if (webDomain && server && server !== webDomain) {
const oldId = `${username}@${server}`
const outdatedSettings = all.value[oldId]
if (outdatedSettings) {
const newAllValue = { ...all.value, [id]: outdatedSettings }
delete newAllValue[oldId]
all.value = newAllValue
}
}
}
all.value[id] = Object.assign(initial(), all.value[id] || {})
return all.value[id]
})
Expand Down

0 comments on commit 29f6a73

Please sign in to comment.