2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-chatboxes
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2019-07-11 12:30:51 +02:00
|
|
|
import "./converse-emoji";
|
2019-09-19 16:54:55 +02:00
|
|
|
import { Collection } from "skeletor.js/src/collection";
|
2018-10-23 03:41:38 +02:00
|
|
|
import converse from "./converse-core";
|
2019-11-01 16:04:55 +01:00
|
|
|
import { isString } from "lodash";
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "./log";
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-11-01 16:04:55 +01:00
|
|
|
const { Strophe } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
Strophe.addNamespace('MESSAGE_CORRECT', 'urn:xmpp:message-correct:0');
|
2018-11-03 14:37:57 +01:00
|
|
|
Strophe.addNamespace('RECEIPTS', 'urn:xmpp:receipts');
|
2018-10-23 03:41:38 +02:00
|
|
|
Strophe.addNamespace('REFERENCE', 'urn:xmpp:reference:0');
|
2019-01-31 19:22:30 +01:00
|
|
|
Strophe.addNamespace('MARKERS', 'urn:xmpp:chat-markers:0');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-chatboxes', {
|
|
|
|
|
2019-07-11 12:30:51 +02:00
|
|
|
dependencies: ["converse-emoji", "converse-roster", "converse-vcard"],
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
const { _converse } = this;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
_converse.api.promises.add([
|
|
|
|
'chatBoxesFetched',
|
2018-12-17 11:42:43 +01:00
|
|
|
'chatBoxesInitialized',
|
2018-10-23 03:41:38 +02:00
|
|
|
'privateChatsAutoJoined'
|
|
|
|
]);
|
|
|
|
|
2019-10-15 12:55:11 +02:00
|
|
|
let msg_counter = 0;
|
|
|
|
|
|
|
|
_converse.incrementMsgCounter = function () {
|
|
|
|
msg_counter += 1;
|
|
|
|
const title = document.title;
|
|
|
|
if (!title) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (title.search(/^Messages \(\d+\) /) === -1) {
|
|
|
|
document.title = `Messages (${msg_counter}) ${title}`;
|
|
|
|
} else {
|
2020-01-27 17:39:35 +01:00
|
|
|
document.title = title.replace(/^Messages \(\d+\) /, `Messages (${msg_counter}) `);
|
2019-10-15 12:55:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_converse.clearMsgCounter = function () {
|
|
|
|
msg_counter = 0;
|
|
|
|
const title = document.title;
|
|
|
|
if (!title) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (title.search(/^Messages \(\d+\) /) !== -1) {
|
|
|
|
document.title = title.replace(/^Messages \(\d+\) /, "");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.ChatBoxes = Collection.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
comparator: 'time_opened',
|
2017-08-16 11:16:22 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
model (attrs, options) {
|
|
|
|
return new _converse.ChatBox(attrs, options);
|
|
|
|
},
|
2017-08-16 11:16:22 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onChatBoxesFetched (collection) {
|
2018-12-04 12:52:25 +01:00
|
|
|
collection.filter(c => !c.isValid()).forEach(c => c.destroy());
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered when a message stanza is been received and processed.
|
2019-07-11 12:30:51 +02:00
|
|
|
* @event _converse#chatBoxesFetched
|
2019-03-29 15:47:23 +01:00
|
|
|
* @type { object }
|
|
|
|
* @property { _converse.ChatBox | _converse.ChatRoom } chatbox
|
|
|
|
* @property { XMLElement } stanza
|
|
|
|
* @example _converse.api.listen.on('message', obj => { ... });
|
|
|
|
* @example _converse.api.waitUntil('chatBoxesFetched').then(() => { ... });
|
|
|
|
*/
|
2019-03-29 21:10:45 +01:00
|
|
|
_converse.api.trigger('chatBoxesFetched');
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
2019-08-07 16:12:48 +02:00
|
|
|
onConnected (reconnecting) {
|
2020-01-21 12:45:34 +01:00
|
|
|
if (reconnecting) { return; }
|
2019-10-24 14:29:15 +02:00
|
|
|
this.browserStorage = _converse.createStore(`converse.chatboxes-${_converse.bare_jid}`);
|
2018-10-23 03:41:38 +02:00
|
|
|
this.fetch({
|
|
|
|
'add': true,
|
2018-12-04 12:52:25 +01:00
|
|
|
'success': c => this.onChatBoxesFetched(c)
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2018-05-02 17:07:57 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2018-04-18 16:55:26 +02:00
|
|
|
|
2018-05-10 22:14:37 +02:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
async function createChatBox (jid, attrs, Model) {
|
2019-11-01 16:04:55 +01:00
|
|
|
jid = Strophe.getBareJidFromJid(jid.toLowerCase());
|
|
|
|
Object.assign(attrs, {'jid': jid, 'id': jid});
|
|
|
|
let chatbox;
|
|
|
|
try {
|
2019-09-19 16:54:55 +02:00
|
|
|
chatbox = new Model(attrs, {'collection': _converse.chatboxes});
|
2019-11-01 16:04:55 +01:00
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(e);
|
2019-11-01 16:04:55 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
await chatbox.initialized;
|
|
|
|
if (!chatbox.isValid()) {
|
|
|
|
chatbox.destroy();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
_converse.chatboxes.add(chatbox);
|
|
|
|
await chatbox.messages.fetched;
|
|
|
|
return chatbox;
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-05-02 17:07:57 +02:00
|
|
|
|
2018-05-10 22:14:37 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2019-03-29 14:15:03 +01:00
|
|
|
_converse.api.listen.on('addClientFeatures', () => {
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.api.disco.own.features.add(Strophe.NS.MESSAGE_CORRECT);
|
|
|
|
_converse.api.disco.own.features.add(Strophe.NS.HTTPUPLOAD);
|
|
|
|
_converse.api.disco.own.features.add(Strophe.NS.OUTOFBAND);
|
|
|
|
});
|
2017-08-16 11:16:22 +02:00
|
|
|
|
2019-03-29 21:36:49 +01:00
|
|
|
_converse.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(() => { ... });
|
|
|
|
*/
|
|
|
|
_converse.api.trigger('chatBoxesInitialized');
|
|
|
|
});
|
2019-05-22 14:03:13 +02:00
|
|
|
|
2019-08-07 16:12:48 +02:00
|
|
|
_converse.api.listen.on('presencesInitialized', (reconnecting) => _converse.chatboxes.onConnected(reconnecting));
|
2019-05-22 14:03:13 +02:00
|
|
|
_converse.api.listen.on('reconnected', () => _converse.chatboxes.forEach(m => m.onReconnection()));
|
2019-10-15 12:55:11 +02:00
|
|
|
_converse.api.listen.on('windowStateChanged', d => (d.state === 'visible') && _converse.clearMsgCounter());
|
2018-10-23 03:41:38 +02:00
|
|
|
/************************ END Event Handlers ************************/
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN API ************************/
|
2019-04-29 09:07:15 +02:00
|
|
|
Object.assign(_converse.api, {
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
2019-11-01 16:04:55 +01:00
|
|
|
* The "chatboxes" namespace.
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
2019-11-01 16:04:55 +01:00
|
|
|
* @namespace _converse.api.chatboxes
|
2018-10-23 03:41:38 +02:00
|
|
|
* @memberOf _converse.api
|
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
chatboxes: {
|
2018-08-01 12:08:18 +02:00
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
* @method _converse.api.chats.create
|
2019-11-01 16:04:55 +01:00
|
|
|
* @param { String|String[] } jids - A JID or array of JIDs
|
|
|
|
* @param { Object } [attrs] An object containing configuration attributes
|
2019-09-19 16:54:55 +02:00
|
|
|
* @param { Model } model - The type of chatbox that should be created
|
2018-08-01 12:08:18 +02:00
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
async create (jids=[], attrs={}, model) {
|
2019-10-24 14:29:15 +02:00
|
|
|
await _converse.api.waitUntil('chatBoxesFetched');
|
2019-09-26 14:47:56 +02:00
|
|
|
if (isString(jids)) {
|
2019-11-01 16:04:55 +01:00
|
|
|
return createChatBox(jids, attrs, model);
|
|
|
|
} else {
|
|
|
|
return Promise.all(jids.map(jid => createChatBox(jid, attrs, model)));
|
2019-04-17 11:52:41 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method _converse.api.chats.get
|
2019-11-01 16:04:55 +01:00
|
|
|
* @param { String|String[] } jids - A JID or array of JIDs
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
async get (jids) {
|
|
|
|
await _converse.api.waitUntil('chatBoxesFetched');
|
2019-07-29 10:19:05 +02:00
|
|
|
if (jids === undefined) {
|
2019-11-01 16:04:55 +01:00
|
|
|
return _converse.chatboxes.models;
|
2019-09-26 14:47:56 +02:00
|
|
|
} else if (isString(jids)) {
|
2019-11-01 16:04:55 +01:00
|
|
|
return _converse.chatboxes.get(jids.toLowerCase());
|
|
|
|
} else {
|
|
|
|
jids = jids.map(j => j.toLowerCase());
|
|
|
|
return _converse.chatboxes.models.filter(m => jids.includes(m.get('jid')));
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
/************************ END API ************************/
|
|
|
|
}
|
|
|
|
});
|