lbry-desktop/ui/redux/actions/file.js

234 lines
6.2 KiB
JavaScript
Raw Normal View History

2021-10-05 22:57:47 +02:00
// @flow
import * as ACTIONS from 'constants/action_types';
import * as ABANDON_STATES from 'constants/abandon_states';
2019-03-05 05:46:57 +01:00
// @if TARGET='app'
import { shell } from 'electron';
2019-03-05 05:46:57 +01:00
// @endif
import Lbry from 'lbry';
import { selectClaimForUri } from 'redux/selectors/claims';
import { doAbandonClaim } from 'redux/actions/claims';
import { batchActions } from 'util/batch-actions';
import { doHideModal } from 'redux/actions/app';
import { goBack } from 'connected-react-router';
2019-08-14 05:56:11 +02:00
import { doSetPlayingUri } from 'redux/actions/content';
import { selectPlayingUri } from 'redux/selectors/content';
2021-10-05 22:57:47 +02:00
import { doToast } from 'redux/actions/notifications';
import { selectBalance } from 'redux/selectors/wallet';
import {
makeSelectFileInfoForUri,
selectDownloadingByOutpoint,
makeSelectStreamingUrlForUri,
} from 'redux/selectors/file_info';
2021-10-05 22:57:47 +02:00
type Dispatch = (action: any) => any;
Selector refactors (#7424) * Attempt to speed up sidebar menu for mobile (#283) * Exclude default homepage data at compile time The youtuber IDs alone is pretty huge, and is unused in the `CUSTOM_HOMEPAGE=true` configuration. * Remove Desktop items and other cleanup - Moved constants out of the component. - Remove SIMPLE_SITE check. - Remove Desktop-only items * Sidebar: limit subscription and tag section Too slow for huge lists Limit to 10 initially, and load everything on "Show more" * Fix makeSelectThumbnailForUri - Fix memo - Expose function to extract directly from claim if client already have it. * Fix and optimize makeSelectIsSubscribed (#273) - It will not return true if the uri provided is canonical, because the compared subscription uri is in permanent form. This was causing certain elements like the Heart to not appear in claim tiles. - It is super slow for large subscriptions not just because of the array size + being a hot selector, but also because it is looking up the claim twice (not memo'd) and also calling `parseURI` to determine if it's a channel, which is unnecessary if you already have the claim. - Optimize the selector to only look up the claim once, and make operations using already-obtained info. * Simplify makeSelectTitleForUri No need to memo given no transformation. * Simplify makeSelectIsUriResolving - Memo not required. `resolvingUris` is very dynamic and is a short array anyways. - Changeg from using `indexOf` to `includes`, which is more concise. * Cost Info selector fixes - no memo required since they are just directly accessing the store. Co-authored-by: infinite-persistence <64950861+infinite-persistence@users.noreply.github.com> Co-authored-by: infinite-persistence <inf.persistence@gmail.com>
2022-01-20 02:46:01 +01:00
type GetState = () => { claims: any, file: FileState, content: any };
2021-10-05 22:57:47 +02:00
export function doOpenFileInFolder(path: string) {
return () => {
shell.showItemInFolder(path);
};
}
2021-10-05 22:57:47 +02:00
export function doOpenFileInShell(path: string) {
return (dispatch: Dispatch) => {
2020-10-16 01:06:05 +02:00
const success = shell.openPath(path);
if (!success) {
dispatch(doOpenFileInFolder(path));
}
2017-06-06 23:19:12 +02:00
};
}
export function doDeleteFile(
outpoint: string,
deleteFromComputer?: boolean,
abandonClaim?: boolean,
cb: any,
claim: Claim
) {
2021-10-05 22:57:47 +02:00
return (dispatch: Dispatch) => {
if (abandonClaim) {
dispatch(doAbandonClaim(claim, cb));
}
2020-03-27 17:49:41 +01:00
// @if TARGET='app'
2020-04-28 00:22:09 +02:00
Lbry.file_delete({
outpoint,
delete_from_download_dir: deleteFromComputer,
});
dispatch({
type: ACTIONS.FILE_DELETE,
data: {
2017-06-06 23:19:12 +02:00
outpoint,
},
});
2020-03-27 17:49:41 +01:00
// @endif
};
}
2021-10-05 22:57:47 +02:00
export function doDeleteFileAndMaybeGoBack(
uri: string,
deleteFromComputer?: boolean,
abandonClaim?: boolean,
doGoBack: (any) => void,
claim: Claim
2021-10-05 22:57:47 +02:00
) {
return (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const playingUri = selectPlayingUri(state);
const { outpoint } = makeSelectFileInfoForUri(uri)(state) || '';
const { nout, txid } = selectClaimForUri(state, uri);
const claimOutpoint = `${txid}:${nout}`;
const actions = [];
if (!abandonClaim) {
actions.push(doHideModal());
}
actions.push(
doDeleteFile(
outpoint || claimOutpoint,
deleteFromComputer,
abandonClaim,
(abandonState) => {
if (abandonState === ABANDON_STATES.DONE) {
if (abandonClaim) {
if (doGoBack) {
dispatch(goBack());
}
dispatch(doHideModal());
}
}
},
claim
)
);
if (playingUri && playingUri.uri === uri) {
2020-12-21 16:31:34 +01:00
actions.push(doSetPlayingUri({ uri: null }));
}
2019-08-30 16:01:39 +02:00
// it would be nice to stay on the claim if you just want to delete it
// we need to alter autoplay to not start downloading again after you delete it
dispatch(batchActions(...actions));
2017-06-06 23:19:12 +02:00
};
}
2021-10-05 22:57:47 +02:00
export function doFileGet(uri: string, saveFile: boolean = true, onSuccess?: (GetResponse) => any) {
return (dispatch: Dispatch, getState: () => any) => {
const state = getState();
const { nout, txid } = selectClaimForUri(state, uri);
2021-10-05 22:57:47 +02:00
const outpoint = `${txid}:${nout}`;
dispatch({
type: ACTIONS.FETCH_FILE_INFO_STARTED,
data: {
outpoint,
},
});
// set save_file argument to True to save the file (old behaviour)
Lbry.get({ uri, save_file: saveFile })
.then((streamInfo: GetResponse) => {
const timeout = streamInfo === null || typeof streamInfo !== 'object' || streamInfo.error === 'Timeout';
if (timeout) {
dispatch({
type: ACTIONS.FETCH_FILE_INFO_FAILED,
data: { outpoint },
});
dispatch(doToast({ message: `File timeout for uri ${uri}`, isError: true }));
} else {
if (streamInfo.purchase_receipt || streamInfo.content_fee) {
dispatch({
type: ACTIONS.PURCHASE_URI_COMPLETED,
data: { uri, purchaseReceipt: streamInfo.purchase_receipt || streamInfo.content_fee },
});
}
dispatch({
type: ACTIONS.FETCH_FILE_INFO_COMPLETED,
data: {
fileInfo: streamInfo,
outpoint: outpoint,
},
});
if (onSuccess) {
onSuccess(streamInfo);
}
}
})
.catch((error) => {
dispatch({
type: ACTIONS.PURCHASE_URI_FAILED,
data: { uri, error },
});
dispatch({
type: ACTIONS.FETCH_FILE_INFO_FAILED,
data: { outpoint },
});
dispatch(
doToast({
message: `Failed to view ${uri}, please try again. If this problem persists, visit https://lbry.com/faq/support for support.`,
isError: true,
})
);
});
};
}
export function doPurchaseUri(
uri: string,
costInfo: { cost: number },
saveFile: boolean = true,
onSuccess?: (GetResponse) => any
) {
return (dispatch: Dispatch, getState: GetState) => {
dispatch({
type: ACTIONS.PURCHASE_URI_STARTED,
data: { uri },
});
const state = getState();
const balance = selectBalance(state);
const fileInfo = makeSelectFileInfoForUri(uri)(state);
const downloadingByOutpoint = selectDownloadingByOutpoint(state);
const alreadyDownloading = fileInfo && !!downloadingByOutpoint[fileInfo.outpoint];
const alreadyStreaming = makeSelectStreamingUrlForUri(uri)(state);
if (!saveFile && (alreadyDownloading || alreadyStreaming)) {
dispatch({
type: ACTIONS.PURCHASE_URI_FAILED,
data: { uri, error: `Already fetching uri: ${uri}` },
});
if (onSuccess) {
onSuccess(fileInfo);
}
return;
}
const { cost } = costInfo;
if (parseFloat(cost) > balance) {
dispatch({
type: ACTIONS.PURCHASE_URI_FAILED,
data: { uri, error: 'Insufficient credits' },
});
Promise.resolve();
return;
}
dispatch(doFileGet(uri, saveFile, onSuccess));
};
}
export function doClearPurchasedUriSuccess() {
return {
type: ACTIONS.CLEAR_PURCHASED_URI_SUCCESS,
};
}