Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update to receipt code

  • Loading branch information...
commit 046185b235cde1cc93b5f0aa328b7cbebb74b2ea 1 parent e16725f
@lloydwatkin lloydwatkin authored
Showing with 53 additions and 38 deletions.
  1. +1 −1  .jshintrc
  2. +8 −0 lib/base.js
  3. +28 −15 lib/chat.js
  4. +16 −22 test/lib/chat.js
View
2  .jshintrc
@@ -10,7 +10,7 @@
"laxcomma": true,
"maxparams": 4,
"maxdepth": 3,
- "maxstatements": 15,
+ "maxstatements": 20,
"maxcomplexity": 10,
"maxlen": 120,
"newcap": true,
View
8 lib/base.js
@@ -101,4 +101,12 @@ Base.prototype._clientError = function(message, original, callback) {
return false
}
+Base.prototype._hasValidCallback = function(callback, data) {
+ if (typeof callback !== 'function') {
+ return this._clientError(
+ 'Missing callback', data
+ )
+ }
+}
+
module.exports = Base
View
43 lib/chat.js
@@ -67,33 +67,46 @@ Chat.prototype._messageReceipt = function(stanza, callback) {
callback(null, true)
}
+Chat.prototype._addState = function(stanza, chatState) {
+ if (!chatState) return
+ stanza.root().c(chatState, { xmlns: state.NS })
+}
+
+Chat.prototype._addReceiptRequest = function(stanza, data, hasValidCallback) {
+ if (!data.receipt) return true
+
+ if (false === hasValidCallback) {
+ this._clientError('Callback required', data)
+ return false
+ }
+ stanza.root().c('request', { xmlns: this.NS_RECEIPT })
+}
+
Chat.prototype.sendMessage = function(data, callback) {
if (!data.to) return this._clientError('Missing \'to\' key', data)
data.type = 'chat'
- if (!data.content && !data.state)
+ if (!data.content && !data.state) {
return this._clientError(
'Message content or chat state not provided', data
)
+ }
var stanza
if (!(stanza = xhtmlIm.builder(data, { to: data.to, type: 'chat' }, this)))
return
- if (data.receipt && (!callback || (typeof callback !== 'function'))) {
- return this._clientError(
- 'Missing callback', data
- )
- }
- if (data.state) {
- stanza.root().c(data.state, { xmlns: state.NS })
- }
- if (data.receipt) {
- stanza.root().c('request', { xmlns: this.NS_RECEIPT })
+ var hasValidCallback = false
+
+ if (callback) {
+ if (false === this._hasValidCallback(callback, data)) return
+ hasValidCallback = true
stanza.root().attrs.id = this._getId()
- var self = this
- this.manager.trackId(stanza.root().attr('id'), function(stanza) {
- self._messageReceipt(stanza, callback)
- })
+ }
+ this._addState(stanza, data.state)
+ if (false === this._addReceiptRequest(stanza, data, hasValidCallback)) {
+ return
}
this.client.send(stanza)
+ if (false === hasValidCallback) return
+ callback(null, { id: stanza.root().attrs.id })
}
module.exports = Chat
View
38 test/lib/chat.js
@@ -278,16 +278,16 @@ describe('Chat', function() {
})
})
-
+
describe('Receipts XEP-0184', function() {
-
+
describe('Incoming', function() {
-
-
+
+
})
-
+
describe('Outgoing', function() {
-
+
it('Errors if receipt requested but no callback provided', function(done) {
var request = {
to: 'user@example.com',
@@ -300,15 +300,15 @@ describe('Chat', function() {
socket.once('xmpp.error.client', function(error) {
error.type.should.equal('modify')
error.condition.should.equal('client-error')
- error.description.should.equal('Missing callback')
+ error.description.should.equal('Callback required')
error.request.should.eql(request)
xmpp.removeAllListeners('stanza')
done()
})
socket.send('xmpp.chat.message', request)
})
-
- it('Errors if receipt requested but non-function callback provided', function(done) {
+
+ it('Errors if non-function callback provided', function(done) {
var request = {
to: 'user@example.com',
content: 'hello',
@@ -327,7 +327,7 @@ describe('Chat', function() {
})
socket.send('xmpp.chat.message', request, true)
})
-
+
it('Sends expected stanza with receipt request', function(done) {
var to = 'user@domain/resource'
var content = 'message'
@@ -341,26 +341,20 @@ describe('Chat', function() {
})
chat.sendMessage({ to: to, content: content, receipt: true }, function() {})
})
-
- it('Handles error response', function(done) {
+
+ it('Retuns message ID', function(done) {
var to = 'user@domain/resource'
var content = 'message'
- xmpp.once('stanza', function() {
- manager.makeCallback(helper.getStanza('iq-error'))
- })
var callback = function(error, success) {
- should.not.exist(success)
- error.should.eql({
- type: 'cancel',
- condition: 'error-condition'
- })
+ should.not.exist(error)
+ success.id.should.exist
done()
}
chat.sendMessage({ to: to, content: content, receipt: true }, callback)
})
-
+
})
-
+
})
})
Please sign in to comment.
Something went wrong with that request. Please try again.