1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-19 15:29:59 +00:00
elk/composables/emojis.ts

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-01-08 06:21:09 +00:00
import type { mastodon } from 'masto'
2022-12-23 22:56:16 +00:00
import type { CustomEmojisInfo } from './push-notifications/types'
import { STORAGE_KEY_CUSTOM_EMOJIS } from '~/constants'
const TTL = 1000 * 60 * 60 * 24 // 1 day
function getDefault(): CustomEmojisInfo {
return {
lastUpdate: 0,
emojis: [],
}
}
export const currentCustomEmojis = process.server
? computed(getDefault)
: useUserLocalStorage(STORAGE_KEY_CUSTOM_EMOJIS, getDefault)
export async function updateCustomEmojis() {
if (Date.now() - currentCustomEmojis.value.lastUpdate < TTL)
return
2023-01-15 08:38:02 +00:00
const { client } = $(useMasto())
const emojis = await client.v1.customEmojis.list()
2022-12-23 22:56:16 +00:00
Object.assign(currentCustomEmojis.value, {
lastUpdate: Date.now(),
emojis,
})
}
2023-01-08 06:21:09 +00:00
function transformEmojiData(emojis: mastodon.v1.CustomEmoji[]) {
2022-12-23 22:56:16 +00:00
const result = []
for (const emoji of emojis) {
if (!emoji.visibleInPicker)
continue
result.push({
id: emoji.shortcode,
native: ':emoji.shortcode:',
name: emoji.shortcode,
skins: [{ src: emoji.url || emoji.staticUrl }],
})
}
return result
}
export const customEmojisData = computed(() => currentCustomEmojis.value.emojis.length
? [{
id: 'custom',
name: `Custom emojis on ${currentServer.value}`,
emojis: transformEmojiData(currentCustomEmojis.value.emojis),
}]
: undefined)
2023-01-07 09:31:48 +00:00
2023-01-08 06:21:09 +00:00
export function useEmojisFallback(emojisGetter: () => mastodon.v1.CustomEmoji[] | undefined) {
2023-01-07 09:31:48 +00:00
return computed(() => {
2023-01-08 06:21:09 +00:00
const result: mastodon.v1.CustomEmoji[] = []
2023-01-07 09:31:48 +00:00
const emojis = emojisGetter()
if (emojis)
result.push(...emojis)
result.push(...currentCustomEmojis.value.emojis)
return emojisArrayToObject(result)
})
}