Comparing version 2.0.0 to 2.1.0
@@ -98,9 +98,113 @@ 'use strict'; | ||
request.patch({ | ||
path: '/conversations/' + conversationId, | ||
body: operations | ||
}, callback || utils.nop); | ||
edit(conversationId, operations, callback); | ||
}, | ||
/** | ||
* Set metadata on a conversation | ||
* | ||
* @param {String} conversationId Conversation UUID | ||
* @param {Object} properties Properties object | ||
* @param {Function} callback Callback function | ||
*/ | ||
setMetadataProperties: function(conversationId, properties, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.conversations.id)); | ||
utils.debug('Conversation setMetadataProperties: ' + conversationId); | ||
var operations = []; | ||
Object.keys(properties).forEach(function(name) { | ||
var fullName = name; | ||
if (name !== 'metadata' && name.indexOf('metadata.') !== 0) { | ||
fullName = 'metadata.' + name; | ||
} | ||
operations.push({ | ||
operation: 'set', | ||
property: fullName, | ||
value: String(properties[name]), | ||
}); | ||
}); | ||
edit(conversationId, operations, callback); | ||
}, | ||
/** | ||
* Delete metadata on a conversation | ||
* | ||
* @param {String} conversationId Conversation UUID | ||
* @param {Object} properties Properties object | ||
* @param {Function} callback Callback function | ||
*/ | ||
deleteMetadataProperties: function(conversationId, properties, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.conversations.id)); | ||
utils.debug('Conversation deleteMetadataProperties: ' + conversationId); | ||
var operations = []; | ||
Object.keys(properties).forEach(function(property) { | ||
if (property !== 'metadata' && property.indexOf('metadata.') !== 0) { | ||
property = 'metadata.' + property; | ||
} | ||
operations.push({ | ||
operation: 'delete', | ||
property: property, | ||
}); | ||
}); | ||
edit(conversationId, operations, callback); | ||
}, | ||
/** | ||
* Participants add/remove/set operations on a conversation | ||
* | ||
* @param {String} conversationId Conversation UUID | ||
* @param {Object} participants Array of participant IDs | ||
* @param {Function} callback Callback function | ||
*/ | ||
addParticipants: function(conversationId, participants, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.conversations.id)); | ||
if (!utils.isArray(participants)) return callback(new Error(utils.i18n.conversations.participants)); | ||
utils.debug('Conversation addParticipants: ' + conversationId); | ||
var operations = participants.map(function(participant) { | ||
return { | ||
operation: 'add', | ||
property: 'participants', | ||
value: participant | ||
}; | ||
}); | ||
edit(conversationId, operations, callback); | ||
}, | ||
removeParticipants: function(conversationId, participants, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.conversations.id)); | ||
if (!utils.isArray(participants)) return callback(new Error(utils.i18n.conversations.participants)); | ||
utils.debug('Conversation removeParticipants: ' + conversationId); | ||
var operations = participants.map(function(participant) { | ||
return { | ||
operation: 'remove', | ||
property: 'participants', | ||
value: participant | ||
}; | ||
}); | ||
edit(conversationId, operations, callback); | ||
}, | ||
replaceParticipants: function(conversationId, participants, callback) { | ||
conversationId = utils.toUUID(conversationId); | ||
if (!conversationId) return callback(new Error(utils.i18n.conversations.id)); | ||
if (!utils.isArray(participants)) return callback(new Error(utils.i18n.conversations.participants)); | ||
utils.debug('Conversation replaceParticipants: ' + conversationId); | ||
var operations = { | ||
operation: 'set', | ||
property: 'participants', | ||
value: participants | ||
}; | ||
edit(conversationId, [operations], callback); | ||
}, | ||
/** | ||
* Delete a conversation | ||
@@ -133,2 +237,9 @@ * | ||
} | ||
function edit(conversationId, operations, callback) { | ||
request.patch({ | ||
path: '/conversations/' + conversationId, | ||
body: operations | ||
}, callback || utils.nop); | ||
} | ||
}; |
@@ -72,3 +72,4 @@ 'use strict'; | ||
userId: 'User ID is required', | ||
operations: 'Conversation operations should be array' | ||
operations: 'Conversation operations should be array', | ||
participants: 'Array of participants is required' | ||
}, | ||
@@ -75,0 +76,0 @@ messages: { |
{ | ||
"name": "layer-api", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Node.js library, which provides a wrapper for the Layer Platform API", | ||
@@ -5,0 +5,0 @@ "publishConfig": { |
@@ -163,2 +163,37 @@ # Layer API for node.js | ||
### conversations.setMetadataProperties(cid, properties, [callback]) | ||
[Set metadata](https://developer.layer.com/docs/platform#set-amp-delete-metadata) on an existing Conversation by providing conversation ID and `properties` object. | ||
##### Arguments | ||
- `cid` - Conversation ID | ||
- `properties` - Metadata properties object | ||
- `callback(err, res)` - *Optional* Callback function returns an error and response objects | ||
##### Examples | ||
```javascript | ||
var properties = { | ||
foo: 'bar' | ||
}; | ||
layer.conversations.setMetadataProperties(cid, properties, function(err, res) { | ||
if (err) return console.error(err); | ||
}); | ||
``` | ||
--------------------------------------- | ||
### conversations.deleteMetadataProperties(cid, properties, [callback]) | ||
[Delete metadata]((https://developer.layer.com/docs/platform#set-amp-delete-metadata) on an existing Conversation by providing conversation ID and `properties` object. | ||
##### Arguments | ||
- `cid` - Conversation ID | ||
- `properties` - Properties object | ||
- `callback(err, res)` - *Optional* Callback function returns an error and response objects | ||
--------------------------------------- | ||
### conversations.edit(cid, operations, [callback]) | ||
@@ -171,12 +206,22 @@ | ||
- `cid` - Conversation ID | ||
- `operations` - Conversation operations array | ||
- `operations` - Layer Patch operations array | ||
- `callback(err, res)` - *Optional* Callback function returns an error and response objects | ||
--------------------------------------- | ||
### conversations.addParticipants(cid, participants, [callback]) | ||
[Add participants](https://developer.layer.com/docs/platform#editing-conversations) to an existing Conversation by providing conversation ID and array of `participants`. | ||
##### Arguments | ||
- `cid` - Conversation ID | ||
- `participants` - Array of participants | ||
- `callback(err, res)` - *Optional* Callback function returns an error and response objects | ||
##### Examples | ||
```javascript | ||
var operations = [ | ||
{operation: 'add', property: 'participants', value: 'user1'} | ||
]; | ||
layer.conversations.edit(cid, operations, function(err, res) { | ||
var participants = ['user1']; | ||
layer.conversations.addParticipants(cid, participants, function(err, res) { | ||
if (err) return console.error(err); | ||
@@ -188,2 +233,26 @@ }); | ||
### conversations.removeParticipants(cid, participants, [callback]) | ||
[Remove participants](https://developer.layer.com/docs/platform#editing-conversations) of an existing Conversation by providing conversation ID and array of `participants`. | ||
##### Arguments | ||
- `cid` - Conversation ID | ||
- `participants` - Array of participants | ||
- `callback(err, res)` - *Optional* Callback function returns an error and response objects | ||
--------------------------------------- | ||
### conversations.replaceParticipants(cid, participants, [callback]) | ||
[Replace all participants](https://developer.layer.com/docs/platform#editing-conversations) of an existing Conversation by providing conversation ID and array of `participants`. | ||
##### Arguments | ||
- `cid` - Conversation ID | ||
- `participants` - Array of participants | ||
- `callback(err, res)` - *Optional* Callback function returns an error and response objects | ||
--------------------------------------- | ||
### conversations.delete(cid, [callback]) | ||
@@ -190,0 +259,0 @@ |
@@ -197,2 +197,112 @@ /*globals describe it*/ | ||
describe('Set metadata properties on a conversation', function() { | ||
nock('https://api.layer.com') | ||
.patch('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid) | ||
.reply(204); | ||
var properties = { | ||
foo: 'bar', | ||
number: 123 | ||
}; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.setMetadataProperties(fixtures.conversations.uuid, properties, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(204); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Delete metadata properties on a conversation', function() { | ||
nock('https://api.layer.com') | ||
.patch('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid) | ||
.reply(204); | ||
var properties = { | ||
foo: 'bar', | ||
number: 123 | ||
}; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.deleteMetadataProperties(fixtures.conversations.uuid, properties, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(204); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Add participants on a conversation', function() { | ||
nock('https://api.layer.com') | ||
.patch('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid) | ||
.reply(204); | ||
var participants = [ | ||
'foo', | ||
'bar' | ||
]; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.addParticipants(fixtures.conversations.uuid, participants, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(204); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Remove participants on a conversation', function() { | ||
nock('https://api.layer.com') | ||
.patch('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid) | ||
.reply(204); | ||
var participants = [ | ||
'foo', | ||
'bar' | ||
]; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.removeParticipants(fixtures.conversations.uuid, participants, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(204); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Replace participants on a conversation', function() { | ||
nock('https://api.layer.com') | ||
.patch('/apps/' + fixtures.appId + '/conversations/' + fixtures.conversations.uuid) | ||
.reply(204); | ||
var participants = [ | ||
'foo', | ||
'bar' | ||
]; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.replaceParticipants(fixtures.conversations.uuid, participants, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
res.status.should.be.eql(204); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Delete a conversation by conversation ID', function() { | ||
@@ -199,0 +309,0 @@ nock('https://api.layer.com') |
@@ -89,3 +89,4 @@ /*globals describe it*/ | ||
should.exist(res); | ||
should(res.body).be.eql(null); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
@@ -97,2 +98,89 @@ done(err); | ||
describe('Set metadata properties on a conversation', function() { | ||
var properties = { | ||
foo: 'bar', | ||
number: 123 | ||
}; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.setMetadataProperties(conversationId, properties, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Delete metadata properties on a conversation', function() { | ||
var properties = { | ||
foo: 'bar' | ||
}; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.deleteMetadataProperties(conversationId, properties, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Add participants to a conversation', function() { | ||
var participants = [ | ||
'userFoo', | ||
'userBar' | ||
]; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.addParticipants(conversationId, participants, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Remove participants to a conversation', function() { | ||
var participants = [ | ||
'userFoo' | ||
]; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.removeParticipants(conversationId, participants, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Replace participants to a conversation', function() { | ||
var participants = [ | ||
'userAdmin' | ||
]; | ||
it('should return a 204', function(done) { | ||
layerAPI.conversations.replaceParticipants(conversationId, participants, function(err, res) { | ||
should.not.exist(err); | ||
should.exist(res); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
describe('Delete a conversation by conversation ID', function() { | ||
@@ -103,3 +191,4 @@ it('should return a 204', function(done) { | ||
should.exist(res); | ||
should(res.body).be.eql(null); | ||
should(res.status).be.eql(204); | ||
should(res.body).be.eql(''); | ||
@@ -106,0 +195,0 @@ done(err); |
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
100620
2009
536