2018-04-19 20:49:47 +02:00
|
|
|
import { Lbry, doNotify } from 'lbry-redux';
|
2017-12-21 18:32:51 +01:00
|
|
|
import Lbryio from 'lbryio';
|
2017-04-02 12:05:34 +02:00
|
|
|
|
2017-04-10 14:32:40 +02:00
|
|
|
function rewardMessage(type, amount) {
|
2017-06-08 23:15:34 +02:00
|
|
|
return {
|
2017-12-21 18:32:51 +01:00
|
|
|
new_developer: __('You earned %s for registering as a new developer.', amount),
|
|
|
|
new_user: __('You earned %s LBC new user reward.', amount),
|
2017-12-27 21:20:11 +01:00
|
|
|
verified_email: __('You earned %s LBC for verifying your email address.', amount),
|
2017-12-21 18:32:51 +01:00
|
|
|
new_channel: __('You earned %s LBC for creating a publisher identity.', amount),
|
|
|
|
first_stream: __('You earned %s LBC for streaming your first video.', amount),
|
|
|
|
many_downloads: __('You earned %s LBC for downloading a bunch of things.', amount),
|
|
|
|
first_publish: __('You earned %s LBC for making your first publication.', amount),
|
|
|
|
featured_download: __('You earned %s LBC for watching a featured download.', amount),
|
|
|
|
referral: __('You earned %s LBC for referring someone.', amount),
|
2018-03-20 21:38:50 +01:00
|
|
|
youtube_creator: __('You earned %s LBC for syncing your YouTube channel.', amount),
|
2017-06-08 23:15:34 +02:00
|
|
|
}[type];
|
2017-04-10 14:32:40 +02:00
|
|
|
}
|
2017-04-02 12:05:34 +02:00
|
|
|
|
|
|
|
const rewards = {};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
rewards.TYPE_NEW_DEVELOPER = 'new_developer';
|
|
|
|
rewards.TYPE_NEW_USER = 'new_user';
|
2017-12-27 21:20:11 +01:00
|
|
|
rewards.TYPE_CONFIRM_EMAIL = 'verified_email';
|
2017-12-21 18:32:51 +01:00
|
|
|
rewards.TYPE_FIRST_CHANNEL = 'new_channel';
|
|
|
|
rewards.TYPE_FIRST_STREAM = 'first_stream';
|
|
|
|
rewards.TYPE_MANY_DOWNLOADS = 'many_downloads';
|
|
|
|
rewards.TYPE_FIRST_PUBLISH = 'first_publish';
|
|
|
|
rewards.TYPE_FEATURED_DOWNLOAD = 'featured_download';
|
|
|
|
rewards.TYPE_REFERRAL = 'referral';
|
2018-03-20 21:38:50 +01:00
|
|
|
rewards.YOUTUBE_CREATOR = 'youtube_creator';
|
2017-08-26 05:21:26 +02:00
|
|
|
rewards.SORT_ORDER = [
|
|
|
|
rewards.TYPE_NEW_USER,
|
|
|
|
rewards.TYPE_CONFIRM_EMAIL,
|
|
|
|
rewards.TYPE_FIRST_STREAM,
|
|
|
|
rewards.TYPE_FIRST_CHANNEL,
|
|
|
|
rewards.TYPE_FIRST_PUBLISH,
|
|
|
|
rewards.TYPE_FEATURED_DOWNLOAD,
|
|
|
|
rewards.TYPE_MANY_DOWNLOADS,
|
|
|
|
rewards.TYPE_REFERRAL,
|
|
|
|
rewards.TYPE_NEW_DEVELOPER,
|
2018-03-22 16:43:35 +01:00
|
|
|
rewards.YOUTUBE_CREATOR,
|
2017-08-26 05:21:26 +02:00
|
|
|
];
|
2017-06-06 06:21:55 +02:00
|
|
|
|
2017-12-28 00:48:11 +01:00
|
|
|
rewards.claimReward = type => {
|
2017-06-08 23:15:34 +02:00
|
|
|
function requestReward(resolve, reject, params) {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (!Lbryio.enabled) {
|
|
|
|
reject(new Error(__('Rewards are not enabled.')));
|
2017-06-08 23:15:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbryio.call('reward', 'new', params, 'post').then(reward => {
|
2017-06-08 23:15:34 +02:00
|
|
|
const message = rewardMessage(type, reward.reward_amount);
|
|
|
|
|
|
|
|
// Display global notice
|
2018-04-19 20:49:47 +02:00
|
|
|
const action = doNotify({
|
2017-06-08 23:15:34 +02:00
|
|
|
message,
|
2017-12-21 18:32:51 +01:00
|
|
|
linkText: __('Show All'),
|
|
|
|
linkTarget: '/rewards',
|
2017-06-08 23:15:34 +02:00
|
|
|
isError: false,
|
2018-05-18 18:51:39 +02:00
|
|
|
displayType: ['snackbar'],
|
2017-06-08 23:15:34 +02:00
|
|
|
});
|
|
|
|
window.app.store.dispatch(action);
|
|
|
|
|
|
|
|
// Add more events here to display other places
|
|
|
|
|
|
|
|
resolve(reward);
|
|
|
|
}, reject);
|
2017-06-02 02:51:52 +02:00
|
|
|
}
|
2017-06-08 23:15:34 +02:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbry.wallet_unused_address().then(address => {
|
2017-06-08 23:15:34 +02:00
|
|
|
const params = {
|
|
|
|
reward_type: type,
|
|
|
|
wallet_address: address,
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case rewards.TYPE_FIRST_CHANNEL:
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbry.claim_list_mine()
|
2017-12-13 22:36:30 +01:00
|
|
|
.then(claims => {
|
|
|
|
const claim = claims
|
|
|
|
.reverse()
|
|
|
|
.find(
|
2017-12-21 18:32:51 +01:00
|
|
|
foundClaim =>
|
|
|
|
foundClaim.name.length &&
|
|
|
|
foundClaim.name[0] === '@' &&
|
|
|
|
foundClaim.txid.length &&
|
|
|
|
foundClaim.category === 'claim'
|
2017-06-08 23:15:34 +02:00
|
|
|
);
|
|
|
|
if (claim) {
|
|
|
|
params.transaction_id = claim.txid;
|
|
|
|
requestReward(resolve, reject, params);
|
|
|
|
} else {
|
2017-12-21 18:32:51 +01:00
|
|
|
reject(new Error(__('Please create a channel identity first.')));
|
2017-06-08 23:15:34 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case rewards.TYPE_FIRST_PUBLISH:
|
2017-12-21 18:32:51 +01:00
|
|
|
Lbry.claim_list_mine()
|
2017-06-08 23:15:34 +02:00
|
|
|
.then(claims => {
|
2017-12-13 22:36:30 +01:00
|
|
|
const claim = claims
|
|
|
|
.reverse()
|
|
|
|
.find(
|
2017-12-21 18:32:51 +01:00
|
|
|
foundClaim =>
|
|
|
|
foundClaim.name.length &&
|
|
|
|
foundClaim.name[0] !== '@' &&
|
|
|
|
foundClaim.txid.length &&
|
|
|
|
foundClaim.category === 'claim'
|
2017-06-08 23:15:34 +02:00
|
|
|
);
|
|
|
|
if (claim) {
|
|
|
|
params.transaction_id = claim.txid;
|
|
|
|
requestReward(resolve, reject, params);
|
|
|
|
} else {
|
|
|
|
reject(
|
|
|
|
claims.length
|
|
|
|
? new Error(
|
|
|
|
__(
|
2017-12-21 18:32:51 +01:00
|
|
|
'Please publish something and wait for confirmation by the network to claim this reward.'
|
2017-06-08 23:15:34 +02:00
|
|
|
)
|
|
|
|
)
|
2017-12-21 18:32:51 +01:00
|
|
|
: new Error(__('Please publish something to claim this reward.'))
|
2017-06-08 23:15:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case rewards.TYPE_FIRST_STREAM:
|
|
|
|
case rewards.TYPE_NEW_USER:
|
|
|
|
default:
|
|
|
|
requestReward(resolve, reject, params);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default rewards;
|