2016-03-09 09:14:09 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
2019-03-04 17:47:45 +01:00
|
|
|
// https://conversejs.org
|
2016-03-09 09:14:09 +01:00
|
|
|
//
|
2019-03-28 10:47:14 +01:00
|
|
|
// Copyright (c) 2019, Jan-Carel Brand <jc@opkode.com>
|
2016-03-09 09:14:09 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
2017-12-06 22:10:21 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
import "converse-chatview";
|
|
|
|
import converse from "@converse/headless/converse-core";
|
|
|
|
import tpl_chatbox from "templates/chatbox.html";
|
|
|
|
|
2019-05-06 11:16:56 +02:00
|
|
|
const { _, dayjs, utils } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-headline', {
|
|
|
|
/* Plugin dependencies are other plugins which might be
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
* this plugin.
|
|
|
|
*
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
* an error will be raised if the plugin is not found. By default it's
|
|
|
|
* false, which means these plugins are only loaded opportunistically.
|
|
|
|
*
|
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
|
|
|
*/
|
|
|
|
dependencies: ["converse-chatview"],
|
|
|
|
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
// New functions which don't exist yet can also be added.
|
|
|
|
|
|
|
|
ChatBoxes: {
|
|
|
|
model (attrs, options) {
|
|
|
|
const { _converse } = this.__super__;
|
|
|
|
if (attrs.type == _converse.HEADLINES_TYPE) {
|
|
|
|
return new _converse.HeadlinesBox(attrs, options);
|
|
|
|
} else {
|
|
|
|
return this.__super__.model.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2017-12-06 22:10:21 +01:00
|
|
|
|
2016-03-20 00:03:00 +01:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
const { _converse } = this,
|
|
|
|
{ __ } = _converse;
|
|
|
|
|
|
|
|
_converse.HeadlinesBox = _converse.ChatBox.extend({
|
2019-04-17 11:52:41 +02:00
|
|
|
defaults () {
|
|
|
|
return {
|
|
|
|
'bookmarked': false,
|
|
|
|
'hidden': _.includes(['mobile', 'fullscreen'], _converse.view_mode),
|
|
|
|
'message_type': 'headline',
|
|
|
|
'num_unread': 0,
|
2019-05-05 19:05:45 +02:00
|
|
|
'time_opened': this.get('time_opened') || (new Date()).getTime(),
|
2019-04-17 11:52:41 +02:00
|
|
|
'type': _converse.HEADLINES_TYPE
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2019-04-17 11:52:41 +02:00
|
|
|
|
|
|
|
initialize () {
|
|
|
|
this.initMessages();
|
|
|
|
this.set({'box_id': `box-${btoa(this.get('jid'))}`});
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_converse.HeadlinesBoxView = _converse.ChatBoxView.extend({
|
|
|
|
className: 'chatbox headlines',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .close-chatbox-button': 'close',
|
|
|
|
'click .toggle-chatbox-button': 'minimize',
|
|
|
|
'keypress textarea.chat-textarea': 'keyPressed'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
this.initDebounced();
|
|
|
|
|
|
|
|
this.disable_mam = true; // Don't do MAM queries for this box
|
|
|
|
this.model.messages.on('add', this.onMessageAdded, this);
|
|
|
|
this.model.on('show', this.show, this);
|
|
|
|
this.model.on('destroy', this.hide, this);
|
|
|
|
this.model.on('change:minimized', this.onMinimizedChanged, this);
|
|
|
|
|
|
|
|
this.render().insertHeading().fetchMessages().insertIntoDOM().hide();
|
2019-03-29 21:10:45 +01:00
|
|
|
_converse.api.trigger('chatBoxOpened', this); // TODO: remove
|
|
|
|
_converse.api.trigger('chatBoxInitialized', this);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render () {
|
|
|
|
this.el.setAttribute('id', this.model.get('box_id'))
|
|
|
|
this.el.innerHTML = tpl_chatbox(
|
2019-04-29 09:07:15 +02:00
|
|
|
Object.assign(this.model.toJSON(), {
|
2018-10-23 03:41:38 +02:00
|
|
|
info_close: '',
|
|
|
|
label_personal_message: '',
|
|
|
|
show_send_button: false,
|
|
|
|
show_toolbar: false,
|
|
|
|
unread_msgs: ''
|
|
|
|
}
|
|
|
|
));
|
|
|
|
this.content = this.el.querySelector('.chat-content');
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Override to avoid the methods in converse-chatview.js
|
|
|
|
'renderMessageForm': _.noop,
|
|
|
|
'afterShown': _.noop
|
|
|
|
});
|
|
|
|
|
2019-01-25 11:53:07 +01:00
|
|
|
async function onHeadlineMessage (message) {
|
2018-10-23 03:41:38 +02:00
|
|
|
/* Handler method for all incoming messages of type "headline". */
|
|
|
|
if (utils.isHeadlineMessage(_converse, message)) {
|
2019-01-25 11:53:07 +01:00
|
|
|
const from_jid = message.getAttribute('from');
|
2018-10-23 03:41:38 +02:00
|
|
|
if (_.includes(from_jid, '@') &&
|
2019-04-17 11:52:41 +02:00
|
|
|
!_converse.roster.get(from_jid) &&
|
2018-10-23 03:41:38 +02:00
|
|
|
!_converse.allow_non_roster_messaging) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_.isNull(message.querySelector('body'))) {
|
|
|
|
// Avoid creating a chat box if we have nothing to show
|
|
|
|
// inside it.
|
|
|
|
return;
|
2016-12-20 11:42:20 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
const chatbox = _converse.chatboxes.create({
|
|
|
|
'id': from_jid,
|
|
|
|
'jid': from_jid,
|
|
|
|
'type': _converse.HEADLINES_TYPE,
|
|
|
|
'from': from_jid
|
|
|
|
});
|
2019-01-25 11:53:07 +01:00
|
|
|
const attrs = await chatbox.getMessageAttributesFromStanza(message, message);
|
|
|
|
await chatbox.messages.create(attrs);
|
2019-03-29 21:10:45 +01:00
|
|
|
_converse.api.trigger('message', {'chatbox': chatbox, 'stanza': message});
|
2017-06-12 20:30:58 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function registerHeadlineHandler () {
|
2019-01-25 11:53:07 +01:00
|
|
|
_converse.connection.addHandler(message => {
|
|
|
|
onHeadlineMessage(message);
|
|
|
|
return true
|
|
|
|
}, null, 'message');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2019-03-29 14:15:03 +01:00
|
|
|
_converse.api.listen.on('connected', registerHeadlineHandler);
|
|
|
|
_converse.api.listen.on('reconnected', registerHeadlineHandler);
|
2018-08-28 14:58:33 +02:00
|
|
|
|
|
|
|
|
2019-03-29 14:15:03 +01:00
|
|
|
_converse.api.listen.on('chatBoxViewsInitialized', () => {
|
2019-04-17 11:52:41 +02:00
|
|
|
const views = _converse.chatboxviews;
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.chatboxes.on('add', item => {
|
2019-04-17 11:52:41 +02:00
|
|
|
if (!views.get(item.get('id')) && item.get('type') === _converse.HEADLINES_TYPE) {
|
|
|
|
views.add(item.get('id'), new _converse.HeadlinesBoxView({model: item}));
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2018-08-28 14:58:33 +02:00
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|