Przeglądaj źródła

Turn converse-chatboxes plugin into folder

JC Brand 4 lat temu
rodzic
commit
f158a996f4

+ 4 - 0
src/headless/core.js

@@ -38,13 +38,17 @@ Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
 Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload:0');
 Strophe.addNamespace('IDLE', 'urn:xmpp:idle:1');
 Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
+Strophe.addNamespace('MARKERS', 'urn:xmpp:chat-markers:0');
 Strophe.addNamespace('MENTIONS', 'urn:xmpp:mmn:0');
+Strophe.addNamespace('MESSAGE_CORRECT', 'urn:xmpp:message-correct:0');
 Strophe.addNamespace('MODERATE', 'urn:xmpp:message-moderate:0');
 Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
 Strophe.addNamespace('OMEMO', 'eu.siacs.conversations.axolotl');
 Strophe.addNamespace('OUTOFBAND', 'jabber:x:oob');
 Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
 Strophe.addNamespace('RAI', 'urn:xmpp:rai:0');
+Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
+Strophe.addNamespace('REFERENCE', 'urn:xmpp:reference:0');
 Strophe.addNamespace('REGISTER', 'jabber:iq:register');
 Strophe.addNamespace('RETRACT', 'urn:xmpp:message-retract:0');
 Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');

+ 1 - 1
src/headless/headless.js

@@ -8,7 +8,7 @@ import "./plugins/bosh.js";         // XEP-0206 BOSH
 import "./plugins/caps.js";         // XEP-0115 Entity Capabilities
 import "./plugins/carbons.js";      // XEP-0280 Message Carbons
 import "./plugins/chat/index.js";   // RFC-6121 Instant messaging
-import "./plugins/chatboxes.js";
+import "./plugins/chatboxes/index.js";
 import "./plugins/disco.js";        // XEP-0030 Service discovery
 import "./plugins/headlines.js";    // Support for headline messages
 import "./plugins/mam/index.js";    // XEP-0313 Message Archive Management

+ 0 - 151
src/headless/plugins/chatboxes.js

