UNPKG

converse.js

Version:
1,110 lines (1,007 loc) 101 kB
// Converse.js // https://conversejs.org // // Copyright (c) 2013-2019, the Converse.js developers // Licensed under the Mozilla Public License (MPLv2) // /** * @module converse-muc-views * @description * XEP-0045 Multi-User Chat Views */ import "converse-modal"; import "backbone.vdomview"; import "formdata-polyfill"; import "@converse/headless/utils/muc"; import BrowserStorage from "backbone.browserStorage"; import { OrderedListView } from "backbone.overview"; import converse from "@converse/headless/converse-core"; import tpl_add_chatroom_modal from "templates/add_chatroom_modal.html"; import tpl_chatarea from "templates/chatarea.html"; import tpl_chatroom from "templates/chatroom.html"; import tpl_chatroom_bottom_panel from "templates/chatroom_bottom_panel.html"; import tpl_chatroom_destroyed from "templates/chatroom_destroyed.html"; import tpl_chatroom_details_modal from "templates/chatroom_details_modal.html"; import tpl_chatroom_disconnect from "templates/chatroom_disconnect.html"; import tpl_chatroom_features from "templates/chatroom_features.html"; import tpl_chatroom_form from "templates/chatroom_form.html"; import tpl_chatroom_head from "templates/chatroom_head.html"; import tpl_chatroom_invite from "templates/chatroom_invite.html"; import tpl_chatroom_nickname_form from "templates/chatroom_nickname_form.html"; import tpl_chatroom_password_form from "templates/chatroom_password_form.html"; import tpl_chatroom_sidebar from "templates/chatroom_sidebar.html"; import tpl_info from "templates/info.html"; import tpl_list_chatrooms_modal from "templates/list_chatrooms_modal.html"; import tpl_moderator_tools_modal from "templates/moderator_tools_modal.html"; import tpl_occupant from "templates/occupant.html"; import tpl_room_description from "templates/room_description.html"; import tpl_room_item from "templates/room_item.html"; import tpl_room_panel from "templates/room_panel.html"; import tpl_rooms_results from "templates/rooms_results.html"; import tpl_spinner from "templates/spinner.html"; import xss from "xss/dist/xss"; const { Backbone, Strophe, sizzle, _, $iq, $pres } = converse.env; const u = converse.env.utils; const ROLES = ['moderator', 'participant', 'visitor']; const AFFILIATIONS = ['admin', 'member', 'outcast', 'owner']; const OWNER_COMMANDS = ['owner']; const ADMIN_COMMANDS = ['admin', 'ban', 'deop', 'destroy', 'member', 'op', 'revoke']; const MODERATOR_COMMANDS = ['kick', 'mute', 'voice', 'modtools']; const VISITOR_COMMANDS = ['nick']; const COMMAND_TO_ROLE = { 'deop': 'participant', 'kick': 'none', 'mute': 'visitor', 'op': 'moderator', 'voice': 'participant' } const COMMAND_TO_AFFILIATION = { 'admin': 'admin', 'ban': 'outcast', 'member': 'member', 'owner': 'owner', 'revoke': 'none' } converse.plugins.add('converse-muc-views', { /* Dependencies are other plugins which might be * overridden or relied upon, and therefore need to be loaded before * this plugin. They are "optional" because they might not be * available, in which case any overrides applicable to them will be * ignored. * * NB: These plugins need to have already been loaded via require.js. * * It's possible to make these dependencies "non-optional". * If the setting "strict_plugin_dependencies" is set to true, * an error will be raised if the plugin is not found. */ dependencies: ["converse-autocomplete", "converse-modal", "converse-controlbox", "converse-chatview"], overrides: { ControlBoxView: { renderControlBoxPane () { const { _converse } = this.__super__; this.__super__.renderControlBoxPane.apply(this, arguments); if (_converse.allow_muc) { this.renderRoomsPanel(); } } } }, initialize () { const { _converse } = this, { __ } = _converse; _converse.api.promises.add(['roomsPanelRendered']); // Configuration values for this plugin // ==================================== // Refer to docs/source/configuration.rst for explanations of these // configuration settings. _converse.api.settings.update({ 'auto_list_rooms': false, 'cache_muc_messages': true, 'locked_muc_nickname': false, 'muc_disable_slash_commands': false, 'muc_show_join_leave': true, 'muc_show_join_leave_status': true, 'muc_mention_autocomplete_min_chars': 0, 'roomconfig_whitelist': [], 'visible_toolbar_buttons': { 'toggle_occupants': true } }); const viewWithRoomsPanel = { renderRoomsPanel () { if (this.roomspanel && u.isInDOM(this.roomspanel.el)) { return this.roomspanel; } this.roomspanel = new _converse.RoomsPanel({ 'model': new (_converse.RoomsPanelModel.extend({ 'id': `converse.roomspanel${_converse.bare_jid}`, // Required by web storage 'browserStorage': new BrowserStorage[_converse.config.get('storage')]( `converse.roomspanel${_converse.bare_jid}`) }))() }); this.roomspanel.model.fetch(); this.el.querySelector('.controlbox-pane').insertAdjacentElement( 'beforeEnd', this.roomspanel.render().el); /** * Triggered once the section of the _converse.ControlBoxView * which shows gropuchats has been rendered. * @event _converse#roomsPanelRendered * @example _converse.api.listen.on('roomsPanelRendered', () => { ... }); */ _converse.api.trigger('roomsPanelRendered'); return this.roomspanel; }, getRoomsPanel () { if (this.roomspanel && u.isInDOM(this.roomspanel.el)) { return this.roomspanel; } else { return this.renderRoomsPanel(); } } } if (_converse.ControlBoxView) { Object.assign(_converse.ControlBoxView.prototype, viewWithRoomsPanel); } /* Insert groupchat info (based on returned #disco IQ stanza) * @function insertRoomInfo * @param { HTMLElement } el - The HTML DOM element that contains the info. * @param { XMLElement } stanza - The IQ stanza containing the groupchat info. */ function insertRoomInfo (el, stanza) { // All MUC features found here: https://xmpp.org/registrar/disco-features.html el.querySelector('span.spinner').remove(); el.querySelector('a.room-info').classList.add('selected'); el.insertAdjacentHTML( 'beforeEnd', tpl_room_description({ 'jid': stanza.getAttribute('from'), 'desc': _.get(_.head(sizzle('field[var="muc#roominfo_description"] value', stanza)), 'textContent'), 'occ': _.get(_.head(sizzle('field[var="muc#roominfo_occupants"] value', stanza)), 'textContent'), 'hidden': sizzle('feature[var="muc_hidden"]', stanza).length, 'membersonly': sizzle('feature[var="muc_membersonly"]', stanza).length, 'moderated': sizzle('feature[var="muc_moderated"]', stanza).length, 'nonanonymous': sizzle('feature[var="muc_nonanonymous"]', stanza).length, 'open': sizzle('feature[var="muc_open"]', stanza).length, 'passwordprotected': sizzle('feature[var="muc_passwordprotected"]', stanza).length, 'persistent': sizzle('feature[var="muc_persistent"]', stanza).length, 'publicroom': sizzle('feature[var="muc_publicroom"]', stanza).length, 'semianonymous': sizzle('feature[var="muc_semianonymous"]', stanza).length, 'temporary': sizzle('feature[var="muc_temporary"]', stanza).length, 'unmoderated': sizzle('feature[var="muc_unmoderated"]', stanza).length, 'label_desc': __('Description:'), 'label_jid': __('Groupchat Address (JID):'), 'label_occ': __('Participants:'), 'label_features': __('Features:'), 'label_requires_auth': __('Requires authentication'), 'label_hidden': __('Hidden'), 'label_requires_invite': __('Requires an invitation'), 'label_moderated': __('Moderated'), 'label_non_anon': __('Non-anonymous'), 'label_open_room': __('Open'), 'label_permanent_room': __('Permanent'), 'label_public': __('Public'), 'label_semi_anon': __('Semi-anonymous'), 'label_temp_room': __('Temporary'), 'label_unmoderated': __('Unmoderated') })); } /** * Show/hide extra information about a groupchat in a listing. * @function toggleRoomInfo * @param { Event } */ function toggleRoomInfo (ev) { const parent_el = u.ancestor(ev.target, '.room-item'); const div_el = parent_el.querySelector('div.room-info'); if (div_el) { u.slideIn(div_el).then(u.removeElement) parent_el.querySelector('a.room-info').classList.remove('selected'); } else { parent_el.insertAdjacentHTML('beforeend', tpl_spinner()); _converse.api.disco.info(ev.target.getAttribute('data-room-jid'), null) .then(stanza => insertRoomInfo(parent_el, stanza)) .catch(e => _converse.log(e, Strophe.LogLevel.ERROR)); } } _converse.ModeratorToolsModal = _converse.BootstrapModal.extend({ events: { 'submit .affiliation-form': 'assignAffiliation', 'submit .role-form': 'assignRole', 'submit .query-affiliation': 'queryAffiliation', 'submit .query-role': 'queryRole', 'click .nav-item .nav-link': 'switchTab', 'click .toggle-form': 'toggleForm', }, initialize (attrs) { this.chatroomview = attrs.chatroomview; _converse.BootstrapModal.prototype.initialize.apply(this, arguments); this.listenTo(this.model, 'change:role', () => { this.users_with_role = this.getUsersWithRole(); this.render(); }); this.listenTo(this.model, 'change:affiliation', async () => { this.loading_users_with_affiliation = true; this.users_with_affiliation = null; this.render(); const affiliation = this.model.get('affiliation'); if (!_converse.muc_fetch_members || affiliation === 'outcast') { this.users_with_affiliation = await this.chatroomview.model.getAffiliationList(affiliation); } else { this.users_with_affiliation = this.getUsersWithAffiliation(); } this.loading_users_with_affiliation = false; this.render(); }); }, toHTML () { const allowed_commands = this.chatroomview.getAllowedCommands(); const allowed_affiliations = allowed_commands.map(c => COMMAND_TO_AFFILIATION[c]).filter(c => c); const allowed_roles = _.uniq(allowed_commands .map(c => COMMAND_TO_ROLE[c]) .filter(c => c)); allowed_affiliations.sort(); allowed_roles.sort(); return tpl_moderator_tools_modal(Object.assign(this.model.toJSON(), { '__': __, 'affiliations': [...AFFILIATIONS, 'none'], 'allowed_affiliations': allowed_affiliations, 'allowed_roles': allowed_roles, 'loading_users_with_affiliation': this.loading_users_with_affiliation, 'roles': ROLES, 'users_with_affiliation': this.users_with_affiliation, 'users_with_role': this.users_with_role })); }, toggleForm (ev) { ev.stopPropagation(); ev.preventDefault(); const form_class = ev.target.getAttribute('data-form'); const form = u.ancestor(ev.target, '.list-group-item').querySelector(`.${form_class}`); if (u.hasClass('hidden', form)) { u.removeClass('hidden', form); } else { u.addClass('hidden', form); } }, getUsersWithAffiliation () { return this.chatroomview.model.occupants .where({'affiliation': this.model.get('affiliation')}) .map(item => { return { 'jid': item.get('jid'), 'nick': item.get('nick'), 'affiliation': item.get('affiliation') } }); }, getUsersWithRole () { return this.chatroomview.model.occupants .where({'role': this.model.get('role')}) .map(item => { return { 'jid': item.get('jid'), 'nick': item.get('nick'), 'role': item.get('role') } }); }, queryRole (ev) { ev.stopPropagation(); ev.preventDefault(); const data = new FormData(ev.target); const role = data.get('role'); this.model.set({'role': null}, {'silent': true}); this.model.set({'role': role}); }, queryAffiliation (ev) { ev.stopPropagation(); ev.preventDefault(); const data = new FormData(ev.target); const affiliation = data.get('affiliation'); this.model.set({'affiliation': null}, {'silent': true}); this.model.set({'affiliation': affiliation}); }, assignAffiliation (ev) { ev.stopPropagation(); ev.preventDefault(); const data = new FormData(ev.target); const affiliation = data.get('affiliation'); const attrs = { 'jid': data.get('jid'), 'reason': data.get('reason') } const current_affiliation = this.model.get('affiliation'); this.chatroomview.model.setAffiliation(affiliation, [attrs]) .then(async () => { this.alert(__('Affiliation changed'), 'primary'); await this.chatroomview.model.occupants.fetchMembers() this.model.set({'affiliation': null}, {'silent': true}); this.model.set({'affiliation': current_affiliation}); }) .catch(err => { this.alert(__('Sorry, something went wrong while trying to set the affiliation'), 'danger'); _converse.log(err, Strophe.LogLevel.ERROR); }); }, assignRole (ev) { ev.stopPropagation(); ev.preventDefault(); const data = new FormData(ev.target); const occupant = this.chatroomview.model.getOccupant(data.get('jid') || data.get('nick')); const role = data.get('role'); const reason = data.get('reason'); const current_role = this.model.get('role'); this.chatroomview.model.setRole(occupant, role, reason, () => { this.alert(__('Role changed'), 'primary'); this.model.set({'role': null}, {'silent': true}); this.model.set({'role': current_role}); }, (e) => { if (sizzle(`not-allowed[xmlns="${Strophe.NS.STANZAS}"]`, e).length) { this.alert(__('You\'re not allowed to make that change'), 'danger'); } else { this.alert(__('Sorry, something went wrong while trying to set the role'), 'danger'); if (u.isErrorObject(e)) { _converse.log(e, Strophe.LogLevel.ERROR); } } } ); } }); _converse.ListChatRoomsModal = _converse.BootstrapModal.extend({ events: { 'submit form': 'showRooms', 'click a.room-info': 'toggleRoomInfo', 'change input[name=nick]': 'setNick', 'change input[name=server]': 'setDomainFromEvent', 'click .open-room': 'openRoom' }, initialize () { _converse.BootstrapModal.prototype.initialize.apply(this, arguments); if (_converse.muc_domain && !this.model.get('muc_domain')) { this.model.save('muc_domain', _converse.muc_domain); } this.listenTo(this.model, 'change:muc_domain', this.onDomainChange); }, toHTML () { const muc_domain = this.model.get('muc_domain') || _converse.muc_domain; return tpl_list_chatrooms_modal(Object.assign(this.model.toJSON(), { 'heading_list_chatrooms': __('Query for Groupchats'), 'label_server_address': __('Server address'), 'label_query': __('Show groupchats'), 'show_form': !_converse.locked_muc_domain, 'server_placeholder': muc_domain ? muc_domain : __('conference.example.org') })); }, afterRender () { if (_converse.locked_muc_domain) { this.updateRoomsList(); } else { this.el.addEventListener('shown.bs.modal', () => this.el.querySelector('input[name="server"]').focus(), false ); } }, openRoom (ev) { ev.preventDefault(); const jid = ev.target.getAttribute('data-room-jid'); const name = ev.target.getAttribute('data-room-name'); this.modal.hide(); _converse.api.rooms.open(jid, {'name': name}); }, toggleRoomInfo (ev) { ev.preventDefault(); toggleRoomInfo(ev); }, onDomainChange () { if (_converse.auto_list_rooms) { this.updateRoomsList(); } }, roomStanzaItemToHTMLElement (groupchat) { const name = Strophe.unescapeNode(groupchat.getAttribute('name') || groupchat.getAttribute('jid')); const div = document.createElement('div'); div.innerHTML = tpl_room_item({ 'name': Strophe.xmlunescape(name), 'jid': groupchat.getAttribute('jid'), 'open_title': __('Click to open this groupchat'), 'info_title': __('Show more information on this groupchat') }); return div.firstElementChild; }, removeSpinner () { sizzle('.spinner', this.el).forEach(u.removeElement); }, informNoRoomsFound () { const chatrooms_el = this.el.querySelector('.available-chatrooms'); chatrooms_el.innerHTML = tpl_rooms_results({'feedback_text': __('No groupchats found')}); const input_el = this.el.querySelector('input[name="server"]'); input_el.classList.remove('hidden') this.removeSpinner(); }, onRoomsFound (iq) { /* Handle the IQ stanza returned from the server, containing * all its public groupchats. */ const available_chatrooms = this.el.querySelector('.available-chatrooms'); const rooms = sizzle('query item', iq); if (rooms.length) { available_chatrooms.innerHTML = tpl_rooms_results({'feedback_text': __('Groupchats found:')}); const fragment = document.createDocumentFragment(); rooms.map(this.roomStanzaItemToHTMLElement) .filter(r => r) .forEach(child => fragment.appendChild(child)); available_chatrooms.appendChild(fragment); this.removeSpinner(); } else { this.informNoRoomsFound(); } return true; }, updateRoomsList () { /* Send an IQ stanza to the server asking for all groupchats */ const iq = $iq({ 'to': this.model.get('muc_domain'), 'from': _converse.connection.jid, 'type': "get" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS}); _converse.api.sendIQ(iq) .then(iq => this.onRoomsFound(iq)) .catch(() => this.informNoRoomsFound()) }, showRooms (ev) { ev.preventDefault(); const data = new FormData(ev.target); this.model.setDomain(data.get('server')); this.updateRoomsList(); }, setDomainFromEvent (ev) { this.model.setDomain(ev.target.value); }, setNick (ev) { this.model.save({nick: ev.target.value}); } }); _converse.AddChatRoomModal = _converse.BootstrapModal.extend({ events: { 'submit form.add-chatroom': 'openChatRoom' }, initialize () { _converse.BootstrapModal.prototype.initialize.apply(this, arguments); this.listenTo(this.model, 'change:muc_domain', this.render); }, toHTML () { let placeholder = ''; if (!_converse.locked_muc_domain) { const muc_domain = this.model.get('muc_domain') || _converse.muc_domain; placeholder = muc_domain ? `name@${muc_domain}` : __('name@conference.example.org'); } return tpl_add_chatroom_modal(Object.assign(this.model.toJSON(), { '__': _converse.__, '_converse': _converse, 'label_room_address': _converse.muc_domain ? __('Groupchat name') : __('Groupchat address'), 'chatroom_placeholder': placeholder })); }, afterRender () { this.el.addEventListener('shown.bs.modal', () => { this.el.querySelector('input[name="chatroom"]').focus(); }, false); }, parseRoomDataFromEvent (form) { const data = new FormData(form); const jid = data.get('chatroom'); let nick; if (_converse.locked_muc_nickname) { nick = _converse.getDefaultMUCNickname(); if (!nick) { throw new Error("Using locked_muc_nickname but no nickname found!"); } } else { nick = data.get('nickname').trim(); } return { 'jid': jid, 'nick': nick } }, openChatRoom (ev) { ev.preventDefault(); const data = this.parseRoomDataFromEvent(ev.target); if (data.nick === "") { // Make sure defaults apply if no nick is provided. data.nick = undefined; } let jid; if (_converse.locked_muc_domain || (_converse.muc_domain && !u.isValidJID(data.jid))) { jid = `${Strophe.escapeNode(data.jid)}@${_converse.muc_domain}`; } else { jid = data.jid this.model.setDomain(jid); } _converse.api.rooms.open(jid, Object.assign(data, {jid})); this.modal.hide(); ev.target.reset(); } }); _converse.RoomDetailsModal = _converse.BootstrapModal.extend({ initialize () { _converse.BootstrapModal.prototype.initialize.apply(this, arguments); this.listenTo(this.model, 'change', this.render); this.listenTo(this.model.occupants, 'add', this.render); this.listenTo(this.model.occupants, 'change', this.render); }, toHTML () { return tpl_chatroom_details_modal(Object.assign( this.model.toJSON(), { '_': _, '__': __, 'display_name': __('Groupchat info for %1$s', this.model.getDisplayName()), 'features': this.model.features.toJSON(), 'num_occupants': this.model.occupants.length, 'topic': u.addHyperlinks(xss.filterXSS(_.get(this.model.get('subject'), 'text'), {'whiteList': {}})) }) ); } }); /** * The View of an open/ongoing groupchat conversation * @class * @namespace _converse.ChatRoomView * @memberOf _converse */ _converse.ChatRoomView = _converse.ChatBoxView.extend({ /* Backbone.NativeView which renders a groupchat, based upon the view * for normal one-on-one chat boxes. */ length: 300, tagName: 'div', className: 'chatbox chatroom hidden', is_chatroom: true, events: { 'change input.fileupload': 'onFileSelection', 'click .chat-msg__action-edit': 'onMessageEditButtonClicked', 'click .chatbox-navback': 'showControlBox', 'click .close-chatbox-button': 'close', 'click .configure-chatroom-button': 'getAndRenderConfigurationForm', 'click .hide-occupants': 'hideOccupants', 'click .new-msgs-indicator': 'viewUnreadMessages', 'click .occupant-nick': 'onOccupantClicked', 'click .send-button': 'onFormSubmitted', 'click .show-room-details-modal': 'showRoomDetailsModal', 'click .toggle-call': 'toggleCall', 'click .toggle-occupants': 'toggleOccupants', 'click .upload-file': 'toggleFileUpload', 'keydown .chat-textarea': 'onKeyDown', 'keyup .chat-textarea': 'onKeyUp', 'paste .chat-textarea': 'onPaste', 'input .chat-textarea': 'inputChanged', 'dragover .chat-textarea': 'onDragOver', 'drop .chat-textarea': 'onDrop', }, initialize () { this.initDebounced(); this.listenTo(this.model.messages, 'add', this.onMessageAdded); this.listenTo(this.model.messages, 'rendered', this.scrollDown); this.model.messages.on('reset', () => { this.content.innerHTML = ''; this.removeAll(); }); this.listenTo(this.model, 'change', this.renderHeading); this.listenTo(this.model, 'change:connection_status', this.onConnectionStatusChanged); this.listenTo(this.model, 'change:hidden_occupants', this.updateOccupantsToggle); this.listenTo(this.model, 'change:subject', this.setChatRoomSubject); this.listenTo(this.model, 'configurationNeeded', this.getAndRenderConfigurationForm); this.listenTo(this.model, 'destroy', this.hide); this.listenTo(this.model, 'show', this.show); this.listenTo(this.model.features, 'change:moderated', this.renderBottomPanel); this.listenTo(this.model.occupants, 'add', this.onOccupantAdded); this.listenTo(this.model.occupants, 'remove', this.onOccupantRemoved); this.listenTo(this.model.occupants, 'change:show', this.showJoinOrLeaveNotification); this.listenTo(this.model.occupants, 'change:role', this.onOccupantRoleChanged); this.listenTo(this.model.occupants, 'change:affiliation', this.onOccupantAffiliationChanged); this.render(); this.updateAfterMessagesFetched(); this.createOccupantsView(); this.onConnectionStatusChanged(); /** * Triggered once a groupchat has been opened * @event _converse#chatRoomOpened * @type { _converse.ChatRoomView } * @example _converse.api.listen.on('chatRoomOpened', view => { ... }); */ _converse.api.trigger('chatRoomOpened', this); _converse.api.trigger('chatBoxInitialized', this); }, render () { this.el.setAttribute('id', this.model.get('box_id')); this.el.innerHTML = tpl_chatroom(); this.renderHeading(); this.renderChatArea(); this.renderBottomPanel(); if (this.model.get('connection_status') !== converse.ROOMSTATUS.ENTERED) { this.showSpinner(); } return this; }, renderHeading (item=null) { /* Render the heading UI of the groupchat. */ const changed = _.get(item, 'changed', {}); const keys = ['affiliation', 'bookmarked', 'jid', 'name', 'description', 'subject']; if (item === null || _.intersection(Object.keys(changed), keys).length) { this.el.querySelector('.chat-head-chatroom').innerHTML = this.generateHeadingHTML(); } }, renderBottomPanel () { const container = this.el.querySelector('.bottom-panel'); if (this.model.features.get('moderated') && this.model.getOwnRole() === 'visitor') { container.innerHTML = tpl_chatroom_bottom_panel({'__': __}); } else { if (!container.firstElementChild || !container.querySelector('.sendXMPPMessage')) { this.renderMessageForm(); this.initMentionAutoComplete(); } } }, renderChatArea () { /* Render the UI container in which groupchat messages will appear. */ if (this.el.querySelector('.chat-area') === null) { const container_el = this.el.querySelector('.chatroom-body'); container_el.insertAdjacentHTML( 'beforeend', tpl_chatarea({'show_send_button': _converse.show_send_button}) ); this.content = this.el.querySelector('.chat-content'); } return this; }, createOccupantsView () { this.model.occupants.chatroomview = this; const view = new _converse.ChatRoomOccupantsView({'model': this.model.occupants}); const container_el = this.el.querySelector('.chatroom-body'); container_el.insertAdjacentElement('beforeend', view.el); }, getAutoCompleteList () { return this.model.occupants.filter('nick').map(o => ({'label': o.get('nick'), 'value': `@${o.get('nick')}`})); }, initMentionAutoComplete () { this.mention_auto_complete = new _converse.AutoComplete(this.el, { 'auto_first': true, 'auto_evaluate': false, 'min_chars': _converse.muc_mention_autocomplete_min_chars, 'match_current_word': true, 'list': () => this.getAutoCompleteList(), 'filter': _converse.FILTER_STARTSWITH, 'ac_triggers': ["Tab", "@"], 'include_triggers': [] }); this.mention_auto_complete.on('suggestion-box-selectcomplete', () => (this.auto_completing = false)); }, onKeyDown (ev) { if (this.mention_auto_complete.onKeyDown(ev)) { return; } return _converse.ChatBoxView.prototype.onKeyDown.call(this, ev); }, onKeyUp (ev) { this.mention_auto_complete.evaluate(ev); return _converse.ChatBoxView.prototype.onKeyUp.call(this, ev); }, showModeratorToolsModal (affiliation) { if (!this.verifyRoles(['moderator'])) { return; } if (_.isUndefined(this.model.modtools_modal)) { const model = new Backbone.Model({'affiliation': affiliation}); this.modtools_modal = new _converse.ModeratorToolsModal({'model': model, 'chatroomview': this}); } else { this.modtools_modal.set('affiliation', affiliation); } this.modtools_modal.show(); }, showRoomDetailsModal (ev) { ev.preventDefault(); if (this.model.room_details_modal === undefined) { this.model.room_details_modal = new _converse.RoomDetailsModal({'model': this.model}); } this.model.room_details_modal.show(ev); }, showChatStateNotification (message) { if (message.get('sender') === 'me') { return; } return _converse.ChatBoxView.prototype.showChatStateNotification.apply(this, arguments); }, onOccupantAffiliationChanged (occupant) { if (occupant.get('jid') === _converse.bare_jid) { this.renderHeading(); } this.informOfOccupantsAffiliationChange(occupant); }, informOfOccupantsAffiliationChange (occupant) { const previous_affiliation = occupant._previousAttributes.affiliation; const current_affiliation = occupant.get('affiliation'); if (previous_affiliation === 'admin') { this.showChatEvent(__("%1$s is no longer an admin of this groupchat", occupant.get('nick'))) } else if (previous_affiliation === 'owner') { this.showChatEvent(__("%1$s is no longer an owner of this groupchat", occupant.get('nick'))) } else if (previous_affiliation === 'outcast') { this.showChatEvent(__("%1$s is no longer banned from this groupchat", occupant.get('nick'))) } if (current_affiliation === 'none' && previous_affiliation === 'member') { this.showChatEvent(__("%1$s is no longer a member of this groupchat", occupant.get('nick'))) } if (current_affiliation === 'member') { this.showChatEvent(__("%1$s is now a member of this groupchat", occupant.get('nick'))) } else if (current_affiliation === 'outcast') { this.showChatEvent(__("%1$s has been banned from this groupchat", occupant.get('nick'))) } else if (current_affiliation === 'admin' || current_affiliation == 'owner') { // For example: AppleJack is now an (admin|owner) of this groupchat this.showChatEvent(__('%1$s is now an %2$s of this groupchat', occupant.get('nick'), current_affiliation)) } }, onOccupantRoleChanged (occupant, changed) { if (occupant.get('jid') === _converse.bare_jid) { this.renderBottomPanel(); } this.informOfOccupantsRoleChange(occupant, changed); }, informOfOccupantsRoleChange (occupant, changed) { if (changed === "none" || occupant.changed.affiliation) { // We don't inform of role changes if they accompany affiliation changes. return; } const previous_role = occupant._previousAttributes.role; if (previous_role === 'moderator') { this.showChatEvent(__("%1$s is no longer a moderator", occupant.get('nick'))) } if (previous_role === 'visitor') { this.showChatEvent(__("%1$s has been given a voice", occupant.get('nick'))) } if (occupant.get('role') === 'visitor') { this.showChatEvent(__("%1$s has been muted", occupant.get('nick'))) } if (occupant.get('role') === 'moderator') { if (!['owner', 'admin'].includes(occupant.get('affiliation'))) { // We only show this message if the user isn't already // an admin or owner, otherwise this isn't new // information. this.showChatEvent(__("%1$s is now a moderator", occupant.get('nick'))) } } }, generateHeadingHTML () { /* Returns the heading HTML to be rendered. */ return tpl_chatroom_head( Object.assign(this.model.toJSON(), { 'isOwner': this.model.getOwnAffiliation() === 'owner', 'title': this.model.getDisplayName(), 'Strophe': Strophe, '_converse': _converse, 'info_close': __('Close and leave this groupchat'), 'info_configure': __('Configure this groupchat'), 'info_details': __('Show more details about this groupchat'), 'description': u.addHyperlinks(xss.filterXSS(_.get(this.model.get('subject'), 'text'), {'whiteList': {}})), })); }, afterShown () { /* Override from converse-chatview, specifically to avoid * the 'active' chat state from being sent out prematurely. * * This is instead done in `onConnectionStatusChanged` below. */ if (u.isPersistableModel(this.model)) { this.model.clearUnreadMsgCounter(); this.model.save(); } this.scrollDown(); }, onConnectionStatusChanged () { const conn_status = this.model.get('connection_status'); if (conn_status === converse.ROOMSTATUS.NICKNAME_REQUIRED) { this.renderNicknameForm(); } else if (conn_status === converse.ROOMSTATUS.PASSWORD_REQUIRED) { this.renderPasswordForm(); } else if (conn_status === converse.ROOMSTATUS.CONNECTING) { this.showSpinner(); } else if (conn_status === converse.ROOMSTATUS.ENTERED) { this.hideSpinner(); if (_converse.auto_focus) { this.focus(); } } else if (conn_status === converse.ROOMSTATUS.DISCONNECTED) { this.showDisconnectMessage(); } else if (conn_status === converse.ROOMSTATUS.DESTROYED) { this.showDestroyedMessage(); } }, getToolbarOptions () { return Object.assign( _converse.ChatBoxView.prototype.getToolbarOptions.apply(this, arguments), { 'label_hide_occupants': __('Hide the list of participants'), 'show_occupants_toggle': _converse.visible_toolbar_buttons.toggle_occupants } ); }, /** * Closes this chat box, which implies leaving the groupchat as well. * @private * @method _converse.ChatRoomView#close */ close () { this.hide(); if (Backbone.history.getFragment() === "converse/room?jid="+this.model.get('jid')) { _converse.router.navigate(''); } this.model.leave(); _converse.ChatBoxView.prototype.close.apply(this, arguments); }, updateOccupantsToggle () { const icon_el = this.el.querySelector('.toggle-occupants'); const chat_area = this.el.querySelector('.chat-area'); if (this.model.get('hidden_occupants')) { u.removeClass('fa-angle-double-right', icon_el); u.addClass('fa-angle-double-left', icon_el); u.addClass('full', chat_area); } else { u.addClass('fa-angle-double-right', icon_el); u.removeClass('fa-angle-double-left', icon_el); u.removeClass('full', chat_area); } }, hideOccupants (ev) { /* Show or hide the right sidebar containing the chat * occupants (and the invite widget). */ if (ev) { ev.preventDefault(); ev.stopPropagation(); } this.model.save({'hidden_occupants': true}); this.scrollDown(); }, toggleOccupants (ev) { /* Show or hide the right sidebar containing the chat * occupants (and the invite widget). */ if (ev) { ev.preventDefault(); ev.stopPropagation(); } this.model.save({'hidden_occupants': !this.model.get('hidden_occupants')}); this.scrollDown(); }, onOccupantClicked (ev) { /* When an occupant is clicked, insert their nickname into * the chat textarea input. */ this.insertIntoTextArea(ev.target.textContent); }, verifyRoles (roles, occupant, show_error=true) { if (!Array.isArray(roles)) { throw new TypeError('roles must be an Array'); } if (!roles.length) { return true; } if (!occupant) { occupant = this.model.occupants.findWhere({'jid': _converse.bare_jid}); } const role = occupant.get('role'); if (roles.includes(role)) { return true; } if (show_error) { this.showErrorMessage(__('Forbidden: you do not have the necessary role in order to do that.')) } return false; }, verifyAffiliations (affiliations, occupant, show_error=true) { if (!Array.isArray(affiliations)) { throw new TypeError('affiliations must be an Array'); } if (!affiliations.length) { return true; } if (!occupant) { occupant = this.model.occupants.findWhere({'jid': _converse.bare_jid}); } const a = occupant.get('affiliation'); if (affiliations.includes(a)) { return true; } if (show_error) { this.showErrorMessage(__('Forbidden: you do not have the necessary affiliation in order to do that.')) } return false; }, validateRoleOrAffiliationChangeArgs (command, args) { if (!args) { this.showErrorMessage( __('Error: the "%1$s" command takes two arguments, the user\'s nickname and optionally a reason.', command) ); return false; } return true; }, getNickOrJIDFromCommandArgs (args) { if (!args.startsWith('@')) { args = '@'+ args; } const [text, references] = this.model.parseTextForReferences(args); // eslint-disable-line no-unused-vars if (!references.length) { this.showErrorMessage(__("Error: couldn't find a groupchat participant based on your arguments")); return; } if (references.length > 1) { this.showErrorMessage(__("Error: found multiple groupchat participant based on your arguments")); return; } const nick_or_jid = references.pop().value; const reason = args.split(nick_or_jid, 2)[1]; if (reason && !reason.startsWith(' ')) { this.showErrorMessage(__("Error: couldn't find a groupchat participant based on your arguments")); return; } return nick_or_jid; }, setAffiliation (command, args, required_affiliations) { const affiliation = COMMAND_TO_AFFILIATION[command]; if (!affiliation) { throw Error(`ChatRoomView#setAffiliation called with invalid command: ${command}`); } if (!this.verifyAffiliations(required_affiliations)) { return false; } if (!this.validateRoleOrAffiliationChangeArgs(command, args)) { return false; } const nick_or_jid = this.getNickOrJIDFromCommandArgs(args); if (!nick_or_jid) { return false; } const reason = args.split(nick_or_jid, 2)[1].trim(); // We're guaranteed to have an occupant due to getNickOrJIDFromCommandArgs const occupant = this.model.getOccupant(nick_or_jid); const attrs = { 'jid': occupant.get('jid'), 'reason': reason } if (_converse.auto_register_muc_nickname && occupant) { attrs['nick'] = occupant.get('nick'); } this.model.setAffiliation(affiliation, [attrs]) .then(() => this.model.occupants.fetchMembers()) .catch(err => this.onCommandError(err)); }, getReason (args) { return args.includes(',') ? args.slice(args.indexOf(',')+1).trim() : null; }, setRole (command, args, required_affiliations=[], required_roles=[]) { /* Check that a command to change a groupchat user's role or * affiliation has anough arguments. */ const role = COMMAND_TO_ROLE[command]; if (!role) { throw Error(`ChatRoomView#setRole called with invalid command: ${command}`); } if (!this.verifyAffiliations(required_affiliations) || !this.verifyRoles(required_roles)) { return false; } if (!this.validateRoleOrAffiliationChangeArgs(command, args)) { return false; } const nick_or_jid = this.getNickOrJIDFromCommandArgs(args); if (!nick_or_jid) { return false; } const reason = args.split(nick_or_jid, 2)[1].trim(); // We're guaranteed to have an occupant due to getNickOrJIDFromCommandArgs const o