diff --git a/API/loader.js b/API/loader.js index af05e75..4a700cc 100644 --- a/API/loader.js +++ b/API/loader.js @@ -7,22 +7,30 @@ var rootDIR = path.resolve('.'); const replyLoader = new DataLoader(function(ids){ + var context = JSON.parse(ids[0]).context; + const redditTokens = context.find((authorization) => { + return authorization.provider === 'reddit'; + }); + const snoowrap = require('snoowrap'); - r = new snoowrap({ + const r = new snoowrap({ userAgent: 'social monitoring research', - accessToken: JSON.parse(ids[0])['token'] + accessToken: redditTokens.access_token, + refreshToken: redditTokens.refresh_token, + clientId: redditTokens.client.client_id, + clientSecret: redditTokens.client.client_secret }); - + var promise_array = []; for (var i=0, length=ids.length; i{ diff --git a/API/redditAPI.js b/API/redditAPI.js index abb61be..ae68911 100644 --- a/API/redditAPI.js +++ b/API/redditAPI.js @@ -20,16 +20,33 @@ function redditAPI(tokens,resolveName, id, args){ console.log(resolveName) switch(resolveName){ case 'search': - args['limit'] = args['count']; - r.search(args).then((listing) => { - console.log(listing); + args.limit = args.count; + + /* + * For some reason if the args have a subreddit attribute, + * snoowrap isn't limiting the search to that subreddit. + * Below is a workaround, but figuring out why it's happening + * in the first place should happen. + * @REFACTOR @EVENTUALLY @TODO + */ + if(args.subreddit) { + r.getSubreddit(args.subreddit).search(args).then((listing) => { + resolve(listing); + }) + .catch((err) => { + console.log(err) + reject(err) + }) + } else { + r.search(args).then((listing) => { resolve(listing); }) .catch((err) =>{ console.log(err); reject(err) }) - break; + } + break; case 'getCompleteReplies': r.getSubmission(id).expandReplies({options:{limit:Infinity,depth:Infinity}}).then(data => { @@ -42,7 +59,8 @@ function redditAPI(tokens,resolveName, id, args){ break; case 'searchSubreddits': - args['limit'] = 1000; + args.limit = 1000; + r.searchSubreddits(args).then((listing) => { listing.fetchAll().then((data) =>{ resolve(data); @@ -60,7 +78,7 @@ function redditAPI(tokens,resolveName, id, args){ if (args['subredditName'] === 'ALL'){ args['subredditName'] = ''; } - r.getSubreddit(args['subredditName']).getNewComments({limit:1000}).then((listing) => { + r.getSubreddit(args.subredditName).getNewComments({limit:1000}).then((listing) => { listing.fetchMore({amount:args['extra'],skipReplies:false,append:true}).then((data) => { resolve(data); }) @@ -274,7 +292,6 @@ function redditAPI(tokens,resolveName, id, args){ case 'upvote': r.getUser(id).getUpvotedContent().then((listing) => { listing.fetchMore({amount:args['extra']}).then((data) => { - //console.log(data); resolve(data); }) .catch((err) =>{ @@ -303,8 +320,6 @@ function redditAPI(tokens,resolveName, id, args){ case 'expansion': agg_comments = []; r.getSubmission(id).expandReplies({options:{limit:Infinity,depth:Infinity}}).then(data => { - - //console.log(data.comments.length); for (var i = 0, length=data.comments.length; i< length; i++){ commentTreeFlaten(data.comments[i]); } @@ -316,43 +331,6 @@ function redditAPI(tokens,resolveName, id, args){ }); break; - /*case 'getUserFlairTemplates': - r.getSubreddit(id).getUserFlairTemplates().then((data) => { - //console.log(data); - resolve(data); - }); - break; - - case 'subreddit_hot': - r.getSubreddit(id).getHot().then((listing) => { - listing.fetchMore({amount:args['extra']}).then((data) => { - //console.log(data); - resolve(data); - }) - }); - break; - - case 'subreddit_new': - r.getSubreddit(id).getNew().then((listing) => { - listing.fetchMore({amount:args['extra']}).then((data) => { - //console.log(data); - resolve(data); - }) - }); - break; - - case 'comment': - r.getUser(id).getComments().then((listing) => { - args['limit'] = 1; - listing.fetchMore({amount:0}).then((data) => { - resolve(data); - }) - .catch((err) =>{ - reject(err) - }) - }); - break;*/ - default: console.log('sorry we can\'t find matching resolve type:' + resolveName); diff --git a/data/schema/reddit-type/redditLinkType.js b/data/schema/reddit-type/redditLinkType.js index 83eddec..ba7be7c 100644 --- a/data/schema/reddit-type/redditLinkType.js +++ b/data/schema/reddit-type/redditLinkType.js @@ -77,8 +77,12 @@ const redditLinkType = module.exports = new GraphQLObjectType({ user_reports: {type:new GraphQLList(GraphQLString)}, visited: {type:GraphQLBoolean}, /*--------------------------nested------------------------*/ - replies: {type:new GraphQLList(redditCommentType), - resolve: ({id},_,context) => replyLoader.load( JSON.stringify({'id':id,'token':context['redditaccesstoken']}) )} // @ REFACTOR + replies: { + type:new GraphQLList(redditCommentType), + resolve: ({id},_,context) => { + return replyLoader.load( JSON.stringify({'id':id,'context':context}) ) + } + } // @ REFACTOR /*author_trophy: {type:new GraphQLList(redditTrophyType), resolve: ({author}) => redditAPI(resolveName='trophy', id=author.name, args={})}, author_overview: {type:new GraphQLList(redditOverviewType), diff --git a/data/schema/redditSchema.js b/data/schema/redditSchema.js index 27b839b..bb9d382 100644 --- a/data/schema/redditSchema.js +++ b/data/schema/redditSchema.js @@ -20,7 +20,6 @@ const redditQueryType = module.exports = new GraphQLObjectType({ }, description:'Searches subreddits by topic.Returns An Array of subreddit objects corresponding to the search results', resolve: (_,args,context) => { - console.log(context); return redditAPI(context, resolveName='searchSubredditTopics',id='',args=args) } }, @@ -31,7 +30,6 @@ const redditQueryType = module.exports = new GraphQLObjectType({ }, description:'Searches subreddits by title and description.Returns A Listing containing Subreddits', resolve: (_,args,context) => { - console.log(context) return redditAPI(context, resolveName = 'searchSubreddits', id='',args=args) } },