diff --git a/src/api/pushshift/index.js b/src/api/pushshift/index.js index 61e7226..3f269ff 100644 --- a/src/api/pushshift/index.js +++ b/src/api/pushshift/index.js @@ -13,15 +13,14 @@ export const getPost = threadID => { } } - return ( - window.fetch(postURL + JSON.stringify(elasticQuery)) - .then(response => response.json()) - .then(response => { - const post = response.hits.hits[0]._source - post.id = toBase36(post.id) - return post - }) - ) + return window.fetch(postURL + JSON.stringify(elasticQuery)) + .then(response => response.json()) + .then(response => { + const post = response.hits.hits[0]._source + post.id = toBase36(post.id) + return post + }) + .catch(() => { throw new Error('Could not get removed post') }) } // export const getComments = threadID => ( @@ -50,24 +49,23 @@ export const getComments = threadID => { ] } - return ( - window.fetch(commentURL + JSON.stringify(elasticQuery)) - .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) + return window.fetch(commentURL + JSON.stringify(elasticQuery)) + .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 }) - ) + }) + .catch(() => { throw new Error('Could not get removed comments') }) } diff --git a/src/api/reddit/comment.js b/src/api/reddit/comment.js deleted file mode 100644 index 1efbd1e..0000000 --- a/src/api/reddit/comment.js +++ /dev/null @@ -1,18 +0,0 @@ -import { chunk, flatten } from '../../utils' -import { getAuth } from './auth' - -export const getComments = commentIDs => { - return getAuth() - .then(auth => ( - Promise.all(chunk(commentIDs, 100) - .map(ids => fetchComments(ids, auth))) - .then(flatten) - )) -} - -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/index.js b/src/api/reddit/index.js index 2c6d727..dcd6cf6 100644 --- a/src/api/reddit/index.js +++ b/src/api/reddit/index.js @@ -1,2 +1,43 @@ -export { getPost, getThreads } from './thread' -export { getComments } from './comment' +import { chunk, flatten } from '../../utils' +import { getAuth } from './auth' + +const errorHandler = () => { + throw new Error('Could not connect to Reddit') +} + +// Thread = Post + Comments +// Return the post itself +export const getPost = (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) + .catch(errorHandler) +) + +// Fetch multiple threads (via the info endpoint) +export const getThreads = threadIDs => { + 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)) + .catch(errorHandler) +} + +// Helper function that fetches a list of comments +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)) +} + +export const getComments = commentIDs => { + return getAuth() + .then(auth => ( + Promise.all(chunk(commentIDs, 100) + .map(ids => fetchComments(ids, auth))) + .then(flatten) + )) + .catch(errorHandler) +} diff --git a/src/api/reddit/thread.js b/src/api/reddit/thread.js deleted file mode 100644 index 3239f41..0000000 --- a/src/api/reddit/thread.js +++ /dev/null @@ -1,18 +0,0 @@ -import { getAuth } from './auth' - -// Thread = Post + Comments -// Return the post itself -export const getPost = (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) -) - -// Fetch multiple threads (via the info endpoint) -export const getThreads = threadIDs => { - 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/removeddit/index.js b/src/api/removeddit/index.js index c3651d1..9ba0ee6 100644 --- a/src/api/removeddit/index.js +++ b/src/api/removeddit/index.js @@ -7,4 +7,5 @@ export const getRemovedThreadIDs = (subreddit = '', page = 1) => { return window.fetch(`${baseURL}/threads?subreddit=${subreddit}&page=${page - 1}`) .then(response => response.json()) + .catch(() => { throw new Error('Could not get removed threads') }) } diff --git a/src/pages/common/Post.js b/src/pages/common/Post.js index d8e2a26..25043df 100644 --- a/src/pages/common/Post.js +++ b/src/pages/common/Post.js @@ -36,8 +36,10 @@ export default (props) => { {thumbnail}