1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-15 21:39:58 +00:00
elk/composables/masto/account.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-01-08 06:21:09 +00:00
import type { mastodon } from 'masto'
2023-01-06 18:40:15 +00:00
export function getDisplayName(account: mastodon.v1.Account, options?: { rich?: boolean }) {
const displayName = account.displayName || account.username || account.acct || ''
2023-01-06 18:40:15 +00:00
if (options?.rich)
return displayName
return displayName.replace(/:([\w-]+):/g, '')
2023-01-06 18:40:15 +00:00
}
export function accountToShortHandle(acct: string) {
2023-01-16 05:10:33 +00:00
return `@${acct.includes('@') ? acct.split('@')[0] : acct}`
}
2023-01-08 06:21:09 +00:00
export function getShortHandle({ acct }: mastodon.v1.Account) {
2023-01-06 18:40:15 +00:00
if (!acct)
return ''
return accountToShortHandle(acct)
2023-01-06 18:40:15 +00:00
}
2023-01-08 06:21:09 +00:00
export function getServerName(account: mastodon.v1.Account) {
2023-01-06 18:40:15 +00:00
if (account.acct?.includes('@'))
return account.acct.split('@')[1]
// We should only lack the server name if we're on the same server as the account
2023-01-15 09:44:36 +00:00
return currentInstance.value ? getInstanceDomain(currentInstance.value) : ''
2023-01-06 18:40:15 +00:00
}
2023-01-08 06:21:09 +00:00
export function getFullHandle(account: mastodon.v1.Account) {
2023-01-06 18:40:15 +00:00
const handle = `@${account.acct}`
if (!currentUser.value || account.acct.includes('@'))
return handle
return `${handle}@${getServerName(account)}`
}
export function toShortHandle(fullHandle: string) {
if (!currentUser.value)
return fullHandle
const server = currentUser.value.server
if (fullHandle.endsWith(`@${server}`))
return fullHandle.slice(0, -server.length - 1)
return fullHandle
}
2023-01-08 06:21:09 +00:00
export function extractAccountHandle(account: mastodon.v1.Account) {
2023-01-06 18:40:15 +00:00
let handle = getFullHandle(account).slice(1)
2023-01-15 09:44:36 +00:00
const uri = currentInstance.value ? getInstanceDomain(currentInstance.value) : currentServer.value
2023-01-06 18:40:15 +00:00
if (currentInstance.value && handle.endsWith(`@${uri}`))
handle = handle.slice(0, -uri.length - 1)
return handle
}
2023-01-08 06:21:09 +00:00
export function useAccountHandle(account: mastodon.v1.Account, fullServer = true) {
2023-01-06 18:40:15 +00:00
return computed(() => fullServer
? getFullHandle(account)
: getShortHandle(account),
)
}