2022-11-24 08:47:14 +03:00
|
|
|
import LRU from 'lru-cache'
|
|
|
|
import type { Account, Status } from 'masto'
|
|
|
|
|
|
|
|
const cache = new LRU<string, any>({
|
|
|
|
max: 1000,
|
|
|
|
})
|
|
|
|
|
2022-12-17 19:55:29 +03:00
|
|
|
if (process.dev && process.client)
|
2022-11-24 08:47:14 +03:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log({ cache })
|
|
|
|
|
2022-11-24 10:53:27 +03:00
|
|
|
export function setCached(key: string, value: any, override = false) {
|
|
|
|
if (override || !cache.has(key))
|
|
|
|
cache.set(key, value)
|
2022-11-24 08:47:14 +03:00
|
|
|
}
|
2022-12-25 17:52:37 +03:00
|
|
|
function removeCached(key: string) {
|
|
|
|
cache.delete(key)
|
|
|
|
}
|
2022-11-24 08:47:14 +03:00
|
|
|
|
2022-12-01 10:24:35 +03:00
|
|
|
export function fetchStatus(id: string, force = false): Promise<Status> {
|
2022-12-12 02:30:26 +03:00
|
|
|
const server = currentServer.value
|
|
|
|
const key = `${server}:status:${id}`
|
2022-11-24 08:47:14 +03:00
|
|
|
const cached = cache.get(key)
|
2022-12-01 10:24:35 +03:00
|
|
|
if (cached && !force)
|
2022-11-24 08:47:14 +03:00
|
|
|
return cached
|
2022-11-26 18:42:58 +03:00
|
|
|
const promise = useMasto().statuses.fetch(id)
|
2022-11-24 08:47:14 +03:00
|
|
|
.then((status) => {
|
|
|
|
cacheStatus(status)
|
|
|
|
return status
|
|
|
|
})
|
|
|
|
cache.set(key, promise)
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2022-12-07 02:38:00 +03:00
|
|
|
export function fetchAccountById(id?: string | null): Promise<Account | null> {
|
|
|
|
if (!id)
|
|
|
|
return Promise.resolve(null)
|
|
|
|
|
2022-12-12 02:30:26 +03:00
|
|
|
const server = currentServer.value
|
|
|
|
const key = `${server}:account:${id}`
|
2022-11-24 08:47:14 +03:00
|
|
|
const cached = cache.get(key)
|
|
|
|
if (cached)
|
|
|
|
return cached
|
2022-12-12 02:30:26 +03:00
|
|
|
const uri = currentInstance.value?.uri
|
2022-11-26 18:42:58 +03:00
|
|
|
const promise = useMasto().accounts.fetch(id)
|
2022-12-12 02:30:26 +03:00
|
|
|
.then((r) => {
|
2022-12-28 19:01:13 +03:00
|
|
|
if (r.acct && !r.acct.includes('@') && uri)
|
2022-12-12 02:30:26 +03:00
|
|
|
r.acct = `${r.acct}@${uri}`
|
|
|
|
|
|
|
|
cacheAccount(r, server, true)
|
|
|
|
return r
|
2022-11-24 08:47:14 +03:00
|
|
|
})
|
|
|
|
cache.set(key, promise)
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:08:10 +03:00
|
|
|
export async function fetchAccountByHandle(acct: string): Promise<Account> {
|
2022-12-12 02:30:26 +03:00
|
|
|
const server = currentServer.value
|
|
|
|
const key = `${server}:account:${acct}`
|
2022-11-24 08:47:14 +03:00
|
|
|
const cached = cache.get(key)
|
|
|
|
if (cached)
|
|
|
|
return cached
|
2022-12-12 02:30:26 +03:00
|
|
|
const uri = currentInstance.value?.uri
|
2022-11-26 18:42:58 +03:00
|
|
|
const account = useMasto().accounts.lookup({ acct })
|
2022-11-24 08:47:14 +03:00
|
|
|
.then((r) => {
|
2022-12-28 19:01:13 +03:00
|
|
|
if (r.acct && !r.acct.includes('@') && uri)
|
2022-12-12 02:30:26 +03:00
|
|
|
r.acct = `${r.acct}@${uri}`
|
2022-12-04 16:19:47 +03:00
|
|
|
|
2022-12-12 02:30:26 +03:00
|
|
|
cacheAccount(r, server, true)
|
2022-11-24 08:47:14 +03:00
|
|
|
return r
|
|
|
|
})
|
|
|
|
cache.set(key, account)
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
2022-11-30 10:08:10 +03:00
|
|
|
export function useAccountByHandle(acct: string) {
|
|
|
|
return useAsyncState(() => fetchAccountByHandle(acct), null).state
|
|
|
|
}
|
|
|
|
|
2022-12-07 02:38:00 +03:00
|
|
|
export function useAccountById(id?: string | null) {
|
2022-11-30 10:08:10 +03:00
|
|
|
return useAsyncState(() => fetchAccountById(id), null).state
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:30:26 +03:00
|
|
|
export function cacheStatus(status: Status, server = currentServer.value, override?: boolean) {
|
|
|
|
setCached(`${server}:status:${status.id}`, status, override)
|
2022-11-24 08:47:14 +03:00
|
|
|
}
|
|
|
|
|
2022-12-25 17:52:37 +03:00
|
|
|
export function removeCachedStatus(id: string, server = currentServer.value) {
|
|
|
|
removeCached(`${server}:status:${id}`)
|
|
|
|
}
|
|
|
|
|
2022-12-12 02:30:26 +03:00
|
|
|
export function cacheAccount(account: Account, server = currentServer.value, override?: boolean) {
|
|
|
|
setCached(`${server}:account:${account.id}`, account, override)
|
|
|
|
setCached(`${server}:account:${account.acct}`, account, override)
|
2022-11-24 08:47:14 +03:00
|
|
|
}
|