Skip to content

Commit

Permalink
Adjustments to the reddit schema to properly support subreddit search…
Browse files Browse the repository at this point in the history
… and replies
  • Loading branch information
MagnificRogue committed Mar 28, 2018
1 parent df92acc commit a85ebff
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 56 deletions.
20 changes: 14 additions & 6 deletions API/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<length; i++){
wait(4000); // synchronized block!
promise_array.push(getCompleteReplies(ids[i]));
// wait(4000); // synchronized block, but why?
promise_array.push(getCompleteReplies(ids[i], r));
}

return Promise.all(promise_array);
});

function getCompleteReplies(id){
function getCompleteReplies(id, r){
//console.log(id);

return new Promise((resolve,reject) =>{
Expand Down
70 changes: 24 additions & 46 deletions API/redditAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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);
Expand All @@ -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);
})
Expand Down Expand Up @@ -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) =>{
Expand Down Expand Up @@ -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]);
}
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions data/schema/reddit-type/redditLinkType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 0 additions & 2 deletions data/schema/redditSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
Expand All @@ -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)
}
},
Expand Down

0 comments on commit a85ebff

Please sign in to comment.