Skip to content

Commit

Permalink
addToMany and followMany API operations added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthisk authored and Matthisk committed Oct 23, 2015
1 parent 8d7a594 commit ca61067
Show file tree
Hide file tree
Showing 10 changed files with 12,212 additions and 7,729 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -106,6 +106,26 @@ to = ['user:2', 'user:3'];
activity = {'to': to, 'actor': 1, 'verb': 'tweet', 'object': 1, 'foreign_id': 'tweet:1'};
user1.addActivity(activity, function(error, response, body) { /* callback */ });

// adding one activity to multiple feeds
var feeds = ['flat:1', 'flat:2', 'flat:3', 'flat:4'];
activity = {
'actor': 'User:2',
'verb': 'pin',
'object': 'Place:42',
'target': 'Board:1'
};

client.addToMany(activity, feeds, function(error, response, body) { /* callback */ });

// Batch create follow relations
var follows = [
{'source': 'flat:1', 'target': 'user:1'},
{'source': 'flat:1', 'target': 'user:2'},
{'source': 'flat:1', 'target': 'user:3'}
];

client.followMany(follows, function(error, response, body) { /* callback */ });

// creating a feed token server side
token = user1.token;
// passed to client via template or api and initialized as such
Expand Down
16 changes: 14 additions & 2 deletions dist/js/getstream.js
Expand Up @@ -206,9 +206,11 @@ return /******/ (function(modules) { // webpackBootstrap
var request = __webpack_require__(3);
var StreamFeed = __webpack_require__(4);
var signing = __webpack_require__(7);
var httpSignature = __webpack_require__(9);
var errors = __webpack_require__(5);
var crypto = __webpack_require__(8);
var utils = __webpack_require__(6);
var BatchOperations = __webpack_require__(9);

var StreamClient = function StreamClient() {
this.initialize.apply(this, arguments);
Expand Down Expand Up @@ -247,7 +249,7 @@ return /******/ (function(modules) { // webpackBootstrap
this.fayeUrl = 'http://localhost:9999/faye/';
}
this.handlers = {};
this.browser = typeof window != 'undefined';
this.browser = typeof window !== 'undefined';
this.node = !this.browser;

if (this.browser && this.apiSecret) {
Expand Down Expand Up @@ -492,8 +494,18 @@ return /******/ (function(modules) { // webpackBootstrap
var callback = this.wrapCallback(cb);
return request(kwargs, callback);
}

};

// If we are in a node environment and batchOperations is available add the methods to the prototype of StreamClient
if (BatchOperations) {
for (var key in BatchOperations) {
if (BatchOperations.hasOwnProperty(key)) {
StreamClient.prototype[key] = BatchOperations[key];
}
}
}

module.exports = StreamClient;
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(1)))

Expand Down Expand Up @@ -1409,7 +1421,7 @@ return /******/ (function(modules) { // webpackBootstrap
/* 8 */
/***/ function(module, exports) {


/* (ignored) */

/***/ },
/* 9 */
Expand Down
4 changes: 2 additions & 2 deletions dist/js_min/getstream.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -10,9 +10,9 @@
"version": "2.2.2",
"browser": {
"request": "browser-request",
"faye": "./node_modules/faye/faye-browser.js",
"crypto": false,
"jsonwebtoken": false
"jsonwebtoken": false,
"./batch_operations": false
},
"config": {
"blanket": {
Expand All @@ -25,7 +25,6 @@
"babel-loader": "^5.3.2",
"blanket": "~1.1.6",
"bluebird": "^2.1.3",
"browser-request": "git://github.com/iriscouch/browser-request",
"connect": "^3.0.1",
"coveralls": "~2.10.1",
"expect.js": "~0.3.1",
Expand All @@ -51,6 +50,7 @@
"Base64": "^0.3.0",
"browser-request": "^0.3.3",
"faye": "^1.0.1",
"http-signature": "^1.0.2",
"jsonwebtoken": "^5.0.1",
"request": "2.63.0"
},
Expand Down
49 changes: 49 additions & 0 deletions src/lib/batch_operations.js
@@ -0,0 +1,49 @@
var httpSignature = require('http-signature');
var request = require('request');

module.exports = {
addToMany: function(activity, feeds, callback) {
var req = this.makeSignedRequest({
url: 'feed/add_to_many/',
body: {
'activity': activity,
'feeds': feeds
}
}, callback);

return req;
},

followMany: function(follows, callback) {
var req = this.makeSignedRequest({
url: 'follow_many/',
body: follows
}, callback);

return req;
},

makeSignedRequest: function(kwargs, cb) {
if(!this.apiSecret) {
throw new errors.SiteError('Missing secret, which is needed to perform signed requests, use var client = stream.connect(key, secret);');
}

this.send('request', 'post', kwargs, cb);

kwargs.url = this.enrichUrl(kwargs.url);
kwargs.json = true;
kwargs.method = 'POST';
kwargs.headers = { 'X-Api-Key' : this.apiKey };

var callback = this.wrapCallback(cb);
var req = request(kwargs, callback);

httpSignature.sign(req, {
algorithm: 'hmac-sha256',
key: this.apiSecret,
keyId: this.apiKey
});

return request;
}
};
19 changes: 16 additions & 3 deletions src/lib/client.js
@@ -1,9 +1,11 @@
var request = require('request');
var StreamFeed = require('./feed');
var signing = require('./signing');
var httpSignature = require('http-signature');
var errors = require('./errors');
var crypto = require('crypto');
var utils = require('./utils');
var BatchOperations = require('./batch_operations');

var StreamClient = function() {
this.initialize.apply(this, arguments);
Expand Down Expand Up @@ -42,7 +44,7 @@ StreamClient.prototype = {
this.fayeUrl = 'http://localhost:9999/faye/';
}
this.handlers = {};
this.browser = typeof(window) != 'undefined';
this.browser = typeof(window) !== 'undefined';
this.node = !this.browser;

if (this.browser && this.apiSecret) {
Expand Down Expand Up @@ -86,6 +88,7 @@ StreamClient.prototype = {
}
},


wrapCallback: function(cb) {
var client = this;

Expand Down Expand Up @@ -147,7 +150,7 @@ StreamClient.prototype = {
}

utils.validateFeedSlug(feedSlug);
utils.validateUserId(userId);
utils.validateUserId(userId);

// raise an error if there is no token
if (!this.apiSecret && !token) {
Expand Down Expand Up @@ -286,7 +289,17 @@ StreamClient.prototype = {
kwargs.method = 'DELETE';
var callback = this.wrapCallback(cb);
return request(kwargs, callback);
}
},

};

// If we are in a node environment and batchOperations is available add the methods to the prototype of StreamClient
if(BatchOperations) {
for(var key in BatchOperations) {
if(BatchOperations.hasOwnProperty(key)) {
StreamClient.prototype[key] = BatchOperations[key];
}
}
}

module.exports = StreamClient;

0 comments on commit ca61067

Please sign in to comment.