2018-04-24 11:08:26 +02:00
|
|
|
// Converse.js
|
2016-02-16 08:46:47 +01:00
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2018-05-11 13:31:48 +02:00
|
|
|
// Copyright (c) 2013-2018, the Converse.js developers
|
2016-02-16 08:46:47 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
|
|
|
|
(function (root, factory) {
|
2017-02-13 15:37:17 +01:00
|
|
|
define([
|
2018-06-06 11:04:23 +02:00
|
|
|
"utils/form",
|
2017-02-14 15:08:39 +01:00
|
|
|
"converse-core",
|
2017-11-10 21:20:13 +01:00
|
|
|
"converse-disco",
|
2017-12-20 20:29:57 +01:00
|
|
|
"backbone.overview",
|
|
|
|
"backbone.orderedlistview",
|
2018-03-29 11:56:24 +02:00
|
|
|
"backbone.vdomview",
|
2018-09-03 14:06:18 +02:00
|
|
|
"utils/muc",
|
|
|
|
"utils/emoji"
|
2016-02-28 20:24:06 +01:00
|
|
|
], factory);
|
2018-09-03 14:06:18 +02:00
|
|
|
}(this, function (u, converse) {
|
2016-02-19 11:43:46 +01:00
|
|
|
"use strict";
|
2016-11-02 23:08:20 +01:00
|
|
|
|
2017-12-14 18:43:00 +01:00
|
|
|
const MUC_ROLE_WEIGHTS = {
|
|
|
|
'moderator': 1,
|
|
|
|
'participant': 2,
|
|
|
|
'visitor': 3,
|
2018-05-09 12:28:34 +02:00
|
|
|
'none': 2,
|
2017-12-14 18:43:00 +01:00
|
|
|
};
|
|
|
|
|
2018-08-09 14:52:03 +02:00
|
|
|
const { Strophe, Backbone, Promise, $iq, $build, $msg, $pres, b64_sha1, sizzle, f, moment, _ } = converse.env;
|
2016-02-16 08:46:47 +01:00
|
|
|
|
|
|
|
// Add Strophe Namespaces
|
|
|
|
Strophe.addNamespace('MUC_ADMIN', Strophe.NS.MUC + "#admin");
|
|
|
|
Strophe.addNamespace('MUC_OWNER', Strophe.NS.MUC + "#owner");
|
|
|
|
Strophe.addNamespace('MUC_REGISTER', "jabber:iq:register");
|
|
|
|
Strophe.addNamespace('MUC_ROOMCONF', Strophe.NS.MUC + "#roomconfig");
|
|
|
|
Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user");
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
converse.MUC_NICK_CHANGED_CODE = "303";
|
2018-02-21 22:40:51 +01:00
|
|
|
|
|
|
|
converse.ROOM_FEATURES = [
|
2017-02-28 22:34:16 +01:00
|
|
|
'passwordprotected', 'unsecured', 'hidden',
|
2017-11-05 18:47:30 +01:00
|
|
|
'publicroom', 'membersonly', 'open', 'persistent',
|
2017-02-28 22:34:16 +01:00
|
|
|
'temporary', 'nonanonymous', 'semianonymous',
|
|
|
|
'moderated', 'unmoderated', 'mam_enabled'
|
|
|
|
];
|
2017-07-21 15:05:22 +02:00
|
|
|
|
|
|
|
converse.ROOMSTATUS = {
|
2017-02-24 11:54:54 +01:00
|
|
|
CONNECTED: 0,
|
|
|
|
CONNECTING: 1,
|
2017-03-02 23:28:22 +01:00
|
|
|
NICKNAME_REQUIRED: 2,
|
2017-04-04 16:45:50 +02:00
|
|
|
PASSWORD_REQUIRED: 3,
|
|
|
|
DISCONNECTED: 4,
|
|
|
|
ENTERED: 5
|
2017-02-24 11:54:54 +01:00
|
|
|
};
|
|
|
|
|
2018-03-29 11:56:24 +02:00
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-muc', {
|
2016-06-10 00:05:55 +02:00
|
|
|
/* Optional dependencies are other plugins which might be
|
2017-03-08 12:15:19 +01:00
|
|
|
* 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.
|
2016-06-09 11:01:54 +02:00
|
|
|
*
|
2017-03-08 12:15:19 +01:00
|
|
|
* It's possible however to make optional dependencies non-optional.
|
|
|
|
* If the setting "strict_plugin_dependencies" is set to true,
|
2016-06-10 00:05:55 +02:00
|
|
|
* an error will be raised if the plugin is not found.
|
2016-06-09 11:01:54 +02:00
|
|
|
*
|
2016-06-10 00:05:55 +02:00
|
|
|
* NB: These plugins need to have already been loaded via require.js.
|
2016-06-09 11:01:54 +02:00
|
|
|
*/
|
2018-05-07 17:08:31 +02:00
|
|
|
dependencies: ["converse-chatboxes", "converse-disco", "converse-controlbox"],
|
2016-02-16 08:46:47 +01:00
|
|
|
|
|
|
|
overrides: {
|
2018-06-07 14:30:10 +02:00
|
|
|
tearDown () {
|
2018-10-05 04:39:19 +02:00
|
|
|
const { _converse } = this.__super__,
|
|
|
|
groupchats = this.chatboxes.where({'type': _converse.CHATROOMS_TYPE});
|
|
|
|
|
|
|
|
_.each(groupchats, gc => u.safeSave(gc, {'connection_status': converse.ROOMSTATUS.DISCONNECTED}));
|
2018-06-07 14:30:10 +02:00
|
|
|
this.__super__.tearDown.call(this, arguments);
|
2017-06-23 18:21:50 +02:00
|
|
|
},
|
|
|
|
|
2017-05-23 18:16:47 +02:00
|
|
|
ChatBoxes: {
|
2017-07-10 17:46:22 +02:00
|
|
|
model (attrs, options) {
|
|
|
|
const { _converse } = this.__super__;
|
2018-08-28 14:58:33 +02:00
|
|
|
if (attrs.type == _converse.CHATROOMS_TYPE) {
|
2017-05-23 18:36:40 +02:00
|
|
|
return new _converse.ChatRoom(attrs, options);
|
2017-05-23 18:16:47 +02:00
|
|
|
} else {
|
|
|
|
return this.__super__.model.apply(this, arguments);
|
|
|
|
}
|
|
|
|
},
|
2016-02-28 20:24:06 +01:00
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-02-16 08:46:47 +01:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this,
|
2017-09-26 18:27:41 +02:00
|
|
|
{ __ } = _converse;
|
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
// Configuration values for this plugin
|
2016-08-11 18:03:02 +02:00
|
|
|
// ====================================
|
|
|
|
// Refer to docs/source/configuration.rst for explanations of these
|
|
|
|
// configuration settings.
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2016-02-13 23:58:50 +01:00
|
|
|
allow_muc: true,
|
2016-08-11 18:03:02 +02:00
|
|
|
allow_muc_invitations: true,
|
|
|
|
auto_join_on_invite: false,
|
|
|
|
auto_join_rooms: [],
|
2018-09-04 11:39:27 +02:00
|
|
|
auto_register_muc_nickname: false,
|
2016-10-26 15:21:36 +02:00
|
|
|
muc_domain: undefined,
|
2016-08-11 18:03:02 +02:00
|
|
|
muc_history_max_stanzas: undefined,
|
|
|
|
muc_instant_rooms: true,
|
2018-04-08 19:44:53 +02:00
|
|
|
muc_nickname_from_jid: false
|
2016-03-13 17:16:53 +01:00
|
|
|
});
|
2018-02-21 22:40:51 +01:00
|
|
|
_converse.api.promises.add(['roomsAutoJoined']);
|
2017-09-29 00:07:16 +02:00
|
|
|
|
2017-10-31 22:08:06 +01:00
|
|
|
|
2017-10-31 23:04:46 +01:00
|
|
|
function openRoom (jid) {
|
2018-01-29 14:51:49 +01:00
|
|
|
if (!u.isValidMUCJID(jid)) {
|
|
|
|
return _converse.log(
|
2017-10-31 23:04:46 +01:00
|
|
|
`Invalid JID "${jid}" provided in URL fragment`,
|
|
|
|
Strophe.LogLevel.WARN
|
|
|
|
);
|
|
|
|
}
|
2017-10-31 22:08:06 +01:00
|
|
|
const promises = [_converse.api.waitUntil('roomsAutoJoined')]
|
2018-02-07 17:30:44 +01:00
|
|
|
if (_converse.allow_bookmarks) {
|
|
|
|
promises.push( _converse.api.waitUntil('bookmarksInitialized'));
|
|
|
|
}
|
2017-10-31 22:08:06 +01:00
|
|
|
Promise.all(promises).then(() => {
|
2017-10-31 23:04:46 +01:00
|
|
|
_converse.api.rooms.open(jid);
|
2017-10-31 22:08:06 +01:00
|
|
|
});
|
|
|
|
}
|
2017-10-31 23:04:46 +01:00
|
|
|
_converse.router.route('converse/room?jid=:jid', openRoom);
|
2017-09-29 00:07:16 +02:00
|
|
|
|
2017-05-07 20:16:13 +02:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
_converse.openChatRoom = function (jid, settings, bring_to_foreground) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Opens a groupchat, making sure that certain attributes
|
2016-12-04 10:43:39 +01:00
|
|
|
* are correct, for example that the "type" is set to
|
|
|
|
* "chatroom".
|
|
|
|
*/
|
2018-08-28 14:58:33 +02:00
|
|
|
settings.type = _converse.CHATROOMS_TYPE;
|
2018-02-21 22:40:51 +01:00
|
|
|
settings.id = jid;
|
|
|
|
settings.box_id = b64_sha1(jid)
|
|
|
|
const chatbox = _converse.chatboxes.getChatBox(jid, settings, true);
|
|
|
|
chatbox.trigger('show', true);
|
|
|
|
return chatbox;
|
2017-10-31 22:07:40 +01:00
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2017-05-23 18:36:40 +02:00
|
|
|
_converse.ChatRoom = _converse.ChatBox.extend({
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
defaults () {
|
2017-06-15 16:22:49 +02:00
|
|
|
return _.assign(
|
|
|
|
_.clone(_converse.ChatBox.prototype.defaults),
|
2018-02-21 22:40:51 +01:00
|
|
|
_.zipObject(converse.ROOM_FEATURES, _.map(converse.ROOM_FEATURES, _.stubFalse)),
|
2017-06-15 16:22:49 +02:00
|
|
|
{
|
|
|
|
// For group chats, we distinguish between generally unread
|
|
|
|
// messages and those ones that specifically mention the
|
|
|
|
// user.
|
|
|
|
//
|
|
|
|
// To keep things simple, we reuse `num_unread` from
|
|
|
|
// _converse.ChatBox to indicate unread messages which
|
|
|
|
// mention the user and `num_unread_general` to indicate
|
|
|
|
// generally unread messages (which *includes* mentions!).
|
|
|
|
'num_unread_general': 0,
|
|
|
|
|
|
|
|
'affiliation': null,
|
2017-07-21 15:05:22 +02:00
|
|
|
'connection_status': converse.ROOMSTATUS.DISCONNECTED,
|
2017-07-19 08:30:04 +02:00
|
|
|
'name': '',
|
2018-08-07 08:52:28 +02:00
|
|
|
'nick': _converse.xmppstatus.get('nickname') || _converse.nickname,
|
2017-06-15 16:22:49 +02:00
|
|
|
'description': '',
|
|
|
|
'features_fetched': false,
|
|
|
|
'roomconfig': {},
|
2018-08-28 14:58:33 +02:00
|
|
|
'type': _converse.CHATROOMS_TYPE,
|
2018-04-17 15:17:39 +02:00
|
|
|
'message_type': 'groupchat'
|
2017-06-15 16:22:49 +02:00
|
|
|
}
|
|
|
|
);
|
2017-06-07 17:47:06 +02:00
|
|
|
},
|
2017-05-24 08:40:09 +02:00
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
initialize() {
|
|
|
|
this.constructor.__super__.initialize.apply(this, arguments);
|
2018-09-04 11:39:27 +02:00
|
|
|
this.on('change:connection_status', this.onConnectionStatusChanged, this);
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
this.occupants = new _converse.ChatRoomOccupants();
|
2018-05-01 18:18:02 +02:00
|
|
|
this.occupants.browserStorage = new Backbone.BrowserStorage.session(
|
|
|
|
b64_sha1(`converse.occupants-${_converse.bare_jid}${this.get('jid')}`)
|
|
|
|
);
|
2018-05-09 12:28:34 +02:00
|
|
|
this.occupants.chatroom = this;
|
2018-05-01 18:18:02 +02:00
|
|
|
this.registerHandlers();
|
2018-04-08 19:44:53 +02:00
|
|
|
},
|
|
|
|
|
2018-09-04 11:39:27 +02:00
|
|
|
async onConnectionStatusChanged () {
|
|
|
|
if (this.get('connection_status') === converse.ROOMSTATUS.ENTERED &&
|
|
|
|
_converse.auto_register_muc_nickname &&
|
2018-09-13 12:39:50 +02:00
|
|
|
!this.get('reserved_nick')) {
|
2018-09-04 11:39:27 +02:00
|
|
|
|
|
|
|
const result = await _converse.api.disco.supports(Strophe.NS.MUC_REGISTER, this.get('jid'));
|
|
|
|
if (result.length) {
|
|
|
|
this.registerNickname()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
registerHandlers () {
|
|
|
|
/* Register presence and message handlers for this chat
|
2018-07-22 10:58:04 +02:00
|
|
|
* groupchat
|
2018-04-08 19:44:53 +02:00
|
|
|
*/
|
|
|
|
const room_jid = this.get('jid');
|
|
|
|
this.removeHandlers();
|
|
|
|
this.presence_handler = _converse.connection.addHandler((stanza) => {
|
|
|
|
_.each(_.values(this.handlers.presence), (callback) => callback(stanza));
|
|
|
|
this.onPresence(stanza);
|
|
|
|
return true;
|
|
|
|
},
|
2018-07-02 12:52:13 +02:00
|
|
|
null, 'presence', null, null, room_jid,
|
2018-04-08 19:44:53 +02:00
|
|
|
{'ignoreNamespaceFragment': true, 'matchBareFromJid': true}
|
|
|
|
);
|
|
|
|
this.message_handler = _converse.connection.addHandler((stanza) => {
|
|
|
|
_.each(_.values(this.handlers.message), (callback) => callback(stanza));
|
|
|
|
this.onMessage(stanza);
|
|
|
|
return true;
|
|
|
|
}, null, 'message', 'groupchat', null, room_jid,
|
|
|
|
{'matchBareFromJid': true}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeHandlers () {
|
|
|
|
/* Remove the presence and message handlers that were
|
2018-07-22 10:58:04 +02:00
|
|
|
* registered for this groupchat.
|
2018-04-08 19:44:53 +02:00
|
|
|
*/
|
|
|
|
if (this.message_handler) {
|
|
|
|
_converse.connection.deleteHandler(this.message_handler);
|
|
|
|
delete this.message_handler;
|
|
|
|
}
|
|
|
|
if (this.presence_handler) {
|
|
|
|
_converse.connection.deleteHandler(this.presence_handler);
|
|
|
|
delete this.presence_handler;
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
addHandler (type, name, callback) {
|
|
|
|
/* Allows 'presence' and 'message' handlers to be
|
|
|
|
* registered. These will be executed once presence or
|
|
|
|
* message stanzas are received, and *before* this model's
|
|
|
|
* own handlers are executed.
|
|
|
|
*/
|
|
|
|
if (_.isNil(this.handlers)) {
|
|
|
|
this.handlers = {};
|
|
|
|
}
|
|
|
|
if (_.isNil(this.handlers[type])) {
|
|
|
|
this.handlers[type] = {};
|
|
|
|
}
|
|
|
|
this.handlers[type][name] = callback;
|
|
|
|
},
|
|
|
|
|
2018-06-04 19:53:33 +02:00
|
|
|
getDisplayName () {
|
|
|
|
return this.get('name') || this.get('jid');
|
|
|
|
},
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
join (nick, password) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Join the groupchat.
|
2018-04-08 19:44:53 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) nick: The user's nickname
|
|
|
|
* (String) password: Optional password, if required by
|
2018-07-22 10:58:04 +02:00
|
|
|
* the groupchat.
|
2018-04-08 19:44:53 +02:00
|
|
|
*/
|
|
|
|
nick = nick ? nick : this.get('nick');
|
|
|
|
if (!nick) {
|
|
|
|
throw new TypeError('join: You need to provide a valid nickname');
|
|
|
|
}
|
|
|
|
if (this.get('connection_status') === converse.ROOMSTATUS.ENTERED) {
|
2018-07-22 10:58:04 +02:00
|
|
|
// We have restored a groupchat from session storage,
|
2018-04-08 19:44:53 +02:00
|
|
|
// so we don't send out a presence stanza again.
|
|
|
|
return this;
|
|
|
|
}
|
2018-09-06 17:09:32 +02:00
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
const stanza = $pres({
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
'to': this.getRoomJIDAndNick(nick)
|
|
|
|
}).c("x", {'xmlns': Strophe.NS.MUC})
|
2018-09-06 17:09:32 +02:00
|
|
|
.c("history", {'maxstanzas': this.get('mam_enabled') ? 0 : _converse.muc_history_max_stanzas}).up();
|
2018-04-08 19:44:53 +02:00
|
|
|
if (password) {
|
|
|
|
stanza.cnode(Strophe.xmlElement("password", [], password));
|
|
|
|
}
|
|
|
|
this.save('connection_status', converse.ROOMSTATUS.CONNECTING);
|
|
|
|
_converse.connection.send(stanza);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
leave (exit_msg) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Leave the groupchat.
|
2018-04-08 19:44:53 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) exit_msg: Optional message to indicate your
|
|
|
|
* reason for leaving.
|
|
|
|
*/
|
|
|
|
this.occupants.browserStorage._clear();
|
2018-05-01 18:18:02 +02:00
|
|
|
this.occupants.reset();
|
2018-09-13 11:23:17 +02:00
|
|
|
const disco_entity = _converse.disco_entities.get(this.get('jid'));
|
|
|
|
if (disco_entity) {
|
|
|
|
disco_entity.destroy();
|
|
|
|
}
|
2018-04-08 19:44:53 +02:00
|
|
|
if (_converse.connection.connected) {
|
|
|
|
this.sendUnavailablePresence(exit_msg);
|
|
|
|
}
|
|
|
|
u.safeSave(this, {'connection_status': converse.ROOMSTATUS.DISCONNECTED});
|
|
|
|
this.removeHandlers();
|
|
|
|
},
|
|
|
|
|
|
|
|
sendUnavailablePresence (exit_msg) {
|
|
|
|
const presence = $pres({
|
|
|
|
type: "unavailable",
|
|
|
|
from: _converse.connection.jid,
|
|
|
|
to: this.getRoomJIDAndNick()
|
|
|
|
});
|
|
|
|
if (exit_msg !== null) {
|
|
|
|
presence.c("status", exit_msg);
|
|
|
|
}
|
|
|
|
_converse.connection.sendPresence(presence);
|
|
|
|
},
|
|
|
|
|
2018-08-15 17:22:24 +02:00
|
|
|
getReferenceForMention (mention, index) {
|
2018-08-16 17:11:04 +02:00
|
|
|
const longest_match = u.getLongestSubstring(
|
|
|
|
mention,
|
|
|
|
this.occupants.map(o => o.getDisplayName())
|
|
|
|
);
|
2018-08-15 17:22:24 +02:00
|
|
|
if (!longest_match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ((mention[longest_match.length] || '').match(/[A-Za-zäëïöüâêîôûáéíóúàèìòùÄËÏÖÜÂÊÎÔÛÁÉÍÓÚÀÈÌÒÙ]/i)) {
|
|
|
|
// avoid false positives, i.e. mentions that have
|
|
|
|
// further alphabetical characters than our longest
|
|
|
|
// match.
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-16 17:11:04 +02:00
|
|
|
const occupant = this.occupants.findOccupant({'nick': longest_match}) ||
|
|
|
|
this.occupants.findOccupant({'jid': longest_match});
|
2018-08-15 17:22:24 +02:00
|
|
|
if (!occupant) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const obj = {
|
|
|
|
'begin': index,
|
|
|
|
'end': index + longest_match.length,
|
2018-08-16 14:19:36 +02:00
|
|
|
'value': longest_match,
|
2018-08-15 17:22:24 +02:00
|
|
|
'type': 'mention'
|
|
|
|
};
|
|
|
|
if (occupant.get('jid')) {
|
|
|
|
obj.uri = `xmpp:${occupant.get('jid')}`
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
extractReference (text, index) {
|
|
|
|
for (let i=index; i<text.length; i++) {
|
|
|
|
if (text[i] !== '@') {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
const match = text.slice(i+1),
|
2018-08-16 17:11:04 +02:00
|
|
|
ref = this.getReferenceForMention(match, i);
|
2018-08-15 17:22:24 +02:00
|
|
|
if (ref) {
|
|
|
|
return [text.slice(0, i) + match, ref, i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
|
2018-08-16 12:15:23 +02:00
|
|
|
parseTextForReferences (text) {
|
2018-08-15 17:22:24 +02:00
|
|
|
const refs = [];
|
|
|
|
let index = 0;
|
|
|
|
while (index < (text || '').length) {
|
|
|
|
const result = this.extractReference(text, index);
|
|
|
|
if (result) {
|
|
|
|
text = result[0]; // @ gets filtered out
|
|
|
|
refs.push(result[1]);
|
|
|
|
index = result[2];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [text, refs];
|
|
|
|
},
|
|
|
|
|
2018-04-24 11:08:26 +02:00
|
|
|
getOutgoingMessageAttributes (text, spoiler_hint) {
|
|
|
|
const is_spoiler = this.get('composing_spoiler');
|
2018-08-15 17:22:24 +02:00
|
|
|
var references;
|
2018-08-16 12:15:23 +02:00
|
|
|
[text, references] = this.parseTextForReferences(text);
|
2018-08-15 17:22:24 +02:00
|
|
|
|
2018-04-24 11:08:26 +02:00
|
|
|
return {
|
2018-05-01 18:18:02 +02:00
|
|
|
'from': `${this.get('jid')}/${this.get('nick')}`,
|
2018-04-24 11:08:26 +02:00
|
|
|
'fullname': this.get('nick'),
|
|
|
|
'is_spoiler': is_spoiler,
|
2018-09-03 14:06:18 +02:00
|
|
|
'message': text ? u.httpToGeoUri(u.shortnameToUnicode(text), _converse) : undefined,
|
2018-08-15 17:22:24 +02:00
|
|
|
'nick': this.get('nick'),
|
|
|
|
'references': references,
|
2018-04-24 11:08:26 +02:00
|
|
|
'sender': 'me',
|
|
|
|
'spoiler_hint': is_spoiler ? spoiler_hint : undefined,
|
2018-05-01 18:18:02 +02:00
|
|
|
'type': 'groupchat'
|
2018-04-24 11:08:26 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
getRoomJIDAndNick (nick) {
|
|
|
|
/* Utility method to construct the JID for the current user
|
2018-07-22 10:58:04 +02:00
|
|
|
* as occupant of the groupchat.
|
2018-04-08 19:44:53 +02:00
|
|
|
*
|
2018-07-22 10:58:04 +02:00
|
|
|
* This is the groupchat JID, with the user's nick added at the
|
2018-04-08 19:44:53 +02:00
|
|
|
* end.
|
|
|
|
*
|
2018-07-22 10:58:04 +02:00
|
|
|
* For example: groupchat@conference.example.org/nickname
|
2018-04-08 19:44:53 +02:00
|
|
|
*/
|
|
|
|
if (nick) {
|
|
|
|
this.save({'nick': nick});
|
|
|
|
} else {
|
|
|
|
nick = this.get('nick');
|
|
|
|
}
|
2018-07-22 10:58:04 +02:00
|
|
|
const groupchat = this.get('jid');
|
|
|
|
const jid = Strophe.getBareJidFromJid(groupchat);
|
2018-04-08 19:44:53 +02:00
|
|
|
return jid + (nick !== null ? `/${nick}` : "");
|
|
|
|
},
|
2018-09-02 15:07:14 +02:00
|
|
|
|
2018-04-22 04:20:14 +02:00
|
|
|
sendChatState () {
|
|
|
|
/* Sends a message with the status of the user in this chat session
|
|
|
|
* as taken from the 'chat_state' attribute of the chat box.
|
|
|
|
* See XEP-0085 Chat State Notifications.
|
|
|
|
*/
|
|
|
|
if (this.get('connection_status') !== converse.ROOMSTATUS.ENTERED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const chat_state = this.get('chat_state');
|
|
|
|
if (chat_state === _converse.GONE) {
|
|
|
|
// <gone/> is not applicable within MUC context
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_converse.connection.send(
|
|
|
|
$msg({'to':this.get('jid'), 'type': 'groupchat'})
|
|
|
|
.c(chat_state, {'xmlns': Strophe.NS.CHATSTATES}).up()
|
|
|
|
.c('no-store', {'xmlns': Strophe.NS.HINTS}).up()
|
|
|
|
.c('no-permanent-store', {'xmlns': Strophe.NS.HINTS})
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2018-03-29 11:56:24 +02:00
|
|
|
directInvite (recipient, reason) {
|
|
|
|
/* Send a direct invitation as per XEP-0249
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) recipient - JID of the person being invited
|
|
|
|
* (String) reason - Optional reason for the invitation
|
|
|
|
*/
|
|
|
|
if (this.get('membersonly')) {
|
2018-07-22 10:58:04 +02:00
|
|
|
// When inviting to a members-only groupchat, we first add
|
2018-03-29 11:56:24 +02:00
|
|
|
// the person to the member list by giving them an
|
|
|
|
// affiliation of 'member' (if they're not affiliated
|
|
|
|
// already), otherwise they won't be able to join.
|
|
|
|
const map = {}; map[recipient] = 'member';
|
|
|
|
const deltaFunc = _.partial(u.computeAffiliationsDelta, true, false);
|
|
|
|
this.updateMemberLists(
|
|
|
|
[{'jid': recipient, 'affiliation': 'member', 'reason': reason}],
|
|
|
|
['member', 'owner', 'admin'],
|
|
|
|
deltaFunc
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const attrs = {
|
|
|
|
'xmlns': 'jabber:x:conference',
|
|
|
|
'jid': this.get('jid')
|
|
|
|
};
|
|
|
|
if (reason !== null) { attrs.reason = reason; }
|
|
|
|
if (this.get('password')) { attrs.password = this.get('password'); }
|
2018-06-30 13:46:56 +02:00
|
|
|
|
2018-03-29 11:56:24 +02:00
|
|
|
const invitation = $msg({
|
|
|
|
from: _converse.connection.jid,
|
|
|
|
to: recipient,
|
|
|
|
id: _converse.connection.getUniqueId()
|
|
|
|
}).c('x', attrs);
|
|
|
|
_converse.connection.send(invitation);
|
|
|
|
_converse.emit('roomInviteSent', {
|
|
|
|
'room': this,
|
|
|
|
'recipient': recipient,
|
|
|
|
'reason': reason
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-10-05 04:39:19 +02:00
|
|
|
async refreshRoomFeatures () {
|
|
|
|
await _converse.api.disco.refreshFeatures(this.get('jid'));
|
2018-09-12 12:32:50 +02:00
|
|
|
return this.getRoomFeatures();
|
|
|
|
},
|
|
|
|
|
|
|
|
async getRoomFeatures () {
|
|
|
|
const features = await _converse.api.disco.getFeatures(this.get('jid')),
|
|
|
|
fields = await _converse.api.disco.getFields(this.get('jid')),
|
|
|
|
identity = await _converse.api.disco.getIdentity('conference', 'text', this.get('jid')),
|
|
|
|
attrs = {
|
|
|
|
'features_fetched': moment().format(),
|
|
|
|
'name': identity && identity.get('name')
|
|
|
|
};
|
2018-10-05 04:39:19 +02:00
|
|
|
|
2018-09-12 12:32:50 +02:00
|
|
|
features.each(feature => {
|
|
|
|
const fieldname = feature.get('var');
|
2018-03-29 10:16:41 +02:00
|
|
|
if (!fieldname.startsWith('muc_')) {
|
|
|
|
if (fieldname === Strophe.NS.MAM) {
|
2018-09-12 12:32:50 +02:00
|
|
|
attrs.mam_enabled = true;
|
2018-03-29 10:16:41 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2018-09-12 12:32:50 +02:00
|
|
|
attrs[fieldname.replace('muc_', '')] = true;
|
2018-03-29 10:16:41 +02:00
|
|
|
});
|
2018-09-12 12:32:50 +02:00
|
|
|
attrs.description = _.get(fields.findWhere({'var': "muc#roominfo_description"}), 'attributes.value');
|
|
|
|
this.save(attrs);
|
2018-03-29 10:16:41 +02:00
|
|
|
},
|
|
|
|
|
2018-03-29 11:56:24 +02:00
|
|
|
requestMemberList (affiliation) {
|
|
|
|
/* Send an IQ stanza to the server, asking it for the
|
2018-07-22 10:58:04 +02:00
|
|
|
* member-list of this groupchat.
|
2018-03-29 11:56:24 +02:00
|
|
|
*
|
|
|
|
* See: http://xmpp.org/extensions/xep-0045.html#modifymember
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) affiliation: The specific member list to
|
|
|
|
* fetch. 'admin', 'owner' or 'member'.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* A promise which resolves once the list has been
|
|
|
|
* retrieved.
|
|
|
|
*/
|
2018-08-09 14:44:39 +02:00
|
|
|
affiliation = affiliation || 'member';
|
|
|
|
const iq = $iq({to: this.get('jid'), type: "get"})
|
|
|
|
.c("query", {xmlns: Strophe.NS.MUC_ADMIN})
|
|
|
|
.c("item", {'affiliation': affiliation});
|
|
|
|
return _converse.api.sendIQ(iq);
|
2018-03-29 11:56:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
setAffiliation (affiliation, members) {
|
|
|
|
/* Send IQ stanzas to the server to set an affiliation for
|
|
|
|
* the provided JIDs.
|
|
|
|
*
|
|
|
|
* See: http://xmpp.org/extensions/xep-0045.html#modifymember
|
|
|
|
*
|
|
|
|
* XXX: Prosody doesn't accept multiple JIDs' affiliations
|
|
|
|
* being set in one IQ stanza, so as a workaround we send
|
|
|
|
* a separate stanza for each JID.
|
2018-09-12 15:28:55 +02:00
|
|
|
* Related ticket: https://issues.prosody.im/345
|
2018-03-29 11:56:24 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String) affiliation: The affiliation
|
|
|
|
* (Object) members: A map of jids, affiliations and
|
|
|
|
* optionally reasons. Only those entries with the
|
|
|
|
* same affiliation as being currently set will be
|
|
|
|
* considered.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* A promise which resolves and fails depending on the
|
|
|
|
* XMPP server response.
|
|
|
|
*/
|
|
|
|
members = _.filter(members, (member) =>
|
|
|
|
// We only want those members who have the right
|
|
|
|
// affiliation (or none, which implies the provided one).
|
|
|
|
_.isUndefined(member.affiliation) ||
|
|
|
|
member.affiliation === affiliation
|
|
|
|
);
|
|
|
|
const promises = _.map(members, _.bind(this.sendAffiliationIQ, this, affiliation));
|
|
|
|
return Promise.all(promises);
|
|
|
|
},
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
saveConfiguration (form) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Submit the groupchat configuration form by sending an IQ
|
2018-04-08 19:44:53 +02:00
|
|
|
* stanza to the server.
|
|
|
|
*
|
|
|
|
* Returns a promise which resolves once the XMPP server
|
|
|
|
* has return a response IQ.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (HTMLElement) form: The configuration form DOM element.
|
|
|
|
* If no form is provided, the default configuration
|
|
|
|
* values will be used.
|
|
|
|
*/
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const inputs = form ? sizzle(':input:not([type=button]):not([type=submit])', form) : [],
|
|
|
|
configArray = _.map(inputs, u.webForm2xForm);
|
|
|
|
this.sendConfiguration(configArray, resolve, reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
autoConfigureChatRoom () {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Automatically configure groupchat based on this model's
|
2018-04-08 19:44:53 +02:00
|
|
|
* 'roomconfig' data.
|
|
|
|
*
|
|
|
|
* Returns a promise which resolves once a response IQ has
|
|
|
|
* been received.
|
|
|
|
*/
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.fetchRoomConfiguration().then((stanza) => {
|
|
|
|
const configArray = [],
|
|
|
|
fields = stanza.querySelectorAll('field'),
|
|
|
|
config = this.get('roomconfig');
|
|
|
|
let count = fields.length;
|
|
|
|
|
|
|
|
_.each(fields, (field) => {
|
|
|
|
const fieldname = field.getAttribute('var').replace('muc#roomconfig_', ''),
|
|
|
|
type = field.getAttribute('type');
|
|
|
|
let value;
|
|
|
|
if (fieldname in config) {
|
|
|
|
switch (type) {
|
|
|
|
case 'boolean':
|
|
|
|
value = config[fieldname] ? 1 : 0;
|
|
|
|
break;
|
|
|
|
case 'list-multi':
|
|
|
|
// TODO: we don't yet handle "list-multi" types
|
|
|
|
value = field.innerHTML;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
value = config[fieldname];
|
|
|
|
}
|
|
|
|
field.innerHTML = $build('value').t(value);
|
|
|
|
}
|
|
|
|
configArray.push(field);
|
|
|
|
if (!--count) {
|
|
|
|
this.sendConfiguration(configArray, resolve, reject);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchRoomConfiguration () {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Send an IQ stanza to fetch the groupchat configuration data.
|
2018-04-08 19:44:53 +02:00
|
|
|
* Returns a promise which resolves once the response IQ
|
|
|
|
* has been received.
|
|
|
|
*/
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
_converse.connection.sendIQ(
|
|
|
|
$iq({
|
|
|
|
'to': this.get('jid'),
|
|
|
|
'type': "get"
|
|
|
|
}).c("query", {xmlns: Strophe.NS.MUC_OWNER}),
|
|
|
|
resolve,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
sendConfiguration (config, callback, errback) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Send an IQ stanza with the groupchat configuration.
|
2018-04-08 19:44:53 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
2018-07-22 10:58:04 +02:00
|
|
|
* (Array) config: The groupchat configuration
|
2018-04-08 19:44:53 +02:00
|
|
|
* (Function) callback: Callback upon succesful IQ response
|
|
|
|
* The first parameter passed in is IQ containing the
|
2018-07-22 10:58:04 +02:00
|
|
|
* groupchat configuration.
|
2018-04-08 19:44:53 +02:00
|
|
|
* The second is the response IQ from the server.
|
|
|
|
* (Function) errback: Callback upon error IQ response
|
|
|
|
* The first parameter passed in is IQ containing the
|
2018-07-22 10:58:04 +02:00
|
|
|
* groupchat configuration.
|
2018-04-08 19:44:53 +02:00
|
|
|
* The second is the response IQ from the server.
|
|
|
|
*/
|
|
|
|
const iq = $iq({to: this.get('jid'), type: "set"})
|
|
|
|
.c("query", {xmlns: Strophe.NS.MUC_OWNER})
|
|
|
|
.c("x", {xmlns: Strophe.NS.XFORM, type: "submit"});
|
|
|
|
_.each(config || [], function (node) { iq.cnode(node).up(); });
|
|
|
|
callback = _.isUndefined(callback) ? _.noop : _.partial(callback, iq.nodeTree);
|
|
|
|
errback = _.isUndefined(errback) ? _.noop : _.partial(errback, iq.nodeTree);
|
|
|
|
return _converse.connection.sendIQ(iq, callback, errback);
|
|
|
|
},
|
|
|
|
|
2018-03-29 11:56:24 +02:00
|
|
|
saveAffiliationAndRole (pres) {
|
|
|
|
/* Parse the presence stanza for the current user's
|
|
|
|
* affiliation.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) pres: A <presence> stanza.
|
|
|
|
*/
|
|
|
|
const item = sizzle(`x[xmlns="${Strophe.NS.MUC_USER}"] item`, pres).pop();
|
|
|
|
const is_self = pres.querySelector("status[code='110']");
|
|
|
|
if (is_self && !_.isNil(item)) {
|
|
|
|
const affiliation = item.getAttribute('affiliation');
|
|
|
|
const role = item.getAttribute('role');
|
|
|
|
if (affiliation) {
|
|
|
|
this.save({'affiliation': affiliation});
|
|
|
|
}
|
|
|
|
if (role) {
|
|
|
|
this.save({'role': role});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
sendAffiliationIQ (affiliation, member) {
|
|
|
|
/* Send an IQ stanza specifying an affiliation change.
|
|
|
|
*
|
|
|
|
* Paremeters:
|
|
|
|
* (String) affiliation: affiliation (could also be stored
|
|
|
|
* on the member object).
|
|
|
|
* (Object) member: Map containing the member's jid and
|
|
|
|
* optionally a reason and affiliation.
|
|
|
|
*/
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const iq = $iq({to: this.get('jid'), type: "set"})
|
|
|
|
.c("query", {xmlns: Strophe.NS.MUC_ADMIN})
|
|
|
|
.c("item", {
|
|
|
|
'affiliation': member.affiliation || affiliation,
|
2018-09-12 15:28:55 +02:00
|
|
|
'nick': member.nick,
|
2018-03-29 11:56:24 +02:00
|
|
|
'jid': member.jid
|
|
|
|
});
|
|
|
|
if (!_.isUndefined(member.reason)) {
|
|
|
|
iq.c("reason", member.reason);
|
|
|
|
}
|
|
|
|
_converse.connection.sendIQ(iq, resolve, reject);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setAffiliations (members) {
|
|
|
|
/* Send IQ stanzas to the server to modify the
|
2018-07-22 10:58:04 +02:00
|
|
|
* affiliations in this groupchat.
|
2018-03-29 11:56:24 +02:00
|
|
|
*
|
|
|
|
* See: http://xmpp.org/extensions/xep-0045.html#modifymember
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) members: A map of jids, affiliations and optionally reasons
|
|
|
|
* (Function) onSuccess: callback for a succesful response
|
|
|
|
* (Function) onError: callback for an error response
|
|
|
|
*/
|
|
|
|
const affiliations = _.uniq(_.map(members, 'affiliation'));
|
2018-05-10 23:48:09 +02:00
|
|
|
return Promise.all(_.map(affiliations, _.partial(this.setAffiliation.bind(this), _, members)));
|
2018-03-29 11:56:24 +02:00
|
|
|
},
|
|
|
|
|
2018-09-13 14:55:12 +02:00
|
|
|
async getJidsWithAffiliations (affiliations) {
|
2018-03-29 11:56:24 +02:00
|
|
|
/* Returns a map of JIDs that have the affiliations
|
|
|
|
* as provided.
|
|
|
|
*/
|
|
|
|
if (_.isString(affiliations)) {
|
|
|
|
affiliations = [affiliations];
|
|
|
|
}
|
2018-09-13 14:55:12 +02:00
|
|
|
const result = await Promise.all(affiliations.map(a =>
|
|
|
|
this.requestMemberList(a)
|
|
|
|
.then(iq => u.parseMemberListIQ(iq))
|
|
|
|
.catch(iq => {
|
|
|
|
_converse.log(iq, Strophe.LogLevel.ERROR);
|
|
|
|
})
|
|
|
|
));
|
|
|
|
return [].concat.apply([], result).filter(p => p);
|
2018-03-29 11:56:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
updateMemberLists (members, affiliations, deltaFunc) {
|
|
|
|
/* Fetch the lists of users with the given affiliations.
|
|
|
|
* Then compute the delta between those users and
|
|
|
|
* the passed in members, and if it exists, send the delta
|
|
|
|
* to the XMPP server to update the member list.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) members: Map of member jids and affiliations.
|
|
|
|
* (String|Array) affiliation: An array of affiliations or
|
|
|
|
* a string if only one affiliation.
|
|
|
|
* (Function) deltaFunc: The function to compute the delta
|
|
|
|
* between old and new member lists.
|
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* A promise which is resolved once the list has been
|
|
|
|
* updated or once it's been established there's no need
|
|
|
|
* to update the list.
|
|
|
|
*/
|
2018-05-10 23:48:09 +02:00
|
|
|
this.getJidsWithAffiliations(affiliations)
|
2018-09-13 14:55:12 +02:00
|
|
|
.then(old_members => this.setAffiliations(deltaFunc(members, old_members)))
|
2018-05-10 23:48:09 +02:00
|
|
|
.then(() => this.occupants.fetchMembers())
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
|
2018-03-29 11:56:24 +02:00
|
|
|
},
|
|
|
|
|
2018-09-12 07:20:57 +02:00
|
|
|
getDefaultNick () {
|
|
|
|
/* The default nickname (used when muc_nickname_from_jid is true)
|
|
|
|
* is the node part of the user's JID.
|
|
|
|
* We put this in a separate method so that it can be
|
|
|
|
* overridden by plugins.
|
|
|
|
*/
|
|
|
|
const nick = _converse.xmppstatus.vcard.get('nickname');
|
|
|
|
if (nick) {
|
|
|
|
return nick;
|
|
|
|
} else if (_converse.muc_nickname_from_jid) {
|
|
|
|
return Strophe.unescapeNode(Strophe.getNodeFromJid(_converse.bare_jid));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
checkForReservedNick () {
|
2018-03-29 10:16:41 +02:00
|
|
|
/* Use service-discovery to ask the XMPP server whether
|
2018-07-22 10:58:04 +02:00
|
|
|
* this user has a reserved nickname for this groupchat.
|
2018-03-29 10:16:41 +02:00
|
|
|
* If so, we'll use that, otherwise we render the nickname form.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Function) callback: Callback upon succesful IQ response
|
|
|
|
* (Function) errback: Callback upon error IQ response
|
|
|
|
*/
|
2018-09-12 07:20:57 +02:00
|
|
|
return _converse.api.sendIQ(
|
2018-03-29 10:16:41 +02:00
|
|
|
$iq({
|
|
|
|
'to': this.get('jid'),
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
'type': "get"
|
|
|
|
}).c("query", {
|
|
|
|
'xmlns': Strophe.NS.DISCO_INFO,
|
|
|
|
'node': 'x-roomuser-item'
|
2018-09-12 07:20:57 +02:00
|
|
|
})
|
|
|
|
).then(iq => {
|
|
|
|
const identity_el = iq.querySelector('query[node="x-roomuser-item"] identity'),
|
|
|
|
nick = identity_el ? identity_el.getAttribute('name') : null;
|
|
|
|
this.save({
|
|
|
|
'reserved_nick': nick,
|
|
|
|
'nick': nick
|
|
|
|
}, {'silent': true});
|
|
|
|
return iq;
|
|
|
|
});
|
2018-03-29 10:16:41 +02:00
|
|
|
},
|
|
|
|
|
2018-09-04 11:39:27 +02:00
|
|
|
async registerNickname () {
|
2018-09-12 15:28:55 +02:00
|
|
|
// See https://xmpp.org/extensions/xep-0045.html#register
|
2018-09-12 15:06:08 +02:00
|
|
|
const nick = this.get('nick'),
|
|
|
|
jid = this.get('jid');
|
2018-09-13 12:39:50 +02:00
|
|
|
let iq, err_msg;
|
2018-09-04 11:39:27 +02:00
|
|
|
try {
|
2018-09-12 12:33:28 +02:00
|
|
|
iq = await _converse.api.sendIQ(
|
2018-09-04 11:39:27 +02:00
|
|
|
$iq({
|
2018-09-12 15:06:08 +02:00
|
|
|
'to': jid,
|
2018-09-12 12:33:28 +02:00
|
|
|
'from': _converse.connection.jid,
|
2018-09-04 11:39:27 +02:00
|
|
|
'type': 'get'
|
|
|
|
}).c('query', {'xmlns': Strophe.NS.MUC_REGISTER})
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2018-09-13 12:39:50 +02:00
|
|
|
if (sizzle('not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]', e).length) {
|
|
|
|
err_msg = __("You're not allowed to register yourself in this groupchat.");
|
|
|
|
} else if (sizzle('registration-required[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]', e).length) {
|
|
|
|
err_msg = __("You're not allowed to register in this groupchat because it's members-only.");
|
2018-09-04 11:39:27 +02:00
|
|
|
}
|
2018-09-13 12:39:50 +02:00
|
|
|
_converse.log(e, Strophe.LogLevel.ERROR);
|
|
|
|
return err_msg;
|
2018-09-04 11:39:27 +02:00
|
|
|
}
|
2018-09-13 14:22:59 +02:00
|
|
|
const required_fields = sizzle('field required', iq).map(f => f.parentElement);
|
|
|
|
if (required_fields.length > 1 && required_fields[0].getAttribute('var') !== 'muc#register_roomnick') {
|
2018-09-12 15:06:08 +02:00
|
|
|
return _converse.log(`Can't register the user register in the groupchat ${jid} due to the required fields`);
|
2018-09-12 12:33:28 +02:00
|
|
|
}
|
2018-09-04 11:39:27 +02:00
|
|
|
try {
|
|
|
|
await _converse.api.sendIQ($iq({
|
2018-09-12 15:06:08 +02:00
|
|
|
'to': jid,
|
2018-09-12 12:33:28 +02:00
|
|
|
'from': _converse.connection.jid,
|
2018-09-04 11:39:27 +02:00
|
|
|
'type': 'set'
|
|
|
|
}).c('query', {'xmlns': Strophe.NS.MUC_REGISTER})
|
|
|
|
.c('x', {'xmlns': Strophe.NS.XFORM, 'type': 'submit'})
|
|
|
|
.c('field', {'var': 'FORM_TYPE'}).c('value').t('http://jabber.org/protocol/muc#register').up().up()
|
2018-09-12 15:06:08 +02:00
|
|
|
.c('field', {'var': 'muc#register_roomnick'}).c('value').t(nick)
|
2018-09-04 11:39:27 +02:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2018-09-13 12:39:50 +02:00
|
|
|
if (sizzle('service-unavailable[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]', e).length) {
|
|
|
|
err_msg = __("Can't register your nickname in this groupchat, it doesn't support registration.");
|
2018-09-04 11:39:27 +02:00
|
|
|
} else if (sizzle('bad-request[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]', e).length) {
|
2018-09-13 12:39:50 +02:00
|
|
|
err_msg = __("Can't register your nickname in this groupchat, invalid data form supplied.");
|
2018-09-04 11:39:27 +02:00
|
|
|
}
|
2018-09-13 12:39:50 +02:00
|
|
|
_converse.log(err_msg);
|
|
|
|
_converse.log(e, Strophe.LogLevel.ERROR);
|
|
|
|
return err_msg;
|
2018-09-04 11:39:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
updateOccupantsOnPresence (pres) {
|
|
|
|
/* Given a presence stanza, update the occupant model
|
|
|
|
* based on its contents.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) pres: The presence stanza
|
|
|
|
*/
|
|
|
|
const data = this.parsePresence(pres);
|
2018-09-10 08:11:09 +02:00
|
|
|
if (data.type === 'error' || (!data.jid && !data.nick)) {
|
2018-04-08 19:44:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-09 12:28:34 +02:00
|
|
|
const occupant = this.occupants.findOccupant(data);
|
2018-05-23 12:48:11 +02:00
|
|
|
if (data.type === 'unavailable' && occupant) {
|
2018-05-24 09:09:26 +02:00
|
|
|
if (!_.includes(data.states, converse.MUC_NICK_CHANGED_CODE) && !occupant.isMember()) {
|
2018-05-23 12:48:11 +02:00
|
|
|
// We only destroy the occupant if this is not a nickname change operation.
|
|
|
|
// and if they're not on the member lists.
|
|
|
|
// Before destroying we set the new data, so
|
|
|
|
// that we can show the disconnection message.
|
2018-04-08 19:44:53 +02:00
|
|
|
occupant.set(data);
|
2018-05-23 12:48:11 +02:00
|
|
|
occupant.destroy();
|
2018-04-08 19:44:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const jid = Strophe.getBareJidFromJid(data.jid);
|
|
|
|
const attributes = _.extend(data, {
|
|
|
|
'jid': jid ? jid : undefined,
|
|
|
|
'resource': data.jid ? Strophe.getResourceFromJid(data.jid) : undefined
|
|
|
|
});
|
|
|
|
if (occupant) {
|
|
|
|
occupant.save(attributes);
|
|
|
|
} else {
|
|
|
|
this.occupants.create(attributes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parsePresence (pres) {
|
2018-05-03 14:50:27 +02:00
|
|
|
const from = pres.getAttribute("from"),
|
2018-05-24 09:09:26 +02:00
|
|
|
type = pres.getAttribute("type"),
|
2018-05-03 14:50:27 +02:00
|
|
|
data = {
|
|
|
|
'from': from,
|
|
|
|
'nick': Strophe.getResourceFromJid(from),
|
2018-05-24 09:09:26 +02:00
|
|
|
'type': type,
|
2018-05-09 12:28:34 +02:00
|
|
|
'states': [],
|
2018-05-24 09:09:26 +02:00
|
|
|
'show': type !== 'unavailable' ? 'online' : 'offline'
|
2018-05-03 14:50:27 +02:00
|
|
|
};
|
2018-04-08 19:44:53 +02:00
|
|
|
_.each(pres.childNodes, function (child) {
|
|
|
|
switch (child.nodeName) {
|
|
|
|
case "status":
|
|
|
|
data.status = child.textContent || null;
|
|
|
|
break;
|
|
|
|
case "show":
|
|
|
|
data.show = child.textContent || 'online';
|
|
|
|
break;
|
|
|
|
case "x":
|
|
|
|
if (child.getAttribute("xmlns") === Strophe.NS.MUC_USER) {
|
|
|
|
_.each(child.childNodes, function (item) {
|
|
|
|
switch (item.nodeName) {
|
|
|
|
case "item":
|
|
|
|
data.affiliation = item.getAttribute("affiliation");
|
|
|
|
data.role = item.getAttribute("role");
|
|
|
|
data.jid = item.getAttribute("jid");
|
|
|
|
data.nick = item.getAttribute("nick") || data.nick;
|
|
|
|
break;
|
|
|
|
case "status":
|
|
|
|
if (item.getAttribute("code")) {
|
|
|
|
data.states.push(item.getAttribute("code"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-05-03 13:47:31 +02:00
|
|
|
} else if (child.getAttribute("xmlns") === Strophe.NS.VCARDUPDATE) {
|
|
|
|
data.image_hash = _.get(child.querySelector('photo'), 'textContent');
|
2018-04-08 19:44:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
|
|
|
isDuplicate (message, original_stanza) {
|
|
|
|
const msgid = message.getAttribute('id'),
|
2018-05-02 15:40:47 +02:00
|
|
|
jid = message.getAttribute('from');
|
2018-04-08 19:44:53 +02:00
|
|
|
if (msgid) {
|
2018-05-02 15:40:47 +02:00
|
|
|
return this.messages.where({'msgid': msgid, 'from': jid}).length;
|
2018-04-08 19:44:53 +02:00
|
|
|
}
|
2018-04-10 20:56:40 +02:00
|
|
|
return false;
|
2018-04-08 19:44:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchFeaturesIfConfigurationChanged (stanza) {
|
|
|
|
const configuration_changed = stanza.querySelector("status[code='104']"),
|
|
|
|
logging_enabled = stanza.querySelector("status[code='170']"),
|
|
|
|
logging_disabled = stanza.querySelector("status[code='171']"),
|
|
|
|
room_no_longer_anon = stanza.querySelector("status[code='172']"),
|
|
|
|
room_now_semi_anon = stanza.querySelector("status[code='173']"),
|
|
|
|
room_now_fully_anon = stanza.querySelector("status[code='173']");
|
|
|
|
|
|
|
|
if (configuration_changed || logging_enabled || logging_disabled ||
|
|
|
|
room_no_longer_anon || room_now_semi_anon || room_now_fully_anon) {
|
2018-09-12 12:32:50 +02:00
|
|
|
this.refreshRoomFeatures();
|
2018-04-08 19:44:53 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onMessage (stanza) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Handler for all MUC messages sent to this groupchat.
|
2018-04-08 19:44:53 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) stanza: The message stanza.
|
|
|
|
*/
|
|
|
|
this.fetchFeaturesIfConfigurationChanged(stanza);
|
|
|
|
|
|
|
|
const original_stanza = stanza,
|
|
|
|
forwarded = stanza.querySelector('forwarded');
|
|
|
|
if (!_.isNull(forwarded)) {
|
|
|
|
stanza = forwarded.querySelector('message');
|
|
|
|
}
|
|
|
|
if (this.isDuplicate(stanza, original_stanza)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-16 00:49:00 +02:00
|
|
|
const jid = stanza.getAttribute('from'),
|
|
|
|
resource = Strophe.getResourceFromJid(jid),
|
|
|
|
sender = resource && Strophe.unescapeNode(resource) || '';
|
|
|
|
|
|
|
|
if (!this.handleMessageCorrection(stanza)) {
|
|
|
|
if (sender === '') {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-07 10:06:36 +02:00
|
|
|
const subject_el = stanza.querySelector('subject');
|
|
|
|
if (subject_el) {
|
|
|
|
const subject = _.propertyOf(subject_el)('textContent') || '';
|
|
|
|
u.safeSave(this, {'subject': {'author': sender, 'text': subject}});
|
|
|
|
}
|
2018-08-18 11:28:51 +02:00
|
|
|
this.createMessage(stanza, original_stanza)
|
|
|
|
.then(msg => this.incrementUnreadMsgCounter(msg))
|
|
|
|
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
|
2018-04-08 19:44:53 +02:00
|
|
|
}
|
|
|
|
if (sender !== this.get('nick')) {
|
|
|
|
// We only emit an event if it's not our own message
|
|
|
|
_converse.emit('message', {'stanza': original_stanza, 'chatbox': this});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onPresence (pres) {
|
|
|
|
/* Handles all MUC presence stanzas.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) pres: The stanza
|
|
|
|
*/
|
|
|
|
if (pres.getAttribute('type') === 'error') {
|
|
|
|
this.save('connection_status', converse.ROOMSTATUS.DISCONNECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const is_self = pres.querySelector("status[code='110']");
|
|
|
|
if (is_self && pres.getAttribute('type') !== 'unavailable') {
|
|
|
|
this.onOwnPresence(pres);
|
|
|
|
}
|
|
|
|
this.updateOccupantsOnPresence(pres);
|
|
|
|
if (this.get('role') !== 'none' && this.get('connection_status') === converse.ROOMSTATUS.CONNECTING) {
|
|
|
|
this.save('connection_status', converse.ROOMSTATUS.CONNECTED);
|
|
|
|
}
|
|
|
|
},
|
2018-09-02 15:07:14 +02:00
|
|
|
|
2018-04-08 19:44:53 +02:00
|
|
|
onOwnPresence (pres) {
|
|
|
|
/* Handles a received presence relating to the current
|
|
|
|
* user.
|
|
|
|
*
|
2018-07-22 10:58:04 +02:00
|
|
|
* For locked groupchats (which are by definition "new"), the
|
|
|
|
* groupchat will either be auto-configured or created instantly
|
|
|
|
* (with default config) or a configuration groupchat will be
|
2018-04-08 19:44:53 +02:00
|
|
|
* rendered.
|
|
|
|
*
|
2018-07-22 10:58:04 +02:00
|
|
|
* If the groupchat is not locked, then the groupchat will be
|
2018-04-08 19:44:53 +02:00
|
|
|
* auto-configured only if applicable and if the current
|
2018-07-22 10:58:04 +02:00
|
|
|
* user is the groupchat's owner.
|
2018-04-08 19:44:53 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) pres: The stanza
|
|
|
|
*/
|
|
|
|
this.saveAffiliationAndRole(pres);
|
|
|
|
|
|
|
|
const locked_room = pres.querySelector("status[code='201']");
|
|
|
|
if (locked_room) {
|
|
|
|
if (this.get('auto_configure')) {
|
2018-09-12 12:32:50 +02:00
|
|
|
this.autoConfigureChatRoom().then(() => this.refreshRoomFeatures());
|
2018-04-08 19:44:53 +02:00
|
|
|
} else if (_converse.muc_instant_rooms) {
|
|
|
|
// Accept default configuration
|
2018-09-12 12:32:50 +02:00
|
|
|
this.saveConfiguration().then(() => this.getRoomFeatures());
|
2018-04-08 19:44:53 +02:00
|
|
|
} else {
|
|
|
|
this.trigger('configurationNeeded');
|
2018-07-22 10:58:04 +02:00
|
|
|
return; // We haven't yet entered the groupchat, so bail here.
|
2018-04-08 19:44:53 +02:00
|
|
|
}
|
|
|
|
} else if (!this.get('features_fetched')) {
|
2018-07-22 10:58:04 +02:00
|
|
|
// The features for this groupchat weren't fetched.
|
|
|
|
// That must mean it's a new groupchat without locking
|
2018-04-08 19:44:53 +02:00
|
|
|
// (in which case Prosody doesn't send a 201 status),
|
|
|
|
// otherwise the features would have been fetched in
|
|
|
|
// the "initialize" method already.
|
|
|
|
if (this.get('affiliation') === 'owner' && this.get('auto_configure')) {
|
2018-09-12 12:32:50 +02:00
|
|
|
this.autoConfigureChatRoom().then(() => this.refreshRoomFeatures());
|
2018-04-08 19:44:53 +02:00
|
|
|
} else {
|
|
|
|
this.getRoomFeatures();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.save('connection_status', converse.ROOMSTATUS.ENTERED);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
isUserMentioned (message) {
|
2017-05-23 18:36:40 +02:00
|
|
|
/* Returns a boolean to indicate whether the current user
|
|
|
|
* was mentioned in a message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (String): The text message
|
|
|
|
*/
|
2018-08-16 14:19:36 +02:00
|
|
|
const nick = this.get('nick');
|
|
|
|
if (message.get('references').length) {
|
|
|
|
const mentions = message.get('references').filter(ref => (ref.type === 'mention')).map(ref => ref.value);
|
|
|
|
return _.includes(mentions, nick);
|
|
|
|
} else {
|
|
|
|
return (new RegExp(`\\b${nick}\\b`)).test(message.get('message'));
|
|
|
|
}
|
2017-05-23 18:36:40 +02:00
|
|
|
},
|
|
|
|
|
2018-08-16 14:19:36 +02:00
|
|
|
incrementUnreadMsgCounter (message) {
|
2017-05-23 18:36:40 +02:00
|
|
|
/* Given a newly received message, update the unread counter if
|
|
|
|
* necessary.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement): The <messsage> stanza
|
|
|
|
*/
|
2018-08-16 14:19:36 +02:00
|
|
|
if (!message) { return; }
|
|
|
|
const body = message.get('message');
|
|
|
|
if (_.isNil(body)) { return; }
|
|
|
|
if (u.isNewMessage(message) && this.isHidden()) {
|
2018-03-29 10:16:41 +02:00
|
|
|
const settings = {'num_unread_general': this.get('num_unread_general') + 1};
|
2018-08-16 14:19:36 +02:00
|
|
|
if (this.isUserMentioned(message)) {
|
2018-03-29 10:16:41 +02:00
|
|
|
settings.num_unread = this.get('num_unread') + 1;
|
2017-05-24 08:40:09 +02:00
|
|
|
_converse.incrementMsgCounter();
|
|
|
|
}
|
2018-03-29 10:16:41 +02:00
|
|
|
this.save(settings);
|
2017-05-23 18:36:40 +02:00
|
|
|
}
|
|
|
|
},
|
2017-05-24 08:40:09 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
clearUnreadMsgCounter() {
|
2017-12-06 14:59:01 +01:00
|
|
|
u.safeSave(this, {
|
2017-06-19 11:08:57 +02:00
|
|
|
'num_unread': 0,
|
|
|
|
'num_unread_general': 0
|
|
|
|
});
|
2017-05-24 08:40:09 +02:00
|
|
|
}
|
2017-05-23 18:36:40 +02:00
|
|
|
});
|
|
|
|
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2018-02-21 22:29:21 +01:00
|
|
|
_converse.ChatRoomOccupant = Backbone.Model.extend({
|
2018-05-09 12:28:34 +02:00
|
|
|
|
|
|
|
defaults: {
|
|
|
|
'show': 'offline'
|
|
|
|
},
|
|
|
|
|
2018-02-21 22:29:21 +01:00
|
|
|
initialize (attributes) {
|
|
|
|
this.set(_.extend({
|
|
|
|
'id': _converse.connection.getUniqueId(),
|
|
|
|
}, attributes));
|
2018-05-03 14:50:27 +02:00
|
|
|
|
|
|
|
this.on('change:image_hash', this.onAvatarChanged, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
onAvatarChanged () {
|
|
|
|
const hash = this.get('image_hash');
|
2018-05-25 15:55:34 +02:00
|
|
|
const vcards = [];
|
|
|
|
if (this.get('jid')) {
|
2018-06-05 11:14:51 +02:00
|
|
|
vcards.push(_converse.vcards.findWhere({'jid': this.get('jid')}));
|
2018-05-03 14:50:27 +02:00
|
|
|
}
|
2018-06-05 11:14:51 +02:00
|
|
|
vcards.push(_converse.vcards.findWhere({'jid': this.get('from')}));
|
2018-05-25 15:55:34 +02:00
|
|
|
|
|
|
|
_.forEach(_.filter(vcards, undefined), (vcard) => {
|
|
|
|
if (hash && vcard.get('image_hash') !== hash) {
|
|
|
|
_converse.api.vcard.update(vcard);
|
|
|
|
}
|
|
|
|
});
|
2018-05-10 23:28:03 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getDisplayName () {
|
|
|
|
return this.get('nick') || this.get('jid');
|
2018-05-24 09:09:26 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
isMember () {
|
|
|
|
return _.includes(['admin', 'owner', 'member'], this.get('affiliation'));
|
2018-02-21 22:29:21 +01:00
|
|
|
}
|
|
|
|
});
|
2017-12-14 14:07:39 +01:00
|
|
|
|
2017-12-07 07:05:37 +01:00
|
|
|
|
2018-02-21 22:29:21 +01:00
|
|
|
_converse.ChatRoomOccupants = Backbone.Collection.extend({
|
|
|
|
model: _converse.ChatRoomOccupant,
|
2017-02-18 11:03:26 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
comparator (occupant1, occupant2) {
|
|
|
|
const role1 = occupant1.get('role') || 'none';
|
|
|
|
const role2 = occupant2.get('role') || 'none';
|
|
|
|
if (MUC_ROLE_WEIGHTS[role1] === MUC_ROLE_WEIGHTS[role2]) {
|
2018-05-10 23:28:03 +02:00
|
|
|
const nick1 = occupant1.getDisplayName().toLowerCase();
|
|
|
|
const nick2 = occupant2.getDisplayName().toLowerCase();
|
2018-05-09 14:35:05 +02:00
|
|
|
return nick1 < nick2 ? -1 : (nick1 > nick2? 1 : 0);
|
2018-02-21 22:40:51 +01:00
|
|
|
} else {
|
|
|
|
return MUC_ROLE_WEIGHTS[role1] < MUC_ROLE_WEIGHTS[role2] ? -1 : 1;
|
2017-02-17 22:17:19 +01:00
|
|
|
}
|
|
|
|
},
|
2018-05-09 12:28:34 +02:00
|
|
|
|
|
|
|
fetchMembers () {
|
|
|
|
this.chatroom.getJidsWithAffiliations(['member', 'owner', 'admin'])
|
2018-08-30 13:39:50 +02:00
|
|
|
.then(new_members => {
|
2018-08-09 14:52:03 +02:00
|
|
|
const new_jids = new_members.map(m => m.jid).filter(m => !_.isUndefined(m)),
|
|
|
|
new_nicks = new_members.map(m => !m.jid && m.nick || undefined).filter(m => !_.isUndefined(m)),
|
|
|
|
removed_members = this.filter(m => {
|
|
|
|
return f.includes(m.get('affiliation'), ['admin', 'member', 'owner']) &&
|
|
|
|
!f.includes(m.get('nick'), new_nicks) &&
|
|
|
|
!f.includes(m.get('jid'), new_jids);
|
|
|
|
});
|
|
|
|
|
|
|
|
_.each(removed_members, (occupant) => {
|
|
|
|
if (occupant.get('jid') === _converse.bare_jid) { return; }
|
2018-05-11 00:17:44 +02:00
|
|
|
if (occupant.get('show') === 'offline') {
|
|
|
|
occupant.destroy();
|
|
|
|
}
|
|
|
|
});
|
2018-08-09 14:52:03 +02:00
|
|
|
_.each(new_members, (attrs) => {
|
|
|
|
let occupant;
|
|
|
|
if (attrs.jid) {
|
|
|
|
occupant = this.findOccupant({'jid': attrs.jid});
|
|
|
|
} else {
|
|
|
|
occupant = this.findOccupant({'nick': attrs.nick});
|
|
|
|
}
|
2018-05-09 12:28:34 +02:00
|
|
|
if (occupant) {
|
|
|
|
occupant.save(attrs);
|
|
|
|
} else {
|
|
|
|
this.create(attrs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
|
|
|
|
},
|
|
|
|
|
|
|
|
findOccupant (data) {
|
|
|
|
/* Try to find an existing occupant based on the passed in
|
|
|
|
* data object.
|
|
|
|
*
|
|
|
|
* If we have a JID, we use that as lookup variable,
|
|
|
|
* otherwise we use the nick. We don't always have both,
|
|
|
|
* but should have at least one or the other.
|
|
|
|
*/
|
|
|
|
const jid = Strophe.getBareJidFromJid(data.jid);
|
|
|
|
if (jid !== null) {
|
|
|
|
return this.where({'jid': jid}).pop();
|
|
|
|
} else {
|
|
|
|
return this.where({'nick': data.nick}).pop();
|
|
|
|
}
|
2018-05-10 22:42:43 +02:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
});
|
2017-02-17 22:17:19 +01:00
|
|
|
|
2017-12-20 11:02:01 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
_converse.RoomsPanelModel = Backbone.Model.extend({
|
|
|
|
defaults: {
|
|
|
|
'muc_domain': '',
|
2017-02-17 22:17:19 +01:00
|
|
|
},
|
2018-02-21 22:40:51 +01:00
|
|
|
});
|
2017-02-17 22:17:19 +01:00
|
|
|
|
2017-02-15 21:30:32 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
_converse.onDirectMUCInvitation = function (message) {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* A direct MUC invitation to join a groupchat has been received
|
2018-02-21 22:40:51 +01:00
|
|
|
* See XEP-0249: Direct MUC invitations.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (XMLElement) message: The message stanza containing the
|
|
|
|
* invitation.
|
|
|
|
*/
|
2018-03-04 06:37:11 +01:00
|
|
|
const x_el = sizzle('x[xmlns="jabber:x:conference"]', message).pop(),
|
2018-02-21 22:40:51 +01:00
|
|
|
from = Strophe.getBareJidFromJid(message.getAttribute('from')),
|
|
|
|
room_jid = x_el.getAttribute('jid'),
|
|
|
|
reason = x_el.getAttribute('reason');
|
2016-10-27 13:30:58 +02:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
let contact = _converse.roster.get(from),
|
|
|
|
result;
|
2016-03-28 13:42:33 +02:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
if (_converse.auto_join_on_invite) {
|
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
// Invite request might come from someone not your roster list
|
|
|
|
contact = contact? contact.get('fullname'): Strophe.getNodeFromJid(from);
|
|
|
|
if (!reason) {
|
|
|
|
result = confirm(
|
2018-07-22 10:58:04 +02:00
|
|
|
__("%1$s has invited you to join a groupchat: %2$s", contact, room_jid)
|
2018-02-21 22:40:51 +01:00
|
|
|
);
|
2017-12-14 18:30:05 +01:00
|
|
|
} else {
|
2018-02-21 22:40:51 +01:00
|
|
|
result = confirm(
|
2018-07-22 10:58:04 +02:00
|
|
|
__('%1$s has invited you to join a groupchat: %2$s, and left the following reason: "%3$s"',
|
2018-02-21 22:40:51 +01:00
|
|
|
contact, room_jid, reason)
|
2017-07-10 17:46:22 +02:00
|
|
|
);
|
2016-12-06 16:18:33 +01:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
}
|
|
|
|
if (result === true) {
|
|
|
|
const chatroom = _converse.openChatRoom(
|
|
|
|
room_jid, {'password': x_el.getAttribute('password') });
|
2016-12-07 11:34:02 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
if (chatroom.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED) {
|
|
|
|
_converse.chatboxviews.get(room_jid).join();
|
2016-12-06 21:04:08 +01:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
}
|
|
|
|
};
|
2016-12-06 21:04:08 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
if (_converse.allow_muc_invitations) {
|
|
|
|
const registerDirectInvitationHandler = function () {
|
|
|
|
_converse.connection.addHandler(
|
2018-06-30 13:46:56 +02:00
|
|
|
(message) => {
|
2018-02-21 22:40:51 +01:00
|
|
|
_converse.onDirectMUCInvitation(message);
|
|
|
|
return true;
|
|
|
|
}, 'jabber:x:conference', 'message');
|
|
|
|
};
|
|
|
|
_converse.on('connected', registerDirectInvitationHandler);
|
|
|
|
_converse.on('reconnected', registerDirectInvitationHandler);
|
|
|
|
}
|
2016-12-06 16:18:33 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
const getChatRoom = function (jid, attrs, create) {
|
|
|
|
jid = jid.toLowerCase();
|
2018-08-28 14:58:33 +02:00
|
|
|
attrs.type = _converse.CHATROOMS_TYPE;
|
2018-02-21 22:40:51 +01:00
|
|
|
attrs.id = jid;
|
|
|
|
attrs.box_id = b64_sha1(jid)
|
|
|
|
return _converse.chatboxes.getChatBox(jid, attrs, create);
|
|
|
|
};
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
const createChatRoom = function (jid, attrs) {
|
2018-10-13 20:09:01 +02:00
|
|
|
if (jid.startsWith('xmpp:') && jid.endsWith('?join')) {
|
|
|
|
jid = jid.replace(/^xmpp:/, '').replace(/\?join$/, '');
|
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
return getChatRoom(jid, attrs, true);
|
|
|
|
};
|
2016-08-12 14:52:33 +02:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
function autoJoinRooms () {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* Automatically join groupchats, based on the
|
2018-02-21 22:40:51 +01:00
|
|
|
* "auto_join_rooms" configuration setting, which is an array
|
2018-07-22 10:58:04 +02:00
|
|
|
* of strings (groupchat JIDs) or objects (with groupchat JID and other
|
2018-02-21 22:40:51 +01:00
|
|
|
* settings).
|
|
|
|
*/
|
2018-07-22 10:58:04 +02:00
|
|
|
_.each(_converse.auto_join_rooms, function (groupchat) {
|
|
|
|
if (_converse.chatboxes.where({'jid': groupchat}).length) {
|
2017-02-17 22:17:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-07-22 10:58:04 +02:00
|
|
|
if (_.isString(groupchat)) {
|
|
|
|
_converse.api.rooms.open(groupchat);
|
|
|
|
} else if (_.isObject(groupchat)) {
|
|
|
|
_converse.api.rooms.open(groupchat.jid, groupchat.nick);
|
2018-02-21 22:40:51 +01:00
|
|
|
} else {
|
|
|
|
_converse.log(
|
2018-07-22 10:58:04 +02:00
|
|
|
'Invalid groupchat criteria specified for "auto_join_rooms"',
|
2018-02-21 22:40:51 +01:00
|
|
|
Strophe.LogLevel.ERROR);
|
2016-08-12 16:40:04 +02:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
});
|
|
|
|
_converse.emit('roomsAutoJoined');
|
|
|
|
}
|
2016-02-16 08:46:47 +01:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
function disconnectChatRooms () {
|
2018-07-22 10:58:04 +02:00
|
|
|
/* When disconnecting, mark all groupchats as
|
2018-02-21 22:40:51 +01:00
|
|
|
* disconnected, so that they will be properly entered again
|
|
|
|
* when fetched from session storage.
|
|
|
|
*/
|
|
|
|
_converse.chatboxes.each(function (model) {
|
2018-08-28 14:58:33 +02:00
|
|
|
if (model.get('type') === _converse.CHATROOMS_TYPE) {
|
2018-02-21 22:40:51 +01:00
|
|
|
model.save('connection_status', converse.ROOMSTATUS.DISCONNECTED);
|
2016-03-16 10:03:00 +01:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
});
|
|
|
|
}
|
2016-03-16 10:03:00 +01:00
|
|
|
|
2018-09-04 11:39:27 +02:00
|
|
|
function fetchRegistrationForm (room_jid, user_jid) {
|
|
|
|
_converse.api.sendIQ(
|
|
|
|
$iq({
|
|
|
|
'from': user_jid,
|
|
|
|
'to': room_jid,
|
|
|
|
'type': 'get'
|
|
|
|
}).c('query', {'xmlns': Strophe.NS.REGISTER})
|
|
|
|
).then(iq => {
|
|
|
|
|
|
|
|
}).catch(iq => {
|
|
|
|
if (sizzle('item-not-found[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]', iq).length) {
|
|
|
|
this.feedback.set('error', __(`Error: the groupchat ${this.model.getDisplayName()} does not exist.`));
|
|
|
|
} else if (sizzle('not-allowed[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]').length) {
|
2018-10-09 06:51:31 +02:00
|
|
|
this.feedback.set('error', __(`Sorry, you're not allowed to register in this groupchat`));
|
2018-09-04 11:39:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
/************************ BEGIN Event Handlers ************************/
|
|
|
|
_converse.on('addClientFeatures', () => {
|
|
|
|
if (_converse.allow_muc) {
|
2018-05-11 13:31:48 +02:00
|
|
|
_converse.api.disco.own.features.add(Strophe.NS.MUC);
|
2018-02-21 22:40:51 +01:00
|
|
|
}
|
|
|
|
if (_converse.allow_muc_invitations) {
|
2018-05-11 13:31:48 +02:00
|
|
|
_converse.api.disco.own.features.add('jabber:x:conference'); // Invites
|
2018-02-21 22:40:51 +01:00
|
|
|
}
|
|
|
|
});
|
2018-06-06 09:29:32 +02:00
|
|
|
_converse.api.listen.on('chatBoxesFetched', autoJoinRooms);
|
|
|
|
_converse.api.listen.on('disconnecting', disconnectChatRooms);
|
|
|
|
|
|
|
|
_converse.api.listen.on('statusInitialized', () => {
|
|
|
|
// XXX: For websocket connections, we disconnect from all
|
|
|
|
// chatrooms when the page reloads. This is a workaround for
|
|
|
|
// issue #1111 and should be removed once we support XEP-0198
|
|
|
|
const options = {'once': true, 'passive': true};
|
|
|
|
window.addEventListener(_converse.unloadevent, () => {
|
|
|
|
if (_converse.connection._proto instanceof Strophe.Websocket) {
|
|
|
|
disconnectChatRooms();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-02-21 22:40:51 +01:00
|
|
|
/************************ END Event Handlers ************************/
|
2016-12-03 17:39:59 +01:00
|
|
|
|
2016-04-05 13:23:16 +02:00
|
|
|
|
2018-02-21 22:40:51 +01:00
|
|
|
/************************ BEGIN API ************************/
|
2018-07-22 10:58:04 +02:00
|
|
|
// We extend the default converse.js API to add methods specific to MUC groupchats.
|
2016-12-20 10:30:20 +01:00
|
|
|
_.extend(_converse.api, {
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
|
|
|
* The "rooms" namespace groups methods relevant to chatrooms
|
|
|
|
* (aka groupchats).
|
|
|
|
*
|
|
|
|
* @namespace _converse.api.rooms
|
|
|
|
* @memberOf _converse.api
|
|
|
|
*/
|
2016-02-16 08:46:47 +01:00
|
|
|
'rooms': {
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
|
|
|
* Creates a new MUC chatroom (aka groupchat)
|
|
|
|
*
|
|
|
|
* Similar to {@link _converse.api.rooms.open}, but creates
|
|
|
|
* the chatroom in the background (i.e. doesn't cause a
|
|
|
|
* view to open).
|
|
|
|
*
|
|
|
|
* @method _converse.api.rooms.create
|
|
|
|
* @param {(string[]|string)} jid|jids The JID or array of
|
|
|
|
* JIDs of the chatroom(s) to create
|
|
|
|
* @param {object} [attrs] attrs The room attributes
|
|
|
|
*/
|
2018-02-21 22:40:51 +01:00
|
|
|
'create' (jids, attrs) {
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isString(attrs)) {
|
2016-08-19 17:16:36 +02:00
|
|
|
attrs = {'nick': attrs};
|
2017-01-26 15:49:02 +01:00
|
|
|
} else if (_.isUndefined(attrs)) {
|
2016-08-19 17:16:36 +02:00
|
|
|
attrs = {};
|
|
|
|
}
|
2016-11-30 11:00:12 +01:00
|
|
|
if (_.isUndefined(attrs.maximize)) {
|
|
|
|
attrs.maximize = false;
|
|
|
|
}
|
2016-12-20 10:30:20 +01:00
|
|
|
if (!attrs.nick && _converse.muc_nickname_from_jid) {
|
|
|
|
attrs.nick = Strophe.getNodeFromJid(_converse.bare_jid);
|
2016-10-13 18:22:37 +02:00
|
|
|
}
|
2018-02-21 22:40:51 +01:00
|
|
|
if (_.isUndefined(jids)) {
|
|
|
|
throw new TypeError('rooms.create: You need to provide at least one JID');
|
|
|
|
} else if (_.isString(jids)) {
|
|
|
|
return createChatRoom(jids, attrs);
|
|
|
|
}
|
|
|
|
return _.map(jids, _.partial(createChatRoom, _, attrs));
|
|
|
|
},
|
2018-07-30 18:16:32 +02:00
|
|
|
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
|
|
|
* Opens a MUC chatroom (aka groupchat)
|
|
|
|
*
|
|
|
|
* Similar to {@link _converse.api.chats.open}, but for groupchats.
|
|
|
|
*
|
|
|
|
* @method _converse.api.rooms.open
|
|
|
|
* @param {string} jid The room JID or JIDs (if not specified, all
|
|
|
|
* currently open rooms will be returned).
|
|
|
|
* @param {string} attrs A map containing any extra room attributes.
|
|
|
|
* @param {string} [attrs.nick] The current user's nickname for the MUC
|
|
|
|
* @param {boolean} [attrs.auto_configure] A boolean, indicating
|
|
|
|
* whether the room should be configured automatically or not.
|
|
|
|
* If set to `true`, then it makes sense to pass in configuration settings.
|
|
|
|
* @param {object} [attrs.roomconfig] A map of configuration settings to be used when the room gets
|
|
|
|
* configured automatically. Currently it doesn't make sense to specify
|
|
|
|
* `roomconfig` values if `auto_configure` is set to `false`.
|
|
|
|
* For a list of configuration values that can be passed in, refer to these values
|
|
|
|
* in the [XEP-0045 MUC specification](http://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner).
|
|
|
|
* The values should be named without the `muc#roomconfig_` prefix.
|
|
|
|
* @param {boolean} [attrs.maximize] A boolean, indicating whether minimized rooms should also be
|
|
|
|
* maximized, when opened. Set to `false` by default.
|
|
|
|
* @param {boolean} [attrs.bring_to_foreground] A boolean indicating whether the room should be
|
|
|
|
* brought to the foreground and therefore replace the currently shown chat.
|
|
|
|
* If there is no chat currently open, then this option is ineffective.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* this._converse.api.rooms.open('group@muc.example.com')
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // To return an array of rooms, provide an array of room JIDs:
|
|
|
|
* _converse.api.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // To setup a custom nickname when joining the room, provide the optional nick argument:
|
|
|
|
* _converse.api.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // For example, opening a room with a specific default configuration:
|
|
|
|
* _converse.api.rooms.open(
|
|
|
|
* 'myroom@conference.example.org',
|
|
|
|
* { 'nick': 'coolguy69',
|
|
|
|
* 'auto_configure': true,
|
|
|
|
* 'roomconfig': {
|
|
|
|
* 'changesubject': false,
|
|
|
|
* 'membersonly': true,
|
|
|
|
* 'persistentroom': true,
|
|
|
|
* 'publicroom': true,
|
|
|
|
* 'roomdesc': 'Comfy room for hanging out',
|
|
|
|
* 'whois': 'anyone'
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* true
|
|
|
|
* );
|
|
|
|
*/
|
2018-02-21 22:40:51 +01:00
|
|
|
'open' (jids, attrs) {
|
2018-07-30 18:16:32 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
_converse.api.waitUntil('chatBoxesFetched').then(() => {
|
|
|
|
if (_.isUndefined(jids)) {
|
|
|
|
const err_msg = 'rooms.open: You need to provide at least one JID';
|
|
|
|
_converse.log(err_msg, Strophe.LogLevel.ERROR);
|
|
|
|
reject(new TypeError(err_msg));
|
|
|
|
} else if (_.isString(jids)) {
|
|
|
|
resolve(_converse.api.rooms.create(jids, attrs).trigger('show'));
|
|
|
|
} else {
|
|
|
|
resolve(_.map(jids, (jid) => _converse.api.rooms.create(jid, attrs).trigger('show')));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-02-16 08:46:47 +01:00
|
|
|
},
|
2018-07-30 18:16:32 +02:00
|
|
|
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
|
|
|
* Returns an object representing a MUC chatroom (aka groupchat)
|
|
|
|
*
|
|
|
|
* @method _converse.api.rooms.get
|
|
|
|
* @param {string} [jid] The room JID (if not specified, all rooms will be returned).
|
|
|
|
* @param {object} attrs A map containing any extra room attributes For example, if you want
|
|
|
|
* to specify the nickname, use `{'nick': 'bloodninja'}`. Previously (before
|
|
|
|
* version 1.0.7, the second parameter only accepted the nickname (as a string
|
|
|
|
* value). This is currently still accepted, but then you can't pass in any
|
|
|
|
* other room attributes. If the nickname is not specified then the node part of
|
|
|
|
* the user's JID will be used.
|
|
|
|
* @param {boolean} create A boolean indicating whether the room should be created
|
|
|
|
* if not found (default: `false`)
|
|
|
|
* @example
|
|
|
|
* _converse.api.waitUntil('roomsAutoJoined').then(() => {
|
|
|
|
* const create_if_not_found = true;
|
|
|
|
* _converse.api.rooms.get(
|
|
|
|
* 'group@muc.example.com',
|
|
|
|
* {'nick': 'dread-pirate-roberts'},
|
|
|
|
* create_if_not_found
|
|
|
|
* )
|
|
|
|
* });
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
'get' (jids, attrs, create) {
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isString(attrs)) {
|
2016-08-19 17:16:36 +02:00
|
|
|
attrs = {'nick': attrs};
|
2017-01-26 15:49:02 +01:00
|
|
|
} else if (_.isUndefined(attrs)) {
|
2016-08-19 17:16:36 +02:00
|
|
|
attrs = {};
|
|
|
|
}
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isUndefined(jids)) {
|
2017-06-12 20:30:58 +02:00
|
|
|
const result = [];
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.chatboxes.each(function (chatbox) {
|
2018-08-28 14:58:33 +02:00
|
|
|
if (chatbox.get('type') === _converse.CHATROOMS_TYPE) {
|
2018-02-21 22:40:51 +01:00
|
|
|
result.push(chatbox);
|
2016-06-24 10:54:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2016-08-19 17:16:36 +02:00
|
|
|
if (!attrs.nick) {
|
2016-12-20 10:30:20 +01:00
|
|
|
attrs.nick = Strophe.getNodeFromJid(_converse.bare_jid);
|
2016-06-16 17:20:11 +02:00
|
|
|
}
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isString(jids)) {
|
2018-03-11 13:56:53 +01:00
|
|
|
return getChatRoom(jids, attrs);
|
2017-11-10 15:53:59 +01:00
|
|
|
}
|
2018-03-11 13:56:53 +01:00
|
|
|
return _.map(jids, _.partial(getChatRoom, _, attrs));
|
2017-11-10 15:53:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-02-21 22:40:51 +01:00
|
|
|
/************************ END API ************************/
|
2016-02-16 08:46:47 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|