Skip to content

Commit

Permalink
Merge pull request #244 from oddnetworks/viewer-watchlist
Browse files Browse the repository at this point in the history
Viewer Watchlist
  • Loading branch information
blainsmith authored Oct 26, 2016
2 parents 9965483 + e48b9fb commit b7c89fc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
3 changes: 1 addition & 2 deletions lib/middleware/response-json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ module.exports = function (options) {
res.body.links = {
self: `${baseUrl}${req.url}`
};
next();
} else {
let baseSelfLink;
const linksQueries = res.body.linksQueries;
Expand Down Expand Up @@ -248,7 +247,7 @@ module.exports = function (options) {
res.body.meta.channel = req.identity.channel.id;
}
if (req.identity.platform) {
res.body.meta.platform = req.identity.platform.platformType;
res.body.meta.platform = req.identity.platform.id;
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/services/identity/controllers/identity-item-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class IdentityItemController extends Controller {
}

get(req, res, next) {
if (this.type === 'viewer' && !this.isAdminRequest(req) && req.params.id !== req.identity.viewer.id) {
return next(Boom.unauthorized('Viewer specified in JWT does not match requested viewer.'));
}

const type = this.type;
const id = req.params.id;
const args = {type, id};
Expand All @@ -23,6 +27,11 @@ class IdentityItemController extends Controller {
if (type !== 'channel') {
args.channel = channel.id;
}

if (req.query.include) {
args.include = req.query.include.split(',');
}

return this.bus.query({role: 'store', cmd: 'get', type}, args);
})
.then(resource => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ const objectSort = require('object-property-natural-sort');
const utils = require('../../../utils');
const Controller = require('../../../controllers/controller');

class ViewerRelationshipController {
class ViewerRelationshipController extends Controller {
constructor(options) {
super();
this.bus = options.bus;
this.type = 'viewer';
this.relationship = options.relationship;
}

get(req, res, next) {
if (!this.isAdminRequest(req) && req.params.id !== req.identity.viewer.id) {
return next(Boom.unauthorized('Viewer specified in JWT does not match requested viewer.'));
}

const relationshipKey = this.relationship;

const args = {
Expand Down Expand Up @@ -100,12 +105,13 @@ class ViewerRelationshipController {
post(req, res, next) {
const args = {
id: req.params.id,
type: this.type
type: this.type,
channel: req.identity.channel.id
};

req.body = utils.arrayify(req.body);
req.body.forEach((resource, index) => {
if (resource.type !== 'video' || resource.type !== 'collection') {
if (resource.type !== 'video' && resource.type !== 'collection') {
return next(Boom.forbidden(`Resource number ${index}, id ${resource.id}, type ${resource.type} is not of type video or collection.`));
}
});
Expand All @@ -116,18 +122,18 @@ class ViewerRelationshipController {
viewer.relationships[this.relationship].data = viewer.relationships[this.relationship].data || [];
viewer.relationships[this.relationship].data = utils.arrayify(viewer.relationships[this.relationship].data);

// Union the current with the new to get unique entries
viewer.relationships[this.relationship].data = _.union(viewer.relationships[this.relationship].data, req.body);
viewer.relationships[this.relationship].data.concat(req.body);
viewer.relationships[this.relationship].data = _.uniqWith(viewer.relationships[this.relationship].data, _.isEqual);

if (viewer.relationships[this.relationship].data.length === 1) {
viewer.relationships[this.relationship].data = viewer.relationships[this.relationship].data[0];
}

return this.bus.query({role: 'store', cmd: 'set', type: this.type}, viewer);
return this.bus.sendCommand({role: 'store', cmd: 'set', type: this.type}, viewer);
})
.then(viewer => {
res.body = viewer.relationships[this.relationship];
res.status(200);
.then(() => {
res.sendStatus(202);
return null;
})
.catch(next);
}
Expand Down Expand Up @@ -165,6 +171,8 @@ class ViewerRelationshipController {
.then(viewer => {
res.body = viewer.relationships[this.relationship];
res.status(200);
next();
return null;
})
.catch(next);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/services/identity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ module.exports = function (bus, options) {
router.all(
`/${type}s/:id`,
middleware['request-authorize']({bus, audience: {
get: ['admin'],
get: ['admin', 'platform'],
patch: ['admin'],
delete: ['admin']
}}),
Expand Down
4 changes: 2 additions & 2 deletions spec/middleware/response-json-api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('Middleware Response JSON API', function () {
});

it('adds a meta block', function () {
expect(res.body.meta).toEqual({channel: 'channel-id', platform: 'APPLE_TV'});
expect(res.body.meta).toEqual({channel: 'channel-id', platform: 'platform-id'});
});
});

Expand Down Expand Up @@ -275,7 +275,7 @@ describe('Middleware Response JSON API', function () {
});

it('adds a meta block', function () {
expect(res.body.meta).toEqual({channel: 'channel-id', platform: 'APPLE_TV'});
expect(res.body.meta).toEqual({channel: 'channel-id', platform: 'platform-id'});
});
});

Expand Down

0 comments on commit b7c89fc

Please sign in to comment.