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}
{props.title} - {props.link_flair_text && - {props.link_flair_text}} + { + props.link_flair_text && + {props.link_flair_text} + } ({props.domain})
submitted {prettyDate(props.created_utc)} by  @@ -47,15 +49,9 @@ export default (props) => { {props.selftext &&
}
- - {props.num_comments} comments -   - - reddit -   - - ceddit - + {props.num_comments} comments  + reddit  + ceddit
diff --git a/src/pages/subreddit/SubredditSort.js b/src/pages/subreddit/SubredditSort.js index 83e8469..cf8ed50 100644 --- a/src/pages/subreddit/SubredditSort.js +++ b/src/pages/subreddit/SubredditSort.js @@ -6,7 +6,6 @@ export default (props) => { return (
top post of /r/{props.subreddit} from: - {/* diff --git a/src/pages/thread/CommentSection.js b/src/pages/thread/CommentSection.js index 65840fc..bc93ef8 100644 --- a/src/pages/thread/CommentSection.js +++ b/src/pages/thread/CommentSection.js @@ -1,7 +1,5 @@ import React from 'react' import Comment from './Comment' -import CommentInfo from './CommentInfo' -import SortBy from './SortBy' import {connect, sort, filter} from '../../state' import { topSort, bottomSort, newSort, oldSort, @@ -112,20 +110,12 @@ const commentSection = (props) => { console.timeEnd('render comment section') return ( -
- ( + - - {commentTree.map(comment => ( - - ))} -
+ )) ) } diff --git a/src/pages/thread/index.js b/src/pages/thread/index.js index d4415e5..68d89c0 100644 --- a/src/pages/thread/index.js +++ b/src/pages/thread/index.js @@ -10,46 +10,42 @@ import { getComments as getPushshiftComments } from '../../api/pushshift' import { isDeleted, isRemoved } from '../../utils' +import CommentInfo from './CommentInfo' +import SortBy from './SortBy' +import { connect } from '../../state' -export default class Thread extends React.Component { - constructor (props) { - super(props) - - this.state = { - post: {}, - pushshiftComments: [], - removed: [], - deleted: [], - loadingComments: true - } +class Thread extends React.Component { + state = { + post: {}, + pushshiftComments: [], + removed: [], + deleted: [], + loadingComments: true } componentDidMount () { const { subreddit, threadID } = this.props.match.params - Promise.all([ - // Get thread from reddit - getPost(subreddit, threadID) - .then(post => { - this.setState({ post }) - // Fetch the thread from pushshift if it was deleted/removed - if (isDeleted(post.selftext)) { - getRemovedPost(threadID) - .then(removedPost => { - removedPost.removed = true - this.setState({ post: removedPost }) - }) - } - }), - // Get comment ids from pushshift - getPushshiftComments(threadID) - ]) - .then(results => { - const pushshiftComments = results[1] + // Get thread from reddit + getPost(subreddit, threadID) + .then(post => { + this.setState({ post }) + // Fetch the thread from pushshift if it was deleted/removed + if (isDeleted(post.selftext)) { + getRemovedPost(threadID) + .then(removedPost => { + removedPost.removed = true + this.setState({ post: removedPost }) + }) + } + }) + .catch(error => console.log(error)) + // Get comment ids from pushshift + getPushshiftComments(threadID) + .then(pushshiftComments => { // Extract ids from pushshift response const ids = pushshiftComments.map(comment => comment.id) - console.log('Number of comments from Pushshift:', ids.length) // Get all the comments from reddit return ( @@ -74,7 +70,6 @@ export default class Thread extends React.Component { ) }) .then(redditComments => { - console.log('Number of comments from Reddit:', redditComments.length) const removed = [] const deleted = [] @@ -107,14 +102,24 @@ export default class Thread extends React.Component { { !this.state.loadingComments && - + + + + + }
) } } + +export default connect(Thread)