@@ -1,151 +0,0 @@
-/**
- * @module converse-chatboxes
- * @copyright 2020, the Converse.js contributors
- * @license Mozilla Public License (MPLv2)
- */
-import "./emoji/index.js";
-import { Collection } from "@converse/skeletor/src/collection";
-import { _converse, api, converse } from "../core.js";
-import log from "../log";
-
-const { Strophe } = converse.env;
-
-Strophe.addNamespace('MESSAGE_CORRECT', 'urn:xmpp:message-correct:0');
-Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
-Strophe.addNamespace('REFERENCE', 'urn:xmpp:reference:0');
-Strophe.addNamespace('MARKERS', 'urn:xmpp:chat-markers:0');
-
-
-converse.plugins.add('converse-chatboxes', {
-
-    dependencies: ["converse-emoji", "converse-roster", "converse-vcard"],
-
-    initialize () {
-        /* The initialize function gets called as soon as the plugin is
-         * loaded by converse.js's plugin machinery.
-         */
-
-        api.promises.add([
-            'chatBoxesFetched',
-            'chatBoxesInitialized',
-            'privateChatsAutoJoined'
-        ]);
-
-        _converse.ChatBoxes = Collection.extend({
-            comparator: 'time_opened',
-
-            model (attrs, options) {
-                return new _converse.ChatBox(attrs, options);
-            },
-
-            onChatBoxesFetched (collection) {
-                collection.filter(c => !c.isValid()).forEach(c => c.destroy());
-                /**
-                 * Triggered once all chat boxes have been recreated from the browser cache
-                 * @event _converse#chatBoxesFetched
-                 * @type { object }
-                 * @property { _converse.ChatBox | _converse.ChatRoom } chatbox
-                 * @property { XMLElement } stanza
-                 * @example _converse.api.listen.on('chatBoxesFetched', obj => { ... });
-                 * @example _converse.api.waitUntil('chatBoxesFetched').then(() => { ... });
-                 */
-                api.trigger('chatBoxesFetched');
-            },
-
-            onConnected (reconnecting) {
-                if (reconnecting) { return; }
-                this.browserStorage = _converse.createStore(`converse.chatboxes-${_converse.bare_jid}`);
-                this.fetch({
-                    'add': true,
-                    'success': c => this.onChatBoxesFetched(c)
-                });
-            }
-        });
-
-        async function createChatBox (jid, attrs, Model) {
-            jid = Strophe.getBareJidFromJid(jid.toLowerCase());
-            Object.assign(attrs, {'jid': jid, 'id': jid});
-            let chatbox;
-            try {
-                chatbox = new Model(attrs, {'collection': _converse.chatboxes});
-            } catch (e) {
-                log.error(e);
-                return null;
-            }
-            await chatbox.initialized;
-            if (!chatbox.isValid()) {
-                chatbox.destroy();
-                return null;
-            }
-            _converse.chatboxes.add(chatbox);
-            return chatbox;
-        }
-
-
-        /************************ BEGIN Event Handlers ************************/
-        api.listen.on('addClientFeatures', () => {
-            api.disco.own.features.add(Strophe.NS.MESSAGE_CORRECT);
-            api.disco.own.features.add(Strophe.NS.HTTPUPLOAD);
-            api.disco.own.features.add(Strophe.NS.OUTOFBAND);
-        });
-
-        api.listen.on('pluginsInitialized', () => {
-            _converse.chatboxes = new _converse.ChatBoxes();
-            /**
-             * Triggered once the _converse.ChatBoxes collection has been initialized.
-             * @event _converse#chatBoxesInitialized
-             * @example _converse.api.listen.on('chatBoxesInitialized', () => { ... });
-             * @example _converse.api.waitUntil('chatBoxesInitialized').then(() => { ... });
-             */
-            api.trigger('chatBoxesInitialized');
-        });
-
-        api.listen.on('presencesInitialized', (reconnecting) => _converse.chatboxes.onConnected(reconnecting));
-        api.listen.on('reconnected', () => _converse.chatboxes.forEach(m => m.onReconnection()));
-        /************************ END Event Handlers ************************/
-
-
-        /************************ BEGIN API ************************/
-        Object.assign(api, {
-            /**
-             * The "chatboxes" namespace.
-             *
-             * @namespace api.chatboxes
-             * @memberOf api
-             */
-            chatboxes: {
-                /**
-                 * @method api.chats.create
-                 * @param { String|String[] } jids - A JID or array of JIDs
-                 * @param { Object } [attrs] An object containing configuration attributes
-                 * @param { Model } model - The type of chatbox that should be created
-                 */
-                async create (jids=[], attrs={}, model) {
-                    await api.waitUntil('chatBoxesFetched');
-                    if (typeof jids === 'string') {
-                        return createChatBox(jids, attrs, model);
-                    } else {
-                        return Promise.all(jids.map(jid => createChatBox(jid, attrs, model)));
-                    }
-                },
-
-                /**
-                 * @method api.chats.get
-                 * @param { String|String[] } jids - A JID or array of JIDs
-                 */
-                async get (jids) {
-                    await api.waitUntil('chatBoxesFetched');
-                    if (jids === undefined) {
-                        return _converse.chatboxes.models;
-                    } else if (typeof jids === 'string') {
-                        return _converse.chatboxes.get(jids.toLowerCase());
-                    } else {
-                        jids = jids.map(j => j.toLowerCase());
-                        return _converse.chatboxes.models.filter(m => jids.includes(m.get('jid')));
-                    }
-                }
-            }
-        });
-        /************************ END API ************************/
-    }
-});

+ 41 - 0
src/headless/plugins/chatboxes/api.js

@@ -0,0 +1,41 @@
+import { _converse, api } from "../../core.js";
+import { createChatBox } from './utils.js';
+
+/**
+ * The "chatboxes" namespace.
+ *
+ * @namespace api.chatboxes
+ * @memberOf api
+ */
+export default {
+    /**
+     * @method api.chats.create
+     * @param { String|String[] } jids - A JID or array of JIDs
+     * @param { Object } [attrs] An object containing configuration attributes
+     * @param { Model } model - The type of chatbox that should be created
+     */
+    async create (jids=[], attrs={}, model) {
+        await api.waitUntil('chatBoxesFetched');
+        if (typeof jids === 'string') {
+            return createChatBox(jids, attrs, model);
+        } else {
+            return Promise.all(jids.map(jid => createChatBox(jid, attrs, model)));
+        }
+    },
+
+    /**
+     * @method api.chats.get
+     * @param { String|String[] } jids - A JID or array of JIDs
+     */
+    async get (jids) {
+        await api.waitUntil('chatBoxesFetched');
+        if (jids === undefined) {
+            return _converse.chatboxes.models;
+        } else if (typeof jids === 'string') {
+            return _converse.chatboxes.get(jids.toLowerCase());
+        } else {
+            jids = jids.map(j => j.toLowerCase());
+            return _converse.chatboxes.models.filter(m => jids.includes(m.get('jid')));
+        }
+    }
+}

+ 36 - 0
src/headless/plugins/chatboxes/chatboxes.js

