lbry-desktop/ui/js/rewards.js

160 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-06-08 23:15:34 +02:00
import lbry from "lbry";
import lbryio from "lbryio";
2017-11-13 22:02:23 +01:00
import { doShowSnackBar } from "redux/actions/app";
2017-04-10 14:32:40 +02:00
function rewardMessage(type, amount) {
2017-06-08 23:15:34 +02:00
return {
new_developer: __(
"You earned %s for registering as a new developer.",
amount
),
new_user: __("You earned %s LBC new user reward.", amount),
confirm_email: __(
"You earned %s LBC for verifying your email address.",
amount
),
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 some of the things.",
amount
),
first_publish: __(
"You earned %s LBC for making your first publication.",
amount
),
2017-07-20 21:37:25 +02:00
featured_download: __(
"You earned %s LBC for watching a featured download.",
amount
),
2017-06-08 23:15:34 +02:00
}[type];
2017-04-10 14:32:40 +02:00
}
const rewards = {};
2017-06-23 12:49:23 +02:00
rewards.TYPE_NEW_DEVELOPER = "new_developer";
rewards.TYPE_NEW_USER = "new_user";
rewards.TYPE_CONFIRM_EMAIL = "confirm_email";
rewards.TYPE_FIRST_CHANNEL = "new_channel";
rewards.TYPE_FIRST_STREAM = "first_stream";
rewards.TYPE_MANY_DOWNLOADS = "many_downloads";
rewards.TYPE_FIRST_PUBLISH = "first_publish";
2017-06-08 23:15:34 +02:00
rewards.TYPE_FEATURED_DOWNLOAD = "featured_download";
2017-08-25 21:51:54 +02:00
rewards.TYPE_REFERRAL = "referral";
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,
];
2017-06-06 06:21:55 +02:00
rewards.claimReward = function(type) {
2017-06-08 23:15:34 +02:00
function requestReward(resolve, reject, params) {
2017-06-22 00:27:07 +02:00
if (!lbryio.enabled) {
2017-06-08 23:15:34 +02:00
reject(new Error(__("Rewards are not enabled.")));
return;
}
lbryio.call("reward", "new", params, "post").then(reward => {
const message = rewardMessage(type, reward.reward_amount);
// Display global notice
const action = doShowSnackBar({
message,
linkText: __("Show All"),
linkTarget: "/rewards",
isError: false,
});
window.app.store.dispatch(action);
// Add more events here to display other places
resolve(reward);
}, reject);
}
2017-06-08 23:15:34 +02:00
return new Promise((resolve, reject) => {
lbry.wallet_unused_address().then(address => {
const params = {
reward_type: type,
wallet_address: address,
};
switch (type) {
case rewards.TYPE_FIRST_CHANNEL:
lbry
.claim_list_mine()
.then(function(claims) {
let claim = claims.reverse().find(function(claim) {
return (
claim.name.length &&
claim.name[0] == "@" &&
claim.txid.length &&
claim.category == "claim"
2017-06-08 23:15:34 +02:00
);
});
if (claim) {
params.transaction_id = claim.txid;
requestReward(resolve, reject, params);
} else {
reject(
new Error(__("Please create a channel identity first."))
);
}
})
.catch(reject);
break;
case rewards.TYPE_FIRST_PUBLISH:
lbry
.claim_list_mine()
.then(claims => {
let claim = claims.reverse().find(function(claim) {
return (
claim.name.length &&
claim.name[0] != "@" &&
claim.txid.length &&
claim.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(
__(
"Please publish something and wait for confirmation by the network to claim this reward."
)
)
: new Error(
__("Please publish something to claim this reward.")
)
);
}
})
.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;