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