2019-08-30 04:15:30 +02:00
|
|
|
import {
|
|
|
|
cacheFirstUpdateAfter,
|
|
|
|
cacheFirstUpdateOnlyIfNotInCache
|
|
|
|
} from '../_utils/sync'
|
2018-08-30 04:03:12 +02:00
|
|
|
import { database } from '../_database/database'
|
2018-02-28 08:18:07 +01:00
|
|
|
import { getCustomEmoji } from '../_api/emoji'
|
|
|
|
import { store } from '../_store/store'
|
2019-12-24 03:10:03 +01:00
|
|
|
import isEqual from 'lodash-es/isEqual'
|
2018-02-28 08:18:07 +01:00
|
|
|
|
2019-08-30 04:15:30 +02:00
|
|
|
async function syncEmojiForInstance (instanceName, syncMethod) {
|
|
|
|
await syncMethod(
|
2018-02-28 08:18:07 +01:00
|
|
|
() => getCustomEmoji(instanceName),
|
2018-08-30 04:03:12 +02:00
|
|
|
() => database.getCustomEmoji(instanceName),
|
|
|
|
emoji => database.setCustomEmoji(instanceName, emoji),
|
2018-02-28 08:18:07 +01:00
|
|
|
emoji => {
|
2019-08-03 22:49:37 +02:00
|
|
|
const { customEmoji } = store.get()
|
2019-12-24 03:10:03 +01:00
|
|
|
if (!isEqual(customEmoji[instanceName], emoji)) { // avoid triggering updates if nothing's changed
|
|
|
|
customEmoji[instanceName] = emoji
|
|
|
|
store.set({ customEmoji })
|
|
|
|
}
|
2018-02-28 08:18:07 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-30 04:15:30 +02:00
|
|
|
export async function updateCustomEmojiForInstance (instanceName) {
|
|
|
|
await syncEmojiForInstance(instanceName, cacheFirstUpdateAfter)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setupCustomEmojiForInstance (instanceName) {
|
|
|
|
await syncEmojiForInstance(instanceName, cacheFirstUpdateOnlyIfNotInCache)
|
|
|
|
}
|
|
|
|
|
2018-03-03 23:51:48 +01:00
|
|
|
export function insertEmoji (realm, emoji) {
|
2019-08-03 22:49:37 +02:00
|
|
|
const emojiText = emoji.custom ? emoji.colons : emoji.native
|
|
|
|
const { composeSelectionStart } = store.get()
|
|
|
|
const idx = composeSelectionStart || 0
|
|
|
|
const oldText = store.getComposeData(realm, 'text') || ''
|
|
|
|
const pre = oldText.substring(0, idx)
|
|
|
|
const post = oldText.substring(idx)
|
|
|
|
const newText = `${pre}${emojiText} ${post}`
|
2018-08-30 06:42:57 +02:00
|
|
|
store.setComposeData(realm, { text: newText })
|
2018-02-28 08:18:07 +01:00
|
|
|
}
|