From 9d0123fd0048a3623b173e6b82bc7bbaa3c15380 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Pasquier <contact@jbpasquier.eu> Date: Tue, 28 Apr 2020 14:40:46 +0200 Subject: [PATCH] minor: add RAI --- plugins/converse-rai.js | 89 +++++++++++++++++++++++++++++++++++++++++ sib-chat.js | 19 +++++++-- 2 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 plugins/converse-rai.js diff --git a/plugins/converse-rai.js b/plugins/converse-rai.js new file mode 100644 index 0000000..20fa970 --- /dev/null +++ b/plugins/converse-rai.js @@ -0,0 +1,89 @@ +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + define(["converse"], factory); + } else { + factory(converse); + } +}(this, function (converse) { + var raiDialog = null, + _converse = null; + + converse.plugins.add("rai", { + 'dependencies': [], + + 'initialize': function () { + _converse = this._converse; + + _converse.api.settings.update({ + rai_notification: true, + rai_notification_label: "Room Activity Indicator" + }); + + _converse.api.listen.on('connected', function () { + setupRoomActivityIndicators(function (supported) { + if (supported) listenForRoomActivityIndicators(); + }); + }); + + } + }); + + function setupRoomActivityIndicators(callback) { + try { + const id = Math.random().toString(36).substr(2, 9); + const to = "conference." + _converse.domain; + _converse.connection.send(converse.env.$pres({ + to: to, + id: id + }).c('rai', { + 'xmlns': "xmpp:prosody.im/protocol/rai" + })); + + if (callback) callback(true); + } catch (e) { + if (callback) callback(false); + } + } + + function listenForRoomActivityIndicators() { + + _converse.connection.addHandler(function (message) { + + message.querySelectorAll('activity').forEach(function (activity) { + if (activity) { + const jid = activity.innerHTML; + _converse.api.trigger('chatRoomActivityIndicators', jid); + + if (_converse.api.settings.get("rai_notification")) { + notifyText(_converse.api.settings.get("rai_notification_label"), jid, null, function (notificationId, buttonIndex) { + _converse.api.rooms.open(jid); + }); + } + } + }); + + return true; + + }, "xmpp:prosody.im/protocol/rai", 'message'); + } + + function notifyText(message, title, notifyId, callback) { + + if (!notifyId) notifyId = Math.random().toString(36).substr(2, 9); + + var prompt = new Notification(title, { + body: message, + requireInteraction: true + }); + + prompt.onclick = function (event) { + event.preventDefault(); + if (callback) callback(notifyId, 0); + } + + prompt.onclose = function (event) { + event.preventDefault(); + if (callback) callback(notifyId, 1); + } + } +})); \ No newline at end of file diff --git a/sib-chat.js b/sib-chat.js index 90cecc7..42e8463 100644 --- a/sib-chat.js +++ b/sib-chat.js @@ -1,5 +1,6 @@ import 'https://cdn.happy-dev.fr/conversejs/dist/converse.min.js'; import 'https://cdn.happy-dev.fr/conversejs/dist/emojis.js'; +import 'https://unpkg.com/@startinblox/component-chat/plugins/converse-rai.js'; import { Helpers, store } from 'https://unpkg.com/@startinblox/core@0.9'; import { Sib } from "https://unpkg.com/@startinblox/core@0.9/dist/libs/Sib.js"; import { StoreMixin } from 'https://unpkg.com/@startinblox/core@0.9/dist/mixins/storeMixin.js'; @@ -50,7 +51,7 @@ export const SibChat = { }); this.element.shadowRoot.append(...Helpers.importCSS( 'https://cdn.happy-dev.fr/conversejs/dist/converse.min.css', - 'https://unpkg.com/@startinblox/component-chat@0.5/themes/converse-hd.css' + 'https://unpkg.com/@startinblox/component-chat/themes/converse-hd.css' )); if (window.converse_sib === undefined) { this.initializeConverse(); @@ -145,6 +146,19 @@ export const SibChat = { } }); + // Initialize rai plugin + converse.plugins.add('conversejs-rai', { + initialize() { + this._converse.api.listen.on('chatRoomActivityIndicators', function (jid) { + window.dispatchEvent(new CustomEvent('newMessage', { + detail: { + jid: jid + } + })); + }); + } + }); + // Initialize deferred resolution plugin converse.plugins.add('conversejs-sib-focused', { initialize() { @@ -195,7 +209,6 @@ export const SibChat = { 'auto_register_muc_nickname': true, 'bosh_service_url': this.element.dataset.boshServiceUrl, "clear_messages_on_reconnection": false, - 'i18n': 'en', 'jid': jabberID.toLowerCase(), 'i18n': this.element.dataset.i18n, 'message_archiving': 'always', @@ -220,7 +233,7 @@ export const SibChat = { fileupload: false, toggle_occupants: false }, - 'whitelisted_plugins': ['conversejs-sib-connected', 'conversejs-sib-focused', 'conversejs-changechat'], + 'whitelisted_plugins': ['rai', 'conversejs-sib-connected', 'conversejs-sib-focused', 'conversejs-changechat', 'conversejs-rai'], }); converse_sib.loaded_deferred.resolve(); -- GitLab