Skip to content

Commit

Permalink
Step 7.13: Create subscriptionLogic
Browse files Browse the repository at this point in the history
  • Loading branch information
srtucker22 authored and Simon Tucker committed Aug 26, 2018
1 parent f212f1e commit 1cbaab9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"faker": "^4.1.0",
"graphql": "^0.13.2",
"graphql-date": "^1.0.3",
"iterall": "^1.2.2",
"jsonwebtoken": "^8.3.0",
"lodash": "^4.17.4",
"sequelize": "^4.4.2",
Expand Down
25 changes: 25 additions & 0 deletions server/data/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,28 @@ export const userLogic = {
});
},
};

export const subscriptionLogic = {
groupAdded(params, args, ctx) {
return getAuthenticatedUser(ctx)
.then((user) => {
if (user.id !== args.userId) {
throw new ForbiddenError('Unauthorized');
}

return Promise.resolve();
});
},
messageAdded(params, args, ctx) {
return getAuthenticatedUser(ctx)
.then(user => user.getGroups({ where: { id: { $in: args.groupIds } }, attributes: ['id'] })
.then((groups) => {
// user attempted to subscribe to some groups without access
if (args.groupIds.length > groups.length) {
throw new ForbiddenError('Unauthorized');
}

return Promise.resolve();
}));
},
};
12 changes: 9 additions & 3 deletions server/data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import jwt from 'jsonwebtoken';
import { Group, Message, User } from './connectors';
import { pubsub } from '../subscriptions';
import { JWT_SECRET } from '../config';
import { groupLogic, messageLogic, userLogic } from './logic';
import { groupLogic, messageLogic, userLogic, subscriptionLogic } from './logic';

const MESSAGE_ADDED_TOPIC = 'messageAdded';
const GROUP_ADDED_TOPIC = 'groupAdded';
Expand Down Expand Up @@ -106,7 +106,10 @@ export const resolvers = {
Subscription: {
messageAdded: {
subscribe: withFilter(
() => pubsub.asyncIterator(MESSAGE_ADDED_TOPIC),
(payload, args, ctx) => pubsub.asyncAuthIterator(
MESSAGE_ADDED_TOPIC,
subscriptionLogic.messageAdded(payload, args, ctx),
),
(payload, args, ctx) => {
return ctx.user.then((user) => {
return Boolean(
Expand All @@ -120,7 +123,10 @@ export const resolvers = {
},
groupAdded: {
subscribe: withFilter(
() => pubsub.asyncIterator(GROUP_ADDED_TOPIC),
(payload, args, ctx) => pubsub.asyncAuthIterator(
GROUP_ADDED_TOPIC,
subscriptionLogic.groupAdded(payload, args, ctx),
),
(payload, args, ctx) => {
return ctx.user.then((user) => {
return Boolean(
Expand Down
19 changes: 19 additions & 0 deletions server/subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { $$asyncIterator } from 'iterall';
import { PubSub } from 'apollo-server';

export const pubsub = new PubSub();

pubsub.asyncAuthIterator = (messages, authPromise) => {
const asyncIterator = pubsub.asyncIterator(messages);
return {
next() {
return authPromise.then(() => asyncIterator.next());
},
return() {
return authPromise.then(() => asyncIterator.return());
},
throw(error) {
return asyncIterator.throw(error);
},
[$$asyncIterator]() {
return asyncIterator;
},
};
};

export default pubsub;

0 comments on commit 1cbaab9

Please sign in to comment.