1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-14 12:59:59 +00:00
elk/composables/cache.ts

113 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-11-24 05:47:14 +00:00
import LRU from 'lru-cache'
2023-01-08 06:21:09 +00:00
import type { mastodon } from 'masto'
2022-11-24 05:47:14 +00:00
const cache = new LRU<string, any>({
max: 1000,
})
if (process.dev && process.client)
2022-11-24 05:47:14 +00:00
// eslint-disable-next-line no-console
console.log({ cache })
2022-11-24 07:53:27 +00:00
export function setCached(key: string, value: any, override = false) {
if (override || !cache.has(key))
cache.set(key, value)
2022-11-24 05:47:14 +00:00
}
function removeCached(key: string) {
cache.delete(key)
}
2022-11-24 05:47:14 +00:00
2023-01-08 06:21:09 +00:00
export function fetchStatus(id: string, force = false): Promise<mastodon.v1.Status> {
const server = currentServer.value
2023-01-14 21:56:47 +00:00
const userId = currentUser.value?.account.id
const key = `${server}:${userId}:status:${id}`
2022-11-24 05:47:14 +00:00
const cached = cache.get(key)
2022-12-01 07:24:35 +00:00
if (cached && !force)
2022-11-24 05:47:14 +00:00
return cached
2023-01-15 08:38:02 +00:00
const promise = useMastoClient().v1.statuses.fetch(id)
2022-11-24 05:47:14 +00:00
.then((status) => {
cacheStatus(status)
return status
})
cache.set(key, promise)
return promise
}
2023-01-08 06:21:09 +00:00
export function fetchAccountById(id?: string | null): Promise<mastodon.v1.Account | null> {
2022-12-06 23:38:00 +00:00
if (!id)
return Promise.resolve(null)
const server = currentServer.value
2023-01-14 21:56:47 +00:00
const userId = currentUser.value?.account.id
const key = `${server}:${userId}:account:${id}`
2022-11-24 05:47:14 +00:00
const cached = cache.get(key)
if (cached)
return cached
2023-01-15 09:44:36 +00:00
const domain = currentInstance.value ? getInstanceDomain(currentInstance.value) : null
2023-01-15 08:38:02 +00:00
const promise = useMastoClient().v1.accounts.fetch(id)
.then((r) => {
2023-01-08 06:21:09 +00:00
if (r.acct && !r.acct.includes('@') && domain)
r.acct = `${r.acct}@${domain}`
cacheAccount(r, server, true)
return r
2022-11-24 05:47:14 +00:00
})
cache.set(key, promise)
return promise
}
2023-01-08 06:21:09 +00:00
export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Account> {
const server = currentServer.value
2023-01-14 21:56:47 +00:00
const userId = currentUser.value?.account.id
const key = `${server}:${userId}:account:${acct}`
2022-11-24 05:47:14 +00:00
const cached = cache.get(key)
if (cached)
return cached
2023-01-15 09:44:36 +00:00
const domain = currentInstance.value ? getInstanceDomain(currentInstance.value) : undefined
async function lookupAccount() {
const client = useMastoClient()
let account: mastodon.v1.Account
if (!isGotoSocial.value)
account = await client.v1.accounts.lookup({ acct })
else
account = (await client.v1.search({ q: `@${acct}`, type: 'accounts' })).accounts[0]
if (account.acct && !account.acct.includes('@') && domain)
account.acct = `${account.acct}@${domain}`
return account
}
const account = lookupAccount()
.then((r) => {
cacheAccount(r, server, true)
2022-11-24 05:47:14 +00:00
return r
})
cache.set(key, account)
return account
}
2022-11-30 07:08:10 +00:00
export function useAccountByHandle(acct: string) {
return useAsyncState(() => fetchAccountByHandle(acct), null).state
}
2022-12-06 23:38:00 +00:00
export function useAccountById(id?: string | null) {
2022-11-30 07:08:10 +00:00
return useAsyncState(() => fetchAccountById(id), null).state
}
2023-01-08 06:21:09 +00:00
export function cacheStatus(status: mastodon.v1.Status, server = currentServer.value, override?: boolean) {
2023-01-14 21:56:47 +00:00
const userId = currentUser.value?.account.id
setCached(`${server}:${userId}:status:${status.id}`, status, override)
2022-11-24 05:47:14 +00:00
}
export function removeCachedStatus(id: string, server = currentServer.value) {
2023-01-14 21:56:47 +00:00
const userId = currentUser.value?.account.id
removeCached(`${server}:${userId}:status:${id}`)
}
2023-01-08 06:21:09 +00:00
export function cacheAccount(account: mastodon.v1.Account, server = currentServer.value, override?: boolean) {
2023-01-14 21:56:47 +00:00
const userId = currentUser.value?.account.id
setCached(`${server}:${userId}:account:${account.id}`, account, override)
setCached(`${server}:${userId}:account:${account.acct}`, account, override)
2022-11-24 05:47:14 +00:00
}