lbry-desktop/src/ui/redux/actions/content.js

438 lines
13 KiB
JavaScript
Raw Normal View History

// @flow
2019-03-18 06:09:50 +01:00
import type { Dispatch, GetState } from 'types/redux';
2018-10-19 22:38:07 +02:00
import * as NOTIFICATION_TYPES from 'constants/subscriptions';
2018-11-03 03:17:55 +01:00
import { PAGE_SIZE } from 'constants/claim';
import * as MODALS from 'constants/modal_types';
2019-03-05 05:46:57 +01:00
// @if TARGET='app'
import { ipcRenderer } from 'electron';
2019-03-05 05:46:57 +01:00
// @endif
import { doOpenModal } from 'redux/actions/app';
2019-03-28 17:53:13 +01:00
import { navigate } from '@reach/router';
2018-10-19 22:38:07 +02:00
import { setSubscriptionLatest, doUpdateUnreadSubscriptions } from 'redux/actions/subscriptions';
import { makeSelectUnreadByChannel } from 'redux/selectors/subscriptions';
2017-04-28 17:14:44 +02:00
import {
2018-04-18 06:03:01 +02:00
ACTIONS,
SETTINGS,
Lbry,
Lbryapi,
buildURI,
2017-09-08 05:15:05 +02:00
makeSelectFileInfoForUri,
selectFileInfosByOutpoint,
2017-07-21 10:02:29 +02:00
selectDownloadingByOutpoint,
2018-04-18 06:03:01 +02:00
selectBalance,
2018-10-19 22:38:07 +02:00
makeSelectChannelForClaimUri,
parseURI,
2018-10-31 18:11:32 +01:00
creditsToString,
2018-11-21 22:20:55 +01:00
doError,
2019-03-28 17:53:13 +01:00
makeSelectCostInfoForUri,
2018-04-18 06:03:01 +02:00
} from 'lbry-redux';
import { makeSelectClientSetting, selectosNotificationsEnabled } from 'redux/selectors/settings';
import analytics from 'analytics';
2019-03-28 17:53:13 +01:00
import { formatLbryUriForWeb } from 'util/uri';
const DOWNLOAD_POLL_INTERVAL = 250;
export function doUpdateLoadStatus(uri: string, outpoint: string) {
2019-03-13 06:59:07 +01:00
// Updates the loading status for a uri as it's downloading
// Calls file_list and checks the written_bytes value to see if the number has increased
// Not needed on web as users aren't actually downloading the file
// @if TARGET='app'
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch, getState: GetState) => {
const setNextStatusUpdate = () =>
setTimeout(() => {
2018-10-18 14:38:12 +02:00
// We need to check if outpoint still exists first because user are able to delete file (outpoint) while downloading.
2018-10-18 17:23:08 +02:00
// If a file is already deleted, no point to still try update load status
const byOutpoint = selectFileInfosByOutpoint(getState());
if (byOutpoint[outpoint]) {
dispatch(doUpdateLoadStatus(uri, outpoint));
}
}, DOWNLOAD_POLL_INTERVAL);
2019-03-13 06:59:07 +01:00
Lbry.file_list({
outpoint,
full_status: true,
}).then(([fileInfo]) => {
if (!fileInfo || fileInfo.written_bytes === 0) {
// download hasn't started yet
setNextStatusUpdate();
} else if (fileInfo.completed) {
const state = getState();
// TODO this isn't going to get called if they reload the client before
// the download finished
dispatch({
type: ACTIONS.DOWNLOADING_COMPLETED,
data: {
uri,
outpoint,
fileInfo,
},
});
2018-10-19 22:38:07 +02:00
const channelUri = makeSelectChannelForClaimUri(uri, true)(state);
const { claimName: channelName } = parseURI(channelUri);
const unreadForChannel = makeSelectUnreadByChannel(channelUri)(state);
if (unreadForChannel && unreadForChannel.type === NOTIFICATION_TYPES.DOWNLOADING) {
2018-10-19 22:38:07 +02:00
const count = unreadForChannel.uris.length;
2018-08-14 15:27:29 +02:00
if (selectosNotificationsEnabled(state)) {
2018-10-19 22:38:07 +02:00
const notif = new window.Notification(channelName, {
body: `Posted ${fileInfo.metadata.title}${
count > 1 && count < 10 ? ` and ${count - 1} other new items` : ''
}${count > 9 ? ' and 9+ other new items' : ''}`,
silent: false,
});
notif.onclick = () => {
2019-03-28 17:53:13 +01:00
navigate(formatLbryUriForWeb(uri));
};
}
2018-10-19 22:38:07 +02:00
dispatch(doUpdateUnreadSubscriptions(channelUri, null, NOTIFICATION_TYPES.DOWNLOADED));
} else {
// If notifications are disabled(false) just return
if (!selectosNotificationsEnabled(getState())) return;
2018-10-19 22:38:07 +02:00
const notif = new window.Notification('LBRY Download Complete', {
body: fileInfo.metadata.title,
silent: false,
});
2019-03-05 05:46:57 +01:00
// @if TARGET='app'
notif.onclick = () => {
ipcRenderer.send('focusWindow', 'main');
};
2019-03-05 05:46:57 +01:00
// @ENDIF
}
} else {
// ready to play
const { total_bytes: totalBytes, written_bytes: writtenBytes } = fileInfo;
const progress = (writtenBytes / totalBytes) * 100;
2017-06-06 23:19:12 +02:00
dispatch({
type: ACTIONS.DOWNLOADING_PROGRESSED,
data: {
uri,
outpoint,
fileInfo,
progress,
},
});
setNextStatusUpdate();
}
});
2017-06-06 23:19:12 +02:00
};
2019-03-13 06:59:07 +01:00
// @endif
}
2019-03-18 06:09:50 +01:00
export function doStartDownload(uri: string, outpoint: string) {
return (dispatch: Dispatch, getState: GetState) => {
2017-06-06 23:19:12 +02:00
const state = getState();
2017-08-08 11:36:14 +02:00
if (!outpoint) {
throw new Error('outpoint is required to begin a download');
2017-08-08 11:36:14 +02:00
}
2017-07-30 21:20:36 +02:00
const { downloadingByOutpoint = {} } = state.fileInfo;
if (downloadingByOutpoint[outpoint]) return;
Lbry.file_list({ outpoint, full_status: true }).then(([fileInfo]) => {
dispatch({
type: ACTIONS.DOWNLOADING_STARTED,
data: {
uri,
outpoint,
fileInfo,
},
2017-06-06 23:19:12 +02:00
});
dispatch(doUpdateLoadStatus(uri, outpoint));
});
};
}
2019-03-18 06:09:50 +01:00
export function doDownloadFile(uri: string, streamInfo: { outpoint: string }) {
return (dispatch: Dispatch) => {
dispatch(doStartDownload(uri, streamInfo.outpoint));
2017-06-06 23:19:12 +02:00
};
}
2019-03-18 06:09:50 +01:00
export function doSetPlayingUri(uri: ?string) {
return (dispatch: Dispatch) => {
dispatch({
type: ACTIONS.SET_PLAYING_URI,
data: { uri },
});
};
}
2019-03-18 06:09:50 +01:00
function handleLoadVideoError(uri: string, errorType: string = '') {
return (dispatch: Dispatch, getState: GetState) => {
// suppress error when another media is playing
const { playingUri } = getState().content;
const errorText = typeof errorType === 'object' ? errorType.message : errorType;
if (playingUri && playingUri === uri) {
dispatch({
type: ACTIONS.LOADING_VIDEO_FAILED,
data: { uri },
});
dispatch(doSetPlayingUri(null));
// this is not working, but should be it's own separate modal in the future (https://github.com/lbryio/lbry-desktop/issues/892)
if (errorType === 'timeout') {
doOpenModal(MODALS.FILE_TIMEOUT, { uri });
} else {
dispatch(
doError(
2019-03-20 22:43:00 +01:00
`Failed to download ${uri}, please try again or see error details:\n\n${errorText}\n\nIf this problem persists, visit https://lbry.com/support for help. `
)
);
}
}
};
}
2019-03-18 06:09:50 +01:00
export function doLoadVideo(uri: string, shouldRecordViewEvent: boolean = false) {
return (dispatch: Dispatch) => {
dispatch({
type: ACTIONS.LOADING_VIDEO_STARTED,
data: {
2017-06-06 23:19:12 +02:00
uri,
},
});
Lbry.get({ uri })
2017-08-08 11:36:14 +02:00
.then(streamInfo => {
// need error code from SDK to capture properly
2017-08-08 11:36:14 +02:00
const timeout =
streamInfo === null || typeof streamInfo !== 'object' || streamInfo.error === 'Timeout';
2017-08-08 11:36:14 +02:00
if (timeout) {
dispatch(handleLoadVideoError(uri, 'timeout'));
2017-08-08 11:36:14 +02:00
} else {
dispatch(doDownloadFile(uri, streamInfo));
if (shouldRecordViewEvent) {
analytics.apiLogView(
`${streamInfo.claim_name}#${streamInfo.claim_id}`,
streamInfo.outpoint,
streamInfo.claim_id
);
}
2017-08-08 11:36:14 +02:00
}
})
.catch(error => {
dispatch(handleLoadVideoError(uri, error));
});
};
}
2019-03-18 06:09:50 +01:00
export function doPurchaseUri(
uri: string,
specificCostInfo?: ?{},
shouldRecordViewEvent?: boolean = false
) {
return (dispatch: Dispatch, getState: GetState) => {
2017-06-06 23:19:12 +02:00
const state = getState();
const balance = selectBalance(state);
2017-09-08 05:15:05 +02:00
const fileInfo = makeSelectFileInfoForUri(uri)(state);
2017-07-21 10:02:29 +02:00
const downloadingByOutpoint = selectDownloadingByOutpoint(state);
const alreadyDownloading = fileInfo && !!downloadingByOutpoint[fileInfo.outpoint];
function attemptPlay(cost, instantPurchaseMax = null) {
2019-02-18 18:33:02 +01:00
// If you have a file entry with correct manifest, you won't pay for the key fee again
if (cost > 0 && (!instantPurchaseMax || cost > instantPurchaseMax) && !fileInfo) {
dispatch(doOpenModal(MODALS.AFFIRM_PURCHASE, { uri }));
} else {
dispatch(doLoadVideo(uri, shouldRecordViewEvent));
}
}
// we already fully downloaded the file.
if (fileInfo && fileInfo.completed) {
2019-02-18 18:33:02 +01:00
// If path is null or bytes written is 0 means the user has deleted/moved the
// file manually on their file system, so we need to dispatch a
// doLoadVideo action to reconstruct the file from the blobs
2019-03-18 06:09:50 +01:00
if (!fileInfo.download_path || !fileInfo.written_bytes) {
2019-02-18 18:33:02 +01:00
dispatch(doLoadVideo(uri, shouldRecordViewEvent));
2019-03-18 06:09:50 +01:00
}
Promise.resolve();
return;
}
// we are already downloading the file
if (alreadyDownloading) {
Promise.resolve();
return;
}
const costInfo = makeSelectCostInfoForUri(uri)(state) || specificCostInfo;
const { cost } = costInfo;
2017-04-27 09:05:41 +02:00
if (cost > balance) {
dispatch(doSetPlayingUri(null));
Promise.resolve();
return;
}
if (cost === 0 || !makeSelectClientSetting(SETTINGS.INSTANT_PURCHASE_ENABLED)(state)) {
attemptPlay(cost);
} else {
const instantPurchaseMax = makeSelectClientSetting(SETTINGS.INSTANT_PURCHASE_MAX)(state);
if (instantPurchaseMax.currency === 'LBC') {
attemptPlay(cost, instantPurchaseMax.amount);
} else {
// Need to convert currency of instant purchase maximum before trying to play
2018-04-18 06:03:01 +02:00
Lbryapi.getExchangeRates().then(({ LBC_USD }) => {
attemptPlay(cost, instantPurchaseMax.amount / LBC_USD);
});
}
}
2017-06-06 23:19:12 +02:00
};
}
2017-05-13 00:50:51 +02:00
export function doFetchClaimsByChannel(
uri: string,
page: number = 1,
pageSize: number = PAGE_SIZE
) {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
2017-05-13 00:50:51 +02:00
dispatch({
type: ACTIONS.FETCH_CHANNEL_CLAIMS_STARTED,
2017-07-17 08:06:04 +02:00
data: { uri, page },
2017-06-06 23:19:12 +02:00
});
2017-05-13 00:50:51 +02:00
Lbry.claim_list_by_channel({ uri, page, page_size: pageSize }).then(result => {
const claimResult = result[uri] || {};
const { claims_in_channel: claimsInChannel, returned_page: returnedPage } = claimResult;
2018-11-03 03:17:55 +01:00
if (claimsInChannel && claimsInChannel.length) {
if (page === 1) {
2018-11-03 03:17:55 +01:00
const latest = claimsInChannel[0];
dispatch(
setSubscriptionLatest(
{
channelName: latest.channel_name,
uri: buildURI(
{
contentName: latest.channel_name,
claimId: latest.value.publisherSignature.certificateId,
},
false
),
},
buildURI({ contentName: latest.name, claimId: latest.claim_id }, false)
)
);
}
}
dispatch({
type: ACTIONS.FETCH_CHANNEL_CLAIMS_COMPLETED,
data: {
uri,
claims: claimsInChannel || [],
page: returnedPage || undefined,
},
});
});
2017-06-06 23:19:12 +02:00
};
}
2019-03-18 06:09:50 +01:00
export function doPlayUri(uri: string) {
return (dispatch: Dispatch) => {
dispatch(doSetPlayingUri(uri));
2019-03-15 17:15:31 +01:00
// @if TARGET='app'
dispatch(doPurchaseUri(uri));
2019-03-15 17:15:31 +01:00
// @endif
};
}
2017-07-10 16:49:12 +02:00
export function doFetchChannelListMine() {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
2017-07-10 16:49:12 +02:00
dispatch({
2018-02-21 06:41:30 +01:00
type: ACTIONS.FETCH_CHANNEL_LIST_STARTED,
2017-07-10 16:49:12 +02:00
});
const callback = channels => {
dispatch({
2018-02-21 06:41:30 +01:00
type: ACTIONS.FETCH_CHANNEL_LIST_COMPLETED,
2017-07-10 16:49:12 +02:00
data: { claims: channels },
});
};
2018-02-21 06:41:30 +01:00
Lbry.channel_list().then(callback);
2017-07-10 16:49:12 +02:00
};
}
2017-06-17 19:59:18 +02:00
2018-10-31 18:11:32 +01:00
export function doCreateChannel(name: string, amount: number) {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
2017-06-17 19:59:18 +02:00
dispatch({
type: ACTIONS.CREATE_CHANNEL_STARTED,
2017-06-17 19:59:18 +02:00
});
2019-03-18 06:09:50 +01:00
return new Promise<void>((resolve, reject) => {
Lbry.channel_new({
channel_name: name,
2018-10-31 18:11:32 +01:00
amount: creditsToString(amount),
}).then(
2018-01-02 20:54:57 +01:00
newChannelClaim => {
const channelClaim = newChannelClaim;
channelClaim.name = name;
dispatch({
type: ACTIONS.CREATE_CHANNEL_COMPLETED,
2018-01-02 20:54:57 +01:00
data: { channelClaim },
});
2018-01-02 20:54:57 +01:00
resolve(channelClaim);
},
error => {
reject(error);
}
);
2017-06-17 19:59:18 +02:00
});
};
}
export function savePosition(claimId: string, outpoint: string, position: number) {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
dispatch({
type: ACTIONS.SET_CONTENT_POSITION,
data: { claimId, outpoint, position },
});
};
}
export function doSetContentHistoryItem(uri: string) {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
dispatch({
type: ACTIONS.SET_CONTENT_LAST_VIEWED,
data: { uri, lastViewed: Date.now() },
});
};
}
export function doClearContentHistoryUri(uri: string) {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
dispatch({
type: ACTIONS.CLEAR_CONTENT_HISTORY_URI,
data: { uri },
});
};
}
2018-08-01 16:06:43 +02:00
export function doClearContentHistoryAll() {
2019-03-18 06:09:50 +01:00
return (dispatch: Dispatch) => {
dispatch({ type: ACTIONS.CLEAR_CONTENT_HISTORY_ALL });
};
}
2018-08-01 16:06:43 +02:00
2019-03-18 06:09:50 +01:00
export function doSetHistoryPage(page: string) {
return (dispatch: Dispatch) => {
2018-08-01 16:06:43 +02:00
dispatch({
type: ACTIONS.SET_CONTENT_HISTORY_PAGE,
data: { page },
});
};
}