lbry-redux/src/redux/selectors/file_info.js

241 lines
6.7 KiB
JavaScript
Raw Normal View History

2018-04-05 03:57:29 +01:00
import {
selectClaimsByUri,
selectIsFetchingClaimListMine,
selectMyClaims,
selectClaimsById,
} from 'redux/selectors/claims';
import { createSelector } from 'reselect';
import { buildURI } from 'lbryURI';
export const selectState = state => state.fileInfo || {};
export const selectFileInfosByOutpoint = createSelector(
selectState,
state => state.byOutpoint || {}
);
export const selectIsFetchingFileList = createSelector(
selectState,
state => state.isFetchingFileList
);
export const selectIsFetchingFileListDownloadedOrPublished = createSelector(
selectIsFetchingFileList,
selectIsFetchingClaimListMine,
(isFetchingFileList, isFetchingClaimListMine) => isFetchingFileList || isFetchingClaimListMine
);
export const makeSelectFileInfoForUri = uri =>
createSelector(
selectClaimsByUri,
selectFileInfosByOutpoint,
(claims, byOutpoint) => {
const claim = claims[uri];
const outpoint = claim ? `${claim.txid}:${claim.nout}` : undefined;
return outpoint ? byOutpoint[outpoint] : undefined;
}
);
2018-04-05 03:57:29 +01:00
export const selectDownloadingByOutpoint = createSelector(
selectState,
state => state.downloadingByOutpoint || {}
);
export const makeSelectDownloadingForUri = uri =>
createSelector(
selectDownloadingByOutpoint,
makeSelectFileInfoForUri(uri),
(byOutpoint, fileInfo) => {
if (!fileInfo) return false;
return byOutpoint[fileInfo.outpoint];
}
);
export const selectUrisLoading = createSelector(
selectState,
state => state.urisLoading || {}
);
2018-04-05 03:57:29 +01:00
export const makeSelectLoadingForUri = uri =>
createSelector(
selectUrisLoading,
byUri => byUri && byUri[uri]
);
2018-04-05 03:57:29 +01:00
export const selectFileInfosDownloaded = createSelector(
selectFileInfosByOutpoint,
selectMyClaims,
(byOutpoint, myClaims) =>
Object.values(byOutpoint).filter(fileInfo => {
const myClaimIds = myClaims.map(claim => claim.claim_id);
return (
fileInfo &&
myClaimIds.indexOf(fileInfo.claim_id) === -1 &&
2019-07-23 12:22:26 -04:00
(fileInfo.completed || fileInfo.written_bytes > 0 || fileInfo.blobs_completed > 0 )
2018-04-05 03:57:29 +01:00
);
})
);
// export const selectFileInfoForUri = (state, props) => {
// const claims = selectClaimsByUri(state),
// claim = claims[props.uri],
// fileInfos = selectAllFileInfos(state),
// outpoint = claim ? `${claim.txid}:${claim.nout}` : undefined;
// return outpoint && fileInfos ? fileInfos[outpoint] : undefined;
// };
export const selectDownloadingFileInfos = createSelector(
selectDownloadingByOutpoint,
selectFileInfosByOutpoint,
(downloadingByOutpoint, fileInfosByOutpoint) => {
const outpoints = Object.keys(downloadingByOutpoint);
const fileInfos = [];
outpoints.forEach(outpoint => {
const fileInfo = fileInfosByOutpoint[outpoint];
if (fileInfo) fileInfos.push(fileInfo);
});
return fileInfos;
}
);
export const selectTotalDownloadProgress = createSelector(
selectDownloadingFileInfos,
fileInfos => {
const progress = [];
2018-04-05 03:57:29 +01:00
fileInfos.forEach(fileInfo => {
progress.push((fileInfo.written_bytes / fileInfo.total_bytes) * 100);
});
2018-04-05 03:57:29 +01:00
const totalProgress = progress.reduce((a, b) => a + b, 0);
2018-04-05 03:57:29 +01:00
if (fileInfos.length > 0) return totalProgress / fileInfos.length / 100.0;
return -1;
}
);
2018-04-05 03:57:29 +01:00
export const selectSearchDownloadUris = query =>
createSelector(
selectFileInfosDownloaded,
selectClaimsById,
(fileInfos, claimsById) => {
if (!query || !fileInfos.length) {
return null;
2018-04-05 03:57:29 +01:00
}
const queryParts = query.toLowerCase().split(' ');
const searchQueryDictionary = {};
queryParts.forEach(subQuery => {
searchQueryDictionary[subQuery] = subQuery;
});
const arrayContainsQueryPart = array => {
for (let i = 0; i < array.length; i += 1) {
const subQuery = array[i];
if (searchQueryDictionary[subQuery]) {
return true;
}
}
return false;
};
const downloadResultsFromQuery = [];
fileInfos.forEach(fileInfo => {
const { channel_name: channelName, claim_name: claimName, metadata } = fileInfo;
const { author, description, title } = metadata;
if (channelName) {
const lowerCaseChannel = channelName.toLowerCase();
const strippedOutChannelName = lowerCaseChannel.slice(1); // trim off the @
if (searchQueryDictionary[channelName] || searchQueryDictionary[strippedOutChannelName]) {
downloadResultsFromQuery.push(fileInfo);
return;
}
}
2018-04-05 03:57:29 +01:00
const nameParts = claimName.toLowerCase().split('-');
if (arrayContainsQueryPart(nameParts)) {
2018-04-05 03:57:29 +01:00
downloadResultsFromQuery.push(fileInfo);
return;
}
2019-06-06 12:02:43 -04:00
if (title) {
const titleParts = title.toLowerCase().split(' ');
if (arrayContainsQueryPart(titleParts)) {
downloadResultsFromQuery.push(fileInfo);
return;
}
2018-04-05 03:57:29 +01:00
}
if (author) {
const authorParts = author.toLowerCase().split(' ');
if (arrayContainsQueryPart(authorParts)) {
downloadResultsFromQuery.push(fileInfo);
return;
}
2018-04-05 03:57:29 +01:00
}
if (description) {
const descriptionParts = description.toLowerCase().split(' ');
if (arrayContainsQueryPart(descriptionParts)) {
downloadResultsFromQuery.push(fileInfo);
}
}
});
return downloadResultsFromQuery.length
? downloadResultsFromQuery.map(fileInfo => {
2019-07-26 10:45:14 -04:00
const {
channel_name: channelName,
claim_id: claimId,
claim_name: claimName,
} = fileInfo;
const uriParams = {};
if (channelName) {
const claim = claimsById[claimId];
if (claim && claim.signing_channel) {
uriParams.claimId = claim.signing_channel.claim_id;
2018-04-05 03:57:29 +01:00
} else {
uriParams.claimId = claimId;
}
2019-07-26 10:45:14 -04:00
uriParams.channelName = channelName;
uriParams.contentName = claimName;
} else {
uriParams.claimId = claimId;
uriParams.claimName = claimName;
}
2018-04-05 03:57:29 +01:00
2019-07-26 10:45:14 -04:00
const uri = buildURI(uriParams);
return uri;
})
: null;
}
);
2018-10-22 23:31:57 -04:00
export const selectFileListPublishedSort = createSelector(
selectState,
2018-10-22 23:31:57 -04:00
state => state.fileListPublishedSort
);
2018-10-22 23:31:57 -04:00
export const selectFileListDownloadedSort = createSelector(
selectState,
2018-10-22 23:31:57 -04:00
state => state.fileListDownloadedSort
);
2019-06-09 22:45:47 -04:00
export const selectDownloadedUris = createSelector(
selectFileInfosDownloaded,
// We should use permament_url but it doesn't exist in file_list
2019-06-26 18:01:49 -04:00
info =>
info
.slice()
.reverse()
2019-07-05 13:35:17 -04:00
.map(claim => `lbry://${claim.claim_name}#${claim.claim_id}`)
2019-06-09 22:45:47 -04:00
);