diff --git a/production/letsencrypt.conf b/hosting/letsencrypt.conf similarity index 100% rename from production/letsencrypt.conf rename to hosting/letsencrypt.conf diff --git a/production/nginx-basic b/hosting/nginx-basic similarity index 100% rename from production/nginx-basic rename to hosting/nginx-basic diff --git a/production/nginx-ssl b/hosting/nginx-ssl similarity index 100% rename from production/nginx-ssl rename to hosting/nginx-ssl diff --git a/src/api/pushshift/index.js b/src/api/pushshift/index.js index 38a6461..67bc1f8 100644 --- a/src/api/pushshift/index.js +++ b/src/api/pushshift/index.js @@ -1,4 +1,4 @@ -import { json, toBase10, toBase36 } from 'utils' +import { toBase10, toBase36 } from 'utils' const baseURL = 'https://elastic.pushshift.io' const postURL = `${baseURL}/rs/submissions/_search?source=` @@ -15,9 +15,9 @@ export const getPost = threadID => { return ( window.fetch(postURL + JSON.stringify(elasticQuery)) - .then(json) - .then(jsonData => jsonData.hits.hits[0]._source) - .then(post => { + .then(response => response.json()) + .then(response => { + const post = response.hits.hits[0]._source post.id = toBase36(post.id) return post }) @@ -52,20 +52,22 @@ export const getComments = threadID => { return ( window.fetch(commentURL + JSON.stringify(elasticQuery)) - .then(json) - .then(jsonData => jsonData.hits.hits) - .then(comments => comments.map(comment => { - comment._source.id = toBase36(comment._id) - comment._source.link_id = toBase36(comment._source.link_id) + .then(response => response.json()) + .then(response => { + const comments = response.hits.hits + return comments.map(comment => { + comment._source.id = toBase36(comment._id) + comment._source.link_id = toBase36(comment._source.link_id) - // Missing parent id === direct reply to thread - if (!comment._source.parent_id) { - comment._source.parent_id = threadID - } else { - comment._source.parent_id = toBase36(comment._source.parent_id) - } + // Missing parent id === direct reply to thread + if (!comment._source.parent_id) { + comment._source.parent_id = threadID + } else { + comment._source.parent_id = toBase36(comment._source.parent_id) + } - return comment._source - })) + return comment._source + }) + }) ) } diff --git a/src/api/reddit/auth.js b/src/api/reddit/auth.js index 3feedb4..18f7fd3 100644 --- a/src/api/reddit/auth.js +++ b/src/api/reddit/auth.js @@ -1,11 +1,42 @@ -import { getToken } from './token' +// Change this to your own client ID: https://www.reddit.com/prefs/apps +// The app NEEDS TO BE an installed app and NOT a web apps -// Header for general api calls -export const getAuth = () => ( - getToken() +// Current using dummy ID from throwaway +const clientID = '33W8M1OOxPv80A' + +// Token for reddit API +let token + +const getToken = () => { + // We have already gotten a token + if (token !== undefined) { + return Promise.resolve(token) + } + + // Headers for getting reddit api token + const tokenInit = { + headers: { + Authorization: `Basic ${window.btoa(`${clientID}:`)}`, + 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' + }, + method: 'POST', + body: `grant_type=${encodeURIComponent('https://oauth.reddit.com/grants/installed_client')}&device_id=DO_NOT_TRACK_THIS_DEVICE` + } + + return window.fetch('https://www.reddit.com/api/v1/access_token', tokenInit) + .then(response => response.json()) + .then(response => { + token = response.access_token + return token + }) +} + +// Get header for general api calls +export const getAuth = () => { + return getToken() .then(token => ({ headers: { Authorization: `bearer ${token}` } })) -) +} diff --git a/src/api/reddit/clientID.js b/src/api/reddit/clientID.js deleted file mode 100644 index 2f8a3f1..0000000 --- a/src/api/reddit/clientID.js +++ /dev/null @@ -1,5 +0,0 @@ -// Change this to your own client ID: https://www.reddit.com/prefs/apps -// The app NEEDS TO BE an installed app and NOT a web apps - -// Current using dummy ID from throwaway -export default '33W8M1OOxPv80A' diff --git a/src/api/reddit/comment.js b/src/api/reddit/comment.js index 6cc163a..1efbd1e 100644 --- a/src/api/reddit/comment.js +++ b/src/api/reddit/comment.js @@ -1,16 +1,18 @@ -import { chunk, flatten, json } from 'utils' +import { chunk, flatten } from '../../utils' import { getAuth } from './auth' -export const getComments = commentIDs => ( - getAuth() +export const getComments = commentIDs => { + return getAuth() .then(auth => ( - Promise.all(chunk(commentIDs, 100).map(ids => fetchComments(ids, auth))).then(flatten) + Promise.all(chunk(commentIDs, 100) + .map(ids => fetchComments(ids, auth))) + .then(flatten) )) -) +} -export const fetchComments = (commentIDs, auth) => ( - window.fetch(`https://oauth.reddit.com/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`, auth) - .then(json) +export const fetchComments = (commentIDs, auth) => { + return window.fetch(`https://oauth.reddit.com/api/info?id=${commentIDs.map(id => `t1_${id}`).join()}`, auth) + .then(response => response.json()) .then(results => results.data.children) .then(commentsData => commentsData.map(commentData => commentData.data)) -) +} diff --git a/src/api/reddit/thread.js b/src/api/reddit/thread.js index 64f4a0e..3239f41 100644 --- a/src/api/reddit/thread.js +++ b/src/api/reddit/thread.js @@ -1,54 +1,18 @@ -import { json } from 'utils' import { getAuth } from './auth' -const cachedThreads = {} - // Thread = Post + Comments // Return the post itself export const getPost = (subreddit, threadID) => ( - getThread(subreddit, threadID) + getAuth() + .then(auth => window.fetch(`https://oauth.reddit.com/r/${subreddit}/comments/${threadID}/_/`, auth)) + .then(response => response.json()) .then(thread => thread[0].data.children[0].data) ) -export const getThread = (subreddit, threadID, commentID = '') => { - // We have already downloaded the thread and can use a cached copy - if (cachedThreads.hasOwnProperty(threadID)) { - if (cachedThreads[threadID].hasOwnProperty(commentID)) { - return Promise.resolve(cachedThreads[threadID][commentID]) - } - } - - const url = `https://oauth.reddit.com/r/${subreddit}/comments/${threadID}/_/${commentID}` - // Fetch thread from reddit - return ( - getAuth() - .then(auth => window.fetch(url, auth)) - .then(json) - .then(thread => { - // Create cache object for thread if it doesn't exists - if (!cachedThreads.hasOwnProperty(threadID)) { - cachedThreads[threadID] = {} - } - - // Save the thread for later - cachedThreads[threadID][commentID] = thread - - // Return the thread - return thread - }) - ) -} - +// Fetch multiple threads (via the info endpoint) export const getThreads = threadIDs => { - const threadString = threadIDs.map(id => `t3_${id}`).join() - - return ( - getAuth() - .then(auth => window.fetch(`https://oauth.reddit.com/api/info?id=${threadString}`, auth)) - .then(json) - .then(response => { - const threads = response.data.children - return threads.map(threadData => threadData.data) - }) - ) + return getAuth() + .then(auth => window.fetch(`https://oauth.reddit.com/api/info?id=${threadIDs.map(id => `t3_${id}`).join()}`, auth)) + .then(response => response.json()) + .then(response => response.data.children.map(threadData => threadData.data)) } diff --git a/src/api/reddit/token.js b/src/api/reddit/token.js deleted file mode 100644 index 9237f5f..0000000 --- a/src/api/reddit/token.js +++ /dev/null @@ -1,30 +0,0 @@ -import { json } from 'utils' -import clientID from './clientID' - -// Token for reddit API -let token = null - -// Headers for getting reddit api token -const tokenInit = { - headers: { - Authorization: `Basic ${window.btoa(`${clientID}:`)}`, - 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' - }, - method: 'POST', - body: `grant_type=${encodeURIComponent('https://oauth.reddit.com/grants/installed_client')}&device_id=DO_NOT_TRACK_THIS_DEVICE` -} - -export const getToken = () => { - if (token !== null) { - return Promise.resolve(token) - } - - return ( - window.fetch('https://www.reddit.com/api/v1/access_token', tokenInit) - .then(json) - .then(response => { - token = response.access_token - return token - }) - ) -} diff --git a/src/api/removeddit/index.js b/src/api/removeddit/index.js index 583c3de..1377851 100644 --- a/src/api/removeddit/index.js +++ b/src/api/removeddit/index.js @@ -1,8 +1,6 @@ -import { json } from 'utils' - const baseURL = 'https://removeddit.com/api' -export const getRemovedThreadIDs = (subreddit = '', page = 1) => ( - window.fetch(`${baseURL}/threads?subreddit=${subreddit}&page=${page - 1}`) - .then(json) -) +export const getRemovedThreadIDs = (subreddit = '', page = 1) => { + return window.fetch(`${baseURL}/threads?subreddit=${subreddit}&page=${page - 1}`) + .then(response => response.json()) +}