diff --git a/ui/redux/actions/collections.js b/ui/redux/actions/collections.js index 0d5b3ad398..cc3db8201f 100644 --- a/ui/redux/actions/collections.js +++ b/ui/redux/actions/collections.js @@ -34,6 +34,9 @@ import { getThumbnailFromClaim } from 'util/claim'; import { creditsToString } from 'util/format-credits'; import { doToast } from 'redux/actions/notifications'; +import { Lbryio } from 'lbryinc'; +import { selectUser } from 'redux/selectors/user'; + const FETCH_BATCH_SIZE = 50; export const doFetchCollectionListMine = @@ -243,87 +246,136 @@ export const doToggleCollectionSavedForId = (collectionId: string) => (dispatch: dispatch({ type: ACTIONS.COLLECTION_TOGGLE_SAVE, data: collectionId }); }; -const doFetchCollectionItems = (items: Array, pageSize?: number) => async (dispatch: Dispatch) => { - const sortResults = (resultItems: Array) => { - const newItems: Array = []; - - items.forEach((item) => { - const index = resultItems.findIndex((i) => [i.canonical_url, i.permanent_url, i.claim_id].includes(item)); +const doFetchCollectionItems = + (items: Array, pageSize?: number, isFromEdit?: boolean) => async (dispatch: Dispatch, getState: GetState) => { + const sortResults = (resultItems: Array) => { + const newItems: Array = []; - if (index >= 0) newItems.push(resultItems[index]); - }); + items.forEach((item) => { + const index = resultItems.findIndex((i) => [i.canonical_url, i.permanent_url, i.claim_id].includes(item)); - return newItems; - }; + if (index >= 0) newItems.push(resultItems[index]); + }); - const mergeBatches = (arrayOfResults: Array) => { - let resultItems = []; + return newItems; + }; - arrayOfResults.forEach((result: any) => { - // $FlowFixMe - const claims = result.items || Object.values(result).map((item) => item.stream || item); - resultItems = resultItems.concat(claims); - }); + const mergeBatches = (arrayOfResults: Array) => { + let resultItems = []; - return resultItems; - }; + arrayOfResults.forEach((result: any) => { + // $FlowFixMe + const claims = result.items || Object.values(result).map((item) => item.stream || item); + resultItems = resultItems.concat(claims); + }); - try { - const batchSize = pageSize || FETCH_BATCH_SIZE; - const uriBatches: Array> = []; - const idBatches: Array> = []; + return resultItems; + }; - const totalItems = items.length; + try { + const batchSize = pageSize || FETCH_BATCH_SIZE; + const uriBatches: Array> = []; + const idBatches: Array> = []; - for (let i = 0; i < Math.ceil(totalItems / batchSize); i++) { - const batchInitialIndex = i * batchSize; - const batchLength = (i + 1) * batchSize; + const totalItems = items.length; - // --> Filter in case null/undefined are collection items - const batchItems = items.slice(batchInitialIndex, batchLength).filter(Boolean); + /// Temporary debug + const state = getState(); + const user = selectUser(state); + if (isFromEdit && user.id === 645368535) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ----------------------------- `, + }); + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug:items: ${JSON.stringify( + items.map((item) => { + const match = item.match(/[a-f0-9]{40}/); + const claimId = match && match[0]; + return claimId || item; + }) + )}`, + }); + } + /// ^^^ + for (let i = 0; i < Math.ceil(totalItems / batchSize); i++) { + const batchInitialIndex = i * batchSize; + const batchLength = (i + 1) * batchSize; + + // --> Filter in case null/undefined are collection items + const batchItems = items.slice(batchInitialIndex, batchLength).filter(Boolean); + + const uris = new Set([]); + const ids = new Set([]); + batchItems.forEach((item) => { + if (item.startsWith('lbry://')) { + uris.add(item); + } else { + ids.add(item); + } + }); - const uris = new Set([]); - const ids = new Set([]); - batchItems.forEach((item) => { - if (item.startsWith('lbry://')) { - uris.add(item); - } else { - ids.add(item); + if (uris.size > 0) { + uriBatches[i] = dispatch(doResolveUris(Array.from(uris), true)); + } + if (ids.size > 0) { + idBatches[i] = dispatch(doResolveClaimIds(Array.from(ids))); } - }); - - if (uris.size > 0) { - uriBatches[i] = dispatch(doResolveUris(Array.from(uris), true)); } - if (ids.size > 0) { - idBatches[i] = dispatch(doResolveClaimIds(Array.from(ids))); + const itemsInBatches = await Promise.all([...uriBatches, ...idBatches]); + const resultItems = sortResults(mergeBatches(itemsInBatches.filter(Boolean))); + + // The resolve calls will NOT return items when they still are in a previous call's 'Processing' state. + const itemsWereFetching = resultItems.length !== items.length; + + // Temporary debug + if (isFromEdit && user.id === 645368535) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug:resultItems: ${JSON.stringify( + resultItems.map((item) => { + if (!item.claim_id) { + return item; + } + return item.claim_id; + }) + )}`, + }); } - } - const itemsInBatches = await Promise.all([...uriBatches, ...idBatches]); - const resultItems = sortResults(mergeBatches(itemsInBatches.filter(Boolean))); - - // The resolve calls will NOT return items when they still are in a previous call's 'Processing' state. - const itemsWereFetching = resultItems.length !== items.length; - - if (resultItems && !itemsWereFetching) { - return resultItems; - } else { + /// ^^^ + if (resultItems && !itemsWereFetching) { + return resultItems; + } else { + return null; + } + } catch (e) { + await Lbryio.call('event', 'desktop_error', { error_message: `Playlist debug:error: ${e.message || e}` }); return null; } - } catch (e) { - return null; - } -}; + }; export const doFetchItemsInCollection = - (params: { collectionId: string, pageSize?: number }) => async (dispatch: Dispatch, getState: GetState) => { + (params: { collectionId: string, pageSize?: number, isFromEdit?: boolean }) => + async (dispatch: Dispatch, getState: GetState) => { let state = getState(); - const { collectionId, pageSize } = params; + const { collectionId, pageSize, isFromEdit } = params; const isAlreadyFetching = selectAreCollectionItemsFetchingForId(state, collectionId); + const user = selectUser(state); + const shouldLog = user.id === 645368535; + + if (isAlreadyFetching && shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: Already fetching, collectionId: ${collectionId}`, + }); + } + if (isAlreadyFetching) return Promise.resolve(); + if (shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ITEM_RESOLVE_START, collectionId: ${collectionId}`, + }); + } dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_START, data: collectionId }); const isPrivate = selectHasPrivateCollectionForId(state, collectionId); @@ -338,6 +390,11 @@ export const doFetchItemsInCollection = } if (!isPrivate && hasClaim === null) { + if (shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ITEM_RESOLVE_FAIL (public), collectionId: ${collectionId}`, + }); + } return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_FAIL, data: collectionId }); } @@ -347,15 +404,21 @@ export const doFetchItemsInCollection = const collection = selectCollectionForId(state, collectionId); if (collection.items.length > 0) { - promisedCollectionItemsFetch = collection.items && dispatch(doFetchCollectionItems(collection.items, pageSize)); + promisedCollectionItemsFetch = + collection.items && dispatch(doFetchCollectionItems(collection.items, pageSize, isFromEdit)); } else { + if (shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ITEM_RESOLVE_FAIL (private), collectionId: ${collectionId}`, + }); + } return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_FAIL, data: collectionId }); } } else { const claim = selectClaimForClaimId(state, collectionId); const claimIds = getClaimIdsInCollectionClaim(claim); - promisedCollectionItemsFetch = claimIds && dispatch(doFetchCollectionItems(claimIds, pageSize)); + promisedCollectionItemsFetch = claimIds && dispatch(doFetchCollectionItems(claimIds, pageSize, isFromEdit)); } // -- Await results: @@ -364,6 +427,11 @@ export const doFetchItemsInCollection = } if (!collectionItems || collectionItems.length === 0) { + if (shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ITEM_RESOLVE_FAIL (general), collectionId: ${collectionId}`, + }); + } return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_FAIL, data: collectionId }); } @@ -375,6 +443,12 @@ export const doFetchItemsInCollection = const newPrivateCollection = { ...collection, items: newItems, key: collectionKey }; + if (shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ITEM_RESOLVE_SUCCESS (private), collectionId: ${collectionId}`, + }); + } + return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_SUCCESS, data: { resolvedCollection: newPrivateCollection }, @@ -415,6 +489,11 @@ export const doFetchItemsInCollection = ...resolveAuxParams(collectionType, claim), }; + if (shouldLog) { + await Lbryio.call('event', 'desktop_error', { + error_message: `Playlist debug: ITEM_RESOLVE_SUCCESS (public), collectionId: ${collectionId}`, + }); + } return dispatch( batchActions( { type: ACTIONS.COLLECTION_ITEMS_RESOLVE_SUCCESS, data: { resolvedCollection: newStoreCollectionClaim } }, @@ -524,7 +603,7 @@ export const doCollectionEdit = let collectionUrls = selectUrlsForCollectionId(state, collectionId); if (collectionUrls === undefined) { - await dispatch(doFetchItemsInCollection({ collectionId })); + await dispatch(doFetchItemsInCollection({ collectionId, isFromEdit: true })); state = getState(); collectionUrls = selectUrlsForCollectionId(state, collectionId); }