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

494 lines
12 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
import lbry from "lbry";
import lbryio from "lbryio";
import lbryuri from "lbryuri";
import { selectBalance } from "selectors/wallet";
2017-04-28 17:14:44 +02:00
import {
2017-09-08 05:15:05 +02:00
makeSelectFileInfoForUri,
2017-07-21 10:02:29 +02:00
selectDownloadingByOutpoint,
2017-06-06 23:19:12 +02:00
} from "selectors/file_info";
import { selectResolvingUris } from "selectors/content";
2017-09-08 05:15:05 +02:00
import { makeSelectCostInfoForUri } from "selectors/cost_info";
import { doAlertError, doOpenModal } from "actions/app";
2017-06-08 23:15:34 +02:00
import { doClaimEligiblePurchaseRewards } from "actions/rewards";
import { selectBadgeNumber } from "selectors/app";
import { selectTotalDownloadProgress } from "selectors/file_info";
import setBadge from "util/setBadge";
import setProgressBar from "util/setProgressBar";
2017-06-08 07:10:58 +02:00
import batchActions from "util/batchActions";
2017-07-15 20:44:50 +02:00
import * as modals from "constants/modal_types";
2017-04-23 11:56:50 +02:00
const { ipcRenderer } = require("electron");
const DOWNLOAD_POLL_INTERVAL = 250;
export function doResolveUri(uri) {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
uri = lbryuri.normalize(uri);
2017-06-06 23:19:12 +02:00
const state = getState();
const alreadyResolving = selectResolvingUris(state).indexOf(uri) !== -1;
2017-05-15 18:34:33 +02:00
if (!alreadyResolving) {
dispatch({
2017-05-15 18:34:33 +02:00
type: types.RESOLVE_URI_STARTED,
2017-06-06 23:19:12 +02:00
data: { uri },
});
2017-05-15 18:34:33 +02:00
2017-06-06 23:19:12 +02:00
lbry.resolve({ uri }).then(resolutionInfo => {
const { claim, certificate } = resolutionInfo
? resolutionInfo
: { claim: null, certificate: null };
2017-05-15 18:34:33 +02:00
dispatch({
type: types.RESOLVE_URI_COMPLETED,
data: {
uri,
claim,
certificate,
2017-06-06 23:19:12 +02:00
},
});
});
2017-05-15 18:34:33 +02:00
}
2017-06-06 23:19:12 +02:00
};
2017-05-15 18:34:33 +02:00
}
export function doCancelResolveUri(uri) {
return function(dispatch, getState) {
uri = lbryuri.normalize(uri);
const state = getState();
const alreadyResolving = selectResolvingUris(state).indexOf(uri) !== -1;
if (alreadyResolving) {
lbry.cancelResolve({ uri });
dispatch({
type: types.RESOLVE_URI_CANCELED,
data: {
uri,
},
});
}
};
}
export function doCancelAllResolvingUris() {
return function(dispatch, getState) {
const state = getState();
const resolvingUris = selectResolvingUris(state);
const actions = [];
resolvingUris.forEach(uri => actions.push(doCancelResolveUri(uri)));
dispatch(batchActions(...actions));
};
}
2017-05-04 05:44:08 +02:00
export function doFetchFeaturedUris() {
2017-04-23 11:56:50 +02:00
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
2017-04-23 11:56:50 +02:00
dispatch({
type: types.FETCH_FEATURED_CONTENT_STARTED,
2017-06-06 23:19:12 +02:00
});
2017-04-23 11:56:50 +02:00
const success = ({ Categories, Uris }) => {
2017-06-06 23:19:12 +02:00
let featuredUris = {};
2017-06-08 07:10:58 +02:00
const actions = [];
2017-05-04 05:44:08 +02:00
2017-06-06 23:19:12 +02:00
Categories.forEach(category => {
2017-05-04 05:44:08 +02:00
if (Uris[category] && Uris[category].length) {
2017-06-08 07:10:58 +02:00
const uris = Uris[category];
featuredUris[category] = uris;
uris.forEach(uri => {
actions.push(doResolveUri(uri));
});
2017-05-04 05:44:08 +02:00
}
2017-06-06 23:19:12 +02:00
});
2017-05-04 05:44:08 +02:00
2017-06-08 07:10:58 +02:00
actions.push({
2017-04-23 11:56:50 +02:00
type: types.FETCH_FEATURED_CONTENT_COMPLETED,
data: {
categories: Categories,
uris: featuredUris,
success: true,
2017-06-06 23:19:12 +02:00
},
});
2017-06-08 07:10:58 +02:00
dispatch(batchActions(...actions));
2017-06-06 23:19:12 +02:00
};
2017-04-23 11:56:50 +02:00
const failure = () => {
2017-05-04 05:44:08 +02:00
dispatch({
type: types.FETCH_FEATURED_CONTENT_COMPLETED,
data: {
categories: [],
2017-06-06 23:19:12 +02:00
uris: {},
},
});
};
lbryio
.call("discover", "list", { version: "early-access" })
.then(success, failure);
};
2017-04-23 11:56:50 +02:00
}
2017-07-30 00:56:08 +02:00
export function doFetchRewardedContent() {
return function(dispatch, getState) {
const state = getState();
const success = nameToClaimId => {
dispatch({
2017-07-30 00:56:08 +02:00
type: types.FETCH_REWARD_CONTENT_COMPLETED,
data: {
claimIds: Object.values(nameToClaimId),
success: true,
},
});
};
const failure = () => {
dispatch({
2017-07-30 00:56:08 +02:00
type: types.FETCH_REWARD_CONTENT_COMPLETED,
data: {
claimIds: [],
success: false,
},
});
};
lbryio.call("reward", "list_featured").then(success, failure);
};
}
export function doUpdateLoadStatus(uri, outpoint) {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
2017-06-06 23:19:12 +02:00
lbry
.file_list({
outpoint: outpoint,
full_status: true,
})
.then(([fileInfo]) => {
if (!fileInfo || fileInfo.written_bytes == 0) {
// download hasn't started yet
setTimeout(() => {
dispatch(doUpdateLoadStatus(uri, outpoint));
}, DOWNLOAD_POLL_INTERVAL);
2017-06-06 23:19:12 +02:00
} else if (fileInfo.completed) {
// TODO this isn't going to get called if they reload the client before
// the download finished
dispatch({
type: types.DOWNLOADING_COMPLETED,
data: {
uri,
outpoint,
fileInfo,
},
});
const badgeNumber = selectBadgeNumber(getState());
setBadge(badgeNumber === 0 ? "" : `${badgeNumber}`);
const totalProgress = selectTotalDownloadProgress(getState());
setProgressBar(totalProgress);
2017-06-26 19:16:36 +02:00
const notif = new window.Notification("LBRY Download Complete", {
body: fileInfo.metadata.stream.metadata.title,
silent: false,
});
notif.onclick = () => {
ipcRenderer.send("focusWindow", "main");
};
2017-06-06 23:19:12 +02:00
} else {
// ready to play
const { total_bytes, written_bytes } = fileInfo;
const progress = written_bytes / total_bytes * 100;
dispatch({
type: types.DOWNLOADING_PROGRESSED,
data: {
uri,
outpoint,
fileInfo,
progress,
},
});
const totalProgress = selectTotalDownloadProgress(getState());
setProgressBar(totalProgress);
2017-06-06 23:19:12 +02:00
setTimeout(() => {
dispatch(doUpdateLoadStatus(uri, outpoint));
}, DOWNLOAD_POLL_INTERVAL);
2017-06-06 23:19:12 +02:00
}
});
};
}
export function doStartDownload(uri, outpoint) {
return function(dispatch, 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-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: types.DOWNLOADING_STARTED,
data: {
uri,
outpoint,
fileInfo,
},
2017-06-06 23:19:12 +02:00
});
dispatch(doUpdateLoadStatus(uri, outpoint));
});
};
}
export function doDownloadFile(uri, streamInfo) {
return function(dispatch, getState) {
const state = getState();
dispatch(doStartDownload(uri, streamInfo.outpoint));
2017-06-06 23:19:12 +02:00
lbryio
.call("file", "view", {
uri: uri,
outpoint: streamInfo.outpoint,
claim_id: streamInfo.claim_id,
})
.catch(() => {});
2017-05-26 20:19:41 +02:00
2017-06-08 23:15:34 +02:00
dispatch(doClaimEligiblePurchaseRewards());
2017-06-06 23:19:12 +02:00
};
}
2017-05-15 05:50:59 +02:00
export function doLoadVideo(uri) {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
dispatch({
type: types.LOADING_VIDEO_STARTED,
data: {
2017-06-06 23:19:12 +02:00
uri,
},
});
2017-08-08 11:36:14 +02:00
lbry
.get({ uri })
.then(streamInfo => {
const timeout =
streamInfo === null ||
typeof streamInfo !== "object" ||
streamInfo.error == "Timeout";
if (timeout) {
dispatch({
type: types.LOADING_VIDEO_FAILED,
data: { uri },
});
2017-05-02 09:07:13 +02:00
2017-09-08 05:15:05 +02:00
dispatch(doOpenModal(modals.FILE_TIMEOUT, { uri }));
2017-08-08 11:36:14 +02:00
} else {
dispatch(doDownloadFile(uri, streamInfo));
}
})
.catch(error => {
dispatch({
type: types.LOADING_VIDEO_FAILED,
2017-06-06 23:19:12 +02:00
data: { uri },
});
2017-08-08 11:36:14 +02:00
dispatch(doAlertError(error));
2017-07-30 21:57:42 +02:00
});
2017-06-06 23:19:12 +02:00
};
}
2017-09-08 05:15:05 +02:00
export function doPurchaseUri(uri) {
return function(dispatch, 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];
2017-09-08 05:15:05 +02:00
const costInfo = makeSelectCostInfoForUri(uri)(state);
2017-06-06 23:19:12 +02:00
const { cost } = costInfo;
if (
alreadyDownloading ||
(fileInfo && fileInfo.completed && fileInfo.written_bytes > 0)
) {
return;
}
// we already fully downloaded the file.
if (
cost === 0 ||
(fileInfo && (fileInfo.completed || fileInfo.download_directory))
) {
return dispatch(doLoadVideo(uri));
}
2017-04-27 09:05:41 +02:00
if (cost > balance) {
return dispatch(doOpenModal(modals.INSUFFICIENT_CREDITS));
}
2017-04-27 09:05:41 +02:00
return dispatch(doOpenModal(modals.AFFIRM_PURCHASE, { uri }));
2017-06-06 23:19:12 +02:00
};
}
2017-05-13 00:50:51 +02:00
2017-07-17 08:06:04 +02:00
export function doFetchClaimsByChannel(uri, page) {
2017-05-13 00:50:51 +02:00
return function(dispatch, getState) {
dispatch({
type: types.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
2017-06-14 23:49:23 +02:00
lbry.claim_list_by_channel({ uri, page }).then(result => {
const claimResult = result[uri],
2017-07-17 08:06:04 +02:00
claims = claimResult ? claimResult.claims_in_channel : [],
currentPage = claimResult ? claimResult.returned_page : undefined;
2017-05-13 00:50:51 +02:00
dispatch({
type: types.FETCH_CHANNEL_CLAIMS_COMPLETED,
2017-05-13 00:50:51 +02:00
data: {
uri,
2017-07-17 08:06:04 +02:00
claims,
page: currentPage,
2017-06-06 23:19:12 +02:00
},
});
});
};
}
2017-08-24 23:12:23 +02:00
export function doFetchClaimCountByChannel(uri) {
return function(dispatch, getState) {
dispatch({
type: types.FETCH_CHANNEL_CLAIM_COUNT_STARTED,
data: { uri },
});
lbry.claim_list_by_channel({ uri }).then(result => {
const claimResult = result[uri],
totalClaims = claimResult ? claimResult.claims_in_channel : 0;
dispatch({
type: types.FETCH_CHANNEL_CLAIM_COUNT_COMPLETED,
data: {
uri,
totalClaims,
},
});
});
};
}
export function doFetchClaimListMine() {
return function(dispatch, getState) {
dispatch({
2017-06-06 23:19:12 +02:00
type: types.FETCH_CLAIM_LIST_MINE_STARTED,
});
2017-06-06 23:19:12 +02:00
lbry.claim_list_mine().then(claims => {
2017-05-13 00:50:51 +02:00
dispatch({
type: types.FETCH_CLAIM_LIST_MINE_COMPLETED,
2017-05-13 00:50:51 +02:00
data: {
2017-06-06 23:19:12 +02:00
claims,
},
});
});
};
2017-06-06 06:21:55 +02:00
}
2017-07-10 16:49:12 +02:00
export function doPlayUri(uri) {
return function(dispatch, getState) {
dispatch(doSetPlayingUri(uri));
dispatch(doPurchaseUri(uri));
};
}
export function doSetPlayingUri(uri) {
return function(dispatch, getState) {
dispatch({
type: types.SET_PLAYING_URI,
data: { uri },
});
};
}
2017-07-10 16:49:12 +02:00
export function doFetchChannelListMine() {
return function(dispatch, getState) {
dispatch({
type: types.FETCH_CHANNEL_LIST_MINE_STARTED,
});
const callback = channels => {
dispatch({
type: types.FETCH_CHANNEL_LIST_MINE_COMPLETED,
data: { claims: channels },
});
};
lbry.channel_list_mine().then(callback);
};
}
2017-06-17 19:59:18 +02:00
export function doCreateChannel(name, amount) {
return function(dispatch, getState) {
dispatch({
type: types.CREATE_CHANNEL_STARTED,
});
return new Promise((resolve, reject) => {
lbry
.channel_new({
channel_name: name,
amount: parseFloat(amount),
})
.then(
channelClaim => {
channelClaim.name = name;
dispatch({
type: types.CREATE_CHANNEL_COMPLETED,
data: { channelClaim },
});
resolve(channelClaim);
},
err => {
reject(err);
2017-06-17 19:59:18 +02:00
}
);
});
};
}
export function doPublish(params) {
return function(dispatch, getState) {
return new Promise((resolve, reject) => {
const success = claim => {
resolve(claim);
if (claim === true) dispatch(doFetchClaimListMine());
else
setTimeout(() => dispatch(doFetchClaimListMine()), 20000, {
once: true,
});
2017-06-17 19:59:18 +02:00
};
const failure = err => reject(err);
2017-06-17 19:59:18 +02:00
lbry.publishDeprecated(params, null, success, failure);
2017-06-17 19:59:18 +02:00
});
};
}