2017-10-31 18:05:15 +01:00
|
|
|
const logger = require('winston');
|
2017-11-04 01:10:08 +01:00
|
|
|
const { returnShortId } = require('../helpers/sequelizeHelpers.js');
|
2017-12-16 18:55:29 +01:00
|
|
|
const { claim, site } = require('../config/speechConfig.js');
|
|
|
|
const { defaultTitle, defaultThumbnail, defaultDescription } = claim;
|
2017-12-14 21:32:20 +01:00
|
|
|
const { host } = site;
|
2017-12-06 20:53:31 +01:00
|
|
|
|
|
|
|
function determineFileExtensionFromContentType (contentType) {
|
|
|
|
switch (contentType) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/jpg':
|
2017-12-14 00:32:58 +01:00
|
|
|
return 'jpeg';
|
2017-12-06 20:53:31 +01:00
|
|
|
case 'image/png':
|
|
|
|
return 'png';
|
|
|
|
case 'image/gif':
|
|
|
|
return 'gif';
|
|
|
|
case 'video/mp4':
|
|
|
|
return 'mp4';
|
|
|
|
default:
|
2017-12-14 00:32:58 +01:00
|
|
|
logger.debug('setting unknown file type as file extension jpeg');
|
|
|
|
return 'jpeg';
|
2017-12-06 20:53:31 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-06 23:47:50 +01:00
|
|
|
function determineContentTypeFromFileExtension (fileExtension) {
|
|
|
|
switch (fileExtension) {
|
|
|
|
case 'jpeg':
|
|
|
|
case 'jpg':
|
2017-12-14 00:32:58 +01:00
|
|
|
return 'image/jpeg';
|
2017-12-06 23:47:50 +01:00
|
|
|
case 'png':
|
|
|
|
return 'image/png';
|
|
|
|
case 'gif':
|
|
|
|
return 'image/gif';
|
|
|
|
case 'mp4':
|
|
|
|
return 'video/mp4';
|
|
|
|
default:
|
2017-12-14 00:32:58 +01:00
|
|
|
logger.debug('setting unknown file type as type image/jpeg');
|
|
|
|
return 'image/jpeg';
|
2017-12-06 23:47:50 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-06 23:41:03 +01:00
|
|
|
function ifEmptyReturnOther (value, replacement) {
|
|
|
|
if (value === '') {
|
|
|
|
return replacement;
|
2017-12-06 20:53:31 +01:00
|
|
|
}
|
2017-12-06 23:41:03 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function determineThumbnail (storedThumbnail, defaultThumbnail) {
|
|
|
|
return ifEmptyReturnOther(storedThumbnail, defaultThumbnail);
|
|
|
|
};
|
|
|
|
|
|
|
|
function determineOgTitle (storedTitle, defaultTitle) {
|
|
|
|
return ifEmptyReturnOther(storedTitle, defaultTitle);
|
|
|
|
};
|
|
|
|
|
|
|
|
function determineOgDescription (storedDescription, defaultDescription) {
|
|
|
|
return ifEmptyReturnOther(storedDescription, defaultDescription);
|
2017-12-06 20:53:31 +01:00
|
|
|
};
|
2017-10-31 18:05:15 +01:00
|
|
|
|
2017-12-06 23:47:50 +01:00
|
|
|
function determineOgThumbnailContentType (thumbnail) {
|
2017-12-07 23:29:17 +01:00
|
|
|
if (thumbnail) {
|
|
|
|
if (thumbnail.lastIndexOf('.') !== -1) {
|
|
|
|
return determineContentTypeFromFileExtension(thumbnail.substring(thumbnail.lastIndexOf('.')));
|
|
|
|
}
|
2017-12-06 23:47:50 +01:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:23:28 +01:00
|
|
|
function addOpengraphDataToClaim (claim) {
|
2017-12-14 21:32:20 +01:00
|
|
|
claim['host'] = host;
|
|
|
|
claim['embedUrl'] = `${host}/${claim.claimId}/${claim.name}`;
|
|
|
|
claim['showUrl'] = `${host}/${claim.claimId}/${claim.name}`;
|
|
|
|
claim['source'] = `${host}/${claim.claimId}/${claim.name}.${claim.fileExt}`;
|
|
|
|
claim['directFileUrl'] = `${host}/${claim.claimId}/${claim.name}.${claim.fileExt}`;
|
|
|
|
claim['ogTitle'] = determineOgTitle(claim.title, defaultTitle);
|
|
|
|
claim['ogDescription'] = determineOgDescription(claim.description, defaultDescription);
|
2017-12-06 23:47:50 +01:00
|
|
|
claim['ogThumbnailContentType'] = determineOgThumbnailContentType(claim.thumbnail);
|
2017-12-07 00:15:21 +01:00
|
|
|
return claim;
|
2017-12-06 23:23:28 +01:00
|
|
|
};
|
|
|
|
|
2017-12-07 00:15:21 +01:00
|
|
|
function prepareClaimData (claim) {
|
|
|
|
// logger.debug('preparing claim data based on resolved data:', claim);
|
2017-12-14 21:32:20 +01:00
|
|
|
claim['thumbnail'] = determineThumbnail(claim.thumbnail, defaultThumbnail);
|
2017-12-07 00:15:21 +01:00
|
|
|
claim['fileExt'] = determineFileExtensionFromContentType(claim.contentType);
|
|
|
|
claim = addOpengraphDataToClaim(claim);
|
|
|
|
return claim;
|
2017-12-06 23:23:28 +01:00
|
|
|
};
|
|
|
|
|
2017-11-08 16:25:21 +01:00
|
|
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
|
2017-08-10 18:47:59 +02:00
|
|
|
const Claim = sequelize.define(
|
|
|
|
'Claim',
|
|
|
|
{
|
|
|
|
address: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
amount: {
|
2017-11-08 16:25:21 +01:00
|
|
|
type : DECIMAL(19, 8),
|
2017-08-15 01:16:32 +02:00
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
claimId: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
claimSequence: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
decodedClaim: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : BOOLEAN,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
depth: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
effectiveAmount: {
|
2017-11-08 16:25:21 +01:00
|
|
|
type : DECIMAL(19, 8),
|
2017-08-15 01:16:32 +02:00
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
hasSignature: {
|
|
|
|
type : BOOLEAN,
|
2017-08-15 01:16:32 +02:00
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
height: {
|
2017-11-08 00:21:42 +01:00
|
|
|
type : INTEGER,
|
2017-08-15 01:16:32 +02:00
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
hex: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : TEXT('long'),
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
name: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
nout: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
txid: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
validAtHeight: {
|
2017-11-08 00:21:42 +01:00
|
|
|
type : INTEGER,
|
2017-08-10 18:47:59 +02:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
outpoint: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
claimType: {
|
2017-08-15 01:16:32 +02:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
2017-08-15 22:48:42 +02:00
|
|
|
certificateId: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
2017-08-10 18:47:59 +02:00
|
|
|
author: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
description: {
|
2017-08-15 22:48:42 +02:00
|
|
|
type : TEXT('long'),
|
2017-08-10 18:47:59 +02:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
language: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-15 22:48:42 +02:00
|
|
|
},
|
|
|
|
license: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
licenseUrl: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
nsfw: {
|
|
|
|
type : BOOLEAN,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
preview: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
thumbnail: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
metadataVersion: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
contentType: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
source: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
sourceType: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
sourceVersion: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
streamVersion: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
valueVersion: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
2017-10-26 17:31:38 +02:00
|
|
|
channelName: {
|
|
|
|
type : STRING,
|
|
|
|
allowNull: true,
|
|
|
|
default : null,
|
|
|
|
},
|
2017-08-10 18:47:59 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-09-15 23:41:47 +02:00
|
|
|
Claim.associate = db => {
|
|
|
|
Claim.belongsTo(db.File, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-31 18:05:15 +01:00
|
|
|
Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) {
|
2017-11-27 19:22:47 +01:00
|
|
|
logger.debug(`Claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`);
|
2017-10-31 18:05:15 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
2017-10-31 19:54:33 +01:00
|
|
|
where: { name: claimName },
|
2017-10-31 18:05:15 +01:00
|
|
|
order: [['height', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2017-11-14 23:52:20 +01:00
|
|
|
throw new Error('No claim(s) found with that claim name');
|
2017-10-31 18:05:15 +01:00
|
|
|
default:
|
2017-11-04 01:10:08 +01:00
|
|
|
resolve(returnShortId(result, claimId));
|
2017-10-31 18:05:15 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-06 00:11:16 +01:00
|
|
|
Claim.getAllChannelClaims = function (channelClaimId) {
|
|
|
|
logger.debug(`Claim.getAllChannelClaims for ${channelClaimId}`);
|
2017-10-31 19:54:33 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
2017-12-06 00:11:16 +01:00
|
|
|
where: { certificateId: channelClaimId },
|
2017-10-31 19:54:33 +01:00
|
|
|
order: [['height', 'ASC']],
|
2017-12-07 18:28:44 +01:00
|
|
|
raw : true, // returns an array of only data, not an array of instances
|
2017-10-31 19:54:33 +01:00
|
|
|
})
|
2017-12-06 20:53:31 +01:00
|
|
|
.then(channelClaimsArray => {
|
2017-12-07 18:28:44 +01:00
|
|
|
// logger.debug('channelclaimsarray length:', channelClaimsArray.length);
|
2017-12-06 20:53:31 +01:00
|
|
|
switch (channelClaimsArray.length) {
|
2017-10-31 19:54:33 +01:00
|
|
|
case 0:
|
|
|
|
return resolve(null);
|
|
|
|
default:
|
2017-12-06 20:53:31 +01:00
|
|
|
channelClaimsArray.forEach(claim => {
|
|
|
|
claim['fileExt'] = determineFileExtensionFromContentType(claim.contentType);
|
2017-12-14 21:32:20 +01:00
|
|
|
claim['thumbnail'] = determineThumbnail(claim.thumbnail, defaultThumbnail);
|
2017-12-06 20:53:31 +01:00
|
|
|
return claim;
|
|
|
|
});
|
|
|
|
return resolve(channelClaimsArray);
|
2017-10-31 19:54:33 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-06 00:11:16 +01:00
|
|
|
Claim.getClaimIdByLongChannelId = function (channelClaimId, claimName) {
|
|
|
|
logger.debug(`finding claim id for claim ${claimName} from channel ${channelClaimId}`);
|
2017-10-31 23:28:11 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-11-01 00:00:12 +01:00
|
|
|
this
|
2017-10-31 23:28:11 +01:00
|
|
|
.findAll({
|
2017-12-06 00:11:16 +01:00
|
|
|
where: { name: claimName, certificateId: channelClaimId },
|
2017-10-31 23:28:11 +01:00
|
|
|
order: [['id', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2017-12-06 02:08:34 +01:00
|
|
|
return resolve(null);
|
2017-10-31 23:28:11 +01:00
|
|
|
case 1:
|
|
|
|
return resolve(result[0].claimId);
|
|
|
|
default:
|
|
|
|
logger.error(`${result.length} records found for ${claimName} from channel ${claimName}`);
|
|
|
|
return resolve(result[0].claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-01 00:00:12 +01:00
|
|
|
Claim.getLongClaimIdFromShortClaimId = function (name, shortId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: {
|
|
|
|
name,
|
|
|
|
claimId: {
|
2017-11-09 03:55:47 +01:00
|
|
|
$like: `${shortId}%`,
|
2017-11-01 00:00:12 +01:00
|
|
|
}},
|
|
|
|
order: [['height', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2017-12-06 02:08:34 +01:00
|
|
|
return resolve(null);
|
2017-11-01 00:00:12 +01:00
|
|
|
default: // note results must be sorted
|
|
|
|
return resolve(result[0].claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Claim.getTopFreeClaimIdByClaimName = function (name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: { name },
|
|
|
|
order: [['effectiveAmount', 'DESC'], ['height', 'ASC']], // note: maybe height and effective amount need to switch?
|
|
|
|
})
|
|
|
|
.then(result => {
|
2017-11-27 19:22:47 +01:00
|
|
|
logger.debug('length of result', result.length);
|
2017-11-01 00:00:12 +01:00
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2017-12-06 02:08:34 +01:00
|
|
|
return resolve(null);
|
2017-11-01 00:00:12 +01:00
|
|
|
default:
|
2017-11-27 19:22:47 +01:00
|
|
|
return resolve(result[0].dataValues.claimId);
|
2017-11-01 00:00:12 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-21 21:53:43 +01:00
|
|
|
Claim.validateLongClaimId = function (name, claimId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.findOne({
|
|
|
|
where: {name, claimId},
|
|
|
|
})
|
|
|
|
.then(result => {
|
2017-11-28 00:57:20 +01:00
|
|
|
if (!result) {
|
2017-12-06 02:08:34 +01:00
|
|
|
return resolve(null);
|
2017-11-28 00:57:20 +01:00
|
|
|
};
|
|
|
|
resolve(claimId);
|
2017-11-21 21:53:43 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-01 00:00:12 +01:00
|
|
|
Claim.getLongClaimId = function (claimName, claimId) {
|
|
|
|
logger.debug(`getLongClaimId(${claimName}, ${claimId})`);
|
|
|
|
if (claimId && (claimId.length === 40)) { // if a full claim id is provided
|
2017-11-21 21:53:43 +01:00
|
|
|
return this.validateLongClaimId(claimName, claimId);
|
2017-11-01 00:00:12 +01:00
|
|
|
} else if (claimId && claimId.length < 40) {
|
|
|
|
return this.getLongClaimIdFromShortClaimId(claimName, claimId); // if a short claim id is provided
|
|
|
|
} else {
|
|
|
|
return this.getTopFreeClaimIdByClaimName(claimName); // if no claim id is provided
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-01 00:44:32 +01:00
|
|
|
Claim.resolveClaim = function (name, claimId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: { name, claimId },
|
|
|
|
})
|
2017-12-06 23:23:28 +01:00
|
|
|
.then(claimArray => {
|
2017-12-07 00:15:21 +01:00
|
|
|
logger.debug('claims found on resolve:', claimArray.length);
|
2017-12-06 23:23:28 +01:00
|
|
|
switch (claimArray.length) {
|
|
|
|
case 0:
|
|
|
|
return resolve(null);
|
2017-11-01 00:44:32 +01:00
|
|
|
case 1:
|
2017-12-06 23:23:28 +01:00
|
|
|
return resolve(prepareClaimData(claimArray[0].dataValues));
|
2017-11-01 00:44:32 +01:00
|
|
|
default:
|
2017-12-06 23:23:28 +01:00
|
|
|
logger.error(`more than one entry matches that name (${name}) and claimID (${claimId})`);
|
|
|
|
return resolve(prepareClaimData(claimArray[0].dataValues));
|
2017-11-01 00:44:32 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-10 18:47:59 +02:00
|
|
|
return Claim;
|
|
|
|
};
|