diff --git a/flow-typed/Comment.js b/flow-typed/Comment.js index 8b2ad570c..50521e725 100644 --- a/flow-typed/Comment.js +++ b/flow-typed/Comment.js @@ -111,7 +111,7 @@ declare type ReactionListResponse = { declare type CommentListParams = { page: number, // pagination: which page of results page_size: number, // pagination: nr of comments to show in a page (max 200) - claim_id: string, // claim id of claim being commented on + claim_id?: string, // claim id of claim being commented on channel_name?: string, // signing channel name of claim (enables 'commentsEnabled' check) channel_id?: string, // signing channel claim id of claim (enables 'commentsEnabled' check) author_claim_id?: string, // filters comments to just this author diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index cdc9ce281..3da0f9ccd 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -3,7 +3,15 @@ import * as ACTIONS from 'constants/action_types'; import * as REACTION_TYPES from 'constants/reactions'; import * as PAGES from 'constants/pages'; import { SORT_BY, BLOCK_LEVEL } from 'constants/comment'; -import { Lbry, parseURI, buildURI, selectClaimsByUri, selectMyChannelClaims, isURIEqual } from 'lbry-redux'; +import { + Lbry, + parseURI, + buildURI, + selectClaimsByUri, + selectMyChannelClaims, + isURIEqual, + doClaimSearch, +} from 'lbry-redux'; import { doToast, doSeeNotifications } from 'redux/actions/notifications'; import { makeSelectMyReactionsForComment, @@ -200,6 +208,103 @@ export function doCommentList( }; } +export function doCommentListOwn( + channelId: string, + page: number = 1, + pageSize: number = 99999, + sortBy: number = SORT_BY.NEWEST +) { + return async (dispatch: Dispatch, getState: GetState) => { + const state = getState(); + const myChannelClaims = selectMyChannelClaims(state); + if (!myChannelClaims) { + console.error('Failed to fetch channel list.'); // eslint-disable-line + return; + } + + const channelClaim = myChannelClaims.find((x) => x.claim_id === channelId); + if (!channelClaim) { + console.error('You do not own this channel.'); // eslint-disable-line + return; + } + + const channelSignature = await channelSignName(channelClaim.claim_id, channelClaim.name); + if (!channelSignature) { + console.error('Failed to sign channel name.'); // eslint-disable-line + return; + } + + dispatch({ + type: ACTIONS.COMMENT_LIST_STARTED, + data: {}, + }); + + return Comments.comment_list({ + page, + page_size: pageSize, + sort_by: sortBy, + author_claim_id: channelId, + requestor_channel_name: channelClaim.name, + requestor_channel_id: channelClaim.claim_id, + signature: channelSignature.signature, + signing_ts: channelSignature.signing_ts, + }) + .then((result: CommentListResponse) => { + const { items: comments, total_items, total_filtered_items, total_pages } = result; + + if (!comments) { + dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: 'No more comments.' }); + return; + } + + dispatch( + doClaimSearch({ + page: 1, + page_size: 20, + no_totals: true, + claim_ids: comments.map((c) => c.claim_id), + }) + ) + .then((result) => { + dispatch({ + type: ACTIONS.COMMENT_LIST_COMPLETED, + data: { + comments, + totalItems: total_items, + totalFilteredItems: total_filtered_items, + totalPages: total_pages, + uri: channelClaim.canonical_url, // hijack "Discussion Page" + claimId: channelClaim.claim_id, // hijack "Discussion Page" + }, + }); + }) + .catch((err) => { + dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: err }); + }); + }) + .catch((error) => { + switch (error.message) { + case FETCH_API_FAILED_TO_FETCH: + dispatch( + doToast({ + isError: true, + message: Comments.isCustomServer + ? __('Failed to fetch comments. Verify custom server settings.') + : __('Failed to fetch comments.'), + }) + ); + dispatch(doToast({ isError: true, message: `${error.message}` })); + dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: error }); + break; + + default: + dispatch(doToast({ isError: true, message: `${error.message}` })); + dispatch({ type: ACTIONS.COMMENT_LIST_FAILED, data: error }); + } + }); + }; +} + export function doCommentById(commentId: string, toastIfNotFound: boolean = true) { return (dispatch: Dispatch, getState: GetState) => { dispatch({