Comparing version 1.0.6 to 1.1.0
@@ -38,3 +38,3 @@ 'use strict'; | ||
else throw new Error(utils.i18n.layerapi.agent); | ||
} | ||
} | ||
else if (config.agentOptions) { | ||
@@ -92,4 +92,3 @@ this.agent = new https.Agent(config.agentOptions); | ||
if (socket.authorized) write(); | ||
// TODO: should also check if authorized here and throw if not | ||
else socket.on('secureConnect', function() { write(); }) | ||
else socket.on('secureConnect', function() { write(); }) | ||
}); | ||
@@ -96,0 +95,0 @@ } |
@@ -59,3 +59,3 @@ 'use strict'; | ||
else conversationUUID = utils.toUUID(conversationId); | ||
utils.debug('Conversation getFromUser: ' + userId); | ||
utils.debug('Conversation getFromUser: ' + userId + ', ' + conversationUUID); | ||
@@ -62,0 +62,0 @@ request.get({ |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var querystring = require('querystring'); | ||
var utils = require('../utils'); | ||
@@ -40,5 +42,4 @@ | ||
*/ | ||
sendTexFromUser: function(conversationId, userId, text, callback) { | ||
send(null, conversationId, utils.messageText('user_id', userId, text), callback); | ||
}, | ||
sendTextFromUser: fromUser, | ||
sendTexFromUser: fromUser, // Deprecated | ||
@@ -53,7 +54,65 @@ /** | ||
*/ | ||
sendTexFromName: function(conversationId, name, text, callback) { | ||
send(null, conversationId, utils.messageText('name', name, text), callback); | ||
sendTextFromName: fromName, | ||
sendTexFromName: fromName, // Deprecated | ||
/** | ||
* Retrieve messages in a conversation | ||
* | ||
* @param {String} conversationId Conversation ID | ||
* @param {Function} callback Callback function | ||
*/ | ||
getAll: function(conversationId, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.messages.cid)); | ||
utils.debug('Message getAll: ' + conversationId); | ||
request.get({ | ||
path: '/conversations/' + conversationId + '/messages' | ||
}, callback || utils.nop); | ||
}, | ||
/** | ||
* Retrieve messages in a conversation from a user | ||
* | ||
* @param {String} userId User ID | ||
* @param {String} conversationId Conversation ID | ||
* @param {Function} callback Callback function | ||
*/ | ||
getAllFromUser: function(userId, conversationId, callback) { | ||
if (!userId) return callback(new Error(utils.i18n.messages.userId)); | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.messages.cid)); | ||
utils.debug('Message getAllFromUser: ' + userId + ', ' + conversationId); | ||
request.get({ | ||
path: '/users/' + querystring.escape(userId) + '/conversations/' + conversationId + '/messages' | ||
}, callback || utils.nop); | ||
}, | ||
/** | ||
* Retrieve a message in a conversation from a user | ||
* | ||
* @param {String} userId User ID | ||
* @param {String} messageId Message ID | ||
* @param {Function} callback Callback function | ||
*/ | ||
getFromUser: function(userId, messageId, callback) { | ||
if (!userId) return callback(new Error(utils.i18n.messages.userId)); | ||
messageId = utils.toUUID(messageId); | ||
if (!messageId) return callback(new Error(utils.i18n.messages.mid)); | ||
utils.debug('Message getFromUser: ' + userId + ', ' + messageId); | ||
request.get({ | ||
path: '/users/' + querystring.escape(userId) + '/messages/' + messageId | ||
}, callback || utils.nop); | ||
} | ||
}; | ||
function fromUser(conversationId, userId, text, callback) { | ||
send(null, conversationId, utils.messageText('user_id', userId, text), callback); | ||
} | ||
function fromName(conversationId, name, text, callback) { | ||
send(null, conversationId, utils.messageText('name', name, text), callback); | ||
} | ||
function send(dedupe, conversationId, body, callback) { | ||
@@ -60,0 +119,0 @@ if (dedupe !== null && !utils.toUUID(dedupe)) return callback(new Error(utils.i18n.dedupe)); |
@@ -76,3 +76,5 @@ 'use strict'; | ||
cid: 'Conversation ID is required', | ||
body: 'Message body is required' | ||
mid: 'Message ID is required', | ||
body: 'Message body is required', | ||
userId: 'User ID is required' | ||
}, | ||
@@ -79,0 +81,0 @@ participants: { |
{ | ||
"name": "layer-api", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Node.js library, which provides a wrapper for the Layer Platform API", | ||
@@ -22,9 +22,9 @@ "publishConfig": { | ||
"devDependencies": { | ||
"mocha": "^2.2.5", | ||
"nock": "^2.7.0", | ||
"should": "^7.0.4" | ||
"mocha": "^2.4.5", | ||
"nock": "^2.18.2", | ||
"should": "^7.1.1" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^2.9.34" | ||
"bluebird": "^2.10.2" | ||
} | ||
} |
@@ -35,3 +35,3 @@ # Layer API for node.js | ||
// Send a Message | ||
layer.messages.sendTexFromUser(cid, 'abcd', 'Hello, World!', function(err, res) { | ||
layer.messages.sendTextFromUser(cid, 'abcd', 'Hello, World!', function(err, res) { | ||
console.log(err || res.body); | ||
@@ -224,3 +224,3 @@ }); | ||
### messages.sendTexFromUser(cid, userId, text, [callback]) | ||
### messages.sendTextFromUser(cid, userId, text, [callback]) | ||
@@ -238,3 +238,3 @@ Shorthand method for sending a plain text Message by providing `userId` and `text`. | ||
### messages.sendTexFromName(cid, name, text, [callback]) | ||
### messages.sendTextFromName(cid, name, text, [callback]) | ||
@@ -250,2 +250,41 @@ Shorthand method for sending a plain text Message by providing `name` and `text`. | ||
### messages.getAll(cid, [callback]) | ||
[Retrieve](https://developer.layer.com/docs/platform#retrieving-messages) all messages in a conversation by providing `cid`. Response `body` will result in array of messages. | ||
##### Arguments | ||
- `cid` - Conversation ID | ||
- `callback(err, res)` - Callback function returns an error and response objects | ||
##### Examples | ||
```javascript | ||
layer.messages.getAll(cid, function(err, res) { | ||
if (err) return console.error(err); | ||
var messages = res.body; | ||
}); | ||
``` | ||
### messages.getAllFromUser(userId, cid, [callback]) | ||
Retrieve all messages in a conversation from a specific user by providing `userId` and `cid`. Response `body` will result in array of messages. | ||
##### Arguments | ||
- `userId` - User ID | ||
- `cid` - Conversation ID | ||
- `callback(err, res)` - Callback function returns an error and response objects | ||
### messages.getFromUser(userId, messageId, [callback]) | ||
Retrieve a single message in a conversation from a specific user by providing `userId` and `messageId`. Response `body` will result in a single message representation. | ||
##### Arguments | ||
- `userId` - User ID | ||
- `messageId` - Message ID | ||
- `callback(err, res)` - Callback function returns an error and response objects | ||
## Announcements | ||
@@ -252,0 +291,0 @@ |
@@ -40,2 +40,4 @@ { | ||
"messages": { | ||
"id": "layer:///messages/940de862-3c96-11e4-baad-164230d1df67", | ||
"uuid": "940de862-3c96-11e4-baad-164230d1df67", | ||
"success": { | ||
@@ -42,0 +44,0 @@ "id": "layer:///messages/940de862-3c96-11e4-baad-164230d1df67", |
@@ -12,3 +12,4 @@ /*globals describe it*/ | ||
var participants = ['test123'], | ||
conversationId = null; | ||
conversationId = null, | ||
messageId = null; | ||
@@ -89,5 +90,5 @@ var message = { | ||
describe('Sending a message to a conversation using sendTexFromName', function() { | ||
it('should return an error', function(done) { | ||
layerAPI.messages.sendTexFromName(conversationId, message.sender.name, 'hello world', function(err, res) { | ||
describe('Sending a message to a conversation using sendTextFromName', function() { | ||
it('should return a message object', function(done) { | ||
layerAPI.messages.sendTextFromName(conversationId, message.sender.name, 'hello world', function(err, res) { | ||
should.not.exist(err); | ||
@@ -98,2 +99,3 @@ should.exist(res); | ||
res.body.should.have.properties(['conversation', 'id', 'parts', 'sender']); | ||
messageId = res.body.id; | ||
@@ -105,5 +107,5 @@ done(); | ||
describe('Sending a message to a conversation using sendTexFromUser', function() { | ||
it('should return an error', function(done) { | ||
layerAPI.messages.sendTexFromUser(conversationId, participants[0], 'hello world2', function(err, res) { | ||
describe('Sending a message to a conversation using sendTextFromUser', function() { | ||
it('should return a message object', function(done) { | ||
layerAPI.messages.sendTextFromUser(conversationId, participants[0], 'hello world2', function(err, res) { | ||
should.not.exist(err); | ||
@@ -119,2 +121,44 @@ should.exist(res); | ||
}); | ||
describe('Retrieving messages in a conversation', function() { | ||
it('should return array of messages', function(done) { | ||
layerAPI.messages.getAll(conversationId, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(200); | ||
res.body.length.should.be.eql(3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Retrieving all messages in a conversation from a user', function() { | ||
it('should return array of messages', function(done) { | ||
layerAPI.messages.getAllFromUser(participants[0], conversationId, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(200); | ||
res.body.length.should.be.eql(3); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Retrieving a messages in a conversation from a user', function() { | ||
it('should return array of messages', function(done) { | ||
layerAPI.messages.getFromUser(participants[0], messageId, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(200); | ||
res.body.id.should.be.eql(messageId); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -25,8 +25,8 @@ /*globals describe it*/ | ||
should(typeof layerApi.messages.sendDedupe).be.eql('function'); | ||
should(typeof layerApi.messages.sendTexFromUser).be.eql('function'); | ||
should(typeof layerApi.messages.sendTexFromUser).be.eql('function'); | ||
should(typeof layerApi.messages.sendTextFromUser).be.eql('function'); | ||
should(typeof layerApi.messages.sendTextFromUser).be.eql('function'); | ||
should(typeof layerApi.announcements.send).be.eql('function'); | ||
should(typeof layerApi.announcements.sendDedupe).be.eql('function'); | ||
should(typeof layerApi.blocklist.get).be.eql('function'); | ||
@@ -80,2 +80,14 @@ should(typeof layerApi.blocklist.block).be.eql('function'); | ||
}); | ||
describe('Passing config with invalid agent', function() { | ||
it('should throw an error', function() { | ||
try { | ||
new LayerAPI({token: fixtures.token, appId: fixtures.appIdFull, agent: 123}); | ||
} | ||
catch (err) { | ||
should.exist(err); | ||
err.message.should.be.eql(utils.i18n.layerapi.agent); | ||
} | ||
}); | ||
}); | ||
}); |
@@ -69,3 +69,3 @@ /*globals describe it*/ | ||
describe('Sending a message to a conversation using sendTexFromUser', function() { | ||
describe('Sending a message to a conversation using sendTextFromUser', function() { | ||
nock('https://api.layer.com') | ||
@@ -78,3 +78,3 @@ .post('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid + '/messages') | ||
var text = 'testing 123'; | ||
layerAPI.messages.sendTexFromUser(fixtures.conversations.uuid, userId, text, function(err, res) { | ||
layerAPI.messages.sendTextFromUser(fixtures.conversations.uuid, userId, text, function(err, res) { | ||
should.not.exist(err); | ||
@@ -91,3 +91,3 @@ should.exist(res); | ||
describe('Sending a message to a conversation using sendTexFromName', function() { | ||
describe('Sending a message to a conversation using sendTextFromName', function() { | ||
nock('https://api.layer.com') | ||
@@ -100,3 +100,3 @@ .post('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid + '/messages') | ||
var text = 'testing 123'; | ||
layerAPI.messages.sendTexFromName(fixtures.conversations.uuid, name, text, function(err, res) { | ||
layerAPI.messages.sendTextFromName(fixtures.conversations.uuid, name, text, function(err, res) { | ||
should.not.exist(err); | ||
@@ -140,2 +140,53 @@ should.exist(res); | ||
}); | ||
describe('Retrieving all messages in a conversation from a user', function() { | ||
nock('https://api.layer.com') | ||
.get('/apps/' + fixtures.appId + '/users/12345/conversations/' + fixtures.conversations.uuid + '/messages') | ||
.reply(200, [fixtures.messages.success]); | ||
it('should return array of messages', function(done) { | ||
layerAPI.messages.getAllFromUser(12345, fixtures.conversations.uuid, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.body.length.should.be.eql(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Retrieving messages in a conversation', function() { | ||
nock('https://api.layer.com') | ||
.get('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid + '/messages') | ||
.reply(200, [fixtures.messages.success]); | ||
it('should return array of messages', function(done) { | ||
layerAPI.messages.getAll(fixtures.conversations.uuid, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.body.length.should.be.eql(1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('Retrieving a messages in a conversation from a user', function() { | ||
nock('https://api.layer.com') | ||
.get('/apps/' + fixtures.appId + '/users/12345/messages/' + fixtures.messages.uuid) | ||
.reply(200, fixtures.messages.success); | ||
it('should return array of messages', function(done) { | ||
layerAPI.messages.getFromUser(12345, fixtures.messages.id, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.body.id.should.be.eql(fixtures.messages.id); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
84142
1661
432
Updatedbluebird@^2.10.2