Commit 7aee9c44 authored by JC Brand's avatar JC Brand

muc: simplify `api.rooms` methods.

- Let `api.rooms.open` call `api.rooms.get` directly, instead of indirectly via `api.rooms.create`
- Remove the `createChatRoom` function
- Also strip URI parts from JID in `api.rooms.get` and use utility method for doing so
parent 1af233cc
......@@ -4815,9 +4815,9 @@
}
},
"@octokit/endpoint": {
"version": "6.0.6",
"resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-6.0.6.tgz",
"integrity": "sha512-7Cc8olaCoL/mtquB7j/HTbPM+sY6Ebr4k2X2y4JoXpVKQ7r5xB4iGQE0IoO58wIPsUk4AzoT65AMEpymSbWTgQ==",
"version": "6.0.8",
"resolved": "https://registry.npmjs.org/@octokit/endpoint/-/endpoint-6.0.8.tgz",
"integrity": "sha512-MuRrgv+bM4Q+e9uEvxAB/Kf+Sj0O2JAOBA131uo1o6lgdq1iS8ejKwtqHgdfY91V3rN9R/hdGKFiQYMzVzVBEQ==",
"dev": true,
"requires": {
"@octokit/types": "^5.0.0",
......
......@@ -116,7 +116,10 @@ converse.plugins.add('converse-bookmarks', {
async openBookmarkedRoom (bookmark) {
if ( api.settings.get('muc_respect_autojoin') && bookmark.get('autojoin')) {
const groupchat = await api.rooms.create(bookmark.get('jid'), bookmark.get('nick'));
const groupchat = await api.rooms.create(
bookmark.get('jid'),
{'nick': bookmark.get('nick')}
);
groupchat.maybeShow();
}
return bookmark;
......
......@@ -1102,8 +1102,9 @@ converse.plugins.add('converse-muc', {
'jid': this.get('jid')
};
if (reason !== null) { attrs.reason = reason; }
if (this.get('password')) { attrs.password = this.get('password'); }
if (this.get('password')) {
attrs.password = this.get('password');
}
const invitation = $msg({
'from': _converse.connection.jid,
'to': recipient,
......@@ -2684,13 +2685,6 @@ converse.plugins.add('converse-muc', {
api.listen.on('reconnected', registerDirectInvitationHandler);
}
const createChatRoom = function (jid, attrs) {
if (jid.startsWith('xmpp:') && jid.endsWith('?join')) {
jid = jid.replace(/^xmpp:/, '').replace(/\?join$/, '');
}
return api.rooms.get(jid, attrs, true);
};
/* Automatically join groupchats, based on the
* "auto_join_rooms" configuration setting, which is an array
* of strings (groupchat JIDs) or objects (with groupchat JID and other settings).
......@@ -2823,9 +2817,9 @@ converse.plugins.add('converse-muc', {
if (jids === undefined) {
throw new TypeError('rooms.create: You need to provide at least one JID');
} else if (typeof jids === 'string') {
return createChatRoom(jids, attrs);
return api.rooms.get(u.getJIDFromURI(jids), attrs, true);
}
return jids.map(jid => createChatRoom(jid, attrs));
return jids.map(jid => api.rooms.get(u.getJIDFromURI(jid), attrs, true));
},
/**
......@@ -2894,11 +2888,11 @@ converse.plugins.add('converse-muc', {
log.error(err_msg);
throw(new TypeError(err_msg));
} else if (typeof jids === 'string') {
const room = await api.rooms.create(jids, attrs);
const room = await api.rooms.get(jids, attrs, true);
room && room.maybeShow(force);
return room;
} else {
const rooms = await Promise.all(jids.map(jid => api.rooms.create(jid, attrs)));
const rooms = await Promise.all(jids.map(jid => api.rooms.get(jid, attrs, true)));
rooms.forEach(r => r.maybeShow(force));
return rooms;
}
......@@ -2909,12 +2903,8 @@ converse.plugins.add('converse-muc', {
*
* @method 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 {object} [attrs] A map containing any extra room attributes For example, if you want
* to specify a nickname and password, use `{'nick': 'bloodninja', 'password': 'secret'}`.
* @param {boolean} create A boolean indicating whether the room should be created
* if not found (default: `false`)
* @returns { Promise<_converse.ChatRoom> }
......@@ -2930,6 +2920,7 @@ converse.plugins.add('converse-muc', {
*/
async get (jids, attrs={}, create=false) {
async function _get (jid) {
jid = u.getJIDFromURI(jid);
let model = await api.chatboxes.get(jid);
if (!model && create) {
model = await api.chatboxes.create(jid, attrs, _converse.ChatRoom);
......@@ -2954,5 +2945,3 @@ converse.plugins.add('converse-muc', {
/************************ END API ************************/
}
});
......@@ -34,6 +34,12 @@ const parser = new DOMParser();
const parserErrorNS = parser.parseFromString('invalid', 'text/xml')
.getElementsByTagName("parsererror")[0].namespaceURI;
u.getJIDFromURI = function (jid) {
return jid.startsWith('xmpp:') && jid.endsWith('?join')
? jid.replace(/^xmpp:/, '').replace(/\?join$/, '')
: jid;
}
u.toStanza = function (string) {
const node = parser.parseFromString(string, "text/xml");
if (node.getElementsByTagNameNS(parserErrorNS, 'parsererror').length) {
......
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