Skip to content

Commit

Permalink
[Add] feed filter ๊ตฌํ˜„ #140
Browse files Browse the repository at this point in the history
์นœ๊ตฌ๊ฐ€ ๋“ฑ๋กํ•œ ๊ธ€๋งŒ ํ”ผ๋“œ์— ๋œจ๊ฒŒ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์นœ๊ตฌ์— ๋Œ€ํ•ด์„œ๋งŒ ์ƒˆ ํ”ผ๋“œ๊ฐ€ ๋œจ๋„๋ก ์„ค์ •
  • Loading branch information
WooYeonSeo committed Dec 4, 2019
1 parent a4a64fb commit d68b38a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 16 deletions.
9 changes: 5 additions & 4 deletions client/src/composition/Feed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ const GET_FEEDS = gql`
`;

const FEEDS_SUBSCRIPTION = gql`
subscription subscribeFeed {
feeds {
subscription subscribeFeed($userEmail: String!) {
feeds(userEmail: $userEmail) {
cursor
feedItems {
searchUser {
Expand Down Expand Up @@ -117,7 +117,8 @@ const FeedList = () => {
const { data: value } = await fetchMore({
variables: {
first: OFFSET,
currentCursor: data && data.feeds ? data.feeds.cursor : ''
currentCursor:
data && data.feeds ? data.feeds.cursor : '9999-12-31T09:29:26.050Z'
},
updateQuery: (prev, { fetchMoreResult }) => {
if (
Expand Down Expand Up @@ -151,7 +152,7 @@ const FeedList = () => {
const subscribeToNewComments = () => {
return subscribeToMore({
document: FEEDS_SUBSCRIPTION,
variables: {},
variables: { userEmail: '[email protected]' },
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev;
const { data: newFeeds } = subscriptionData;
Expand Down
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express-session": "^1.17.0",
"graphql-tools": "^4.0.6",
"graphql-yoga": "^1.18.3",
"helmet": "^3.21.2",
Expand Down
7 changes: 6 additions & 1 deletion server/src/api/feed/feed.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,10 @@ type Mutation {
}

type Subscription {
feeds: IFeeds
feeds(userEmail: String): ISubsFeeds
}

type ISubsFeeds {
cursor: String
feedItems: [IFeed]
}
35 changes: 29 additions & 6 deletions server/src/api/feed/feed.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
MATCH_FEEDS,
UPDATE_LIKE,
DELETE_LIKE,
GET_NEW_FEED
GET_NEW_FEED,
GET_FRIENDS
} from '../../schema/feed/query';
import { ParseResultRecords } from '../../utils/parseData';
import {
Expand All @@ -17,6 +18,7 @@ import { WRITING_FEED_QUERY, createImageNodeAndRelation } from './feed.query';
import console = require('console');
import { FeedsQueryArgs } from 'src/types/graph';
import { dateToISO, objToDate } from '../../utils/dateutil';
import { withFilter } from 'graphql-subscriptions';

const session = db.session();

Expand Down Expand Up @@ -62,6 +64,18 @@ const createImages = async (feedId, files) => {
}
};

const checkIsFriend = async (friendEmail, myEmail) => {
const result = await session.run(GET_FRIENDS, {
userEmail: myEmail,
friendEmail
});
const [parsedResult] = ParseResultRecords(result.records);
if (parsedResult.isFriend > 0) {
return true;
}
return false;
};

const mutationResolvers: MutationResolvers = {
enrollFeed: async (
_,
Expand Down Expand Up @@ -90,7 +104,8 @@ const mutationResolvers: MutationResolvers = {
feeds: {
cursor: '',
feedItems: parsedRegisterdFeed
}
},
pubUserEmail: email
});
return true;
},
Expand Down Expand Up @@ -148,10 +163,18 @@ export default {
Mutation: mutationResolvers,
Subscription: {
feeds: {
subscribe: (_, __, { pubsub }) => {
// console.log('subscripbed');
return pubsub.asyncIterator(NEW_FEED);
}
subscribe: withFilter(
(_, __, { pubsub }) => {
return pubsub.asyncIterator(NEW_FEED);
},
async (_, variables, context) => {
const myEmail = context.email;
const friendEmail = variables.userEmail;
const isFriend = checkIsFriend(friendEmail, myEmail);

return isFriend;
}
)
}
}
};
3 changes: 2 additions & 1 deletion server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class App {
const pubsub = new PubSub();
this.app = new GraphQLServer({
schema,
context: ({ request, response }) => ({
context: ({ request, response, connection }) => ({
req: request,
res: response,
email: connection ? connection.context.email : undefined,
pubsub
})
});
Expand Down
15 changes: 14 additions & 1 deletion server/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Options } from 'graphql-yoga';
import config from './utils/config';
import app from './app';
import './db';
import { decodeJWT } from './utils/jwt';

const PORT: string | number = config.port;
const ENDPOINT: string = '/graphql';
Expand All @@ -17,7 +18,19 @@ const appOptions: Options = {
port: PORT,
endpoint: ENDPOINT,
playground: PLAYGROUND,
cors: corsOptions
cors: corsOptions,
subscriptions: {
onConnect: (connectionParams, webSocket, context) => {
console.log('connection');
const token = context.request.headers.cookie
.split(';')
.filter(e => e.startsWith('token='))[0]
.split('token=')[1];
const { email } = decodeJWT(token);
// const email = '[email protected]';
return { email };
}
}
};

const handleStart = () => {
Expand Down
7 changes: 6 additions & 1 deletion server/src/schema/feed/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ length(filter(x IN cp WHERE x.email= {useremail} )) AS hasLiked, comments
order by feed.createdAt desc
`;

export { MATCH_FEEDS, UPDATE_LIKE, DELETE_LIKE, GET_NEW_FEED };
const GET_FRIENDS = `MATCH (searchUser:User)-[:FRIEND]->(friend:User)
where searchUser.email = {userEmail} and friend.email = {friendEmail}
with collect(friend) as t
return length(t) as isFriend`;

export { MATCH_FEEDS, UPDATE_LIKE, DELETE_LIKE, GET_NEW_FEED, GET_FRIENDS };
5 changes: 3 additions & 2 deletions server/src/utils/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ function encodeJWT(target: IKey<string | number>): string {
});
}

function decodeJWT(token): object | string {
return jwt.verify(token, SECRET);
function decodeJWT(token): any {
const decodedToken = jwt.verify(token, SECRET);
return decodedToken;
}

export { encodeJWT, decodeJWT };

0 comments on commit d68b38a

Please sign in to comment.