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-26 19:08:26 +02:00
|
|
|
import {
|
|
|
|
selectBalance,
|
|
|
|
} from 'selectors/wallet'
|
2017-04-28 17:14:44 +02:00
|
|
|
import {
|
2017-05-15 05:50:59 +02:00
|
|
|
selectFileInfoForUri,
|
2017-05-19 01:14:26 +02:00
|
|
|
selectUrisDownloading,
|
2017-04-28 17:14:44 +02:00
|
|
|
} from 'selectors/file_info'
|
2017-05-15 18:34:33 +02:00
|
|
|
import {
|
|
|
|
selectResolvingUris
|
|
|
|
} from 'selectors/content'
|
2017-04-28 17:14:44 +02:00
|
|
|
import {
|
2017-05-15 05:50:59 +02:00
|
|
|
selectCostInfoForUri,
|
2017-04-28 17:14:44 +02:00
|
|
|
} from 'selectors/cost_info'
|
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-23 11:56:50 +02:00
|
|
|
|
2017-04-26 19:08:26 +02:00
|
|
|
export function doResolveUri(uri) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
|
2017-05-21 16:42:34 +02:00
|
|
|
uri = lbryuri.normalize(uri)
|
|
|
|
|
2017-05-15 18:34:33 +02:00
|
|
|
const state = getState()
|
|
|
|
const alreadyResolving = selectResolvingUris(state).indexOf(uri) !== -1
|
2017-04-26 19:08:26 +02:00
|
|
|
|
2017-05-15 18:34:33 +02:00
|
|
|
if (!alreadyResolving) {
|
2017-04-26 19:08:26 +02:00
|
|
|
dispatch({
|
2017-05-15 18:34:33 +02:00
|
|
|
type: types.RESOLVE_URI_STARTED,
|
|
|
|
data: { uri }
|
|
|
|
})
|
|
|
|
|
|
|
|
lbry.resolve({ uri }).then((resolutionInfo) => {
|
|
|
|
const {
|
2017-04-26 19:08:26 +02:00
|
|
|
claim,
|
|
|
|
certificate,
|
2017-05-15 18:34:33 +02:00
|
|
|
} = resolutionInfo ? resolutionInfo : { claim : null, certificate: null }
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: types.RESOLVE_URI_COMPLETED,
|
|
|
|
data: {
|
|
|
|
uri,
|
|
|
|
claim,
|
|
|
|
certificate,
|
|
|
|
}
|
|
|
|
})
|
2017-04-26 19:08:26 +02:00
|
|
|
})
|
2017-05-15 18:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doCancelResolveUri(uri) {
|
|
|
|
return function(dispatch, getState) {
|
|
|
|
lbry.cancelResolve({ uri })
|
2017-04-23 18:10:45 +02:00
|
|
|
dispatch({
|
2017-05-18 19:58:28 +02:00
|
|
|
type: types.RESOLVE_URI_CANCELED,
|
|
|
|
data: { uri }
|
2017-04-23 18:10:45 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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: {
|
2017-05-19 01:14:26 +02:00
|
|
|
categories: Categories,
|
|
|
|
uris: featuredUris,
|
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
|
2017-04-26 19:08:26 +02:00
|
|
|
dispatch({
|
|
|
|
type: types.DOWNLOADING_COMPLETED,
|
|
|
|
data: {
|
|
|
|
uri,
|
2017-05-18 19:58:28 +02:00
|
|
|
outpoint,
|
2017-04-26 19:08:26 +02:00
|
|
|
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,
|
2017-05-18 19:58:28 +02:00
|
|
|
outpoint,
|
2017-04-26 19:08:26 +02:00
|
|
|
fileInfo,
|
|
|
|
progress,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setTimeout(() => { dispatch(doUpdateLoadStatus(uri, outpoint)) }, 250)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2017-05-18 19:58:28 +02:00
|
|
|
outpoint: streamInfo.outpoint,
|
2017-04-27 17:10:39 +02:00
|
|
|
fileInfo,
|
|
|
|
}
|
|
|
|
})
|
2017-05-31 09:31:08 +02:00
|
|
|
|
|
|
|
dispatch(doUpdateLoadStatus(uri, streamInfo.outpoint))
|
2017-04-26 19:08:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
lbryio.call('file', 'view', {
|
|
|
|
uri: uri,
|
|
|
|
outpoint: streamInfo.outpoint,
|
2017-05-26 20:19:41 +02:00
|
|
|
claim_id: streamInfo.claim_id,
|
2017-04-26 19:08:26 +02:00
|
|
|
}).catch(() => {})
|
2017-05-26 17:58:56 +02:00
|
|
|
|
|
|
|
rewards.claimEligiblePurchaseRewards()
|
2017-05-26 20:19:41 +02:00
|
|
|
|
2017-04-26 19:08:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 05:50:59 +02:00
|
|
|
export function doLoadVideo(uri) {
|
2017-04-26 19:08:26 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 19:40:46 +02:00
|
|
|
export function doPurchaseUri(uri, purchaseModalName) {
|
2017-04-26 19:08:26 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
const state = getState()
|
|
|
|
const balance = selectBalance(state)
|
2017-05-15 05:50:59 +02:00
|
|
|
const fileInfo = selectFileInfoForUri(state, { uri })
|
2017-05-19 01:14:26 +02:00
|
|
|
const downloadingByUri = selectUrisDownloading(state)
|
2017-05-01 06:51:19 +02:00
|
|
|
const alreadyDownloading = !!downloadingByUri[uri]
|
2017-04-26 19:08:26 +02:00
|
|
|
|
2017-05-26 20:36:18 +02:00
|
|
|
// we already fully downloaded the file.
|
|
|
|
if (fileInfo && fileInfo.completed) {
|
|
|
|
// If written_bytes is false that 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
|
|
|
|
if (!fileInfo.written_bytes) dispatch(doLoadVideo(uri))
|
|
|
|
|
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-05-26 20:36:18 +02:00
|
|
|
const costInfo = selectCostInfoForUri(state, { uri })
|
|
|
|
const { cost } = costInfo
|
|
|
|
|
2017-04-28 17:14:44 +02:00
|
|
|
// the file is free or we have partially downloaded it
|
2017-05-15 05:50:59 +02:00
|
|
|
if (cost <= 0.01 || (fileInfo && fileInfo.download_directory)) {
|
|
|
|
dispatch(doLoadVideo(uri))
|
2017-04-27 17:10:39 +02:00
|
|
|
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-05-25 19:40:46 +02:00
|
|
|
dispatch(doOpenModal(purchaseModalName))
|
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
|
|
|
}
|
|
|
|
}
|
2017-05-13 00:50:51 +02:00
|
|
|
|
2017-05-15 05:50:59 +02:00
|
|
|
export function doFetchClaimsByChannel(uri) {
|
2017-05-13 00:50:51 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
dispatch({
|
|
|
|
type: types.FETCH_CHANNEL_CLAIMS_STARTED,
|
|
|
|
data: { uri }
|
|
|
|
})
|
|
|
|
|
|
|
|
lbry.resolve({ uri }).then((resolutionInfo) => {
|
|
|
|
const {
|
|
|
|
claims_in_channel,
|
|
|
|
} = resolutionInfo ? resolutionInfo : { claims_in_channel: [] }
|
|
|
|
|
|
|
|
dispatch({
|
2017-05-19 01:14:26 +02:00
|
|
|
type: types.FETCH_CHANNEL_CLAIMS_COMPLETED,
|
2017-05-13 00:50:51 +02:00
|
|
|
data: {
|
|
|
|
uri,
|
|
|
|
claims: claims_in_channel
|
|
|
|
}
|
|
|
|
})
|
2017-05-19 01:14:26 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 15:01:11 +02:00
|
|
|
export function doFetchClaimListMine() {
|
2017-05-19 01:14:26 +02:00
|
|
|
return function(dispatch, getState) {
|
|
|
|
dispatch({
|
2017-05-28 15:01:11 +02:00
|
|
|
type: types.FETCH_CLAIM_LIST_MINE_STARTED
|
2017-05-19 01:14:26 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
lbry.claim_list_mine().then((claims) => {
|
2017-05-13 00:50:51 +02:00
|
|
|
dispatch({
|
2017-05-28 15:01:11 +02:00
|
|
|
type: types.FETCH_CLAIM_LIST_MINE_COMPLETED,
|
2017-05-13 00:50:51 +02:00
|
|
|
data: {
|
2017-05-19 01:14:26 +02:00
|
|
|
claims
|
2017-05-13 00:50:51 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|