@@ -0,0 +1,36 @@
+import { Collection } from "@converse/skeletor/src/collection";
+import { _converse, api } from "../../core.js";
+
+const ChatBoxes = Collection.extend({
+    comparator: 'time_opened',
+
+    model (attrs, options) {
+        return new _converse.ChatBox(attrs, options);
+    },
+
+    onChatBoxesFetched (collection) {
+        collection.filter(c => !c.isValid()).forEach(c => c.destroy());
+        /**
+            * Triggered once all chat boxes have been recreated from the browser cache
+            * @event _converse#chatBoxesFetched
+            * @type { object }
+            * @property { _converse.ChatBox | _converse.ChatRoom } chatbox
+            * @property { XMLElement } stanza
+            * @example _converse.api.listen.on('chatBoxesFetched', obj => { ... });
+            * @example _converse.api.waitUntil('chatBoxesFetched').then(() => { ... });
+            */
+        api.trigger('chatBoxesFetched');
+    },
+
+    onConnected (reconnecting) {
+        if (reconnecting) { return; }
+        this.browserStorage = _converse.createStore(`converse.chatboxes-${_converse.bare_jid}`);
+        this.fetch({
+            'add': true,
+            'success': c => this.onChatBoxesFetched(c)
+        });
+    }
+});
+
+
+export default ChatBoxes;

+ 50 - 0
src/headless/plugins/chatboxes/index.js

@@ -0,0 +1,50 @@
+/**
+ * @copyright 2020, the Converse.js contributors
+ * @license Mozilla Public License (MPLv2)
+ */
+import "../emoji/index.js";
+import ChatBoxes from './chatboxes.js';
+import chatboxes_api from './api.js';
+import { _converse, api, converse } from "../../core.js";
+
+const { Strophe } = converse.env;
+
+
+converse.plugins.add('converse-chatboxes', {
+
+    dependencies: ["converse-emoji", "converse-roster", "converse-vcard"],
+
+    initialize () {
+
+        api.promises.add([
+            'chatBoxesFetched',
+            'chatBoxesInitialized',
+            'privateChatsAutoJoined'
+        ]);
+
+        Object.assign(api, { 'chatboxes': chatboxes_api});
+
+        _converse.ChatBoxes = ChatBoxes;
+
+
+        api.listen.on('addClientFeatures', () => {
+            api.disco.own.features.add(Strophe.NS.MESSAGE_CORRECT);
+            api.disco.own.features.add(Strophe.NS.HTTPUPLOAD);
+            api.disco.own.features.add(Strophe.NS.OUTOFBAND);
+        });
+
+        api.listen.on('pluginsInitialized', () => {
+            _converse.chatboxes = new _converse.ChatBoxes();
+            /**
+             * Triggered once the _converse.ChatBoxes collection has been initialized.
+             * @event _converse#chatBoxesInitialized
+             * @example _converse.api.listen.on('chatBoxesInitialized', () => { ... });
+             * @example _converse.api.waitUntil('chatBoxesInitialized').then(() => { ... });
+             */
+            api.trigger('chatBoxesInitialized');
+        });
+
+        api.listen.on('presencesInitialized', (reconnecting) => _converse.chatboxes.onConnected(reconnecting));
+        api.listen.on('reconnected', () => _converse.chatboxes.forEach(m => m.onReconnection()));
+    }
+});

+ 24 - 0
src/headless/plugins/chatboxes/utils.js

@@ -0,0 +1,24 @@
+import { _converse, converse } from "../../core.js";
+import log from "../../log";
+
+const { Strophe } = converse.env;
+
+
+export async function createChatBox (jid, attrs, Model) {
+    jid = Strophe.getBareJidFromJid(jid.toLowerCase());
+    Object.assign(attrs, {'jid': jid, 'id': jid});
+    let chatbox;
+    try {
+        chatbox = new Model(attrs, {'collection': _converse.chatboxes});
+    } catch (e) {
+        log.error(e);
+        return null;
+    }
+    await chatbox.initialized;
+    if (!chatbox.isValid()) {
+        chatbox.destroy();
+        return null;
+    }
+    _converse.chatboxes.add(chatbox);
+    return chatbox;
+}

+ 1 - 1
src/plugins/chatboxviews/index.js

@@ -4,7 +4,7 @@
  * @license Mozilla Public License (MPLv2)
  */
 import './view.js';
-import '@converse/headless/plugins/chatboxes';
+import '@converse/headless/plugins/chatboxes/index.js';
 import 'components/converse.js';
 import ChatBoxViews from './container.js';
 import { ViewWithAvatar } from 'shared/avatar.js';

+ 1 - 1
src/plugins/rosterview/index.js

@@ -4,7 +4,7 @@
  * @license Mozilla Public License (MPLv2)
  */
 import "../modal";
-import "@converse/headless/plugins/chatboxes";
+import "@converse/headless/plugins/chatboxes/index.js";
 import "@converse/headless/plugins/roster/index.js";
 import "modals/add-contact.js";
 import './rosterview.js';