1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-05 00:19:59 +00:00
elk/composables/masto/account.ts
Shinigami c1e89582f8
fix: remove emoji in status title (#932)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
2023-01-11 17:47:36 +00:00

54 lines
1.7 KiB
TypeScript

import type { mastodon } from 'masto'
export function getDisplayName(account: mastodon.v1.Account, options?: { rich?: boolean }) {
const displayName = account.displayName || account.username || account.acct || ''
if (options?.rich)
return displayName
return displayName.replace(/:([\w-]+?):/g, '')
}
export function getShortHandle({ acct }: mastodon.v1.Account) {
if (!acct)
return ''
return `@${acct.includes('@') ? acct.split('@')[0] : acct}`
}
export function getServerName(account: mastodon.v1.Account) {
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
return currentInstance.value?.uri || ''
}
export function getFullHandle(account: mastodon.v1.Account) {
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
}
export function extractAccountHandle(account: mastodon.v1.Account) {
let handle = getFullHandle(account).slice(1)
const uri = currentInstance.value?.uri ?? currentServer.value
if (currentInstance.value && handle.endsWith(`@${uri}`))
handle = handle.slice(0, -uri.length - 1)
return handle
}
export function useAccountHandle(account: mastodon.v1.Account, fullServer = true) {
return computed(() => fullServer
? getFullHandle(account)
: getShortHandle(account),
)
}