Skip to content

Commit

Permalink
add targetFeedsExtraData param for reactions with feed targets
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarbugli committed Aug 21, 2019
1 parent 322fbce commit 140cbb8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 105 deletions.
24 changes: 19 additions & 5 deletions src/lib/reaction.js
Expand Up @@ -48,16 +48,16 @@ StreamReaction.prototype = {
},

_convertTargetFeeds: function(targetFeeds = []) {
return targetFeeds.map(
(elem) => (typeof elem === 'string' ? elem : elem.id),
return targetFeeds.map((elem) =>
typeof elem === 'string' ? elem : elem.id,
);
},

add: function(
kind,
activity,
data = {},
{ targetFeeds = [], userId } = {},
{ targetFeeds = [], userId, targetFeedsExtraData } = {},
callback,
) {
/**
Expand All @@ -84,6 +84,9 @@ StreamReaction.prototype = {
target_feeds: targetFeeds,
user_id: userId,
};
if (targetFeedsExtraData != null) {
body.target_feeds_extra_data = targetFeedsExtraData;
}
return this.client.post(
{
url: this.buildURL(),
Expand All @@ -98,7 +101,7 @@ StreamReaction.prototype = {
kind,
reaction,
data = {},
{ targetFeeds = [], userId } = {},
{ targetFeeds = [], userId, targetFeedsExtraData } = {},
callback,
) {
/**
Expand All @@ -125,6 +128,9 @@ StreamReaction.prototype = {
target_feeds: targetFeeds,
user_id: userId,
};
if (targetFeedsExtraData != null) {
body.target_feeds_extra_data = targetFeedsExtraData;
}
return this.client.post(
{
url: this.buildURL(),
Expand Down Expand Up @@ -204,7 +210,12 @@ StreamReaction.prototype = {
);
},

update: function(id, data, { targetFeeds = [] } = {}, callback) {
update: function(
id,
data,
{ targetFeeds = [], targetFeedsExtraData } = {},
callback,
) {
/**
* update reaction
* @method add
Expand All @@ -222,6 +233,9 @@ StreamReaction.prototype = {
data: data,
target_feeds: targetFeeds,
};
if (targetFeedsExtraData != null) {
body.target_feeds_extra_data = targetFeedsExtraData;
}
return this.client.put(
{
url: this.buildURL(id),
Expand Down
22 changes: 15 additions & 7 deletions test/integration/cloud/reaction.js
Expand Up @@ -684,8 +684,12 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
ctx.bob.feed('notification', ctx.alice.userId).id,
ctx.bob.feed('notification', ctx.carl.userId).id,
],
targetFeedsExtraData: {
custom: 'yay',
},
},
);
delete ctx.response.target_feeds_extra_data;
comment = ctx.response;
});

Expand Down Expand Up @@ -725,16 +729,16 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
ctx.requestShouldNotError(async () => {
ctx.response = await ctx.alice.feed('user', ctx.bob.userId).get();
});
ctx.responseShouldHaveActivityWithFields('reaction');
ctx.responseShouldHaveActivityWithFields('reaction', 'custom');
ctx.activityShould('contain the expected data', () => {
expectedCommentData = {
verb: 'comment',
foreign_id: `reaction:${comment.id}`,
time: comment.created_at.slice(0, -1), // chop off the Z suffix
target: '',
origin: null,
custom: 'yay',
};

ctx.activity.should.include(expectedCommentData);
ctx.activity.actor.should.eql(ctx.bob.currentUser.full);
ctx.shouldEqualBesideDuration(ctx.activity.object, eatActivity);
Expand All @@ -749,7 +753,7 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
.feed('notification', ctx.alice.userId)
.get();
});
ctx.responseShouldHaveActivityInGroupWithFields('reaction');
ctx.responseShouldHaveActivityInGroupWithFields('reaction', 'custom');
ctx.activityShould('be the same as on bob his feed', () => {
ctx.activity.should.eql(commentActivity);
});
Expand All @@ -761,7 +765,7 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
.feed('notification', ctx.carl.userId)
.get();
});
ctx.responseShouldHaveActivityInGroupWithFields('reaction');
ctx.responseShouldHaveActivityInGroupWithFields('reaction', 'custom');
ctx.activityShould('be the same as on bob his feed', () => {
ctx.activity.should.eql(commentActivity);
});
Expand Down Expand Up @@ -799,7 +803,11 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
ctx.bob.feed('notification', ctx.alice.userId).id,
ctx.bob.feed('notification', ctx.dave.userId).id,
],
targetFeedsExtraData: {
custom: 'yay',
},
});
delete ctx.response.target_feeds_extra_data;
});

ctx.responseShouldHaveFields(...ctx.fields.reactionResponse);
Expand All @@ -820,7 +828,7 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
ctx.requestShouldNotError(async () => {
ctx.response = await ctx.alice.feed('user', ctx.bob.userId).get();
});
ctx.responseShouldHaveActivityWithFields('reaction');
ctx.responseShouldHaveActivityWithFields('reaction', 'custom');
ctx.activityShould('contain the expected data', () => {
ctx.activity.should.include(expectedCommentData);
commentActivity = ctx.activity;
Expand All @@ -833,7 +841,7 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
.feed('notification', ctx.alice.userId)
.get();
});
ctx.responseShouldHaveActivityInGroupWithFields('reaction');
ctx.responseShouldHaveActivityInGroupWithFields('reaction', 'custom');
ctx.activityShould('be the same as on bob his feed', () => {
ctx.activity.should.eql(commentActivity);
});
Expand All @@ -854,7 +862,7 @@ describe('Reaction CRUD and posting reactions to feeds', () => {
.feed('notification', ctx.dave.userId)
.get();
});
ctx.responseShouldHaveActivityInGroupWithFields('reaction');
ctx.responseShouldHaveActivityInGroupWithFields('reaction', 'custom');
ctx.activityShould('be the same as on bob his feed', () => {
ctx.activity.should.eql(commentActivity);
});
Expand Down
20 changes: 6 additions & 14 deletions test/integration/utils/hooks.js
Expand Up @@ -54,10 +54,7 @@ function initBrowser() {
}

function beforeEachNode() {
this.client = stream.connect(
config.API_KEY,
config.API_SECRET,
);
this.client = stream.connect(config.API_KEY, config.API_SECRET);
this.client = stream.connect(
config.API_KEY,
config.API_SECRET,
Expand Down Expand Up @@ -87,16 +84,11 @@ function beforeEachNode() {

function beforeEachBrowser() {
this.client = stream.connect(config.API_KEY);
this.client = stream.connect(
config.API_KEY,
null,
config.APP_ID,
{
group: 'browserTestCycle',
location: 'qa',
protocol: 'https',
},
);
this.client = stream.connect(config.API_KEY, null, config.APP_ID, {
group: 'browserTestCycle',
location: 'qa',
protocol: 'https',
});

if (this.localRun) {
this.client.baseUrl = 'http://localhost:8000/api/';
Expand Down
29 changes: 8 additions & 21 deletions test/unit/browser/client_test.js
Expand Up @@ -12,10 +12,7 @@ describe('[UNIT] Stream Client (browser)', function() {
it("shouldn't allow secret keys", function() {
// apiKey, apiSecret, appId, options
function createFn() {
stream.connect(
'abcdefgh',
'123456789',
);
stream.connect('abcdefgh', '123456789');
}

expect(createFn).to.throwException(function(e) {
Expand All @@ -24,29 +21,19 @@ describe('[UNIT] Stream Client (browser)', function() {
});

it('should store config on the client', function() {
var client = stream.connect(
'abcdefgh',
null,
1000,
{ option: true },
);
var client = stream.connect('abcdefgh', null, 1000, { option: true });

expect(client.apiSecret).to.be(null);
expect(client.browser).to.be(true);
});

it('should store config on the client', function() {
var client = stream.connect(
'abcdefgh',
null,
1000,
{
version: 'v2.0',
fayeUrl: 'https://hello.world',
expireTokens: true,
location: 'nederland',
},
);
var client = stream.connect('abcdefgh', null, 1000, {
version: 'v2.0',
fayeUrl: 'https://hello.world',
expireTokens: true,
location: 'nederland',
});

expect(client.version).to.be('v2.0');
expect(client.expireTokens).to.be(true);
Expand Down
50 changes: 12 additions & 38 deletions test/unit/node/client_test.js
Expand Up @@ -629,10 +629,7 @@ describe('[UNIT] Stream Client (Node)', function() {

describe('getAnalyticsToken', function() {
it('generate correct token', function() {
var client = stream.connect(
'12345',
'abcdefghijklmnop',
);
var client = stream.connect('12345', 'abcdefghijklmnop');
var token = client.getAnalyticsToken();
expect(token).to.be(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXNvdXJjZSI6ImFuYWx5dGljcyIsImFjdGlvbiI6IioiLCJ1c2VyX2lkIjoiKiJ9.f7KFu7U2Uw_yq__9hV4-wr9S0KXo7w3wxTELOAY4qdc',
Expand All @@ -642,21 +639,15 @@ describe('[UNIT] Stream Client (Node)', function() {

describe('createUserSessionToken', function() {
it('with userId only', function() {
var client = stream.connect(
'12345',
'abcdefghijklmnop',
);
var client = stream.connect('12345', 'abcdefghijklmnop');
var token = client.createUserSessionToken('42');
expect(token).to.be(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiNDIifQ.fJP44ZlP7bly-2HvbPxBO7WUGJhc1i2hpj4TnXmtYLE',
);
});

it('with extra data', function() {
var client = stream.connect(
'12345',
'abcdefghijklmnop',
);
var client = stream.connect('12345', 'abcdefghijklmnop');
var token = client.createUserSessionToken('42', { a: 'b' });
const jwtBody = jwtDecode(token);
expect(jwtBody).to.eql({
Expand All @@ -668,12 +659,9 @@ describe('[UNIT] Stream Client (Node)', function() {
);
});
it('with expireTokens', function() {
const client = stream.connect(
'12345',
'abcdefghijklmnop',
1234,
{ expireTokens: true },
);
const client = stream.connect('12345', 'abcdefghijklmnop', 1234, {
expireTokens: true,
});
const token = client.createUserSessionToken('42');
const timestamp = Date.now() / 1000;
const jwtBody = jwtDecode(token);
Expand All @@ -686,34 +674,23 @@ describe('[UNIT] Stream Client (Node)', function() {
it('#LOCAL', function() {
process.env['LOCAL'] = 1;

var client = stream.connect(
'12345',
'abcdefghijklmnop',
);
var client = stream.connect('12345', 'abcdefghijklmnop');
expect(client.baseUrl).to.be('http://localhost:8000/api/');

delete process.env['LOCAL'];
});

it('#LOCAL', function() {
var client = stream.connect(
'12345',
'abcdefghijklmnop',
null,
{
location: 'nl-NL',
},
);
var client = stream.connect('12345', 'abcdefghijklmnop', null, {
location: 'nl-NL',
});
expect(client.baseUrl).to.be('https://nl-NL-api.stream-io-api.com/api/');
});

it('#LOCAL_FAYE', function() {
process.env['LOCAL_FAYE'] = 1;

var client = stream.connect(
'12345',
'abcdefghijklmnop',
);
var client = stream.connect('12345', 'abcdefghijklmnop');
expect(client.fayeUrl).to.be('http://localhost:9999/faye/');

delete process.env['LOCAL_FAYE'];
Expand All @@ -722,10 +699,7 @@ describe('[UNIT] Stream Client (Node)', function() {
it('#STREAM_BASE_URL', function() {
process.env['STREAM_BASE_URL'] = 'https://local.stream-io-api.com/api/';

var client = stream.connect(
'12345',
'abcdefghijklmnop',
);
var client = stream.connect('12345', 'abcdefghijklmnop');
expect(client.baseUrl).to.be('https://local.stream-io-api.com/api/');

delete process.env['STREAM_BASE_URL'];
Expand Down
12 changes: 2 additions & 10 deletions test/unit/node/heroku_test.js
Expand Up @@ -105,11 +105,7 @@ describe('[UNIT] Stream client (Heroku)', function() {
it('heroku_overwrite (stream-io-api.com)', function(done) {
var url = 'https://thierry:pass@stream-io-api.com/?app_id=1';
process.env.STREAM_URL = url;
this.client = stream.connect(
'a',
'b',
'c',
);
this.client = stream.connect('a', 'b', 'c');
expect(this.client.apiKey).to.eql('a');
expect(this.client.apiSecret).to.eql('b');
expect(this.client.appId).to.eql('c');
Expand All @@ -120,11 +116,7 @@ describe('[UNIT] Stream client (Heroku)', function() {
it('heroku_overwrite (getstream.io)', function(done) {
var url = 'https://thierry:pass@getstream.io/?app_id=1';
process.env.STREAM_URL = url;
this.client = stream.connect(
'a',
'b',
'c',
);
this.client = stream.connect('a', 'b', 'c');
expect(this.client.apiKey).to.eql('a');
expect(this.client.apiSecret).to.eql('b');
expect(this.client.appId).to.eql('c');
Expand Down
12 changes: 2 additions & 10 deletions types/getstream/tests-getstream.ts
@@ -1,16 +1,8 @@
new stream.Client('key', undefined, 'apiSecret');

stream.connect(
'abc',
'def',
'ghi',
); // $ExpectType StreamClient
stream.connect('abc', 'def', 'ghi'); // $ExpectType StreamClient

const client = stream.connect(
'abc',
'def',
'ghi',
);
const client = stream.connect('abc', 'def', 'ghi');
client.feed('feedSlug', 'user'); // $ExpectType Feed

let feed = client.feed('feedSlug', 'user');
Expand Down

0 comments on commit 140cbb8

Please sign in to comment.