2017-04-23 11:56:50 +02:00
|
|
|
import * as types from 'constants/action_types'
|
|
|
|
import lbry from 'lbry'
|
2017-04-24 16:17:36 +02:00
|
|
|
import lbryio from 'lbryio'
|
2017-04-29 11:50:29 +02:00
|
|
|
import lbryuri from 'lbryuri'
|
2017-05-01 08:39:16 +02:00
|
|
|
import rewards from 'rewards'
|
2017-04-24 09:25:27 +02:00
|
|
|
import {
|
|
|
|
selectCurrentUri,
|
|
|
|
} from 'selectors/app'
|
2017-04-26 19:08:26 +02:00
|
|
|
import {
|
|
|
|
selectBalance,
|
|
|
|
} from 'selectors/wallet'
|
2017-04-24 16:17:36 +02:00
|
|
|
import {
|
|
|
|
selectSearchTerm,
|
|
|
|
} from 'selectors/content'
|
2017-04-28 17:14:44 +02:00
|
|
|
import {
|
|
|
|
selectCurrentUriFileInfo,
|
2017-05-01 06:51:19 +02:00
|
|
|
selectDownloadingByUri,
|
2017-04-28 17:14:44 +02:00
|
|
|
} from 'selectors/file_info'
|
|
|
|
import {
|
|
|
|
selectCurrentUriCostInfo,
|
|
|
|
} from 'selectors/cost_info'
|
2017-04-24 09:25:27 +02:00
|
|
|
import {
|
|
|
|
selectCurrentResolvedUriClaimOutpoint,
|
|
|
|
} from 'selectors/content'
|
2017-04-30 18:01:43 +02:00
|
|
|
import {
|
|
|
|
selectClaimsByUri,
|
|
|
|
} from 'selectors/claims'
|
2017-04-26 19:08:26 +02:00
|
|
|
import {
|
|
|
|
doOpenModal,
|
|
|
|
} from 'actions/app'
|
2017-04-29 19:02:25 +02:00
|
|
|
import {
|
|
|
|
doFetchCostInfoForUri,
|
|
|
|
} from 'actions/cost_info'
|
2017-04-26 19:08:26 +02:00
|
|
|
import batchActions from 'util/batchActions'
|
2017-04-23 11:56:50 +02:00
|
|
|
|
2017-04-26 19:08:26 +02:00
|
|
|
export function doResolveUri(uri) {
|
|
|
|
return function(dispatch, getState) {
|
2017-04-23 16:01:00 +02:00
|
|
|
dispatch({
|
2017-04-26 19:08:26 +02:00
|
|
|
type: types.RESOLVE_URI_STARTED,
|
|
|
|
data: { uri }
|
|
|
|
})
|
|
|
|
|
|
|
|
lbry.resolve({ uri }).then((resolutionInfo) => {
|
|
|
|
const {
|
2017-04-23 16:01:00 +02:00
|
|
|
claim,
|
|
|
|
certificate,
|
2017-04-26 19:08:26 +02:00
|
|
|
} = resolutionInfo
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.RESOLVE_URI_COMPLETED,
|
|
|
|
data: {
|
|
|
|
uri,
|
|
|
|
claim,
|
|
|
|
certificate,
|
|
|
|
}
|
|
|
|
})
|
2017-04-29 19:02:25 +02:00
|
|
|
|
|
|
|
dispatch(doFetchCostInfoForUri(uri))
|
2017-04-23 16:01:00 +02:00
|
|
|
})
|
2017-04-26 19:08:26 +02:00
|
|
|
}
|
2017-04-23 16:01:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 18:10:45 +02:00
|
|
|
export function doFetchDownloadedContent() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_DOWNLOADED_CONTENT_STARTED,
|
|
|
|
})
|
|
|
|
|
|
|
|
lbry.claim_list_mine().then((myClaimInfos) => {
|
|
|
|
lbry.file_list().then((fileInfos) => {
|
|
|
|
const myClaimOutpoints = myClaimInfos.map(({txid, nout}) => txid + ':' + nout);
|
|
|
|
|
2017-04-30 18:01:43 +02:00
|
|
|
fileInfos.forEach(fileInfo => {
|
|
|
|
const uri = lbryuri.build({
|
|
|
|
channelName: fileInfo.channel_name,
|
|
|
|
contentName: fileInfo.name,
|
|
|
|
})
|
|
|
|
const claim = selectClaimsByUri(state)[uri]
|
|
|
|
if (!claim) dispatch(doResolveUri(uri))
|
|
|
|
})
|
|
|
|
|
2017-04-23 18:10:45 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_DOWNLOADED_CONTENT_COMPLETED,
|
|
|
|
data: {
|
|
|
|
fileInfos: fileInfos.filter(({outpoint}) => !myClaimOutpoints.includes(outpoint)),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doFetchPublishedContent() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_PUBLISHED_CONTENT_STARTED,
|
|
|
|
})
|
|
|
|
|
|
|
|
lbry.claim_list_mine().then((claimInfos) => {
|
2017-05-01 08:26:09 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_MY_CLAIMS_COMPLETED,
|
|
|
|
data: {
|
|
|
|
claims: claimInfos,
|
|
|
|
}
|
|
|
|
})
|
2017-04-23 18:10:45 +02:00
|
|
|
lbry.file_list().then((fileInfos) => {
|
|
|
|
const myClaimOutpoints = claimInfos.map(({txid, nout}) => txid + ':' + nout)
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_PUBLISHED_CONTENT_COMPLETED,
|
|
|
|
data: {
|
|
|
|
fileInfos: fileInfos.filter(({outpoint}) => myClaimOutpoints.includes(outpoint)),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 05:44:08 +02:00
|
|
|
export function doFetchFeaturedUris() {
|
2017-04-23 11:56:50 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_FEATURED_CONTENT_STARTED,
|
|
|
|
})
|
|
|
|
|
|
|
|
const success = ({ Categories, Uris }) => {
|
2017-05-04 05:44:08 +02:00
|
|
|
|
|
|
|
let featuredUris = {}
|
|
|
|
|
|
|
|
Categories.forEach((category) => {
|
|
|
|
if (Uris[category] && Uris[category].length) {
|
|
|
|
featuredUris[category] = Uris[category]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-04-23 11:56:50 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_FEATURED_CONTENT_COMPLETED,
|
|
|
|
data: {
|
|
|
|
categories: Categories,
|
2017-05-04 05:44:08 +02:00
|
|
|
uris: featuredUris,
|
2017-04-23 11:56:50 +02:00
|
|
|
}
|
|
|
|
})
|
2017-04-23 16:01:00 +02:00
|
|
|
|
|
|
|
Object.keys(Uris).forEach((category) => {
|
2017-04-29 11:50:29 +02:00
|
|
|
Uris[category].forEach((uri) => {
|
|
|
|
dispatch(doResolveUri(lbryuri.normalize(uri)))
|
|
|
|
})
|
2017-04-23 16:01:00 +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: [],
|
|
|
|
uris: {}
|
|
|
|
}
|
|
|
|
})
|
2017-04-23 11:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lbryio.call('discover', 'list', { version: "early-access" } )
|
|
|
|
.then(success, failure)
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 09:25:27 +02:00
|
|
|
|
2017-04-26 19:08:26 +02:00
|
|
|
export function doUpdateLoadStatus(uri, outpoint) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
|
|
|
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)) }, 250)
|
|
|
|
} else if (fileInfo.completed) {
|
2017-05-01 08:39:16 +02:00
|
|
|
// TODO this isn't going to get called if they reload the client before
|
|
|
|
// the download finished
|
|
|
|
rewards.claimNextPurchaseReward()
|
2017-04-26 19:08:26 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.DOWNLOADING_COMPLETED,
|
|
|
|
data: {
|
|
|
|
uri,
|
|
|
|
fileInfo,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// ready to play
|
|
|
|
const {
|
|
|
|
total_bytes,
|
|
|
|
written_bytes,
|
|
|
|
} = fileInfo
|
|
|
|
const progress = (written_bytes / total_bytes) * 100
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.DOWNLOADING_PROGRESSED,
|
|
|
|
data: {
|
|
|
|
uri,
|
|
|
|
fileInfo,
|
|
|
|
progress,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setTimeout(() => { dispatch(doUpdateLoadStatus(uri, outpoint)) }, 250)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doPlayVideo(uri) {
|
|
|
|
return {
|
|
|
|
type: types.PLAY_VIDEO_STARTED,
|
|
|
|
data: { uri }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doDownloadFile(uri, streamInfo) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
2017-04-27 17:10:39 +02:00
|
|
|
lbry.file_list({ outpoint: streamInfo.outpoint, full_status: true }).then(([fileInfo]) => {
|
|
|
|
dispatch({
|
|
|
|
type: types.DOWNLOADING_STARTED,
|
|
|
|
data: {
|
|
|
|
uri,
|
|
|
|
fileInfo,
|
|
|
|
}
|
|
|
|
})
|
2017-04-26 19:08:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
lbryio.call('file', 'view', {
|
|
|
|
uri: uri,
|
|
|
|
outpoint: streamInfo.outpoint,
|
|
|
|
claimId: streamInfo.claim_id,
|
|
|
|
}).catch(() => {})
|
|
|
|
dispatch(doUpdateLoadStatus(uri, streamInfo.outpoint))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doLoadVideo() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
const uri = selectCurrentUri(state)
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.LOADING_VIDEO_STARTED,
|
|
|
|
data: {
|
|
|
|
uri
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
lbry.get({ uri }).then(streamInfo => {
|
2017-05-02 09:07:13 +02:00
|
|
|
const timeout = streamInfo === null ||
|
|
|
|
typeof streamInfo !== 'object' ||
|
|
|
|
streamInfo.error == 'Timeout'
|
|
|
|
|
|
|
|
if(timeout) {
|
2017-04-26 19:08:26 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.LOADING_VIDEO_FAILED,
|
|
|
|
data: { uri }
|
|
|
|
})
|
|
|
|
dispatch(doOpenModal('timedOut'))
|
|
|
|
} else {
|
|
|
|
dispatch(doDownloadFile(uri, streamInfo))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doWatchVideo() {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
const uri = selectCurrentUri(state)
|
|
|
|
const balance = selectBalance(state)
|
|
|
|
const fileInfo = selectCurrentUriFileInfo(state)
|
|
|
|
const costInfo = selectCurrentUriCostInfo(state)
|
2017-05-01 06:51:19 +02:00
|
|
|
const downloadingByUri = selectDownloadingByUri(state)
|
|
|
|
const alreadyDownloading = !!downloadingByUri[uri]
|
2017-04-26 19:08:26 +02:00
|
|
|
const { cost } = costInfo
|
|
|
|
|
2017-05-02 15:58:35 +02:00
|
|
|
// BUG if you delete a file from the file system system you're going to be
|
|
|
|
// asked to pay for it again. We need to check if the file is in the blobs
|
|
|
|
// here and then dispatch doLoadVideo() which will reconstruct it again from
|
|
|
|
// the blobs. Or perhaps there's another way to see if a file was already
|
|
|
|
// purchased?
|
|
|
|
|
2017-04-28 17:14:44 +02:00
|
|
|
// we already fully downloaded the file
|
|
|
|
if (fileInfo && fileInfo.completed) {
|
2017-04-27 09:05:41 +02:00
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2017-05-01 06:51:19 +02:00
|
|
|
// we are already downloading the file
|
|
|
|
if (alreadyDownloading) {
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:14:44 +02:00
|
|
|
// the file is free or we have partially downloaded it
|
2017-04-27 17:10:39 +02:00
|
|
|
if (cost <= 0.01 || fileInfo.download_directory) {
|
|
|
|
dispatch(doLoadVideo())
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
|
2017-04-27 09:05:41 +02:00
|
|
|
if (cost > balance) {
|
|
|
|
dispatch(doOpenModal('notEnoughCredits'))
|
2017-04-26 19:08:26 +02:00
|
|
|
} else {
|
2017-04-27 09:05:41 +02:00
|
|
|
dispatch(doOpenModal('affirmPurchase'))
|
2017-04-26 19:08:26 +02:00
|
|
|
}
|
2017-04-27 09:05:41 +02:00
|
|
|
|
|
|
|
return Promise.resolve()
|
2017-04-26 19:08:26 +02:00
|
|
|
}
|
|
|
|
}
|