<commit_before>import * as types from 'constants/action_types'
import lbry from 'lbry'
import lbryio from 'lbryio'
import lbryuri from 'lbryuri'
import {
  selectCurrentUri,
} from 'selectors/app'
import {
  selectBalance,
} from 'selectors/wallet'
import {
  selectSearchTerm,
} from 'selectors/content'
import {
  selectCurrentUriFileInfo,
  selectDownloadingByUri,
} from 'selectors/file_info'
import {
  selectCurrentUriCostInfo,
} from 'selectors/cost_info'
import {
  selectCurrentResolvedUriClaimOutpoint,
} from 'selectors/content'
import {
  selectClaimsByUri,
} from 'selectors/claims'
import {
  doOpenModal,
} from 'actions/app'
import {
  doFetchCostInfoForUri,
} from 'actions/cost_info'
import batchActions from 'util/batchActions'

export function doResolveUri(uri) {
  return function(dispatch, getState) {
    dispatch({
      type: types.RESOLVE_URI_STARTED,
      data: { uri }
    })

    lbry.resolve({ uri }).then((resolutionInfo) => {
      const {
        claim,
        certificate,
      } = resolutionInfo

      dispatch({
        type: types.RESOLVE_URI_COMPLETED,
        data: {
          uri,
          claim,
          certificate,
        }
      })

      dispatch(doFetchCostInfoForUri(uri))
    })
  }
}

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);

        fileInfos.forEach(fileInfo => {
          const uri = lbryuri.build({
            channelName: fileInfo.channel_name,
            contentName: fileInfo.name,
          })
          const claim = selectClaimsByUri(state)[uri]
          if (!claim) dispatch(doResolveUri(uri))
        })

        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) => {
      dispatch({
        type: types.FETCH_MY_CLAIMS_COMPLETED,
        data: {
          claims: claimInfos,
        }
      })
      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)),
          }
        })
      })
    })
  }
}

export function doFetchFeaturedContent() {
  return function(dispatch, getState) {
    const state = getState()

    dispatch({
      type: types.FETCH_FEATURED_CONTENT_STARTED,
    })

    const success = ({ Categories, Uris }) => {
      dispatch({
        type: types.FETCH_FEATURED_CONTENT_COMPLETED,
        data: {
          categories: Categories,
          uris: Uris,
        }
      })

      Object.keys(Uris).forEach((category) => {
        Uris[category].forEach((uri) => {
          dispatch(doResolveUri(lbryuri.normalize(uri)))
        })
      })
    }

    const failure = () => {
    }

    lbryio.call('discover', 'list', { version: "early-access" } )
      .then(success, failure)
  }
}

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) {
        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)
      }
    })
  }
}
<commit_msg>Jam claim next purchase reward in<commit_after>import * as types from 'constants/action_types'
import lbry from 'lbry'
import lbryio from 'lbryio'
import lbryuri from 'lbryuri'
import rewards from 'rewards'
import {
  selectCurrentUri,
} from 'selectors/app'
import {
  selectBalance,
} from 'selectors/wallet'
import {
  selectSearchTerm,
} from 'selectors/content'
import {
  selectCurrentUriFileInfo,
  selectDownloadingByUri,
} from 'selectors/file_info'
import {
  selectCurrentUriCostInfo,
} from 'selectors/cost_info'
import {
  selectCurrentResolvedUriClaimOutpoint,
} from 'selectors/content'
import {
  selectClaimsByUri,
} from 'selectors/claims'
import {
  doOpenModal,
} from 'actions/app'
import {
  doFetchCostInfoForUri,
} from 'actions/cost_info'
import batchActions from 'util/batchActions'

export function doResolveUri(uri) {
  return function(dispatch, getState) {
    dispatch({
      type: types.RESOLVE_URI_STARTED,
      data: { uri }
    })

    lbry.resolve({ uri }).then((resolutionInfo) => {
      const {
        claim,
        certificate,
      } = resolutionInfo

      dispatch({
        type: types.RESOLVE_URI_COMPLETED,
        data: {
          uri,
          claim,
          certificate,
        }
      })

      dispatch(doFetchCostInfoForUri(uri))
    })
  }
}

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);

        fileInfos.forEach(fileInfo => {
          const uri = lbryuri.build({
            channelName: fileInfo.channel_name,
            contentName: fileInfo.name,
          })
          const claim = selectClaimsByUri(state)[uri]
          if (!claim) dispatch(doResolveUri(uri))
        })

        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) => {
      dispatch({
        type: types.FETCH_MY_CLAIMS_COMPLETED,
        data: {
          claims: claimInfos,
        }
      })
      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)),
          }
        })
      })
    })
  }
}

export function doFetchFeaturedContent() {
  return function(dispatch, getState) {
    const state = getState()

    dispatch({
      type: types.FETCH_FEATURED_CONTENT_STARTED,
    })

    const success = ({ Categories, Uris }) => {
      dispatch({
        type: types.FETCH_FEATURED_CONTENT_COMPLETED,
        data: {
          categories: Categories,
          uris: Uris,
        }
      })

      Object.keys(Uris).forEach((category) => {
        Uris[category].forEach((uri) => {
          dispatch(doResolveUri(lbryuri.normalize(uri)))
        })
      })
    }

    const failure = () => {
    }

    lbryio.call('discover', 'list', { version: "early-access" } )
      .then(success, failure)
  }
}

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) {
        // TODO this isn't going to get called if they reload the client before
        // the download finished
        rewards.claimNextPurchaseReward()
        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)
      }
    })
  }
}

{'fraction_non_alphanumeric': 0.11836691634138623, 'fraction_numerical': 0.0021099272075113407, 'mean_word_length': 2.307745987438939, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2194351', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2934, 'n_words': 532}