even better handling of claims without value

This commit is contained in:
Alex Grintsvayg 2017-02-14 12:50:46 -05:00
parent a4cf012974
commit 53b3083bb1

View file

@ -78,10 +78,12 @@ function announceNewClaims() {
return setLastBlock(currentHeight);
}
if (lastProcessedBlock < currentHeight)
const testBlock = false;
if (testBlock || lastProcessedBlock < currentHeight)
{
const firstBlockToProcess = lastProcessedBlock + 1,
lastBlockToProcess = currentHeight;
const firstBlockToProcess = testBlock || lastProcessedBlock + 1,
lastBlockToProcess = testBlock || currentHeight;
// console.log('Doing blocks ' + firstBlockToProcess + ' to ' + lastBlockToProcess);
return announceClaimsLoop(firstBlockToProcess, lastBlockToProcess, currentHeight);
@ -130,20 +132,6 @@ function announceClaim(claim, claimBlockHeight, currentHeight) {
lbryCall('getclaimsforname', claim['name']),
])
.then(function ([currentWinningClaim, claimsForName]) {
let takeoverMessage = '';
if (!claim['is controlling'])
{
// the following is based on https://lbry.io/faq/claimtrie-implementation
const lastTakeoverHeight = claimsForName['nLastTakeoverHeight'],
maxDelay = 4032, // 7 days of blocks at 2.5min per block
activationDelay = Math.min(maxDelay, Math.floor((claimBlockHeight - lastTakeoverHeight) / 32)),
takeoverHeight = claimBlockHeight + activationDelay,
secondsPerBlock = 161, // in theory this should be 150, but in practice its closer to 161
takeoverTime = Date.now() + ((takeoverHeight - currentHeight) * secondsPerBlock * 1000);
takeoverMessage = 'Takes effect on approx. *' + moment(takeoverTime, 'x').format('MMMM Do [at] HH:mm [UTC]') + '* (block ' + takeoverHeight + ')';
}
let value;
try
{
@ -151,15 +139,12 @@ function announceClaim(claim, claimBlockHeight, currentHeight) {
}
catch (e)
{
slackPost('New claim for lbry://' + claim['name'] + ' ' + takeoverMessage, {icon_emoji: ':bellhop_bell:'});
return;
}
// console.log(claim);
// console.log(value);
const text = [];
if (value)
{
if (value['author'])
{
text.push(value['author']);
@ -185,26 +170,34 @@ function announceClaim(claim, claimBlockHeight, currentHeight) {
}
text.push(fees.join(', '));
}
if (takeoverMessage)
{
text.push(takeoverMessage);
}
if (!claim['is controlling'])
{
// the following is based on https://lbry.io/faq/claimtrie-implementation
const lastTakeoverHeight = claimsForName['nLastTakeoverHeight'],
maxDelay = 4032, // 7 days of blocks at 2.5min per block
activationDelay = Math.min(maxDelay, Math.floor((claimBlockHeight - lastTakeoverHeight) / 32)),
takeoverHeight = claimBlockHeight + activationDelay,
secondsPerBlock = 161, // in theory this should be 150, but in practice its closer to 161
takeoverTime = Date.now() + ((takeoverHeight - currentHeight) * secondsPerBlock * 1000);
const attachment = !value ? null : {
"fallback": "New claim for lbry://" + claim['name'] + ': "' + value['title'] + '" by ' + value['author'],
text.push('Takes effect on approx. *' + moment(takeoverTime, 'x').format('MMMM Do [at] HH:mm [UTC]') + '* (block ' + takeoverHeight + ')');
}
const attachment = {
"fallback": "New claim for lbry://" + claim['name'],
"color": "#155b4a",
// "pretext": "New claim in block " + claimBlockHeight,
"author_name": 'lbry://' + claim['name'],
"author_link": 'lbry://' + claim['name'],
// "author_name": 'lbry://' + claim['name'],
// "author_link": 'lbry://' + claim['name'],
// "author_icon": "http://flickr.com/icons/bobby.jpg",
"title": escapeSlackHtml(value['title']),
"title": "lbry://" + claim['name'], //escapeSlackHtml(value['title']),
"title_link": "lbry://" + claim['name'],
"text": escapeSlackHtml(text.join("\n")),
// "fields": [],
// "image_url": value['nsfw'] ? null : value['thumbnail'],
"thumb_url": value['nsfw'] ? null : value['thumbnail'],
// "thumb_url": (!value || value['nsfw']) ? null : value['thumbnail'],
"unfurl_links": false,
"unfurl_media": false,
"link_names": false,
@ -213,6 +206,18 @@ function announceClaim(claim, claimBlockHeight, currentHeight) {
"mrkdwn_in": ['text'],
};
if (value)
{
attachment['fallback'] += (': "' + value['title'] + '" by ' + value['author']);
attachment['author_name'] = 'lbry://' + claim['name'];
attachment['author_link'] = 'lbry://' + claim['name'];
attachment['title'] = escapeSlackHtml(value['title']);
if (!value['nsfw'])
{
attachment['thumb_url'] = value['thumbnail'];
}
}
slackPost('', {icon_emoji: ':bellhop_bell:', attachments: [attachment]});
});
}