Skip to content

Commit

Permalink
[TEMP #292] Consolidate presence sending.
Browse files Browse the repository at this point in the history
xmpp.announce() is a one-liner only called once. Destroy.
xmpp.presence() is a wrapper for xmpp.pres(), which is a wrapper for $pres.
Remove and give xmpp.pres() optional arguments instead.
  • Loading branch information
cburschka committed Dec 22, 2015
1 parent a47ea72 commit b60f24c
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions js/core/xmpp.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,22 @@ var xmpp = {
},

/**
* Wrapper for $pres() that fills in the current JID.
* Wrapper for $pres() that fills in the sender JID, an optional target,
* and optional attributes.
*
* @param {Object} target The target of the directed presence.
* @param {Object} attrs The attributes of the <presence/> element.
*/
pres: function() {
return $pres({from: this.connection.jid});
pres: function(target, attrs) {
return $pres({
from: this.currentJid,
to: target && this.jid(target)
}).attrs(attrs);
},


/**
* Wrapper for $msg() that fills in the sender JID and the target JID.
* Wrapper for $msg() that fills in the sender JID and the target.
*
* @param {object} target: A target passed to xmpp.jid().
*/
Expand Down Expand Up @@ -103,14 +111,6 @@ var xmpp = {
return iq;
},

/**
* Announce general availability to the server by sending an empty presence
* with no recipient.
*/
announce: function() {
this.connection.send(this.pres());
},

/**
* Create a unique client identifier from the current millisecond timestamp.
*/
Expand Down Expand Up @@ -167,7 +167,7 @@ var xmpp = {
changeNick: function(nick) {
this.nick.target = nick;
if (this.status == 'online')
this.connection.send(this.presence({nick: nick}));
this.connection.send(this.pres({nick: nick}));
else ui.messageAddInfo(strings.info.nickPrejoin, {nick: nick});
},

Expand All @@ -178,7 +178,10 @@ var xmpp = {
*/
leaveRoom: function(room) {
ui.messageAddInfo(strings.info.leave, {room: this.room.available[room]}, 'verbose');
this.connection.send(this.presence({room: room, nick: this.nick.current}, {type: 'unavailable'}));
this.connection.send(this.pres(
{room: room, nick: this.nick.current},
{type: 'unavailable'}
));
delete this.roster[room];
// The server does not acknowledge the /part command, so we need to change
// the state right here: If the room we left is the current one, enter
Expand Down Expand Up @@ -316,7 +319,7 @@ var xmpp = {
*/
joinRoom: function(room, password) {
this.room.target = room;
var presence = this.presence({room: room, nick: this.nick.target})
var presence = this.pres({room: room, nick: this.nick.target})
.c('x', {xmlns:Strophe.NS.MUC})
.c('history', {since: this.historyEnd[room] || '1970-01-01T00:00:00Z'});
if (password) presence.up().c('password', password);
Expand Down Expand Up @@ -352,19 +355,6 @@ var xmpp = {
), success, error, 15000);
},

/**
* Create a directed presence to a specific target, with specific attributes.
* The stanza is not yet sent, but returned to the caller for additional data.
*
* @param {Object} target The target of the directed presence.
* @param {Object} attrs The attributes of the <presence/> element.
*/
presence: function(target, attrs) {
return this.pres().attrs({
to: this.jid(target)
}).attrs(attrs);
},

/**
* Request a room configuration form and fill it out with the values provided.
*
Expand Down Expand Up @@ -538,7 +528,7 @@ var xmpp = {
* @param {string} status This is an arbitrary status message.
*/
sendStatus: function(show, status) {
var p = this.presence({nick: this.nick.current});
var p = this.pres({nick: this.nick.current});
if (show) p.c('show', {}, show);
if (status) p.c('status', {}, status);
this.userStatus = show;
Expand Down Expand Up @@ -1033,7 +1023,7 @@ var xmpp = {
ui.setStatus(this.status);

if (status == 'prejoin') {
this.announce();
this.connection.send(this.pres());
var room = this.room.target || ui.getFragment() || config.settings.xmpp.room;
if (config.settings.xmpp.autoJoin || ui.urlFragment) {
this.discoverRooms(function (rooms) {
Expand Down Expand Up @@ -1085,7 +1075,7 @@ var xmpp = {
*/
disconnect: function() {
if (this.connection) {
this.connection.send(this.pres().attrs({type: 'unavailable'}));
this.connection.send(this.pres({}, {type: 'unavailable'}));
this.connection.disconnect();
}
}
Expand Down

0 comments on commit b60f24c

Please sign in to comment.