From e97cd1d78ea71575cd1de0c11dd97b9eb203ab87 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Mon, 15 Jul 2024 15:57:28 +0200 Subject: [PATCH] Converse v11, reporting customization in the livechat repo: Reporting the vcard plugin customizations in the livechat repo, to avoid maintaining a Converse fork. --- conversejs/custom/livechat-patch-vcard.js | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 conversejs/custom/livechat-patch-vcard.js diff --git a/conversejs/custom/livechat-patch-vcard.js b/conversejs/custom/livechat-patch-vcard.js new file mode 100644 index 00000000..57ee61ef --- /dev/null +++ b/conversejs/custom/livechat-patch-vcard.js @@ -0,0 +1,57 @@ +// Here we are patching the vCard plugin, to add some specific optimizations. + +import { _converse, api } from '@converse/headless/index.js' +import { + onOccupantAvatarChanged, + setVCardOnModel, + setVCardOnOccupant +} from '@converse/headless/plugins/vcard/utils.js' + +const pluginDefinition = _converse.pluggable.plugins['converse-vcard'] +const originalInitialize = pluginDefinition.initialize + +pluginDefinition.initialize = function initialize () { + const previousListeners = _converse._events.chatRoomInitialized ?? [] + originalInitialize.apply(this) + + _converse.api.settings.extend({ + livechat_load_all_vcards: false + }) + + // Now we must detect the new chatRoomInitialized listener, and remove it: + const listenersToRemove = [] + for (const def of _converse._events.chatRoomInitialized ?? []) { + if (def.callback && !previousListeners.includes(def.callback)) { + listenersToRemove.push(def.callback) + } + } + for (const callback of listenersToRemove) { + console.debug('Livechat patching vcard: we must remove this listener', callback) + api.listen.not('chatRoomInitialized', callback) + } + + // Adding the new listener: + api.listen.on('chatRoomInitialized', (m) => { + console.debug('Patched version of the vcard chatRoomInitialized event.') + setVCardOnModel(m) + + // loadAll: when in readonly mode (ie: OBS integration), always load all avatars. + const loadAll = api.settings.get('livechat_load_all_vcards') === true + let hiddenOccupants = m.get('hidden_occupants') + if (hiddenOccupants !== true || loadAll) { + m.occupants.forEach(setVCardOnOccupant) + } + m.listenTo(m.occupants, 'add', (occupant) => { + if (hiddenOccupants !== true || loadAll) { + setVCardOnOccupant(occupant) + } + }) + m.on('change:hidden_occupants', () => { + hiddenOccupants = m.get('hidden_occupants') + if (hiddenOccupants !== true || loadAll) { + m.occupants.forEach(setVCardOnOccupant) + } + }) + m.listenTo(m.occupants, 'change:image_hash', o => onOccupantAvatarChanged(o)) + }) +}