Commit ebfd0a8f authored by JC Brand's avatar JC Brand

Move various funcitons related to MUC member lists to utils

and out of the MUC views plugin.

Refs #1032
parent 06141b32
......@@ -882,7 +882,7 @@
_converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
spyOn(view, 'saveAffiliationAndRole').and.callThrough();
spyOn(view.model, 'saveAffiliationAndRole').and.callThrough();
// We pretend this is a new room, so no disco info is returned.
var features_stanza = $iq({
......@@ -913,7 +913,7 @@
}).up()
.c('status', {code: '110'});
_converse.connection._dataRecv(test_utils.createRequest(presence));
expect(view.saveAffiliationAndRole).toHaveBeenCalled();
expect(view.model.saveAffiliationAndRole).toHaveBeenCalled();
expect($(view.el.querySelector('.toggle-chatbox-button')).is(':visible')).toBeTruthy();
test_utils.waitUntil(function () {
......@@ -1348,7 +1348,7 @@
// receiving the features for the room.
view.model.set('open', 'true');
spyOn(view, 'directInvite').and.callThrough();
spyOn(view.model, 'directInvite').and.callThrough();
var $input;
$(view.el).find('.chat-area').remove();
......@@ -1376,7 +1376,7 @@
evt.button = 0; // For some reason awesomplete wants this
$hint[0].dispatchEvent(evt);
expect(window.prompt).toHaveBeenCalled();
expect(view.directInvite).toHaveBeenCalled();
expect(view.model.directInvite).toHaveBeenCalled();
expect(sent_stanza.toLocaleString()).toBe(
"<message from='dummy@localhost/resource' to='felix.amsel@localhost' id='" +
sent_stanza.nodeTree.getAttribute('id') +
......@@ -2140,7 +2140,7 @@
});
var view = _converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'onMessageSubmitted').and.callThrough();
spyOn(view, 'setAffiliation').and.callThrough();
spyOn(view.model, 'setAffiliation').and.callThrough();
spyOn(view, 'showStatusNotification').and.callThrough();
spyOn(view, 'validateRoleChangeCommand').and.callThrough();
var textarea = view.el.querySelector('.chat-textarea')
......@@ -2156,7 +2156,7 @@
"Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.",
true
);
expect(view.setAffiliation).not.toHaveBeenCalled();
expect(view.model.setAffiliation).not.toHaveBeenCalled();
// Call now with the correct amount of arguments.
// XXX: Calling onMessageSubmitted directly, trying
......@@ -2165,7 +2165,7 @@
view.onMessageSubmitted('/owner annoyingGuy@localhost You\'re responsible');
expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
expect(view.showStatusNotification.calls.count()).toBe(1);
expect(view.setAffiliation).toHaveBeenCalled();
expect(view.model.setAffiliation).toHaveBeenCalled();
// Check that the member list now gets updated
expect(sent_IQ.toLocaleString()).toBe(
"<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
......@@ -2193,7 +2193,7 @@
});
var view = _converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'onMessageSubmitted').and.callThrough();
spyOn(view, 'setAffiliation').and.callThrough();
spyOn(view.model, 'setAffiliation').and.callThrough();
spyOn(view, 'showStatusNotification').and.callThrough();
spyOn(view, 'validateRoleChangeCommand').and.callThrough();
var textarea = view.el.querySelector('.chat-textarea')
......@@ -2209,7 +2209,7 @@
"Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.",
true
);
expect(view.setAffiliation).not.toHaveBeenCalled();
expect(view.model.setAffiliation).not.toHaveBeenCalled();
// Call now with the correct amount of arguments.
// XXX: Calling onMessageSubmitted directly, trying
// again via triggering Event doesn't work for some weird
......@@ -2217,7 +2217,7 @@
view.onMessageSubmitted('/ban annoyingGuy@localhost You\'re annoying');
expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
expect(view.showStatusNotification.calls.count()).toBe(1);
expect(view.setAffiliation).toHaveBeenCalled();
expect(view.model.setAffiliation).toHaveBeenCalled();
// Check that the member list now gets updated
expect(sent_IQ.toLocaleString()).toBe(
"<iq to='lounge@localhost' type='set' xmlns='jabber:client' id='"+IQ_id+"'>"+
......@@ -2902,7 +2902,7 @@
var name = mock.cur_names[0];
var invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
var reason = "Please join this chat room";
view.directInvite(invitee_jid, reason);
view.model.directInvite(invitee_jid, reason);
// Check in reverse order that we requested all three lists
// (member, owner and admin).
......@@ -3023,26 +3023,26 @@
var remove_absentees = false;
var new_list = [];
var old_list = [];
var delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
var delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
expect(delta.length).toBe(0);
new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
expect(delta.length).toBe(0);
// When remove_absentees is false, then affiliations in the old
// list which are not in the new one won't be removed.
old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
expect(delta.length).toBe(0);
// With exclude_existing set to false, any changed affiliations
// will be included in the delta (i.e. existing affiliations
// are included in the comparison).
old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
expect(delta.length).toBe(1);
expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
expect(delta[0].affiliation).toBe('member');
......@@ -3052,12 +3052,12 @@
remove_absentees = true;
old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
expect(delta.length).toBe(1);
expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
expect(delta[0].affiliation).toBe('none');
delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
expect(delta.length).toBe(2);
expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
expect(delta[0].affiliation).toBe('none');
......@@ -3068,7 +3068,7 @@
// affiliation, we set 'exclude_existing' to true
exclude_existing = true;
old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
delta = roomview.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
expect(delta.length).toBe(0);
done();
}));
......
......@@ -29,7 +29,7 @@ require.config({
"emojione": "node_modules/emojione/lib/js/emojione",
"es6-promise": "node_modules/es6-promise/dist/es6-promise.auto",
"eventemitter": "node_modules/otr/build/dep/eventemitter",
"form-utils": "src/form-utils",
"form-utils": "src/utils/form",
"i18n": "src/i18n",
"jed": "node_modules/jed/jed",
"jquery": "src/jquery-stub",
......@@ -37,27 +37,28 @@ require.config({
"lodash.converter": "3rdparty/lodash.fp",
"lodash.fp": "src/lodash.fp",
"lodash.noconflict": "src/lodash.noconflict",
"muc-utils": "src/utils/muc",
"pluggable": "node_modules/pluggable.js/dist/pluggable",
"polyfill": "src/polyfill",
"sizzle": "node_modules/sizzle/dist/sizzle",
"snabbdom": "node_modules/snabbdom/dist/snabbdom",
"snabbdom-attributes": "node_modules/snabbdom/dist/snabbdom-attributes",
"snabbdom-class": "node_modules/snabbdom/dist/snabbdom-class",
"snabbdom-dataset": "node_modules/snabbdom/dist/snabbdom-dataset",
"snabbdom-eventlisteners": "node_modules/snabbdom/dist/snabbdom-eventlisteners",
"snabbdom-props": "node_modules/snabbdom/dist/snabbdom-props",
"snabbdom-style": "node_modules/snabbdom/dist/snabbdom-style",
"strophe": "node_modules/strophe.js/strophe",
"strophe.disco": "node_modules/strophejs-plugin-disco/strophe.disco",
"strophe.ping": "node_modules/strophejs-plugin-ping/strophe.ping",
"strophe.rsm": "node_modules/strophejs-plugin-rsm/strophe.rsm",
"strophe.vcard": "node_modules/strophejs-plugin-vcard/strophe.vcard",
"text": "node_modules/text/text",
"tovnode": "node_modules/snabbdom/dist/tovnode",
"tpl": "node_modules/lodash-template-loader/loader",
"underscore": "src/underscore-shim",
"utils": "src/utils",
"utils": "src/utils/core",
"vdom-parser": "node_modules/vdom-parser/dist",
"snabbdom": "node_modules/snabbdom/dist/snabbdom",
"snabbdom-attributes": "node_modules/snabbdom/dist/snabbdom-attributes",
"snabbdom-class": "node_modules/snabbdom/dist/snabbdom-class",
"snabbdom-dataset": "node_modules/snabbdom/dist/snabbdom-dataset",
"snabbdom-eventlisteners": "node_modules/snabbdom/dist/snabbdom-eventlisteners",
"snabbdom-props": "node_modules/snabbdom/dist/snabbdom-props",
"snabbdom-style": "node_modules/snabbdom/dist/snabbdom-style",
"tovnode": "node_modules/snabbdom/dist/tovnode",
"xss": "node_modules/xss/dist/xss",
"xss.noconflict": "src/xss.noconflict",
......
This diff is collapsed.
This diff is collapsed.
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
// This is the utilities module.
//
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
// Licensed under the Mozilla Public License (MPLv2)
//
/*global define, escape, Jed */
(function (root, factory) {
define(["converse-core", "utils"], factory);
}(this, function (converse, u) {
"use strict";
const { Strophe, sizzle, _ } = converse.env;
u.computeAffiliationsDelta = function computeAffiliationsDelta (exclude_existing, remove_absentees, new_list, old_list) {
/* Given two lists of objects with 'jid', 'affiliation' and
* 'reason' properties, return a new list containing
* those objects that are new, changed or removed
* (depending on the 'remove_absentees' boolean).
*
* The affiliations for new and changed members stay the
* same, for removed members, the affiliation is set to 'none'.
*
* The 'reason' property is not taken into account when
* comparing whether affiliations have been changed.
*
* Parameters:
* (Boolean) exclude_existing: Indicates whether JIDs from
* the new list which are also in the old list
* (regardless of affiliation) should be excluded
* from the delta. One reason to do this
* would be when you want to add a JID only if it
* doesn't have *any* existing affiliation at all.
* (Boolean) remove_absentees: Indicates whether JIDs
* from the old list which are not in the new list
* should be considered removed and therefore be
* included in the delta with affiliation set
* to 'none'.
* (Array) new_list: Array containing the new affiliations
* (Array) old_list: Array containing the old affiliations
*/
const new_jids = _.map(new_list, 'jid');
const old_jids = _.map(old_list, 'jid');
// Get the new affiliations
let delta = _.map(
_.difference(new_jids, old_jids),
(jid) => new_list[_.indexOf(new_jids, jid)]
);
if (!exclude_existing) {
// Get the changed affiliations
delta = delta.concat(_.filter(new_list, function (item) {
const idx = _.indexOf(old_jids, item.jid);
if (idx >= 0) {
return item.affiliation !== old_list[idx].affiliation;
}
return false;
}));
}
if (remove_absentees) {
// Get the removed affiliations
delta = delta.concat(
_.map(
_.difference(old_jids, new_jids),
(jid) => ({'jid': jid, 'affiliation': 'none'})
)
);
}
return delta;
};
u.parseMemberListIQ = function parseMemberListIQ (iq) {
/* Given an IQ stanza with a member list, create an array of member
* objects.
*/
return _.map(
sizzle(`query[xmlns="${Strophe.NS.MUC_ADMIN}"] item`, iq),
(item) => ({
'jid': item.getAttribute('jid'),
'affiliation': item.getAttribute('affiliation'),
})
);
};
u.marshallAffiliationIQs = function marshallAffiliationIQs () {
/* Marshall a list of IQ stanzas into a map of JIDs and
* affiliations.
*
* Parameters:
* Any amount of XMLElement objects, representing the IQ
* stanzas.
*/
return _.flatMap(arguments[0], u.parseMemberListIQ);
}
}));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment