Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Sep 26, 2019
2 parents 15956b5 + aa95b64 commit 1688b1e
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"bundlesize": [
{
"path": "./dist/algoliasearch.?(jquery|angular).min.js",
"maxSize": "19 kB"
"maxSize": "20 kB"
},
{
"path": "./dist/algoliasearchLite.min.js",
Expand Down
26 changes: 26 additions & 0 deletions src/AlgoliaSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,31 @@ AlgoliaSearch.prototype.assignUserID = function(data, callback) {
});
};

/**
* Assign a array of userIDs to a cluster.
*
* @param {Array} data.userIDs The array of userIDs to assign to a new cluster
* @param {string} data.cluster The cluster to assign the user to
* @return {Promise|undefined} Returns a promise if no callback given
* @example
* client.assignUserIDs({ cluster: 'c1-test', userIDs: ['some-user-1', 'some-user-2'] });
*/
AlgoliaSearch.prototype.assignUserIDs = function(data, callback) {
if (!data.userIDs || !data.cluster) {
throw new errors.AlgoliaSearchError('You have to provide both an array of userIDs and cluster', data);
}
return this._jsonRequest({
method: 'POST',
url: '/1/clusters/mapping/batch',
hostType: 'write',
body: {
cluster: data.cluster,
users: data.userIDs
},
callback: callback
});
};

/**
* Get the top userIDs
*
Expand Down Expand Up @@ -732,6 +757,7 @@ AlgoliaSearch.prototype.disableRateLimitForward = notImplemented;
AlgoliaSearch.prototype.useSecuredAPIKey = notImplemented;
AlgoliaSearch.prototype.disableSecuredAPIKey = notImplemented;
AlgoliaSearch.prototype.generateSecuredApiKey = notImplemented;
AlgoliaSearch.prototype.getSecuredApiKeyRemainingValidity = notImplemented;

function notImplemented() {
var message = 'Not implemented in this environment.\n' +
Expand Down
63 changes: 63 additions & 0 deletions src/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,69 @@ Index.prototype.exists = function(callback) {
});
};

Index.prototype.findObject = function(findCallback, requestOptions, callback) {
requestOptions = requestOptions === undefined ? {} : requestOptions;
var paginate = requestOptions.paginate !== undefined ? requestOptions.paginate : true;
var query = requestOptions.query !== undefined ? requestOptions.query : '';

var that = this;
var page = 0;

var paginateLoop = function() {
requestOptions.page = page;

return that.search(query, requestOptions).then(function(result) {
var hits = result.hits;

for (var position = 0; position < hits.length; position++) {
var hit = hits[position];
if (findCallback(hit)) {
return {
object: hit,
position: position,
page: page
};
}
}

page += 1;

// paginate if option was set and has next page
if (!paginate || page >= result.nbPages) {
throw new errors.ObjectNotFound('Object not found');
}

return paginateLoop();
});
};

var promise = paginateLoop(page);

if (callback === undefined) {
return promise;
}

promise
.then(function(res) {
callback(null, res);
})
.catch(function(err) {
callback(err);
});
};

Index.prototype.getObjectPosition = function(result, objectID) {
var hits = result.hits;

for (var position = 0; position < hits.length; position++) {
if (hits[position].objectID === objectID) {
return position;
}
}

return -1;
};

/*
* Set settings for this index
*
Expand Down
8 changes: 8 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,18 @@ module.exports = {
'JSONPScriptFail',
'<script> was loaded but did not call our provided callback'
),
ValidUntilNotFound: createCustomError(
'ValidUntilNotFound',
'The SecuredAPIKey does not have a validUntil parameter.'
),
JSONPScriptError: createCustomError(
'JSONPScriptError',
'<script> unable to load due to an `error` event on it'
),
ObjectNotFound: createCustomError(
'ObjectNotFound',
'Object not found'
),
Unknown: createCustomError(
'Unknown',
'Unknown error occured'
Expand Down
18 changes: 18 additions & 0 deletions src/server/builds/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,21 @@ AlgoliaSearchNodeJS.prototype.generateSecuredApiKey = function generateSecuredAp

return new Buffer(securedKey + searchParams).toString('base64');
};

AlgoliaSearchNodeJS.prototype.getSecuredApiKeyRemainingValidity = function getSecuredApiKeyRemainingValidity(
securedAPIKey,
) {
var decodedString = new Buffer(securedAPIKey, 'base64').toString('ascii');

var regex = /validUntil=(\d+)/;

var match = decodedString.match(regex);

if (match === null) {
throw new errors.ValidUntilNotFound('ValidUntil not found in api key.');
}

var validUntilMatch = decodedString.match(regex)[1];

return validUntilMatch - Math.round(new Date().getTime() / 1000);
};
4 changes: 3 additions & 1 deletion test/spec/common/client/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test('AlgoliaSearch client API spec', function(t) {
'addUserKey',
'addUserKeyWithValidity',
'assignUserID',
'assignUserIDs',
'batch',
'clearCache',
'copyIndex',
Expand Down Expand Up @@ -74,7 +75,8 @@ test('AlgoliaSearch client API spec', function(t) {
'disableSecuredAPIKey',
'enableRateLimitForward',
'useSecuredAPIKey',
'generateSecuredApiKey'
'generateSecuredApiKey',
'getSecuredApiKeyRemainingValidity'
]);

expectedProperties = expectedProperties.sort();
Expand Down
35 changes: 35 additions & 0 deletions test/spec/common/client/test-cases/assignUserIDs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var testCases = [];

if (process.browser) {
if (require('faux-jax').support.xhr.cors) {
testCases.push({
testName: 'client.assignUserIDs({userID, cluster}, cb)',
object: 'client',
methodName: 'assignUserIDs',
callArguments: [{userIDs: ['one', 'two'], cluster: 'the big one'}],
action: 'write',
expectedRequest: {
method: 'POST',
body: {cluster: 'the big one', users: ['one', 'two']},
URL: {pathname: '/1/clusters/mapping/batch'}
}
});
}
} else {
testCases.push({
testName: 'client.assignUserIDs({userID, cluster}, cb)',
object: 'client',
methodName: 'assignUserIDs',
callArguments: [{userIDs: ['one', 'two'], cluster: 'the big one'}],
action: 'write',
expectedRequest: {
method: 'POST',
body: {cluster: 'the big one', users: ['one', 'two']},
URL: {pathname: '/1/clusters/mapping/batch'}
}
});
}

module.exports = testCases;
Loading

0 comments on commit 1688b1e

Please sign in to comment.