diff --git a/client/src/composition/Feed/index.tsx b/client/src/composition/Feed/index.tsx index 5d9dd312..495a6f06 100644 --- a/client/src/composition/Feed/index.tsx +++ b/client/src/composition/Feed/index.tsx @@ -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 { @@ -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 ( @@ -151,7 +152,7 @@ const FeedList = () => { const subscribeToNewComments = () => { return subscribeToMore({ document: FEEDS_SUBSCRIPTION, - variables: {}, + variables: { userEmail: 'yeonseo007d@gmail.com' }, updateQuery: (prev, { subscriptionData }) => { if (!subscriptionData.data) return prev; const { data: newFeeds } = subscriptionData; diff --git a/server/package.json b/server/package.json index be08cf7c..ca84998c 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/src/api/feed/feed.graphql b/server/src/api/feed/feed.graphql index 277c51b0..ac87cbbb 100644 --- a/server/src/api/feed/feed.graphql +++ b/server/src/api/feed/feed.graphql @@ -77,5 +77,10 @@ type Mutation { } type Subscription { - feeds: IFeeds + feeds(userEmail: String): ISubsFeeds +} + +type ISubsFeeds { + cursor: String + feedItems: [IFeed] } diff --git a/server/src/api/feed/feed.resolvers.ts b/server/src/api/feed/feed.resolvers.ts index cb9b27bf..49adba9d 100644 --- a/server/src/api/feed/feed.resolvers.ts +++ b/server/src/api/feed/feed.resolvers.ts @@ -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 { @@ -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(); @@ -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 ( _, @@ -90,7 +104,8 @@ const mutationResolvers: MutationResolvers = { feeds: { cursor: '', feedItems: parsedRegisterdFeed - } + }, + pubUserEmail: email }); return true; }, @@ -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; + } + ) } } }; diff --git a/server/src/app.ts b/server/src/app.ts index 200101cb..14acc7de 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -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 }) }); diff --git a/server/src/init.ts b/server/src/init.ts index 1900655a..3f1a2fa8 100644 --- a/server/src/init.ts +++ b/server/src/init.ts @@ -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'; @@ -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 = 'vantovan7414@gmail.com'; + return { email }; + } + } }; const handleStart = () => { diff --git a/server/src/schema/feed/query.ts b/server/src/schema/feed/query.ts index dbc7c589..192abed3 100644 --- a/server/src/schema/feed/query.ts +++ b/server/src/schema/feed/query.ts @@ -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 }; diff --git a/server/src/utils/jwt.ts b/server/src/utils/jwt.ts index 0cedc404..8873100f 100644 --- a/server/src/utils/jwt.ts +++ b/server/src/utils/jwt.ts @@ -11,8 +11,9 @@ function encodeJWT(target: IKey): 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 };