2020-01-26 16:21:20 +01:00
|
|
|
|
/**
|
|
|
|
|
* @module converse-chat
|
|
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
|
|
*/
|
2019-11-01 16:04:55 +01:00
|
|
|
|
import filesize from "filesize";
|
2019-11-06 11:01:34 +01:00
|
|
|
|
import log from "./log";
|
2020-04-13 15:20:51 +02:00
|
|
|
|
import st from "./utils/stanza";
|
2020-06-08 16:08:50 +02:00
|
|
|
|
import { Collection } from "@converse/skeletor/src/collection";
|
|
|
|
|
import { Model } from '@converse/skeletor/src/model.js';
|
2020-05-18 10:54:37 +02:00
|
|
|
|
import { _converse, api, converse } from "./converse-core";
|
2020-10-01 11:13:13 +02:00
|
|
|
|
import { find, isMatch, isObject, pick } from "lodash-es";
|
2019-11-01 16:04:55 +01:00
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
|
const { $msg, Strophe, sizzle, utils } = converse.env;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-chat', {
|
|
|
|
|
/* Optional dependencies are other plugins which might be
|
|
|
|
|
* overridden or relied upon, and therefore need to be loaded before
|
|
|
|
|
* this plugin. They are called "optional" because they might not be
|
|
|
|
|
* available, in which case any overrides applicable to them will be
|
|
|
|
|
* ignored.
|
|
|
|
|
*
|
|
|
|
|
* It's possible however to make optional dependencies non-optional.
|
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
|
|
|
|
* an error will be raised if the plugin is not found.
|
|
|
|
|
*
|
|
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
|
|
|
|
*/
|
2019-09-24 15:33:41 +02:00
|
|
|
|
dependencies: ["converse-chatboxes", "converse-disco"],
|
2019-11-01 16:04:55 +01:00
|
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
|
*/
|
|
|
|
|
const { __ } = _converse;
|
|
|
|
|
|
|
|
|
|
// Configuration values for this plugin
|
|
|
|
|
// ====================================
|
|
|
|
|
// Refer to docs/source/configuration.rst for explanations of these
|
|
|
|
|
// configuration settings.
|
2020-06-03 09:17:13 +02:00
|
|
|
|
api.settings.extend({
|
2020-03-30 16:29:09 +02:00
|
|
|
|
'allow_message_corrections': 'all',
|
|
|
|
|
'allow_message_retraction': 'all',
|
2020-10-16 09:02:12 +02:00
|
|
|
|
'allow_message_styling': true,
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'auto_join_private_chats': [],
|
|
|
|
|
'clear_messages_on_reconnection': false,
|
|
|
|
|
'filter_by_resource': false,
|
|
|
|
|
'send_chat_state_notifications': true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
|
const ModelWithContact = Model.extend({
|
2019-11-01 16:04:55 +01:00
|
|
|
|
|
|
|
|
|
initialize () {
|
|
|
|
|
this.rosterContactAdded = u.getResolveablePromise();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async setRosterContact (jid) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const contact = await api.contacts.get(jid);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (contact) {
|
|
|
|
|
this.contact = contact;
|
|
|
|
|
this.set('nickname', contact.get('nickname'));
|
|
|
|
|
this.rosterContactAdded.resolve();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a non-MUC message. These can be either `chat` messages or
|
|
|
|
|
* `headline` messages.
|
|
|
|
|
* @class
|
|
|
|
|
* @namespace _converse.Message
|
|
|
|
|
* @memberOf _converse
|
|
|
|
|
* @example const msg = new _converse.Message({'message': 'hello world!'});
|
|
|
|
|
*/
|
|
|
|
|
_converse.Message = ModelWithContact.extend({
|
|
|
|
|
|
|
|
|
|
defaults () {
|
|
|
|
|
return {
|
|
|
|
|
'msgid': u.getUniqueId(),
|
|
|
|
|
'time': (new Date()).toISOString(),
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': false
|
2019-11-01 16:04:55 +01:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async initialize () {
|
2020-02-27 15:09:54 +01:00
|
|
|
|
if (!this.checkValidity()) { return; }
|
2019-11-01 16:04:55 +01:00
|
|
|
|
this.initialized = u.getResolveablePromise();
|
|
|
|
|
if (this.get('type') === 'chat') {
|
|
|
|
|
ModelWithContact.prototype.initialize.apply(this, arguments);
|
|
|
|
|
this.setRosterContact(Strophe.getBareJidFromJid(this.get('from')));
|
|
|
|
|
}
|
|
|
|
|
if (this.get('file')) {
|
|
|
|
|
this.on('change:put', this.uploadFile, this);
|
|
|
|
|
}
|
2019-09-24 15:33:41 +02:00
|
|
|
|
this.setTimerForEphemeralMessage();
|
2019-12-18 12:42:40 +01:00
|
|
|
|
/**
|
|
|
|
|
* Triggered once a {@link _converse.Message} has been created and initialized.
|
|
|
|
|
* @event _converse#messageInitialized
|
|
|
|
|
* @type { _converse.Message}
|
|
|
|
|
* @example _converse.api.listen.on('messageInitialized', model => { ... });
|
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
await api.trigger('messageInitialized', this, {'Synchronous': true});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
this.initialized.resolve();
|
|
|
|
|
},
|
|
|
|
|
|
2019-09-24 15:33:41 +02:00
|
|
|
|
/**
|
|
|
|
|
* Sets an auto-destruct timer for this message, if it's is_ephemeral.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.Message#setTimerForEphemeralMessage
|
|
|
|
|
* @returns { Boolean } - Indicates whether the message is
|
|
|
|
|
* ephemeral or not, and therefore whether the timer was set or not.
|
|
|
|
|
*/
|
|
|
|
|
setTimerForEphemeralMessage () {
|
|
|
|
|
const setTimer = () => {
|
|
|
|
|
this.ephemeral_timer = window.setTimeout(this.safeDestroy.bind(this), 10000);
|
|
|
|
|
}
|
|
|
|
|
if (this.isEphemeral()) {
|
|
|
|
|
setTimer();
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
this.on('change:is_ephemeral',
|
|
|
|
|
() => this.isEphemeral() ? setTimer() : clearTimeout(this.ephemeral_timer)
|
|
|
|
|
);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-27 15:09:54 +01:00
|
|
|
|
checkValidity () {
|
|
|
|
|
if (Object.keys(this.attributes).length === 3) {
|
|
|
|
|
// XXX: This is an empty message with only the 3 default values.
|
|
|
|
|
// This seems to happen when saving a newly created message
|
|
|
|
|
// fails for some reason.
|
|
|
|
|
// TODO: This is likely fixable by setting `wait` when
|
|
|
|
|
// creating messages. See the wait-for-messages branch.
|
|
|
|
|
this.validationError = "Empty message";
|
|
|
|
|
this.safeDestroy();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
|
2020-03-26 10:35:08 +01:00
|
|
|
|
/**
|
|
|
|
|
* Determines whether this messsage may be retracted by the current user.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.Messages#mayBeRetracted
|
|
|
|
|
* @returns { Boolean }
|
|
|
|
|
*/
|
|
|
|
|
mayBeRetracted () {
|
|
|
|
|
const is_own_message = this.get('sender') === 'me';
|
2020-03-31 13:15:57 +02:00
|
|
|
|
return is_own_message && ['all', 'own'].includes(api.settings.get('allow_message_retraction'));
|
2020-03-26 10:35:08 +01:00
|
|
|
|
},
|
|
|
|
|
|
2019-11-01 16:04:55 +01:00
|
|
|
|
safeDestroy () {
|
|
|
|
|
try {
|
|
|
|
|
this.destroy()
|
|
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error(e);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isEphemeral () {
|
2020-03-18 19:32:03 +01:00
|
|
|
|
return this.get('is_ephemeral');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDisplayName () {
|
|
|
|
|
if (this.get('type') === 'groupchat') {
|
|
|
|
|
return this.get('nick');
|
|
|
|
|
} else if (this.contact) {
|
|
|
|
|
return this.contact.getDisplayName();
|
|
|
|
|
} else if (this.vcard) {
|
|
|
|
|
return this.vcard.getDisplayName();
|
|
|
|
|
} else {
|
|
|
|
|
return this.get('from');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getMessageText () {
|
|
|
|
|
if (this.get('is_encrypted')) {
|
2020-09-18 13:38:39 +02:00
|
|
|
|
return this.get('plaintext') || this.get('body') || __('Undecryptable OMEMO message');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
return this.get('message');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isMeCommand () {
|
|
|
|
|
const text = this.getMessageText();
|
|
|
|
|
if (!text) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return text.startsWith('/me ');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
sendSlotRequestStanza () {
|
|
|
|
|
/* Send out an IQ stanza to request a file upload slot.
|
|
|
|
|
*
|
|
|
|
|
* https://xmpp.org/extensions/xep-0363.html#request
|
|
|
|
|
*/
|
|
|
|
|
if (!this.file) {
|
|
|
|
|
return Promise.reject(new Error("file is undefined"));
|
|
|
|
|
}
|
|
|
|
|
const iq = converse.env.$iq({
|
|
|
|
|
'from': _converse.jid,
|
|
|
|
|
'to': this.get('slot_request_url'),
|
|
|
|
|
'type': 'get'
|
|
|
|
|
}).c('request', {
|
|
|
|
|
'xmlns': Strophe.NS.HTTPUPLOAD,
|
|
|
|
|
'filename': this.file.name,
|
|
|
|
|
'size': this.file.size,
|
|
|
|
|
'content-type': this.file.type
|
|
|
|
|
})
|
2020-03-31 13:15:57 +02:00
|
|
|
|
return api.sendIQ(iq);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async getRequestSlotURL () {
|
|
|
|
|
let stanza;
|
|
|
|
|
try {
|
|
|
|
|
stanza = await this.sendSlotRequestStanza();
|
|
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error(e);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return this.save({
|
|
|
|
|
'type': 'error',
|
|
|
|
|
'message': __("Sorry, could not determine upload URL."),
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': true
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const slot = stanza.querySelector('slot');
|
|
|
|
|
if (slot) {
|
|
|
|
|
this.save({
|
|
|
|
|
'get': slot.querySelector('get').getAttribute('url'),
|
|
|
|
|
'put': slot.querySelector('put').getAttribute('url'),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return this.save({
|
|
|
|
|
'type': 'error',
|
|
|
|
|
'message': __("Sorry, could not determine file upload URL."),
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': true
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
uploadFile () {
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.onreadystatechange = () => {
|
|
|
|
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.info("Status: " + xhr.status);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (xhr.status === 200 || xhr.status === 201) {
|
|
|
|
|
this.save({
|
|
|
|
|
'upload': _converse.SUCCESS,
|
|
|
|
|
'oob_url': this.get('get'),
|
|
|
|
|
'message': this.get('get')
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
xhr.onerror();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.upload.addEventListener("progress", (evt) => {
|
|
|
|
|
if (evt.lengthComputable) {
|
|
|
|
|
this.set('progress', evt.loaded / evt.total);
|
|
|
|
|
}
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
xhr.onerror = () => {
|
|
|
|
|
let message;
|
|
|
|
|
if (xhr.responseText) {
|
|
|
|
|
message = __('Sorry, could not succesfully upload your file. Your server’s response: "%1$s"', xhr.responseText)
|
|
|
|
|
} else {
|
|
|
|
|
message = __('Sorry, could not succesfully upload your file.');
|
|
|
|
|
}
|
|
|
|
|
this.save({
|
|
|
|
|
'type': 'error',
|
|
|
|
|
'upload': _converse.FAILURE,
|
|
|
|
|
'message': message,
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': true
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
xhr.open('PUT', this.get('put'), true);
|
|
|
|
|
xhr.setRequestHeader("Content-type", this.file.type);
|
|
|
|
|
xhr.send(this.file);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
|
_converse.Messages = Collection.extend({
|
2019-11-01 16:04:55 +01:00
|
|
|
|
model: _converse.Message,
|
|
|
|
|
comparator: 'time'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents an open/ongoing chat conversation.
|
|
|
|
|
*
|
|
|
|
|
* @class
|
|
|
|
|
* @namespace _converse.ChatBox
|
|
|
|
|
* @memberOf _converse
|
|
|
|
|
*/
|
|
|
|
|
_converse.ChatBox = ModelWithContact.extend({
|
|
|
|
|
messagesCollection: _converse.Messages,
|
|
|
|
|
|
|
|
|
|
defaults () {
|
|
|
|
|
return {
|
|
|
|
|
'bookmarked': false,
|
|
|
|
|
'chat_state': undefined,
|
2020-10-01 13:43:42 +02:00
|
|
|
|
'hidden': _converse.isUniView() && !api.settings.get('singleton'),
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'message_type': 'chat',
|
|
|
|
|
'nickname': undefined,
|
|
|
|
|
'num_unread': 0,
|
|
|
|
|
'time_sent': (new Date(0)).toISOString(),
|
|
|
|
|
'time_opened': this.get('time_opened') || (new Date()).getTime(),
|
|
|
|
|
'type': _converse.PRIVATE_CHAT_TYPE,
|
|
|
|
|
'url': ''
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async initialize () {
|
|
|
|
|
this.initialized = u.getResolveablePromise();
|
|
|
|
|
ModelWithContact.prototype.initialize.apply(this, arguments);
|
|
|
|
|
|
|
|
|
|
const jid = this.get('jid');
|
|
|
|
|
if (!jid) {
|
|
|
|
|
// XXX: The `validate` method will prevent this model
|
|
|
|
|
// from being persisted if there's no jid, but that gets
|
|
|
|
|
// called after model instantiation, so we have to deal
|
|
|
|
|
// with invalid models here also.
|
|
|
|
|
// This happens when the controlbox is in browser storage,
|
|
|
|
|
// but we're in embedded mode.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-13 11:20:12 +02:00
|
|
|
|
this.set({'box_id': `box-${jid}`});
|
2020-03-31 22:43:55 +02:00
|
|
|
|
this.initNotifications();
|
2020-05-15 14:33:31 +02:00
|
|
|
|
this.initMessages();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
|
|
|
|
|
if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) {
|
|
|
|
|
this.presence = _converse.presences.findWhere({'jid': jid}) || _converse.presences.create({'jid': jid});
|
|
|
|
|
await this.setRosterContact(jid);
|
|
|
|
|
}
|
|
|
|
|
this.on('change:chat_state', this.sendChatState, this);
|
|
|
|
|
await this.fetchMessages();
|
2019-12-18 12:42:40 +01:00
|
|
|
|
/**
|
|
|
|
|
* Triggered once a {@link _converse.ChatBox} has been created and initialized.
|
|
|
|
|
* @event _converse#chatBoxInitialized
|
|
|
|
|
* @type { _converse.ChatBox}
|
|
|
|
|
* @example _converse.api.listen.on('chatBoxInitialized', model => { ... });
|
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
await api.trigger('chatBoxInitialized', this, {'Synchronous': true});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
this.initialized.resolve();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getMessagesCacheKey () {
|
|
|
|
|
return `converse.messages-${this.get('jid')}-${_converse.bare_jid}`;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
initMessages () {
|
|
|
|
|
this.messages = new this.messagesCollection();
|
2020-10-27 22:48:22 +01:00
|
|
|
|
this.messages.fetched = u.getResolveablePromise();
|
2020-10-28 13:02:12 +01:00
|
|
|
|
this.messages.fetched.then(() => {
|
|
|
|
|
/**
|
|
|
|
|
* Triggered whenever a `_converse.ChatBox` instance has fetched its messages from
|
|
|
|
|
* `sessionStorage` but **NOT** from the server.
|
|
|
|
|
* @event _converse#afterMessagesFetched
|
|
|
|
|
* @type {_converse.ChatBoxView | _converse.ChatRoomView}
|
|
|
|
|
* @example _converse.api.listen.on('afterMessagesFetched', view => { ... });
|
|
|
|
|
*/
|
|
|
|
|
api.trigger('afterMessagesFetched', this);
|
|
|
|
|
});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
this.messages.chatbox = this;
|
|
|
|
|
this.messages.browserStorage = _converse.createStore(this.getMessagesCacheKey());
|
|
|
|
|
this.listenTo(this.messages, 'change:upload', message => {
|
|
|
|
|
if (message.get('upload') === _converse.SUCCESS) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.send(this.createMessageStanza(message));
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-03-31 22:43:55 +02:00
|
|
|
|
initNotifications () {
|
|
|
|
|
this.notifications = new Model();
|
2020-03-18 19:32:03 +01:00
|
|
|
|
},
|
|
|
|
|
|
2019-11-01 16:04:55 +01:00
|
|
|
|
afterMessagesFetched () {
|
|
|
|
|
/**
|
|
|
|
|
* Triggered whenever a `_converse.ChatBox` instance has fetched its messages from
|
|
|
|
|
* `sessionStorage` but **NOT** from the server.
|
|
|
|
|
* @event _converse#afterMessagesFetched
|
|
|
|
|
* @type {_converse.ChatBox | _converse.ChatRoom}
|
|
|
|
|
* @example _converse.api.listen.on('afterMessagesFetched', view => { ... });
|
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.trigger('afterMessagesFetched', this);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
fetchMessages () {
|
2020-10-27 22:48:22 +01:00
|
|
|
|
if (this.messages.fetched_flag) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.info(`Not re-fetching messages for ${this.get('jid')}`);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-27 22:48:22 +01:00
|
|
|
|
this.messages.fetched_flag = true;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
const resolve = this.messages.fetched.resolve;
|
|
|
|
|
this.messages.fetch({
|
|
|
|
|
'add': true,
|
|
|
|
|
'success': () => { this.afterMessagesFetched(); resolve() },
|
|
|
|
|
'error': () => { this.afterMessagesFetched(); resolve() }
|
|
|
|
|
});
|
|
|
|
|
return this.messages.fetched;
|
|
|
|
|
},
|
|
|
|
|
|
2020-05-15 14:33:31 +02:00
|
|
|
|
async handleErrorMessageStanza (stanza) {
|
|
|
|
|
const attrs = await st.parseMessage(stanza, _converse);
|
|
|
|
|
if (!await this.shouldShowErrorMessage(attrs)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const message = this.getMessageReferencedByError(attrs);
|
|
|
|
|
if (message) {
|
|
|
|
|
const new_attrs = {
|
|
|
|
|
'error': attrs.error,
|
|
|
|
|
'error_condition': attrs.error_condition,
|
|
|
|
|
'error_text': attrs.error_text,
|
|
|
|
|
'error_type': attrs.error_type,
|
2020-06-25 10:22:44 +02:00
|
|
|
|
'editable': false,
|
2020-05-15 14:33:31 +02:00
|
|
|
|
};
|
|
|
|
|
if (attrs.msgid === message.get('retraction_id')) {
|
|
|
|
|
// The error message refers to a retraction
|
|
|
|
|
new_attrs.retraction_id = undefined;
|
|
|
|
|
if (!attrs.error) {
|
|
|
|
|
if (attrs.error_condition === 'forbidden') {
|
|
|
|
|
new_attrs.error = __("You're not allowed to retract your message.");
|
|
|
|
|
} else {
|
|
|
|
|
new_attrs.error = __('Sorry, an error occurred while trying to retract your message.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (!attrs.error) {
|
|
|
|
|
if (attrs.error_condition === 'forbidden') {
|
|
|
|
|
new_attrs.error = __("You're not allowed to send a message.");
|
|
|
|
|
} else {
|
|
|
|
|
new_attrs.error = __('Sorry, an error occurred while trying to send your message.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
message.save(new_attrs);
|
|
|
|
|
} else {
|
|
|
|
|
this.createMessage(attrs);
|
2020-04-24 17:33:32 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-03-02 16:59:18 +01:00
|
|
|
|
/**
|
|
|
|
|
* Queue an incoming `chat` message stanza for processing.
|
|
|
|
|
* @async
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatRoom#queueMessage
|
2020-07-13 15:45:37 +02:00
|
|
|
|
* @param { Promise<MessageAttributes> } attrs - A promise which resolves to the message attributes
|
2020-03-02 16:59:18 +01:00
|
|
|
|
*/
|
2020-04-24 17:33:32 +02:00
|
|
|
|
queueMessage (attrs) {
|
2020-10-27 22:48:22 +01:00
|
|
|
|
this.msg_chain = (this.msg_chain || this.messages.fetched)
|
|
|
|
|
.then(() => this.onMessage(attrs))
|
|
|
|
|
.catch(e => log.error(e));
|
2020-03-02 16:59:18 +01:00
|
|
|
|
return this.msg_chain;
|
|
|
|
|
},
|
|
|
|
|
|
2020-07-13 15:45:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* @async
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatRoom#onMessage
|
|
|
|
|
* @param { MessageAttributes } attrs_promse - A promise which resolves to the message attributes.
|
|
|
|
|
*/
|
2020-04-24 17:33:32 +02:00
|
|
|
|
async onMessage (attrs) {
|
|
|
|
|
attrs = await attrs;
|
|
|
|
|
if (u.isErrorObject(attrs)) {
|
|
|
|
|
attrs.stanza && log.error(attrs.stanza);
|
|
|
|
|
return log.error(attrs.message);
|
|
|
|
|
}
|
2020-02-07 12:15:23 +01:00
|
|
|
|
const message = this.getDuplicateMessage(attrs);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (message) {
|
2020-04-24 17:33:32 +02:00
|
|
|
|
this.updateMessage(message, attrs);
|
2019-09-24 15:33:41 +02:00
|
|
|
|
} else if (
|
2020-04-24 17:33:32 +02:00
|
|
|
|
!this.handleReceipt(attrs) &&
|
|
|
|
|
!this.handleChatMarker(attrs) &&
|
|
|
|
|
!(await this.handleRetraction(attrs))
|
2019-09-24 15:33:41 +02:00
|
|
|
|
) {
|
2020-04-24 17:33:32 +02:00
|
|
|
|
this.setEditable(attrs, attrs.time);
|
2020-03-18 19:32:03 +01:00
|
|
|
|
|
|
|
|
|
if (attrs['chat_state'] && attrs.sender === 'them') {
|
2020-03-31 22:43:55 +02:00
|
|
|
|
this.notifications.set('chat_state', attrs.chat_state);
|
2020-03-18 19:32:03 +01:00
|
|
|
|
}
|
2020-02-13 15:12:03 +01:00
|
|
|
|
if (u.shouldCreateMessage(attrs)) {
|
2020-03-02 16:59:18 +01:00
|
|
|
|
const msg = this.handleCorrection(attrs) || await this.createMessage(attrs);
|
2020-03-31 22:43:55 +02:00
|
|
|
|
this.notifications.set({'chat_state': null});
|
2020-08-18 21:05:36 +02:00
|
|
|
|
this.handleUnreadMessage(msg);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async clearMessages () {
|
|
|
|
|
try {
|
2019-09-19 16:54:55 +02:00
|
|
|
|
await this.messages.clearStore();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
} catch (e) {
|
|
|
|
|
this.messages.trigger('reset');
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error(e);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
} finally {
|
2020-07-14 22:41:26 +02:00
|
|
|
|
delete this.msg_chain;
|
2020-10-27 22:48:22 +01:00
|
|
|
|
delete this.messages.fetched_flag;
|
|
|
|
|
this.messages.fetched = u.getResolveablePromise();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async close () {
|
|
|
|
|
try {
|
|
|
|
|
await new Promise((success, reject) => {
|
|
|
|
|
return this.destroy({success, 'error': (m, e) => reject(e)})
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error(e);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
} finally {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
if (api.settings.get('clear_messages_on_reconnection')) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
await this.clearMessages();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
announceReconnection () {
|
|
|
|
|
/**
|
|
|
|
|
* Triggered whenever a `_converse.ChatBox` instance has reconnected after an outage
|
|
|
|
|
* @event _converse#onChatReconnected
|
|
|
|
|
* @type {_converse.ChatBox | _converse.ChatRoom}
|
|
|
|
|
* @example _converse.api.listen.on('onChatReconnected', chatbox => { ... });
|
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.trigger('chatReconnected', this);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
2019-11-14 15:16:24 +01:00
|
|
|
|
async onReconnection () {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
if (api.settings.get('clear_messages_on_reconnection')) {
|
2019-11-14 15:16:24 +01:00
|
|
|
|
await this.clearMessages();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
this.announceReconnection();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
validate (attrs) {
|
|
|
|
|
if (!attrs.jid) {
|
|
|
|
|
return 'Ignored ChatBox without JID';
|
|
|
|
|
}
|
|
|
|
|
const room_jids = _converse.auto_join_rooms.map(s => isObject(s) ? s.jid : s);
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const auto_join = api.settings.get('auto_join_private_chats').concat(room_jids);
|
2020-06-03 09:57:14 +02:00
|
|
|
|
if (api.settings.get("singleton") && !auto_join.includes(attrs.jid) && !api.settings.get('auto_join_on_invite')) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
const msg = `${attrs.jid} is not allowed because singleton is true and it's not being auto_joined`;
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn(msg);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDisplayName () {
|
|
|
|
|
if (this.contact) {
|
|
|
|
|
return this.contact.getDisplayName();
|
|
|
|
|
} else if (this.vcard) {
|
|
|
|
|
return this.vcard.getDisplayName();
|
|
|
|
|
} else {
|
|
|
|
|
return this.get('jid');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-03-02 16:59:18 +01:00
|
|
|
|
async createMessageFromError (error) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (error instanceof _converse.TimeoutError) {
|
2020-05-15 14:33:31 +02:00
|
|
|
|
const msg = await this.createMessage({
|
|
|
|
|
'type': 'error',
|
|
|
|
|
'message': error.message,
|
2020-06-11 16:42:48 +02:00
|
|
|
|
'retry_event_id': error.retry_event_id
|
2020-05-15 14:33:31 +02:00
|
|
|
|
});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
msg.error = error;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getOldestMessage () {
|
|
|
|
|
for (let i=0; i<this.messages.length; i++) {
|
|
|
|
|
const message = this.messages.at(i);
|
|
|
|
|
if (message.get('type') === this.get('message_type')) {
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getMostRecentMessage () {
|
|
|
|
|
for (let i=this.messages.length-1; i>=0; i--) {
|
|
|
|
|
const message = this.messages.at(i);
|
|
|
|
|
if (message.get('type') === this.get('message_type')) {
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-04-24 17:33:32 +02:00
|
|
|
|
getUpdatedMessageAttributes (message, attrs) {
|
|
|
|
|
// Filter the attrs object, restricting it to only the `is_archived` key.
|
|
|
|
|
return (({ is_archived }) => ({ is_archived }))(attrs)
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
2020-04-24 17:33:32 +02:00
|
|
|
|
updateMessage (message, attrs) {
|
|
|
|
|
const new_attrs = this.getUpdatedMessageAttributes(message, attrs);
|
2020-10-26 13:09:15 +01:00
|
|
|
|
new_attrs && message.save(new_attrs);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mutator for setting the chat state of this chat session.
|
|
|
|
|
* Handles clearing of any chat state notification timeouts and
|
|
|
|
|
* setting new ones if necessary.
|
|
|
|
|
* Timeouts are set when the state being set is COMPOSING or PAUSED.
|
|
|
|
|
* After the timeout, COMPOSING will become PAUSED and PAUSED will become INACTIVE.
|
|
|
|
|
* See XEP-0085 Chat State Notifications.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#setChatState
|
|
|
|
|
* @param { string } state - The chat state (consts ACTIVE, COMPOSING, PAUSED, INACTIVE, GONE)
|
|
|
|
|
*/
|
|
|
|
|
setChatState (state, options) {
|
|
|
|
|
if (this.chat_state_timeout !== undefined) {
|
|
|
|
|
window.clearTimeout(this.chat_state_timeout);
|
|
|
|
|
delete this.chat_state_timeout;
|
|
|
|
|
}
|
|
|
|
|
if (state === _converse.COMPOSING) {
|
|
|
|
|
this.chat_state_timeout = window.setTimeout(
|
|
|
|
|
this.setChatState.bind(this),
|
|
|
|
|
_converse.TIMEOUTS.PAUSED,
|
|
|
|
|
_converse.PAUSED
|
|
|
|
|
);
|
|
|
|
|
} else if (state === _converse.PAUSED) {
|
|
|
|
|
this.chat_state_timeout = window.setTimeout(
|
|
|
|
|
this.setChatState.bind(this),
|
|
|
|
|
_converse.TIMEOUTS.INACTIVE,
|
|
|
|
|
_converse.INACTIVE
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
this.set('chat_state', state, options);
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
|
2020-05-15 14:33:31 +02:00
|
|
|
|
/**
|
|
|
|
|
* Given an error `<message>` stanza's attributes, find the saved message model which is
|
|
|
|
|
* referenced by that error.
|
|
|
|
|
* @param { Object } attrs
|
|
|
|
|
*/
|
|
|
|
|
getMessageReferencedByError (attrs) {
|
|
|
|
|
const id = attrs.msgid;
|
|
|
|
|
return id && this.messages.models.find(m => [m.get('msgid'), m.get('retraction_id')].includes(id));
|
|
|
|
|
},
|
|
|
|
|
|
2019-11-01 16:04:55 +01:00
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#shouldShowErrorMessage
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2020-05-15 14:33:31 +02:00
|
|
|
|
shouldShowErrorMessage (attrs) {
|
|
|
|
|
const msg = this.getMessageReferencedByError(attrs);
|
2020-10-08 12:13:04 +02:00
|
|
|
|
if (!msg && !attrs.body) {
|
2020-05-15 14:33:31 +02:00
|
|
|
|
// If the error refers to a message not included in our store,
|
|
|
|
|
// and it doesn't have a <body> tag, we assume that this was a
|
|
|
|
|
// CSI message (which we don't store).
|
|
|
|
|
// See https://github.com/conversejs/converse.js/issues/1317
|
|
|
|
|
return;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
// Gets overridden in ChatRoom
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
|
2019-09-24 15:33:41 +02:00
|
|
|
|
isSameUser (jid1, jid2) {
|
|
|
|
|
return u.isSameBareJID(jid1, jid2);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Looks whether we already have a retraction for this
|
|
|
|
|
* incoming message. If so, it's considered "dangling" because it
|
|
|
|
|
* probably hasn't been applied to anything yet, given that the
|
|
|
|
|
* relevant message is only coming in now.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#findDanglingRetraction
|
|
|
|
|
* @param { object } attrs - Attributes representing a received
|
2020-04-13 15:20:51 +02:00
|
|
|
|
* message, as returned by {@link st.parseMessage}
|
2019-09-24 15:33:41 +02:00
|
|
|
|
* @returns { _converse.Message }
|
|
|
|
|
*/
|
|
|
|
|
findDanglingRetraction (attrs) {
|
|
|
|
|
if (!attrs.origin_id || !this.messages.length) {
|
|
|
|
|
return null;
|
2019-11-05 14:40:05 +01:00
|
|
|
|
}
|
2019-09-24 15:33:41 +02:00
|
|
|
|
// Only look for dangling retractions if there are newer
|
|
|
|
|
// messages than this one, since retractions come after.
|
|
|
|
|
if (this.messages.last().get('time') > attrs.time) {
|
|
|
|
|
// Search from latest backwards
|
|
|
|
|
const messages = Array.from(this.messages.models);
|
|
|
|
|
messages.reverse();
|
|
|
|
|
return messages.find(
|
|
|
|
|
({attributes}) =>
|
|
|
|
|
attributes.retracted_id === attrs.origin_id &&
|
|
|
|
|
attributes.from === attrs.from &&
|
|
|
|
|
!attributes.moderated_by
|
|
|
|
|
);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-24 15:33:41 +02:00
|
|
|
|
* Handles message retraction based on the passed in attributes.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#handleRetraction
|
|
|
|
|
* @param { object } attrs - Attributes representing a received
|
2020-04-13 15:20:51 +02:00
|
|
|
|
* message, as returned by {@link st.parseMessage}
|
2019-09-24 15:33:41 +02:00
|
|
|
|
* @returns { Boolean } Returns `true` or `false` depending on
|
|
|
|
|
* whether a message was retracted or not.
|
|
|
|
|
*/
|
2020-03-02 16:59:18 +01:00
|
|
|
|
async handleRetraction (attrs) {
|
2019-11-26 11:46:02 +01:00
|
|
|
|
const RETRACTION_ATTRIBUTES = ['retracted', 'retracted_id', 'editable'];
|
2019-09-24 15:33:41 +02:00
|
|
|
|
if (attrs.retracted) {
|
|
|
|
|
if (attrs.is_tombstone) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const message = this.messages.findWhere({'origin_id': attrs.retracted_id, 'from': attrs.from});
|
|
|
|
|
if (!message) {
|
|
|
|
|
attrs['dangling_retraction'] = true;
|
2020-03-02 16:59:18 +01:00
|
|
|
|
await this.createMessage(attrs);
|
2019-09-24 15:33:41 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
message.save(pick(attrs, RETRACTION_ATTRIBUTES));
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// Check if we have dangling retraction
|
|
|
|
|
const message = this.findDanglingRetraction(attrs);
|
|
|
|
|
if (message) {
|
|
|
|
|
const retraction_attrs = pick(message.attributes, RETRACTION_ATTRIBUTES);
|
|
|
|
|
const new_attrs = Object.assign({'dangling_retraction': false}, attrs, retraction_attrs);
|
|
|
|
|
delete new_attrs['id']; // Delete id, otherwise a new cache entry gets created
|
|
|
|
|
message.save(new_attrs);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines whether the passed in message attributes represent a
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* message which corrects a previously received message, or an
|
|
|
|
|
* older message which has already been corrected.
|
|
|
|
|
* In both cases, update the corrected message accordingly.
|
|
|
|
|
* @private
|
2019-09-24 15:33:41 +02:00
|
|
|
|
* @method _converse.ChatBox#handleCorrection
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* @param { object } attrs - Attributes representing a received
|
2020-04-13 15:20:51 +02:00
|
|
|
|
* message, as returned by {@link st.parseMessage}
|
2019-09-24 15:33:41 +02:00
|
|
|
|
* @returns { _converse.Message|undefined } Returns the corrected
|
|
|
|
|
* message or `undefined` if not applicable.
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*/
|
2019-09-24 15:33:41 +02:00
|
|
|
|
handleCorrection (attrs) {
|
2020-04-24 17:33:32 +02:00
|
|
|
|
if (!attrs.replace_id || !attrs.from) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
|
const message = this.messages.findWhere({'msgid': attrs.replace_id, 'from': attrs.from});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (!message) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const older_versions = message.get('older_versions') || {};
|
|
|
|
|
if ((attrs.time < message.get('time')) && message.get('edited')) {
|
|
|
|
|
// This is an older message which has been corrected afterwards
|
|
|
|
|
older_versions[attrs.time] = attrs['message'];
|
|
|
|
|
message.save({'older_versions': older_versions});
|
|
|
|
|
} else {
|
|
|
|
|
// This is a correction of an earlier message we already received
|
2020-10-23 15:07:56 +02:00
|
|
|
|
if(Object.keys(older_versions).length) {
|
|
|
|
|
older_versions[message.get('edited')] = message.get('message');
|
|
|
|
|
}else {
|
|
|
|
|
older_versions[message.get('time')] = message.get('message');
|
|
|
|
|
}
|
2019-11-01 16:04:55 +01:00
|
|
|
|
attrs = Object.assign(attrs, {'older_versions': older_versions});
|
|
|
|
|
delete attrs['id']; // Delete id, otherwise a new cache entry gets created
|
2020-10-23 15:07:56 +02:00
|
|
|
|
attrs['time'] = message.get('time');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
message.save(attrs);
|
|
|
|
|
}
|
|
|
|
|
return message;
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-07 12:15:23 +01:00
|
|
|
|
/**
|
|
|
|
|
* Returns an already cached message (if it exists) based on the
|
|
|
|
|
* passed in attributes map.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#getDuplicateMessage
|
|
|
|
|
* @param { object } attrs - Attributes representing a received
|
2020-04-13 15:20:51 +02:00
|
|
|
|
* message, as returned by {@link st.parseMessage}
|
2020-02-07 12:15:23 +01:00
|
|
|
|
* @returns {Promise<_converse.Message>}
|
|
|
|
|
*/
|
2020-02-02 17:20:05 +01:00
|
|
|
|
getDuplicateMessage (attrs) {
|
2020-02-07 12:42:19 +01:00
|
|
|
|
const queries = [
|
|
|
|
|
...this.getStanzaIdQueryAttrs(attrs),
|
|
|
|
|
this.getOriginIdQueryAttrs(attrs),
|
|
|
|
|
this.getMessageBodyQueryAttrs(attrs)
|
2020-02-07 12:15:23 +01:00
|
|
|
|
].filter(s => s);
|
|
|
|
|
const msgs = this.messages.models;
|
2020-02-07 12:42:19 +01:00
|
|
|
|
return find(msgs, m => queries.reduce((out, q) => (out || isMatch(m.attributes, q)), false));
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
2020-02-07 12:42:19 +01:00
|
|
|
|
getOriginIdQueryAttrs (attrs) {
|
2020-02-07 12:15:23 +01:00
|
|
|
|
return attrs.origin_id && {'origin_id': attrs.origin_id, 'from': attrs.from};
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-07 12:42:19 +01:00
|
|
|
|
getStanzaIdQueryAttrs (attrs) {
|
2020-02-07 12:15:23 +01:00
|
|
|
|
const keys = Object.keys(attrs).filter(k => k.startsWith('stanza_id '));
|
|
|
|
|
return keys.map(key => {
|
|
|
|
|
const by_jid = key.replace(/^stanza_id /, '');
|
|
|
|
|
const query = {};
|
|
|
|
|
query[`stanza_id ${by_jid}`] = attrs[key];
|
|
|
|
|
return query;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-07 12:42:19 +01:00
|
|
|
|
getMessageBodyQueryAttrs (attrs) {
|
2020-02-07 12:15:23 +01:00
|
|
|
|
if (attrs.message && attrs.msgid) {
|
2020-09-18 13:38:39 +02:00
|
|
|
|
const query = {
|
2020-02-07 12:15:23 +01:00
|
|
|
|
'from': attrs.from,
|
|
|
|
|
'msgid': attrs.msgid
|
|
|
|
|
}
|
2020-09-18 13:38:39 +02:00
|
|
|
|
if (!attrs.is_encrypted) {
|
|
|
|
|
// We can't match the message if it's a reflected
|
|
|
|
|
// encrypted message (e.g. via MAM or in a MUC)
|
|
|
|
|
query['message'] = attrs.message;
|
|
|
|
|
}
|
|
|
|
|
return query;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-02-18 15:42:55 +01:00
|
|
|
|
/**
|
|
|
|
|
* Retract one of your messages in this chat
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBoxView#retractOwnMessage
|
|
|
|
|
* @param { _converse.Message } message - The message which we're retracting.
|
|
|
|
|
*/
|
|
|
|
|
retractOwnMessage(message) {
|
|
|
|
|
this.sendRetractionMessage(message)
|
|
|
|
|
message.save({
|
|
|
|
|
'retracted': (new Date()).toISOString(),
|
|
|
|
|
'retracted_id': message.get('origin_id'),
|
2020-05-15 14:33:31 +02:00
|
|
|
|
'retraction_id': message.get('id'),
|
2020-02-18 15:42:55 +01:00
|
|
|
|
'is_ephemeral': true,
|
|
|
|
|
'editable': false
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2019-09-24 15:33:41 +02:00
|
|
|
|
/**
|
|
|
|
|
* Sends a message stanza to retract a message in this chat
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#sendRetractionMessage
|
|
|
|
|
* @param { _converse.Message } message - The message which we're retracting.
|
|
|
|
|
*/
|
|
|
|
|
sendRetractionMessage (message) {
|
|
|
|
|
const origin_id = message.get('origin_id');
|
|
|
|
|
if (!origin_id) {
|
|
|
|
|
throw new Error("Can't retract message without a XEP-0359 Origin ID");
|
|
|
|
|
}
|
|
|
|
|
const msg = $msg({
|
|
|
|
|
'id': u.getUniqueId(),
|
|
|
|
|
'to': this.get('jid'),
|
|
|
|
|
'type': "chat"
|
|
|
|
|
})
|
|
|
|
|
.c('store', {xmlns: Strophe.NS.HINTS}).up()
|
|
|
|
|
.c("apply-to", {
|
|
|
|
|
'id': origin_id,
|
|
|
|
|
'xmlns': Strophe.NS.FASTEN
|
|
|
|
|
}).c('retract', {xmlns: Strophe.NS.RETRACT})
|
|
|
|
|
return _converse.connection.send(msg);
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-01 17:30:20 +02:00
|
|
|
|
sendMarkerForMessage (msg) {
|
|
|
|
|
if (msg?.get('is_markable')) {
|
|
|
|
|
const from_jid = Strophe.getBareJidFromJid(msg.get('from'));
|
|
|
|
|
this.sendMarker(from_jid, msg.get('msgid'), 'displayed', msg.get('type'));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
sendMarker (to_jid, id, type, msg_type) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
const stanza = $msg({
|
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
|
'id': u.getUniqueId(),
|
|
|
|
|
'to': to_jid,
|
2020-06-01 17:30:20 +02:00
|
|
|
|
'type': msg_type ? msg_type : 'chat'
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}).c(type, {'xmlns': Strophe.NS.MARKERS, 'id': id});
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.send(stanza);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
2020-04-24 17:33:32 +02:00
|
|
|
|
handleChatMarker (attrs) {
|
|
|
|
|
const to_bare_jid = Strophe.getBareJidFromJid(attrs.to);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (to_bare_jid !== _converse.bare_jid) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
|
if (attrs.is_markable) {
|
2020-06-08 12:24:25 +02:00
|
|
|
|
if (this.contact && !attrs.is_archived && !attrs.is_carbon) {
|
2020-04-24 17:33:32 +02:00
|
|
|
|
this.sendMarker(attrs.from, attrs.msgid, 'received');
|
|
|
|
|
}
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return false;
|
2020-04-24 17:33:32 +02:00
|
|
|
|
} else if (attrs.marker_id) {
|
|
|
|
|
const message = this.messages.findWhere({'msgid': attrs.marker_id});
|
|
|
|
|
const field_name = `marker_${attrs.marker}`;
|
|
|
|
|
if (message && !message.get(field_name)) {
|
|
|
|
|
message.save({field_name: (new Date()).toISOString()});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
|
return true;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
sendReceiptStanza (to_jid, id) {
|
|
|
|
|
const receipt_stanza = $msg({
|
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
|
'id': u.getUniqueId(),
|
|
|
|
|
'to': to_jid,
|
|
|
|
|
'type': 'chat',
|
|
|
|
|
}).c('received', {'xmlns': Strophe.NS.RECEIPTS, 'id': id}).up()
|
|
|
|
|
.c('store', {'xmlns': Strophe.NS.HINTS}).up();
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.send(receipt_stanza);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
2020-04-24 17:33:32 +02:00
|
|
|
|
handleReceipt (attrs) {
|
|
|
|
|
if (attrs.sender === 'them') {
|
2020-08-13 11:05:23 +02:00
|
|
|
|
if (attrs.is_valid_receipt_request) {
|
2020-04-24 17:33:32 +02:00
|
|
|
|
this.sendReceiptStanza(attrs.from, attrs.msgid);
|
|
|
|
|
} else if (attrs.receipt_id) {
|
|
|
|
|
const message = this.messages.findWhere({'msgid': attrs.receipt_id});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (message && !message.get('received')) {
|
|
|
|
|
message.save({'received': (new Date()).toISOString()});
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a {@link _converse.Message} return the XML stanza that represents it.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#createMessageStanza
|
|
|
|
|
* @param { _converse.Message } message - The message object
|
|
|
|
|
*/
|
|
|
|
|
createMessageStanza (message) {
|
|
|
|
|
const stanza = $msg({
|
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
|
'to': this.get('jid'),
|
|
|
|
|
'type': this.get('message_type'),
|
|
|
|
|
'id': message.get('edited') && u.getUniqueId() || message.get('msgid'),
|
|
|
|
|
}).c('body').t(message.get('message')).up()
|
|
|
|
|
.c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).root();
|
|
|
|
|
|
|
|
|
|
if (message.get('type') === 'chat') {
|
|
|
|
|
stanza.c('request', {'xmlns': Strophe.NS.RECEIPTS}).root();
|
|
|
|
|
}
|
|
|
|
|
if (message.get('is_spoiler')) {
|
|
|
|
|
if (message.get('spoiler_hint')) {
|
|
|
|
|
stanza.c('spoiler', {'xmlns': Strophe.NS.SPOILER}, message.get('spoiler_hint')).root();
|
|
|
|
|
} else {
|
|
|
|
|
stanza.c('spoiler', {'xmlns': Strophe.NS.SPOILER}).root();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(message.get('references') || []).forEach(reference => {
|
|
|
|
|
const attrs = {
|
|
|
|
|
'xmlns': Strophe.NS.REFERENCE,
|
|
|
|
|
'begin': reference.begin,
|
|
|
|
|
'end': reference.end,
|
|
|
|
|
'type': reference.type,
|
|
|
|
|
}
|
|
|
|
|
if (reference.uri) {
|
|
|
|
|
attrs.uri = reference.uri;
|
|
|
|
|
}
|
|
|
|
|
stanza.c('reference', attrs).root();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (message.get('oob_url')) {
|
|
|
|
|
stanza.c('x', {'xmlns': Strophe.NS.OUTOFBAND}).c('url').t(message.get('oob_url')).root();
|
|
|
|
|
}
|
|
|
|
|
if (message.get('edited')) {
|
|
|
|
|
stanza.c('replace', {
|
|
|
|
|
'xmlns': Strophe.NS.MESSAGE_CORRECT,
|
|
|
|
|
'id': message.get('msgid')
|
|
|
|
|
}).root();
|
|
|
|
|
}
|
|
|
|
|
if (message.get('origin_id')) {
|
|
|
|
|
stanza.c('origin-id', {'xmlns': Strophe.NS.SID, 'id': message.get('origin_id')}).root();
|
|
|
|
|
}
|
|
|
|
|
return stanza;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getOutgoingMessageAttributes (text, spoiler_hint) {
|
|
|
|
|
const is_spoiler = this.get('composing_spoiler');
|
|
|
|
|
const origin_id = u.getUniqueId();
|
2020-06-24 14:07:28 +02:00
|
|
|
|
const body = text ? u.httpToGeoUri(u.shortnamesToUnicode(text), _converse) : undefined;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return {
|
2020-06-22 14:41:38 +02:00
|
|
|
|
'from': _converse.bare_jid,
|
|
|
|
|
'fullname': _converse.xmppstatus.get('fullname'),
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'id': origin_id,
|
2020-06-22 14:41:38 +02:00
|
|
|
|
'is_only_emojis': text ? u.isOnlyEmojis(text) : false,
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'jid': this.get('jid'),
|
2020-06-22 14:41:38 +02:00
|
|
|
|
'message': body,
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'msgid': origin_id,
|
2020-06-22 14:41:38 +02:00
|
|
|
|
'nickname': this.get('nickname'),
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'sender': 'me',
|
|
|
|
|
'spoiler_hint': is_spoiler ? spoiler_hint : undefined,
|
2020-06-22 14:41:38 +02:00
|
|
|
|
'time': (new Date()).toISOString(),
|
|
|
|
|
'type': this.get('message_type'),
|
|
|
|
|
body,
|
|
|
|
|
is_spoiler,
|
|
|
|
|
origin_id
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Responsible for setting the editable attribute of messages.
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* If api.settings.get('allow_message_corrections') is "last", then only the last
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* message sent from me will be editable. If set to "all" all messages
|
|
|
|
|
* will be editable. Otherwise no messages will be editable.
|
|
|
|
|
* @method _converse.ChatBox#setEditable
|
|
|
|
|
* @memberOf _converse.ChatBox
|
|
|
|
|
* @param { Object } attrs An object containing message attributes.
|
|
|
|
|
* @param { String } send_time - time when the message was sent
|
|
|
|
|
*/
|
2020-04-24 17:33:32 +02:00
|
|
|
|
setEditable (attrs, send_time) {
|
2020-06-25 10:22:44 +02:00
|
|
|
|
if (attrs.is_headline || u.isEmptyMessage(attrs) || attrs.sender !== 'me') {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
|
if (api.settings.get('allow_message_corrections') === 'all') {
|
2019-11-26 11:46:02 +01:00
|
|
|
|
attrs.editable = !(attrs.file || attrs.retracted || 'oob_url' in attrs);
|
2020-03-31 13:15:57 +02:00
|
|
|
|
} else if ((api.settings.get('allow_message_corrections') === 'last') && (send_time > this.get('time_sent'))) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
this.set({'time_sent': send_time});
|
|
|
|
|
const msg = this.messages.findWhere({'editable': true});
|
|
|
|
|
if (msg) {
|
|
|
|
|
msg.save({'editable': false});
|
|
|
|
|
}
|
2019-11-26 11:46:02 +01:00
|
|
|
|
attrs.editable = !(attrs.file || attrs.retracted || 'oob_url' in attrs);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-03-18 19:32:03 +01:00
|
|
|
|
/**
|
2020-10-27 22:48:22 +01:00
|
|
|
|
* Queue the creation of a message, to make sure that we don't run
|
|
|
|
|
* into a race condition whereby we're creating a new message
|
|
|
|
|
* before the collection has been fetched.
|
2020-03-18 19:32:03 +01:00
|
|
|
|
* @async
|
|
|
|
|
* @private
|
2020-10-27 22:48:22 +01:00
|
|
|
|
* @method _converse.ChatRoom#queueMessageCreation
|
|
|
|
|
* @param { Object } attrs
|
2020-03-18 19:32:03 +01:00
|
|
|
|
*/
|
2020-10-27 22:48:22 +01:00
|
|
|
|
async createMessage (attrs, options) {
|
|
|
|
|
attrs.time = attrs.time || (new Date()).toISOString();
|
|
|
|
|
await this.messages.fetched;
|
|
|
|
|
const p = this.messages.create(attrs, Object.assign({'wait': true, 'promise':true}, options));
|
|
|
|
|
return p;
|
2020-03-02 16:59:18 +01:00
|
|
|
|
},
|
|
|
|
|
|
2019-11-01 16:04:55 +01:00
|
|
|
|
/**
|
|
|
|
|
* Responsible for sending off a text message inside an ongoing chat conversation.
|
2020-03-18 19:32:03 +01:00
|
|
|
|
* @private
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* @method _converse.ChatBox#sendMessage
|
|
|
|
|
* @memberOf _converse.ChatBox
|
|
|
|
|
* @param { String } text - The chat message text
|
|
|
|
|
* @param { String } spoiler_hint - An optional hint, if the message being sent is a spoiler
|
|
|
|
|
* @returns { _converse.Message }
|
|
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* const chat = api.chats.get('buddy1@example.com');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* chat.sendMessage('hello world');
|
|
|
|
|
*/
|
2020-03-02 16:59:18 +01:00
|
|
|
|
async sendMessage (text, spoiler_hint) {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
const attrs = this.getOutgoingMessageAttributes(text, spoiler_hint);
|
|
|
|
|
let message = this.messages.findWhere('correcting')
|
|
|
|
|
if (message) {
|
|
|
|
|
const older_versions = message.get('older_versions') || {};
|
|
|
|
|
older_versions[message.get('time')] = message.get('message');
|
|
|
|
|
message.save({
|
|
|
|
|
'correcting': false,
|
|
|
|
|
'edited': (new Date()).toISOString(),
|
|
|
|
|
'message': attrs.message,
|
|
|
|
|
'older_versions': older_versions,
|
|
|
|
|
'references': attrs.references,
|
2019-11-20 14:14:20 +01:00
|
|
|
|
'is_only_emojis': attrs.is_only_emojis,
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'origin_id': u.getUniqueId(),
|
|
|
|
|
'received': undefined
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setEditable(attrs, (new Date()).toISOString());
|
2020-03-02 16:59:18 +01:00
|
|
|
|
message = await this.createMessage(attrs);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.send(this.createMessageStanza(message));
|
2020-04-28 17:46:57 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Triggered when a message is being sent out
|
|
|
|
|
* @event _converse#sendMessage
|
|
|
|
|
* @type { Object }
|
2020-05-11 10:33:29 +02:00
|
|
|
|
* @param { Object } data
|
2020-04-28 17:46:57 +02:00
|
|
|
|
* @property { (_converse.ChatBox | _converse.ChatRoom) } data.chatbox
|
2020-05-11 10:33:29 +02:00
|
|
|
|
* @property { (_converse.Message | _converse.ChatRoomMessage) } data.message
|
2020-04-28 17:46:57 +02:00
|
|
|
|
*/
|
|
|
|
|
api.trigger('sendMessage', {'chatbox': this, message});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return message;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sends a message with the current XEP-0085 chat state of the user
|
|
|
|
|
* as taken from the `chat_state` attribute of the {@link _converse.ChatBox}.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse.ChatBox#sendChatState
|
|
|
|
|
*/
|
|
|
|
|
sendChatState () {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
if (api.settings.get('send_chat_state_notifications') && this.get('chat_state')) {
|
|
|
|
|
const allowed = api.settings.get('send_chat_state_notifications');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (Array.isArray(allowed) && !allowed.includes(this.get('chat_state'))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.send(
|
2019-11-01 16:04:55 +01:00
|
|
|
|
$msg({
|
|
|
|
|
'id': u.getUniqueId(),
|
|
|
|
|
'to': this.get('jid'),
|
|
|
|
|
'type': 'chat'
|
|
|
|
|
}).c(this.get('chat_state'), {'xmlns': Strophe.NS.CHATSTATES}).up()
|
|
|
|
|
.c('no-store', {'xmlns': Strophe.NS.HINTS}).up()
|
|
|
|
|
.c('no-permanent-store', {'xmlns': Strophe.NS.HINTS})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async sendFiles (files) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const result = await api.disco.features.get(Strophe.NS.HTTPUPLOAD, _converse.domain);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
const item = result.pop();
|
|
|
|
|
if (!item) {
|
2020-03-02 16:59:18 +01:00
|
|
|
|
this.createMessage({
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'message': __("Sorry, looks like file upload is not supported by your server."),
|
|
|
|
|
'type': 'error',
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': true
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-15 14:33:31 +02:00
|
|
|
|
const data = item.dataforms.where({'FORM_TYPE': {'value': Strophe.NS.HTTPUPLOAD, 'type': "hidden"}}).pop();
|
|
|
|
|
const max_file_size = window.parseInt((data?.attributes || {})['max-file-size']?.value);
|
|
|
|
|
const slot_request_url = item?.id;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
|
|
|
|
|
if (!slot_request_url) {
|
2020-03-02 16:59:18 +01:00
|
|
|
|
this.createMessage({
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'message': __("Sorry, looks like file upload is not supported by your server."),
|
|
|
|
|
'type': 'error',
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': true
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-02 16:59:18 +01:00
|
|
|
|
Array.from(files).forEach(async file => {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (!window.isNaN(max_file_size) && window.parseInt(file.size) > max_file_size) {
|
2020-03-02 16:59:18 +01:00
|
|
|
|
return this.createMessage({
|
2019-11-01 16:04:55 +01:00
|
|
|
|
'message': __('The size of your file, %1$s, exceeds the maximum allowed by your server, which is %2$s.',
|
|
|
|
|
file.name, filesize(max_file_size)),
|
|
|
|
|
'type': 'error',
|
2019-09-24 15:33:41 +02:00
|
|
|
|
'is_ephemeral': true
|
2019-11-01 16:04:55 +01:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const attrs = Object.assign(
|
|
|
|
|
this.getOutgoingMessageAttributes(), {
|
|
|
|
|
'file': true,
|
|
|
|
|
'progress': 0,
|
|
|
|
|
'slot_request_url': slot_request_url
|
|
|
|
|
});
|
|
|
|
|
this.setEditable(attrs, (new Date()).toISOString());
|
2020-03-02 16:59:18 +01:00
|
|
|
|
const message = await this.createMessage(attrs, {'silent': true});
|
2019-11-01 16:04:55 +01:00
|
|
|
|
message.file = file;
|
|
|
|
|
this.messages.trigger('add', message);
|
|
|
|
|
message.getRequestSlotURL();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-09-21 17:52:00 +02:00
|
|
|
|
maybeShow (force) {
|
2020-10-27 11:57:23 +01:00
|
|
|
|
if (force) {
|
|
|
|
|
if (_converse.isUniView()) {
|
|
|
|
|
// We only have one chat visible at any one time.
|
|
|
|
|
// So before opening a chat, we make sure all other chats are hidden.
|
|
|
|
|
const filter = c => !c.get('hidden') &&
|
|
|
|
|
c.get('jid') !== this.get('jid') &&
|
|
|
|
|
c.get('id') !== 'controlbox';
|
|
|
|
|
_converse.chatboxes.filter(filter).forEach(c => u.safeSave(c, {'hidden': true}));
|
|
|
|
|
}
|
|
|
|
|
u.safeSave(this, {'hidden': false});
|
|
|
|
|
}
|
2020-09-21 17:52:00 +02:00
|
|
|
|
if (_converse.isUniView() && this.get('hidden')) {
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
return this.trigger("show");
|
|
|
|
|
}
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicates whether the chat is hidden and therefore
|
|
|
|
|
* whether a newly received message will be visible
|
|
|
|
|
* to the user or not.
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
isHidden () {
|
2020-08-28 15:32:58 +02:00
|
|
|
|
// Note: This methods gets overridden by converse-minimize
|
2020-09-10 10:09:30 +02:00
|
|
|
|
const hidden = _converse.isUniView() && this.get('hidden');
|
2020-09-04 09:19:59 +02:00
|
|
|
|
return hidden || this.isScrolledUp() || _converse.windowState === 'hidden';
|
2019-11-01 16:04:55 +01:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a newly received {@link _converse.Message} instance,
|
|
|
|
|
* update the unread counter if necessary.
|
|
|
|
|
* @private
|
|
|
|
|
* @param {_converse.Message} message
|
|
|
|
|
*/
|
2020-08-18 21:05:36 +02:00
|
|
|
|
handleUnreadMessage (message) {
|
2020-06-01 17:30:20 +02:00
|
|
|
|
if (!message?.get('body')) {
|
|
|
|
|
return
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-06-01 17:30:20 +02:00
|
|
|
|
if (utils.isNewMessage(message)) {
|
|
|
|
|
if (this.isHidden()) {
|
|
|
|
|
const settings = {
|
|
|
|
|
'num_unread': this.get('num_unread') + 1
|
|
|
|
|
};
|
|
|
|
|
if (this.get('num_unread') === 0) {
|
|
|
|
|
settings['first_unread_id'] = message.get('id');
|
|
|
|
|
}
|
|
|
|
|
this.save(settings);
|
|
|
|
|
} else {
|
|
|
|
|
this.sendMarkerForMessage(message);
|
2020-04-28 10:35:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-06-01 17:30:20 +02:00
|
|
|
|
clearUnreadMsgCounter() {
|
|
|
|
|
if (this.get('num_unread') > 0) {
|
|
|
|
|
this.sendMarkerForMessage(this.messages.last());
|
|
|
|
|
}
|
2019-11-01 16:04:55 +01:00
|
|
|
|
u.safeSave(this, {'num_unread': 0});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isScrolledUp () {
|
|
|
|
|
return this.get('scrolled', true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function handleErrorMessage (stanza) {
|
|
|
|
|
const from_jid = Strophe.getBareJidFromJid(stanza.getAttribute('from'));
|
|
|
|
|
if (utils.isSameBareJID(from_jid, _converse.bare_jid)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const chatbox = await api.chatboxes.get(from_jid);
|
2020-05-15 14:33:31 +02:00
|
|
|
|
chatbox?.handleErrorMessageStanza(stanza);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handler method for all incoming single-user chat "message" stanzas.
|
|
|
|
|
* @private
|
|
|
|
|
* @method _converse#handleMessageStanza
|
2020-04-24 17:33:32 +02:00
|
|
|
|
* @param { MessageAttributes } attrs - The message attributes
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*/
|
|
|
|
|
_converse.handleMessageStanza = async function (stanza) {
|
2020-04-24 17:33:32 +02:00
|
|
|
|
if (st.isServerMessage(stanza)) {
|
|
|
|
|
// Prosody sends headline messages with type `chat`, so we need to filter them out here.
|
|
|
|
|
const from = stanza.getAttribute('from');
|
|
|
|
|
return log.info(`handleMessageStanza: Ignoring incoming server message from JID: ${from}`);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
|
const attrs = await st.parseMessage(stanza, _converse);
|
|
|
|
|
if (u.isErrorObject(attrs)) {
|
|
|
|
|
attrs.stanza && log.error(attrs.stanza);
|
|
|
|
|
return log.error(attrs.message);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
|
const has_body = !!sizzle(`body, encrypted[xmlns="${Strophe.NS.OMEMO}"]`, stanza).length;
|
|
|
|
|
const chatbox = await api.chats.get(attrs.contact_jid, {'nickname': attrs.nick }, has_body);
|
2020-09-06 12:30:30 +02:00
|
|
|
|
await chatbox?.queueMessage(attrs);
|
2020-09-10 10:09:30 +02:00
|
|
|
|
/**
|
2020-09-16 18:58:12 +02:00
|
|
|
|
* @typedef { Object } MessageData
|
2020-09-10 10:09:30 +02:00
|
|
|
|
* An object containing the original message stanza, as well as the
|
|
|
|
|
* parsed attributes.
|
|
|
|
|
* @property { XMLElement } stanza
|
|
|
|
|
* @property { MessageAttributes } stanza
|
2020-09-16 18:58:12 +02:00
|
|
|
|
* @property { ChatBox } chatbox
|
2020-09-10 10:09:30 +02:00
|
|
|
|
*/
|
2020-09-16 18:58:12 +02:00
|
|
|
|
const data = {stanza, attrs, chatbox};
|
2019-11-01 16:04:55 +01:00
|
|
|
|
/**
|
|
|
|
|
* Triggered when a message stanza is been received and processed.
|
|
|
|
|
* @event _converse#message
|
|
|
|
|
* @type { object }
|
2020-09-16 18:58:12 +02:00
|
|
|
|
* @property { module:converse-chat~MessageData } data
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*/
|
2020-09-10 10:09:30 +02:00
|
|
|
|
api.trigger('message', data);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function registerMessageHandlers () {
|
|
|
|
|
_converse.connection.addHandler(stanza => {
|
|
|
|
|
if (sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, stanza).pop()) {
|
|
|
|
|
// MAM messages are handled in converse-mam.
|
|
|
|
|
// We shouldn't get MAM messages here because
|
|
|
|
|
// they shouldn't have a `type` attribute.
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.warn(`Received a MAM message with type "chat".`);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
_converse.handleMessageStanza(stanza);
|
|
|
|
|
return true;
|
|
|
|
|
}, null, 'message', 'chat');
|
|
|
|
|
|
|
|
|
|
_converse.connection.addHandler(stanza => {
|
|
|
|
|
// Message receipts are usually without the `type` attribute. See #1353
|
|
|
|
|
if (stanza.getAttribute('type') !== null) {
|
|
|
|
|
// TODO: currently Strophe has no way to register a handler
|
|
|
|
|
// for stanzas without a `type` attribute.
|
|
|
|
|
// We could update it to accept null to mean no attribute,
|
|
|
|
|
// but that would be a backward-incompatible change
|
|
|
|
|
return true; // Gets handled above.
|
|
|
|
|
}
|
|
|
|
|
_converse.handleMessageStanza(stanza);
|
|
|
|
|
return true;
|
|
|
|
|
}, Strophe.NS.RECEIPTS, 'message');
|
|
|
|
|
|
|
|
|
|
_converse.connection.addHandler(stanza => {
|
|
|
|
|
handleErrorMessage(stanza);
|
|
|
|
|
return true;
|
|
|
|
|
}, null, 'message', 'error');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function autoJoinChats () {
|
|
|
|
|
// Automatically join private chats, based on the
|
|
|
|
|
// "auto_join_private_chats" configuration setting.
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.settings.get('auto_join_private_chats').forEach(jid => {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (_converse.chatboxes.where({'jid': jid}).length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-01 11:13:13 +02:00
|
|
|
|
if (typeof jid === 'string') {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.chats.open(jid);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
} else {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error('Invalid jid criteria specified for "auto_join_private_chats"');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/**
|
|
|
|
|
* Triggered once any private chats have been automatically joined as
|
|
|
|
|
* specified by the `auto_join_private_chats` setting.
|
|
|
|
|
* See: https://conversejs.org/docs/html/configuration.html#auto-join-private-chats
|
|
|
|
|
* @event _converse#privateChatsAutoJoined
|
|
|
|
|
* @example _converse.api.listen.on('privateChatsAutoJoined', () => { ... });
|
|
|
|
|
* @example _converse.api.waitUntil('privateChatsAutoJoined').then(() => { ... });
|
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.trigger('privateChatsAutoJoined');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN Route Handlers ************************/
|
|
|
|
|
function openChat (jid) {
|
|
|
|
|
if (!utils.isValidJID(jid)) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
return log.warn(`Invalid JID "${jid}" provided in URL fragment`);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.chats.open(jid);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
_converse.router.route('converse/chat?jid=:jid', openChat);
|
|
|
|
|
/************************ END Route Handlers ************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
api.listen.on('chatBoxesFetched', autoJoinChats);
|
|
|
|
|
api.listen.on('presencesInitialized', registerMessageHandlers);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
|
2020-10-20 13:02:30 +02:00
|
|
|
|
api.listen.on('clearSession', async () => {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (_converse.shouldClearCache()) {
|
2020-10-20 13:02:30 +02:00
|
|
|
|
await Promise.all(_converse.chatboxes.map(c => c.messages && c.messages.clearStore({'silent': true})));
|
|
|
|
|
const filter = (o) => (o.get('type') !== _converse.CONTROLBOX_TYPE);
|
|
|
|
|
_converse.chatboxes.clearStore({'silent': true}, filter);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/************************ END Event Handlers ************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************ BEGIN API ************************/
|
2020-03-31 13:15:57 +02:00
|
|
|
|
Object.assign(api, {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
/**
|
|
|
|
|
* The "chats" namespace (used for one-on-one chats)
|
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* @namespace api.chats
|
|
|
|
|
* @memberOf api
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*/
|
|
|
|
|
chats: {
|
|
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* @method api.chats.create
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* @param {string|string[]} jid|jids An jid or array of jids
|
|
|
|
|
* @param {object} [attrs] An object containing configuration attributes.
|
|
|
|
|
*/
|
|
|
|
|
async create (jids, attrs) {
|
2020-10-01 11:13:13 +02:00
|
|
|
|
if (typeof jids === 'string') {
|
2020-03-24 12:26:34 +01:00
|
|
|
|
if (attrs && !attrs?.fullname) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const contact = await api.contacts.get(jids);
|
2020-03-24 12:26:34 +01:00
|
|
|
|
attrs.fullname = contact?.attributes?.fullname;
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const chatbox = api.chats.get(jids, attrs, true);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (!chatbox) {
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error("Could not open chatbox for JID: "+jids);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return chatbox;
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(jids)) {
|
|
|
|
|
return Promise.all(jids.forEach(async jid => {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const contact = await api.contacts.get(jids);
|
2020-03-24 12:26:34 +01:00
|
|
|
|
attrs.fullname = contact?.attributes?.fullname;
|
2020-03-31 13:15:57 +02:00
|
|
|
|
return api.chats.get(jid, attrs, true).maybeShow();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
}));
|
|
|
|
|
}
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error("chats.create: You need to provide at least one JID");
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Opens a new one-on-one chat.
|
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* @method api.chats.open
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* @param {String|string[]} name - e.g. 'buddy@example.com' or ['buddy1@example.com', 'buddy2@example.com']
|
|
|
|
|
* @param {Object} [attrs] - Attributes to be set on the _converse.ChatBox model.
|
|
|
|
|
* @param {Boolean} [attrs.minimized] - Should the chat be created in minimized state.
|
|
|
|
|
* @param {Boolean} [force=false] - By default, a minimized
|
|
|
|
|
* chat won't be maximized (in `overlayed` view mode) and in
|
|
|
|
|
* `fullscreen` view mode a newly opened chat won't replace
|
|
|
|
|
* another chat already in the foreground.
|
|
|
|
|
* Set `force` to `true` if you want to force the chat to be
|
|
|
|
|
* maximized or shown.
|
|
|
|
|
* @returns {Promise} Promise which resolves with the
|
|
|
|
|
* _converse.ChatBox representing the chat.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // To open a single chat, provide the JID of the contact you're chatting with in that chat:
|
|
|
|
|
* converse.plugins.add('myplugin', {
|
|
|
|
|
* initialize: function() {
|
|
|
|
|
* const _converse = this._converse;
|
|
|
|
|
* // Note, buddy@example.org must be in your contacts roster!
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* api.chats.open('buddy@example.com').then(chat => {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* // Now you can do something with the chat model
|
|
|
|
|
* });
|
|
|
|
|
* }
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // To open an array of chats, provide an array of JIDs:
|
|
|
|
|
* converse.plugins.add('myplugin', {
|
|
|
|
|
* initialize: function () {
|
|
|
|
|
* const _converse = this._converse;
|
|
|
|
|
* // Note, these users must first be in your contacts roster!
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* api.chats.open(['buddy1@example.com', 'buddy2@example.com']).then(chats => {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* // Now you can do something with the chat models
|
|
|
|
|
* });
|
|
|
|
|
* }
|
|
|
|
|
* });
|
|
|
|
|
*/
|
|
|
|
|
async open (jids, attrs, force) {
|
2020-10-01 11:13:13 +02:00
|
|
|
|
if (typeof jids === 'string') {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const chat = await api.chats.get(jids, attrs, true);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (chat) {
|
|
|
|
|
return chat.maybeShow(force);
|
|
|
|
|
}
|
|
|
|
|
return chat;
|
|
|
|
|
} else if (Array.isArray(jids)) {
|
|
|
|
|
return Promise.all(
|
2020-03-31 13:15:57 +02:00
|
|
|
|
jids.map(j => api.chats.get(j, attrs, true).then(c => c && c.maybeShow(force)))
|
2019-11-01 16:04:55 +01:00
|
|
|
|
.filter(c => c)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const err_msg = "chats.open: You need to provide at least one JID";
|
2019-11-06 11:01:34 +01:00
|
|
|
|
log.error(err_msg);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
throw new Error(err_msg);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves a chat or all chats.
|
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* @method api.chats.get
|
2019-11-01 16:04:55 +01:00
|
|
|
|
* @param {String|string[]} jids - e.g. 'buddy@example.com' or ['buddy1@example.com', 'buddy2@example.com']
|
|
|
|
|
* @param {Object} [attrs] - Attributes to be set on the _converse.ChatBox model.
|
|
|
|
|
* @param {Boolean} [create=false] - Whether the chat should be created if it's not found.
|
|
|
|
|
* @returns { Promise<_converse.ChatBox> }
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // To return a single chat, provide the JID of the contact you're chatting with in that chat:
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* const model = await api.chats.get('buddy@example.com');
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // To return an array of chats, provide an array of JIDs:
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* const models = await api.chats.get(['buddy1@example.com', 'buddy2@example.com']);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // To return all open chats, call the method without any parameters::
|
2020-03-31 13:15:57 +02:00
|
|
|
|
* const models = await api.chats.get();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
async get (jids, attrs={}, create=false) {
|
|
|
|
|
async function _get (jid) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
let model = await api.chatboxes.get(jid);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
if (!model && create) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
model = await api.chatboxes.create(jid, attrs, _converse.ChatBox);
|
2019-11-01 16:04:55 +01:00
|
|
|
|
} else {
|
|
|
|
|
model = (model && model.get('type') === _converse.PRIVATE_CHAT_TYPE) ? model : null;
|
|
|
|
|
if (model && Object.keys(attrs).length) {
|
|
|
|
|
model.save(attrs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
if (jids === undefined) {
|
2020-03-31 13:15:57 +02:00
|
|
|
|
const chats = await api.chatboxes.get();
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return chats.filter(c => (c.get('type') === _converse.PRIVATE_CHAT_TYPE));
|
2020-10-01 11:13:13 +02:00
|
|
|
|
} else if (typeof jids === 'string') {
|
2019-11-01 16:04:55 +01:00
|
|
|
|
return _get(jids);
|
|
|
|
|
}
|
|
|
|
|
return Promise.all(jids.map(jid => _get(jid)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
/************************ END API ************************/
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|