2017-08-16 11:16:22 +02:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
|
|
|
/*global define */
|
|
|
|
|
|
|
|
(function (root, factory) {
|
2018-02-15 15:50:11 +01:00
|
|
|
define([
|
|
|
|
"converse-core",
|
|
|
|
"tpl!chatboxes",
|
|
|
|
"backbone.overview"
|
|
|
|
], factory);
|
|
|
|
}(this, function (converse, tpl_chatboxes) {
|
2017-08-16 11:16:22 +02:00
|
|
|
"use strict";
|
2018-02-15 15:50:11 +01:00
|
|
|
|
2018-04-11 15:29:41 +02:00
|
|
|
const { $msg, Backbone, Promise, Strophe, b64_sha1, moment, utils, _ } = converse.env;
|
|
|
|
Strophe.addNamespace('OUTOFBAND', 'jabber:x:oob');
|
2018-02-15 15:50:11 +01:00
|
|
|
|
2017-08-16 11:16:22 +02:00
|
|
|
converse.plugins.add('converse-chatboxes', {
|
|
|
|
|
|
|
|
overrides: {
|
|
|
|
// Overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
|
|
|
|
disconnect: function () {
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
_converse.chatboxviews.closeAllChatBoxes();
|
|
|
|
return this.__super__.disconnect.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
logOut: function () {
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
_converse.chatboxviews.closeAllChatBoxes();
|
|
|
|
return this.__super__.logOut.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2018-03-25 12:53:40 +02:00
|
|
|
initStatus: function (reconnecting) {
|
2017-08-16 11:16:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2018-03-25 12:53:40 +02:00
|
|
|
if (!reconnecting) {
|
|
|
|
_converse.chatboxviews.closeAllChatBoxes();
|
|
|
|
}
|
2017-08-16 11:16:22 +02:00
|
|
|
return this.__super__.initStatus.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2018-04-11 15:29:41 +02:00
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
2017-08-16 11:16:22 +02:00
|
|
|
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.api.promises.add([
|
|
|
|
'chatBoxesFetched',
|
|
|
|
'chatBoxesInitialized'
|
|
|
|
]);
|
2017-08-16 11:16:22 +02:00
|
|
|
|
2017-10-31 23:04:46 +01:00
|
|
|
function openChat (jid) {
|
|
|
|
if (!utils.isValidJID(jid)) {
|
|
|
|
return converse.log(
|
|
|
|
`Invalid JID "${jid}" provided in URL fragment`,
|
|
|
|
Strophe.LogLevel.WARN
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Promise.all([
|
|
|
|
_converse.api.waitUntil('rosterContactsFetched'),
|
|
|
|
_converse.api.waitUntil('chatBoxesFetched')
|
|
|
|
]).then(() => {
|
|
|
|
_converse.api.chats.open(jid);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_converse.router.route('converse/chat?jid=:jid', openChat);
|
|
|
|
|
|
|
|
|
2017-12-06 22:09:46 +01:00
|
|
|
_converse.Message = Backbone.Model.extend({
|
|
|
|
defaults(){
|
|
|
|
return {
|
|
|
|
msgid: _converse.connection.getUniqueId()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_converse.Messages = Backbone.Collection.extend({
|
|
|
|
model: _converse.Message,
|
|
|
|
comparator: 'time'
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_converse.ChatBox = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
'bookmarked': false,
|
|
|
|
'chat_state': undefined,
|
2018-02-21 22:40:51 +01:00
|
|
|
'image': _converse.DEFAULT_IMAGE,
|
|
|
|
'image_type': _converse.DEFAULT_IMAGE_TYPE,
|
2017-12-06 22:09:46 +01:00
|
|
|
'num_unread': 0,
|
2018-02-21 22:40:51 +01:00
|
|
|
'show_avatar': true,
|
|
|
|
'type': 'chatbox',
|
2017-12-06 22:09:46 +01:00
|
|
|
'url': ''
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
this.messages = new _converse.Messages();
|
|
|
|
this.messages.browserStorage = new Backbone.BrowserStorage[_converse.message_storage](
|
|
|
|
b64_sha1(`converse.messages${this.get('jid')}${_converse.bare_jid}`));
|
2018-03-07 10:31:33 +01:00
|
|
|
|
2017-12-06 22:09:46 +01:00
|
|
|
this.save({
|
|
|
|
// The chat_state will be set to ACTIVE once the chat box is opened
|
|
|
|
// and we listen for change:chat_state, so shouldn't set it to ACTIVE here.
|
|
|
|
'box_id' : b64_sha1(this.get('jid')),
|
|
|
|
'time_opened': this.get('time_opened') || moment().valueOf(),
|
|
|
|
'user_id' : Strophe.getNodeFromJid(this.get('jid'))
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-04-12 09:24:41 +02:00
|
|
|
createFileMessageStanza (message, to) {
|
2018-04-11 15:29:41 +02:00
|
|
|
const stanza = $msg({
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
'to': to,
|
|
|
|
'type': 'chat',
|
|
|
|
'id': message.get('msgid')
|
|
|
|
}).c('body').t(message.get('message')).up()
|
|
|
|
.c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).up()
|
|
|
|
.c('x', {'xmlns': Strophe.NS.OUTOFBAND}).c('url').t(message.get('message')).up();
|
|
|
|
|
|
|
|
return stanza;
|
|
|
|
},
|
|
|
|
|
|
|
|
sendFile (file, chatbox) {
|
|
|
|
const self = this;
|
|
|
|
console.log('Send file via http upload');
|
|
|
|
const request_slot_url = 'upload.' + _converse.domain;
|
|
|
|
_converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, request_slot_url)
|
|
|
|
.then((result) => {
|
|
|
|
chatbox.showHelpMessages([__('The file upload starts now')],'info');
|
|
|
|
self.requestSlot(file, request_slot_url, function(data) {
|
|
|
|
if (!data) {
|
|
|
|
// general error
|
|
|
|
console.log('Unknown error while requesting upload slot.');
|
|
|
|
alert(__('File upload failed. Please check the log.'));
|
|
|
|
} else if (data.error) {
|
|
|
|
// specific error
|
|
|
|
console.log('The XMPP-Server return an error of the type: ' + data.error.type);
|
|
|
|
alert(__('File upload failed. Please check the log.'));
|
|
|
|
} else if (data.get && data.put) {
|
|
|
|
console.log('slot received, start upload to ' + data.put);
|
|
|
|
self.uploadFile(data.put, file, function() {
|
|
|
|
console.log(data.put);
|
|
|
|
chatbox.onMessageSubmitted(data.put, null, file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
requestSlot (file, request_slot_url, cb) {
|
|
|
|
const self = this;
|
|
|
|
console.log("try sending file to: " + request_slot_url);
|
|
|
|
const iq = converse.env.$iq({
|
|
|
|
to: request_slot_url,
|
|
|
|
type: 'get'
|
|
|
|
}).c('request', {
|
|
|
|
xmlns: Strophe.NS.HTTPUPLOAD
|
|
|
|
}).c('filename').t(file.name)
|
|
|
|
.up()
|
|
|
|
.c('size').t(file.size);
|
|
|
|
|
|
|
|
_converse.connection.sendIQ(iq, function(stanza) {
|
|
|
|
self.successfulRequestSlotCB(stanza, cb);
|
|
|
|
}, function(stanza) {
|
|
|
|
self.failedRequestSlotCB(stanza, cb);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadFile (url, file, success_cb) {
|
|
|
|
console.log("uploadFile start");
|
|
|
|
const xmlhttp = new XMLHttpRequest();
|
|
|
|
const contentType = 'application/octet-stream';
|
|
|
|
xmlhttp.onreadystatechange = function() {
|
|
|
|
if (xmlhttp.readyState === XMLHttpRequest.DONE) {
|
|
|
|
console.log("Status: " + xmlhttp.status);
|
|
|
|
if (xmlhttp.status === 200 || xmlhttp.status === 201) {
|
|
|
|
console.log('file successful uploaded');
|
|
|
|
if (success_cb) {
|
|
|
|
success_cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('error while uploading file to ' + url);
|
|
|
|
alert(__('Could not upload File please try again.'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xmlhttp.open('PUT', url, true);
|
|
|
|
xmlhttp.setRequestHeader("Content-type", contentType);
|
|
|
|
xmlhttp.send(file);
|
|
|
|
|
|
|
|
console.log("uploadFile end");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process successful response to slot request.
|
|
|
|
*/
|
|
|
|
successfulRequestSlotCB (stanza, cb) {
|
|
|
|
const slot = stanza.getElementsByTagName('slot')[0];
|
|
|
|
|
|
|
|
if (slot != undefined) {
|
|
|
|
var put = slot.getElementsByTagName('put')[0].textContent;
|
|
|
|
var get = slot.getElementsByTagName('get')[0].textContent;
|
|
|
|
cb({
|
|
|
|
put: put,
|
|
|
|
get: get
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.failedRequestSlotCB(stanza, cb);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process failed response to slot request.
|
|
|
|
*/
|
|
|
|
failedRequestSlotCB (stanza, cb) {
|
|
|
|
alert(__('Could not upload File please try again.'));
|
|
|
|
},
|
|
|
|
|
2017-12-06 22:09:46 +01:00
|
|
|
getMessageBody (message) {
|
|
|
|
const type = message.getAttribute('type');
|
|
|
|
return (type === 'error') ?
|
|
|
|
_.propertyOf(message.querySelector('error text'))('textContent') :
|
|
|
|
_.propertyOf(message.querySelector('body'))('textContent');
|
|
|
|
},
|
|
|
|
|
|
|
|
getMessageAttributes (message, delay, original_stanza) {
|
2018-02-02 21:53:53 +01:00
|
|
|
/* Parses a passed in message stanza and returns an object
|
|
|
|
* of attributes.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) message - The message stanza
|
|
|
|
* (XMLElement) delay - The <delay> node from the
|
|
|
|
* stanza, if there was one.
|
|
|
|
* (XMLElement) original_stanza - The original stanza,
|
|
|
|
* that contains the message stanza, if it was
|
|
|
|
* contained, otherwise it's the message stanza itself.
|
|
|
|
*/
|
2018-02-04 18:55:38 +01:00
|
|
|
const { _converse } = this.__super__,
|
|
|
|
{ __ } = _converse;
|
|
|
|
|
2017-12-06 22:09:46 +01:00
|
|
|
delay = delay || message.querySelector('delay');
|
|
|
|
const type = message.getAttribute('type'),
|
|
|
|
body = this.getMessageBody(message);
|
|
|
|
|
|
|
|
const delayed = !_.isNull(delay),
|
|
|
|
is_groupchat = type === 'groupchat',
|
|
|
|
chat_state = message.getElementsByTagName(_converse.COMPOSING).length && _converse.COMPOSING ||
|
|
|
|
message.getElementsByTagName(_converse.PAUSED).length && _converse.PAUSED ||
|
|
|
|
message.getElementsByTagName(_converse.INACTIVE).length && _converse.INACTIVE ||
|
|
|
|
message.getElementsByTagName(_converse.ACTIVE).length && _converse.ACTIVE ||
|
|
|
|
message.getElementsByTagName(_converse.GONE).length && _converse.GONE;
|
|
|
|
|
|
|
|
let from;
|
|
|
|
if (is_groupchat) {
|
|
|
|
from = Strophe.unescapeNode(Strophe.getResourceFromJid(message.getAttribute('from')));
|
|
|
|
} else {
|
|
|
|
from = Strophe.getBareJidFromJid(message.getAttribute('from'));
|
|
|
|
}
|
|
|
|
const time = delayed ? delay.getAttribute('stamp') : moment().format();
|
|
|
|
let sender, fullname;
|
|
|
|
if ((is_groupchat && from === this.get('nick')) || (!is_groupchat && from === _converse.bare_jid)) {
|
|
|
|
sender = 'me';
|
|
|
|
fullname = _converse.xmppstatus.get('fullname') || from;
|
|
|
|
} else {
|
|
|
|
sender = 'them';
|
|
|
|
fullname = this.get('fullname') || from;
|
|
|
|
}
|
2018-02-04 18:55:38 +01:00
|
|
|
const spoiler = message.querySelector(`spoiler[xmlns="${Strophe.NS.SPOILER}"]`);
|
|
|
|
const attrs = {
|
2017-12-06 22:09:46 +01:00
|
|
|
'type': type,
|
2018-04-06 13:56:14 +02:00
|
|
|
'from': from,
|
2017-12-06 22:09:46 +01:00
|
|
|
'chat_state': chat_state,
|
|
|
|
'delayed': delayed,
|
|
|
|
'fullname': fullname,
|
|
|
|
'message': body || undefined,
|
|
|
|
'msgid': message.getAttribute('id'),
|
|
|
|
'sender': sender,
|
2018-02-04 18:55:38 +01:00
|
|
|
'time': time,
|
|
|
|
'is_spoiler': !_.isNull(spoiler)
|
2017-12-06 22:09:46 +01:00
|
|
|
};
|
2018-02-04 18:55:38 +01:00
|
|
|
if (spoiler) {
|
2018-02-06 21:21:21 +01:00
|
|
|
attrs.spoiler_hint = spoiler.textContent.length > 0 ? spoiler.textContent : '';
|
2018-02-04 18:55:38 +01:00
|
|
|
}
|
|
|
|
return attrs;
|
2017-12-06 22:09:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
createMessage (message, delay, original_stanza) {
|
2018-02-02 21:53:53 +01:00
|
|
|
/* Create a Backbone.Message object inside this chat box
|
|
|
|
* based on the identified message stanza.
|
|
|
|
*/
|
2017-12-06 22:09:46 +01:00
|
|
|
return this.messages.create(this.getMessageAttributes.apply(this, arguments));
|
|
|
|
},
|
|
|
|
|
|
|
|
newMessageWillBeHidden () {
|
|
|
|
/* Returns a boolean to indicate whether a newly received
|
2018-02-02 21:53:53 +01:00
|
|
|
* message will be visible to the user or not.
|
|
|
|
*/
|
2017-12-06 22:09:46 +01:00
|
|
|
return this.get('hidden') ||
|
|
|
|
this.get('minimized') ||
|
|
|
|
this.isScrolledUp() ||
|
|
|
|
_converse.windowState === 'hidden';
|
|
|
|
},
|
|
|
|
|
|
|
|
incrementUnreadMsgCounter (stanza) {
|
|
|
|
/* Given a newly received message, update the unread counter if
|
2018-02-14 13:44:17 +01:00
|
|
|
* necessary.
|
|
|
|
*/
|
2017-12-06 22:09:46 +01:00
|
|
|
if (_.isNull(stanza.querySelector('body'))) {
|
|
|
|
return; // The message has no text
|
|
|
|
}
|
|
|
|
if (utils.isNewMessage(stanza) && this.newMessageWillBeHidden()) {
|
|
|
|
this.save({'num_unread': this.get('num_unread') + 1});
|
|
|
|
_converse.incrementMsgCounter();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
clearUnreadMsgCounter() {
|
|
|
|
this.save({'num_unread': 0});
|
|
|
|
},
|
|
|
|
|
|
|
|
isScrolledUp () {
|
|
|
|
return this.get('scrolled', true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-08-16 11:16:22 +02:00
|
|
|
_converse.ChatBoxes = Backbone.Collection.extend({
|
|
|
|
comparator: 'time_opened',
|
|
|
|
|
|
|
|
model (attrs, options) {
|
|
|
|
return new _converse.ChatBox(attrs, options);
|
|
|
|
},
|
|
|
|
|
|
|
|
registerMessageHandler () {
|
|
|
|
_converse.connection.addHandler(
|
|
|
|
this.onMessage.bind(this), null, 'message', 'chat'
|
|
|
|
);
|
|
|
|
_converse.connection.addHandler(
|
|
|
|
this.onErrorMessage.bind(this), null, 'message', 'error'
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
chatBoxMayBeShown (chatbox) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
onChatBoxesFetched (collection) {
|
2018-02-21 22:40:51 +01:00
|
|
|
/* Show chat boxes upon receiving them from sessionStorage */
|
2017-08-16 11:16:22 +02:00
|
|
|
collection.each((chatbox) => {
|
|
|
|
if (this.chatBoxMayBeShown(chatbox)) {
|
|
|
|
chatbox.trigger('show');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_converse.emit('chatBoxesFetched');
|
|
|
|
},
|
|
|
|
|
|
|
|
onConnected () {
|
|
|
|
this.browserStorage = new Backbone.BrowserStorage[_converse.storage](
|
|
|
|
b64_sha1(`converse.chatboxes-${_converse.bare_jid}`));
|
|
|
|
this.registerMessageHandler();
|
|
|
|
this.fetch({
|
|
|
|
add: true,
|
|
|
|
success: this.onChatBoxesFetched.bind(this)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onErrorMessage (message) {
|
|
|
|
/* Handler method for all incoming error message stanzas
|
|
|
|
*/
|
|
|
|
// TODO: we can likely just reuse "onMessage" below
|
|
|
|
const from_jid = Strophe.getBareJidFromJid(message.getAttribute('from'));
|
|
|
|
if (utils.isSameBareJID(from_jid, _converse.bare_jid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Get chat box, but only create a new one when the message has a body.
|
|
|
|
const chatbox = this.getChatBox(from_jid);
|
|
|
|
if (!chatbox) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
chatbox.createMessage(message, null, message);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
onMessage (message) {
|
|
|
|
/* Handler method for all incoming single-user chat "message"
|
2018-02-02 21:53:53 +01:00
|
|
|
* stanzas.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) message - The incoming message stanza
|
|
|
|
*/
|
2017-08-16 11:16:22 +02:00
|
|
|
let contact_jid, delay, resource,
|
|
|
|
from_jid = message.getAttribute('from'),
|
|
|
|
to_jid = message.getAttribute('to');
|
|
|
|
|
|
|
|
const original_stanza = message,
|
|
|
|
to_resource = Strophe.getResourceFromJid(to_jid),
|
|
|
|
is_carbon = !_.isNull(message.querySelector(`received[xmlns="${Strophe.NS.CARBONS}"]`));
|
|
|
|
|
|
|
|
if (_converse.filter_by_resource && (to_resource && to_resource !== _converse.resource)) {
|
|
|
|
_converse.log(
|
|
|
|
`onMessage: Ignoring incoming message intended for a different resource: ${to_jid}`,
|
|
|
|
Strophe.LogLevel.INFO
|
|
|
|
);
|
|
|
|
return true;
|
2018-01-29 15:00:11 +01:00
|
|
|
} else if (utils.isHeadlineMessage(_converse, message)) {
|
2017-08-16 11:16:22 +02:00
|
|
|
// XXX: Ideally we wouldn't have to check for headline
|
|
|
|
// messages, but Prosody sends headline messages with the
|
|
|
|
// wrong type ('chat'), so we need to filter them out here.
|
|
|
|
_converse.log(
|
|
|
|
`onMessage: Ignoring incoming headline message sent with type 'chat' from JID: ${from_jid}`,
|
|
|
|
Strophe.LogLevel.INFO
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const forwarded = message.querySelector('forwarded');
|
|
|
|
if (!_.isNull(forwarded)) {
|
|
|
|
const forwarded_message = forwarded.querySelector('message');
|
|
|
|
const forwarded_from = forwarded_message.getAttribute('from');
|
|
|
|
if (is_carbon && Strophe.getBareJidFromJid(forwarded_from) !== from_jid) {
|
|
|
|
// Prevent message forging via carbons
|
|
|
|
//
|
|
|
|
// https://xmpp.org/extensions/xep-0280.html#security
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
message = forwarded_message;
|
|
|
|
delay = forwarded.querySelector('delay');
|
|
|
|
from_jid = message.getAttribute('from');
|
|
|
|
to_jid = message.getAttribute('to');
|
|
|
|
}
|
|
|
|
|
|
|
|
const from_bare_jid = Strophe.getBareJidFromJid(from_jid),
|
|
|
|
from_resource = Strophe.getResourceFromJid(from_jid),
|
|
|
|
is_me = from_bare_jid === _converse.bare_jid;
|
|
|
|
|
|
|
|
if (is_me) {
|
|
|
|
// I am the sender, so this must be a forwarded message...
|
|
|
|
contact_jid = Strophe.getBareJidFromJid(to_jid);
|
|
|
|
resource = Strophe.getResourceFromJid(to_jid);
|
|
|
|
} else {
|
|
|
|
contact_jid = from_bare_jid;
|
|
|
|
resource = from_resource;
|
|
|
|
}
|
|
|
|
// Get chat box, but only create a new one when the message has a body.
|
2018-03-14 14:02:08 +01:00
|
|
|
const attrs = {
|
|
|
|
'fullname': _.get(_converse.api.contacts.get(contact_jid), 'attributes.fullname')
|
|
|
|
}
|
|
|
|
const chatbox = this.getChatBox(contact_jid, attrs, !_.isNull(message.querySelector('body'))),
|
2018-02-21 22:40:51 +01:00
|
|
|
msgid = message.getAttribute('id');
|
2017-08-16 11:16:22 +02:00
|
|
|
|
|
|
|
if (chatbox) {
|
|
|
|
const messages = msgid && chatbox.messages.findWhere({msgid}) || [];
|
|
|
|
if (_.isEmpty(messages)) {
|
|
|
|
// Only create the message when we're sure it's not a
|
|
|
|
// duplicate
|
|
|
|
chatbox.incrementUnreadMsgCounter(original_stanza);
|
|
|
|
chatbox.createMessage(message, delay, original_stanza);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_converse.emit('message', {'stanza': original_stanza, 'chatbox': chatbox});
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
getChatBox (jid, attrs={}, create) {
|
2017-08-16 11:16:22 +02:00
|
|
|
/* Returns a chat box or optionally return a newly
|
2018-02-21 22:40:51 +01:00
|
|
|
* created one if one doesn't exist.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) jid - The JID of the user whose chat box we want
|
|
|
|
* (Boolean) create - Should a new chat box be created if none exists?
|
|
|
|
* (Object) attrs - Optional chat box atributes.
|
|
|
|
*/
|
|
|
|
if (_.isObject(jid)) {
|
|
|
|
create = attrs;
|
|
|
|
attrs = jid;
|
|
|
|
jid = attrs.jid;
|
|
|
|
}
|
2018-03-13 19:11:49 +01:00
|
|
|
jid = Strophe.getBareJidFromJid(jid.toLowerCase());
|
2018-03-11 13:56:53 +01:00
|
|
|
|
2017-08-16 11:16:22 +02:00
|
|
|
let chatbox = this.get(Strophe.getBareJidFromJid(jid));
|
|
|
|
if (!chatbox && create) {
|
2018-03-14 13:06:38 +01:00
|
|
|
_.extend(attrs, {'jid': jid, 'id': jid});
|
2018-02-21 22:40:51 +01:00
|
|
|
chatbox = this.create(attrs, {
|
|
|
|
'error' (model, response) {
|
|
|
|
_converse.log(response.responseText);
|
|
|
|
}
|
|
|
|
});
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
|
|
|
return chatbox;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_converse.ChatBoxViews = Backbone.Overview.extend({
|
|
|
|
|
|
|
|
_ensureElement () {
|
|
|
|
/* Override method from backbone.js
|
2018-02-21 22:29:21 +01:00
|
|
|
* If the #conversejs element doesn't exist, create it.
|
|
|
|
*/
|
2017-08-16 11:16:22 +02:00
|
|
|
if (!this.el) {
|
2018-02-09 16:02:56 +01:00
|
|
|
let el = _converse.root.querySelector('#conversejs');
|
2017-08-16 11:16:22 +02:00
|
|
|
if (_.isNull(el)) {
|
|
|
|
el = document.createElement('div');
|
|
|
|
el.setAttribute('id', 'conversejs');
|
2018-02-09 16:02:56 +01:00
|
|
|
const body = _converse.root.querySelector('body');
|
|
|
|
if (body) {
|
|
|
|
body.appendChild(el);
|
|
|
|
} else {
|
|
|
|
// Perhaps inside a web component?
|
|
|
|
_converse.root.appendChild(el);
|
2018-01-23 14:47:46 +01:00
|
|
|
}
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
2018-02-15 14:14:55 +01:00
|
|
|
if (_.includes(['mobile', 'fullscreen'], _converse.view_mode)) {
|
|
|
|
el.classList.add('fullscreen');
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
|
|
|
el.innerHTML = '';
|
|
|
|
this.setElement(el, false);
|
|
|
|
} else {
|
|
|
|
this.setElement(_.result(this, 'el'), false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-21 22:29:21 +01:00
|
|
|
initialize () {
|
|
|
|
this.model.on("add", this.onChatBoxAdded, this);
|
|
|
|
this.model.on("destroy", this.removeChat, this);
|
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
2018-02-21 22:40:51 +01:00
|
|
|
try {
|
|
|
|
this.el.innerHTML = tpl_chatboxes();
|
|
|
|
} catch (e) {
|
|
|
|
this._ensureElement();
|
|
|
|
this.el.innerHTML = tpl_chatboxes();
|
|
|
|
}
|
2018-02-21 22:29:21 +01:00
|
|
|
this.row_el = this.el.querySelector('.row');
|
|
|
|
},
|
|
|
|
|
2018-02-15 15:50:11 +01:00
|
|
|
insertRowColumn (el) {
|
|
|
|
/* Add a new DOM element (likely a chat box) into the
|
|
|
|
* the row managed by this overview.
|
|
|
|
*/
|
|
|
|
this.row_el.insertAdjacentElement('afterBegin', el);
|
|
|
|
},
|
|
|
|
|
2017-08-16 11:16:22 +02:00
|
|
|
onChatBoxAdded (item) {
|
|
|
|
// Views aren't created here, since the core code doesn't
|
|
|
|
// contain any views. Instead, they're created in overrides in
|
|
|
|
// plugins, such as in converse-chatview.js and converse-muc.js
|
|
|
|
return this.get(item.get('id'));
|
|
|
|
},
|
|
|
|
|
|
|
|
removeChat (item) {
|
|
|
|
this.remove(item.get('id'));
|
|
|
|
},
|
|
|
|
|
|
|
|
closeAllChatBoxes () {
|
|
|
|
/* This method gets overridden in src/converse-controlbox.js if
|
|
|
|
* the controlbox plugin is active.
|
|
|
|
*/
|
|
|
|
this.each(function (view) { view.close(); });
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
chatBoxMayBeShown (chatbox) {
|
|
|
|
return this.model.chatBoxMayBeShown(chatbox);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-21 22:29:21 +01:00
|
|
|
// TODO: move to converse-chatboxviews.js and use there in the API
|
|
|
|
_converse.getViewForChatBox = function (chatbox) {
|
|
|
|
if (!chatbox) { return; }
|
|
|
|
return _converse.chatboxviews.get(chatbox.get('id'));
|
|
|
|
};
|
|
|
|
|
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2017-08-16 12:31:17 +02:00
|
|
|
_converse.api.listen.on('pluginsInitialized', () => {
|
|
|
|
_converse.chatboxes = new _converse.ChatBoxes();
|
|
|
|
_converse.chatboxviews = new _converse.ChatBoxViews({
|
|
|
|
'model': _converse.chatboxes
|
|
|
|
});
|
|
|
|
_converse.emit('chatBoxesInitialized');
|
2017-08-16 11:16:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
_converse.api.listen.on('beforeTearDown', () => {
|
2017-08-16 14:16:24 +02:00
|
|
|
_converse.chatboxes.remove(); // Don't call off(), events won't get re-registered upon reconnect.
|
|
|
|
delete _converse.chatboxes.browserStorage;
|
2017-08-16 11:16:22 +02:00
|
|
|
});
|
2018-02-22 11:06:30 +01:00
|
|
|
|
|
|
|
_converse.api.listen.on('statusInitialized', () => _converse.chatboxes.onConnected());
|
2018-02-21 22:29:21 +01:00
|
|
|
/************************ END Event Handlers ************************/
|
2017-08-16 11:16:22 +02:00
|
|
|
|
|
|
|
|
2018-02-21 22:29:21 +01:00
|
|
|
/************************ BEGIN API ************************/
|
2017-08-16 11:16:22 +02:00
|
|
|
_.extend(_converse.api, {
|
|
|
|
'chats': {
|
2018-02-21 22:29:21 +01:00
|
|
|
'create' (jids, attrs) {
|
2017-08-16 11:16:22 +02:00
|
|
|
if (_.isUndefined(jids)) {
|
2018-03-14 13:06:38 +01:00
|
|
|
_converse.log(
|
|
|
|
"chats.create: You need to provide at least one JID",
|
|
|
|
Strophe.LogLevel.ERROR
|
|
|
|
);
|
2017-08-16 11:16:22 +02:00
|
|
|
return null;
|
2018-03-14 13:06:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isString(jids)) {
|
2018-03-14 19:25:19 +01:00
|
|
|
if (attrs && !_.get(attrs, 'fullname')) {
|
2018-03-14 13:06:38 +01:00
|
|
|
attrs.fullname = _.get(_converse.api.contacts.get(jids), 'attributes.fullname');
|
|
|
|
}
|
2018-02-21 22:29:21 +01:00
|
|
|
const chatbox = _converse.chatboxes.getChatBox(jids, attrs, true);
|
2017-10-31 23:04:46 +01:00
|
|
|
if (_.isNil(chatbox)) {
|
2018-02-21 22:29:21 +01:00
|
|
|
_converse.log("Could not open chatbox for JID: "+jids, Strophe.LogLevel.ERROR);
|
2017-10-31 23:04:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-02-21 22:29:21 +01:00
|
|
|
return chatbox;
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
2018-03-14 13:06:38 +01:00
|
|
|
return _.map(jids, (jid) => {
|
|
|
|
attrs.fullname = _.get(_converse.api.contacts.get(jid), 'attributes.fullname');
|
|
|
|
return _converse.chatboxes.getChatBox(jid, attrs, true).trigger('show');
|
|
|
|
});
|
2018-02-21 22:29:21 +01:00
|
|
|
},
|
|
|
|
'open' (jids, attrs) {
|
|
|
|
if (_.isUndefined(jids)) {
|
|
|
|
_converse.log("chats.open: You need to provide at least one JID", Strophe.LogLevel.ERROR);
|
|
|
|
return null;
|
|
|
|
} else if (_.isString(jids)) {
|
|
|
|
const chatbox = _converse.api.chats.create(jids, attrs);
|
|
|
|
chatbox.trigger('show');
|
|
|
|
return chatbox;
|
|
|
|
}
|
|
|
|
return _.map(jids, (jid) => _converse.api.chats.create(jid, attrs).trigger('show'));
|
2017-08-16 11:16:22 +02:00
|
|
|
},
|
|
|
|
'get' (jids) {
|
|
|
|
if (_.isUndefined(jids)) {
|
|
|
|
const result = [];
|
|
|
|
_converse.chatboxes.each(function (chatbox) {
|
|
|
|
// FIXME: Leaky abstraction from MUC. We need to add a
|
|
|
|
// base type for chat boxes, and check for that.
|
|
|
|
if (chatbox.get('type') !== 'chatroom') {
|
2018-02-21 22:29:21 +01:00
|
|
|
result.push(chatbox);
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
} else if (_.isString(jids)) {
|
2018-02-21 22:29:21 +01:00
|
|
|
return _converse.chatboxes.getChatBox(jids);
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
2018-02-21 22:29:21 +01:00
|
|
|
return _.map(jids, _.partial(_converse.chatboxes.getChatBox.bind(_converse.chatboxes), _, {}, true));
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-02-21 22:40:51 +01:00
|
|
|
/************************ END API ************************/
|
2017-08-16 11:16:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return converse;
|
|
|
|
}));
|