Commit cd731ed6 authored by JC Brand's avatar JC Brand

Some refactoring to make plugins easier.

- Refactored sound notification to make it a bit more generic
- Split up onMessage
parent 4ebed3c9
......@@ -196,7 +196,7 @@
test_utils.openChatRoom('lounge', 'localhost', 'dummy');
spyOn(converse, 'emit');
converse.play_sounds = true;
spyOn(converse, 'playNotification');
spyOn(converse, 'notifyOfNewMessage');
var view = this.chatboxviews.get('lounge@localhost');
if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
var text = 'This message will play a sound because it mentions dummy';
......@@ -207,7 +207,7 @@
type: 'groupchat'
}).c('body').t(text);
view.onChatRoomMessage(message.nodeTree);
expect(converse.playNotification).toHaveBeenCalled();
expect(converse.notifyOfNewMessage).toHaveBeenCalled();
text = "This message won't play a sound";
message = $msg({
......@@ -217,7 +217,7 @@
type: 'groupchat'
}).c('body').t(text);
view.onChatRoomMessage(message.nodeTree);
expect(converse.playNotification, 1);
expect(converse.notifyOfNewMessage, 1);
converse.play_sounds = false;
text = "This message won't play a sound because it is sent by dummy";
......@@ -228,7 +228,7 @@
type: 'groupchat'
}).c('body').t(text);
view.onChatRoomMessage(message.nodeTree);
expect(converse.playNotification, 1);
expect(converse.notifyOfNewMessage, 1);
converse.play_sounds = false;
}.bind(converse));
......
......@@ -88,6 +88,32 @@
converse.OPENED = 'opened';
converse.CLOSED = 'closed';
var KEY = {
ENTER: 13,
FORWARD_SLASH: 47
};
var PRETTY_CONNECTION_STATUS = {
0: 'ERROR',
1: 'CONNECTING',
2: 'CONNFAIL',
3: 'AUTHENTICATING',
4: 'AUTHFAIL',
5: 'CONNECTED',
6: 'DISCONNECTED',
7: 'DISCONNECTING',
8: 'ATTACHED',
9: 'REDIRECT'
};
// XEP-0085 Chat states
// http://xmpp.org/extensions/xep-0085.html
var INACTIVE = 'inactive';
var ACTIVE = 'active';
var COMPOSING = 'composing';
var PAUSED = 'paused';
var GONE = 'gone';
// TODO Refactor into external MAM plugin
// XEP-0059 Result Set Management
var RSM_ATTRIBUTES = ['max', 'first', 'last', 'after', 'before', 'index', 'count'];
......@@ -174,6 +200,38 @@
converse.connection.sendIQ(stanza, null, errback);
};
converse.isOnlyChatStateNotification = function ($msg) {
// See XEP-0085 Chat State Notification
return (
$msg.find('body').length === 0 && (
$msg.find(ACTIVE).length !== 0 ||
$msg.find(COMPOSING).length !== 0 ||
$msg.find(INACTIVE).length !== 0 ||
$msg.find(PAUSED).length !== 0 ||
$msg.find(GONE).length !== 0
)
);
};
converse.log = function (txt, level) {
var logger;
if (typeof console === "undefined" || typeof console.log === "undefined") {
logger = { log: function () {}, error: function () {} };
} else {
logger = console;
}
if (converse.debug) {
if (level === 'error') {
logger.log('ERROR: '+txt);
} else {
logger.log(txt);
}
}
};
converse.initialize = function (settings, callback) {
"use strict";
var converse = this;
......@@ -203,33 +261,6 @@
Strophe.addNamespace('RSM', 'http://jabber.org/protocol/rsm');
Strophe.addNamespace('XFORM', 'jabber:x:data');
// Constants
// ---------
var KEY = {
ENTER: 13,
FORWARD_SLASH: 47
};
var PRETTY_CONNECTION_STATUS = {
0: 'ERROR',
1: 'CONNECTING',
2: 'CONNFAIL',
3: 'AUTHENTICATING',
4: 'AUTHFAIL',
5: 'CONNECTED',
6: 'DISCONNECTED',
7: 'DISCONNECTING',
8: 'ATTACHED',
9: 'REDIRECT'
};
// XEP-0085 Chat states
// http://xmpp.org/extensions/xep-0085.html
var INACTIVE = 'inactive';
var ACTIVE = 'active';
var COMPOSING = 'composing';
var PAUSED = 'paused';
var GONE = 'gone';
this.TIMEOUTS = { // Set as module attr so that we can override in tests.
'PAUSED': 20000,
'INACTIVE': 90000
......@@ -483,7 +514,23 @@
window.setInterval(this.onEverySecond.bind(this), 1000);
};
this.playNotification = function () {
this.shouldNotifyOfNewMessage = function ($message) {
var $forwarded = $message.find('forwarded');
if ($forwarded.length) {
return false;
}
var is_me = Strophe.getBareJidFromJid($message.attr('from')) === converse.bare_jid;
return !converse.isOnlyChatStateNotification($message) && !is_me;
};
this.notifyOfNewMessage = function ($message) {
/* Plays a sound to notify that a new message was recieved.
*
* Returns true if the notification was made and false otherwise.
*/
if (!this.shouldNotifyOfNewMessage($message)) {
return false;
}
var audio;
if (converse.play_sounds && typeof Audio !== "undefined") {
audio = new Audio(converse.sounds_path+"msg_received.ogg");
......@@ -494,6 +541,7 @@
audio.play();
}
}
return true;
};
this.giveFeedback = function (message, klass) {
......@@ -508,22 +556,6 @@
});
};
this.log = function (txt, level) {
var logger;
if (typeof console === "undefined" || typeof console.log === "undefined") {
logger = { log: function () {}, error: function () {} };
} else {
logger = console;
}
if (this.debug) {
if (level === 'error') {
logger.log('ERROR: '+txt);
} else {
logger.log(txt);
}
}
};
this.rejectPresenceSubscription = function (jid, message) {
/* Reject or cancel another user's subscription to our presence updates.
* Parameters:
......@@ -757,11 +789,11 @@
}.bind(this));
$(window).on("blur focus", function (ev) {
if ((this.windowState !== ev.type) && (ev.type === 'focus')) {
if ((converse.windowState !== ev.type) && (ev.type === 'focus')) {
converse.clearMsgCounter();
}
this.windowState = ev.type;
}.bind(this));
converse.windowState = ev.type;
});
$(window).on("resize", _.debounce(function (ev) {
this.chatboxviews.trimChats();
......@@ -903,28 +935,6 @@
});
},
isOnlyChatStateNotification: function ($msg) {
// See XEP-0085 Chat State Notification
return (
$msg.find('body').length === 0 && (
$msg.find(ACTIVE).length !== 0 ||
$msg.find(COMPOSING).length !== 0 ||
$msg.find(INACTIVE).length !== 0 ||
$msg.find(PAUSED).length !== 0 ||
$msg.find(GONE).length !== 0
)
);
},
shouldPlayNotification: function ($message) {
var $forwarded = $message.find('forwarded');
if ($forwarded.length) {
return false;
}
var is_me = Strophe.getBareJidFromJid($message.attr('from')) === converse.bare_jid;
return !this.isOnlyChatStateNotification($message) && !is_me;
},
createMessage: function ($message, $delay, archive_id) {
$delay = $delay || $message.find('delay');
var body = $message.children('body').text(),
......@@ -1348,34 +1358,21 @@
return this.scrollDown();
},
onMessageAdded: function (message) {
/* Handler that gets called when a new message object is created.
*
* Parameters:
* (Object) message - The message Backbone object that was added.
*/
if (typeof this.clear_status_timeout !== 'undefined') {
window.clearTimeout(this.clear_status_timeout);
delete this.clear_status_timeout;
}
if (!message.get('message')) {
handleChatStateMessage: function (message) {
if (message.get('chat_state') === COMPOSING) {
this.showStatusNotification(message.get('fullname')+' '+__('is typing'));
this.clear_status_timeout = window.setTimeout(this.clearStatusNotification.bind(this), 10000);
return;
} else if (message.get('chat_state') === PAUSED) {
this.showStatusNotification(message.get('fullname')+' '+__('has stopped typing'));
return;
} else if (_.contains([INACTIVE, ACTIVE], message.get('chat_state'))) {
this.$content.find('div.chat-event').remove();
return;
} else if (message.get('chat_state') === GONE) {
this.showStatusNotification(message.get('fullname')+' '+__('has gone away'));
return;
}
} else {
},
handleTextMessage: function (message) {
this.showMessage(_.clone(message.attributes));
}
if ((message.get('sender') !== 'me') && (converse.windowState === 'blur')) {
converse.incrementMsgCounter();
}
......@@ -1384,6 +1381,23 @@
}
},
onMessageAdded: function (message) {
/* Handler that gets called when a new message object is created.
*
* Parameters:
* (Object) message - The message Backbone object that was added.
*/
if (typeof this.clear_status_timeout !== 'undefined') {
window.clearTimeout(this.clear_status_timeout);
delete this.clear_status_timeout;
}
if (!message.get('message')) {
this.handleChatStateMessage(message);
} else {
this.handleTextMessage(message);
}
},
createMessageStanza: function (message) {
return $msg({
from: converse.connection.jid,
......@@ -1903,9 +1917,7 @@
if (msgid && chatbox.messages.findWhere({msgid: msgid})) {
return true; // We already have this message stored.
}
if (chatbox.shouldPlayNotification($message)) {
converse.playNotification();
}
converse.notifyOfNewMessage($message);
chatbox.createMessage($message, $delay, archive_id);
converse.roster.addResource(contact_jid, resource);
converse.emit('message', message);
......
......@@ -916,7 +916,7 @@
}
this.model.createMessage($message, $delay, archive_id);
if (!delayed && sender !== this.model.get('nick') && (new RegExp("\\b"+this.model.get('nick')+"\\b")).test(body)) {
converse.playNotification();
converse.notifyOfNewMessage();
}
if (sender !== this.model.get('nick')) {
// We only emit an event if it's not our own message
......
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