2017-10-31 10:05:15 -07:00
|
|
|
const logger = require('winston');
|
2018-04-26 19:19:11 -07:00
|
|
|
const returnShortId = require('./utils/returnShortId.js');
|
2018-06-05 19:34:24 -07:00
|
|
|
const { assetDefaults: { thumbnail: defaultThumbnail }, details: { host } } = require('@config/siteConfig');
|
2017-12-06 11:53:31 -08:00
|
|
|
|
2018-04-29 12:17:23 -07:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
|
|
|
|
2017-12-06 11:53:31 -08:00
|
|
|
function determineFileExtensionFromContentType (contentType) {
|
|
|
|
switch (contentType) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/jpg':
|
2017-12-13 15:32:58 -08:00
|
|
|
return 'jpeg';
|
2017-12-06 11:53:31 -08:00
|
|
|
case 'image/png':
|
|
|
|
return 'png';
|
|
|
|
case 'image/gif':
|
|
|
|
return 'gif';
|
|
|
|
case 'video/mp4':
|
|
|
|
return 'mp4';
|
|
|
|
default:
|
2017-12-13 15:32:58 -08:00
|
|
|
logger.debug('setting unknown file type as file extension jpeg');
|
|
|
|
return 'jpeg';
|
2017-12-06 11:53:31 -08:00
|
|
|
}
|
2018-04-29 12:17:23 -07:00
|
|
|
}
|
2017-12-06 11:53:31 -08:00
|
|
|
|
2017-12-06 14:41:03 -08:00
|
|
|
function determineThumbnail (storedThumbnail, defaultThumbnail) {
|
2017-12-21 10:50:47 -08:00
|
|
|
if (storedThumbnail === '') {
|
|
|
|
return defaultThumbnail;
|
2017-12-06 14:47:50 -08:00
|
|
|
}
|
2017-12-21 10:50:47 -08:00
|
|
|
return storedThumbnail;
|
2018-04-29 12:17:23 -07:00
|
|
|
}
|
2017-12-06 14:23:28 -08:00
|
|
|
|
2017-12-06 15:15:21 -08:00
|
|
|
function prepareClaimData (claim) {
|
|
|
|
// logger.debug('preparing claim data based on resolved data:', claim);
|
2017-12-14 12:32:20 -08:00
|
|
|
claim['thumbnail'] = determineThumbnail(claim.thumbnail, defaultThumbnail);
|
2017-12-06 15:15:21 -08:00
|
|
|
claim['fileExt'] = determineFileExtensionFromContentType(claim.contentType);
|
2018-03-09 18:23:19 -08:00
|
|
|
claim['host'] = host;
|
2017-12-06 15:15:21 -08:00
|
|
|
return claim;
|
2018-04-29 12:17:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function isLongClaimId (claimId) {
|
|
|
|
return (claimId && (claimId.length === 40));
|
|
|
|
}
|
|
|
|
|
|
|
|
function isShortClaimId (claimId) {
|
|
|
|
return (claimId && (claimId.length < 40));
|
|
|
|
}
|
2017-12-06 14:23:28 -08:00
|
|
|
|
2017-11-08 07:25:21 -08:00
|
|
|
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
|
2017-08-10 09:47:59 -07:00
|
|
|
const Claim = sequelize.define(
|
|
|
|
'Claim',
|
|
|
|
{
|
|
|
|
address: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
amount: {
|
2017-11-08 07:25:21 -08:00
|
|
|
type : DECIMAL(19, 8),
|
2017-08-14 16:16:32 -07:00
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
claimId: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
claimSequence: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
decodedClaim: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : BOOLEAN,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
depth: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
effectiveAmount: {
|
2017-11-08 07:25:21 -08:00
|
|
|
type : DECIMAL(19, 8),
|
2017-08-14 16:16:32 -07:00
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
hasSignature: {
|
|
|
|
type : BOOLEAN,
|
2017-08-14 16:16:32 -07:00
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
height: {
|
2017-11-07 15:21:42 -08:00
|
|
|
type : INTEGER,
|
2017-08-14 16:16:32 -07:00
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
hex: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : TEXT('long'),
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
name: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
nout: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : INTEGER,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
txid: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
validAtHeight: {
|
2017-11-07 15:21:42 -08:00
|
|
|
type : INTEGER,
|
2017-08-10 09:47:59 -07:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
outpoint: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
claimType: {
|
2017-08-14 16:16:32 -07:00
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
2017-08-15 13:48:42 -07:00
|
|
|
certificateId: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
2017-08-10 09:47:59 -07:00
|
|
|
author: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
description: {
|
2017-08-15 13:48:42 -07:00
|
|
|
type : TEXT('long'),
|
2017-08-10 09:47:59 -07:00
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
language: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-15 13:48:42 -07:00
|
|
|
},
|
|
|
|
license: {
|
|
|
|
type : STRING,
|
|
|
|
default: null,
|
2017-08-10 09:47:59 -07: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 08:31:38 -07:00
|
|
|
channelName: {
|
|
|
|
type : STRING,
|
|
|
|
allowNull: true,
|
|
|
|
default : null,
|
|
|
|
},
|
2017-08-10 09:47:59 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
freezeTableName: true,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-09-15 14:41:47 -07:00
|
|
|
Claim.associate = db => {
|
|
|
|
Claim.belongsTo(db.File, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-31 10:05:15 -07:00
|
|
|
Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) {
|
2017-11-27 10:22:47 -08:00
|
|
|
logger.debug(`Claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`);
|
2017-10-31 10:05:15 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
2017-10-31 11:54:33 -07:00
|
|
|
where: { name: claimName },
|
2017-10-31 10:05:15 -07:00
|
|
|
order: [['height', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2017-11-14 17:52:20 -05:00
|
|
|
throw new Error('No claim(s) found with that claim name');
|
2017-10-31 10:05:15 -07:00
|
|
|
default:
|
2017-11-03 17:10:08 -07:00
|
|
|
resolve(returnShortId(result, claimId));
|
2017-10-31 10:05:15 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-05 15:11:16 -08:00
|
|
|
Claim.getAllChannelClaims = function (channelClaimId) {
|
|
|
|
logger.debug(`Claim.getAllChannelClaims for ${channelClaimId}`);
|
2017-10-31 11:54:33 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
2017-12-05 15:11:16 -08:00
|
|
|
where: { certificateId: channelClaimId },
|
2018-06-14 13:06:19 -07:00
|
|
|
order: [['height', 'DESC']],
|
2017-12-07 09:28:44 -08:00
|
|
|
raw : true, // returns an array of only data, not an array of instances
|
2017-10-31 11:54:33 -07:00
|
|
|
})
|
2017-12-06 11:53:31 -08:00
|
|
|
.then(channelClaimsArray => {
|
|
|
|
switch (channelClaimsArray.length) {
|
2017-10-31 11:54:33 -07:00
|
|
|
case 0:
|
|
|
|
return resolve(null);
|
|
|
|
default:
|
2017-12-06 11:53:31 -08:00
|
|
|
channelClaimsArray.forEach(claim => {
|
|
|
|
claim['fileExt'] = determineFileExtensionFromContentType(claim.contentType);
|
2017-12-14 12:32:20 -08:00
|
|
|
claim['thumbnail'] = determineThumbnail(claim.thumbnail, defaultThumbnail);
|
2017-12-06 11:53:31 -08:00
|
|
|
return claim;
|
|
|
|
});
|
|
|
|
return resolve(channelClaimsArray);
|
2017-10-31 11:54:33 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-05 15:11:16 -08:00
|
|
|
Claim.getClaimIdByLongChannelId = function (channelClaimId, claimName) {
|
|
|
|
logger.debug(`finding claim id for claim ${claimName} from channel ${channelClaimId}`);
|
2017-10-31 15:28:11 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-31 16:00:12 -07:00
|
|
|
this
|
2017-10-31 15:28:11 -07:00
|
|
|
.findAll({
|
2017-12-05 15:11:16 -08:00
|
|
|
where: { name: claimName, certificateId: channelClaimId },
|
2017-10-31 15:28:11 -07:00
|
|
|
order: [['id', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2017-12-05 17:08:34 -08:00
|
|
|
return resolve(null);
|
2017-10-31 15:28:11 -07:00
|
|
|
case 1:
|
|
|
|
return resolve(result[0].claimId);
|
|
|
|
default:
|
2018-01-09 15:11:26 -08:00
|
|
|
logger.error(`${result.length} records found for "${claimName}" in channel "${channelClaimId}"`);
|
2017-10-31 15:28:11 -07:00
|
|
|
return resolve(result[0].claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-29 12:17:23 -07:00
|
|
|
Claim.validateLongClaimId = function (name, claimId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.findOne({
|
|
|
|
where: {
|
|
|
|
name,
|
|
|
|
claimId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
if (!result) {
|
|
|
|
return reject(NO_CLAIM);
|
|
|
|
}
|
|
|
|
resolve(claimId);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error(error);
|
|
|
|
reject(NO_CLAIM);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-31 16:00:12 -07:00
|
|
|
Claim.getLongClaimIdFromShortClaimId = function (name, shortId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: {
|
|
|
|
name,
|
|
|
|
claimId: {
|
2018-05-14 15:23:50 -07:00
|
|
|
[sequelize.Op.like]: `${shortId}%`,
|
2017-10-31 16:00:12 -07:00
|
|
|
}},
|
|
|
|
order: [['height', 'ASC']],
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2018-04-29 12:17:23 -07:00
|
|
|
return reject(NO_CLAIM);
|
|
|
|
default:
|
2017-10-31 16:00:12 -07:00
|
|
|
return resolve(result[0].claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-04-29 12:17:23 -07:00
|
|
|
logger.error(error);
|
|
|
|
reject(NO_CLAIM);
|
2017-10-31 16:00:12 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Claim.getTopFreeClaimIdByClaimName = function (name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: { name },
|
2018-04-29 12:17:23 -07:00
|
|
|
order: [['effectiveAmount', 'DESC'], ['height', 'ASC']],
|
2017-10-31 16:00:12 -07:00
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
switch (result.length) {
|
|
|
|
case 0:
|
2018-04-29 12:17:23 -07:00
|
|
|
return reject(NO_CLAIM);
|
2017-10-31 16:00:12 -07:00
|
|
|
default:
|
2017-11-27 10:22:47 -08:00
|
|
|
return resolve(result[0].dataValues.claimId);
|
2017-10-31 16:00:12 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-04-29 12:17:23 -07:00
|
|
|
logger.error(error);
|
|
|
|
reject(NO_CLAIM);
|
2018-02-21 17:02:57 -08:00
|
|
|
});
|
2017-11-21 12:53:43 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-10-31 16:00:12 -07:00
|
|
|
Claim.getLongClaimId = function (claimName, claimId) {
|
2018-04-29 12:17:23 -07:00
|
|
|
// logger.debug(`getLongClaimId(${claimName}, ${claimId})`);
|
|
|
|
if (isLongClaimId(claimId)) {
|
2017-11-21 12:53:43 -08:00
|
|
|
return this.validateLongClaimId(claimName, claimId);
|
2018-04-29 12:17:23 -07:00
|
|
|
} else if (isShortClaimId(claimId)) {
|
|
|
|
return this.getLongClaimIdFromShortClaimId(claimName, claimId);
|
2017-10-31 16:00:12 -07:00
|
|
|
} else {
|
2018-04-29 12:17:23 -07:00
|
|
|
return this.getTopFreeClaimIdByClaimName(claimName);
|
2017-10-31 16:00:12 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-31 16:44:32 -07:00
|
|
|
Claim.resolveClaim = function (name, claimId) {
|
2018-01-30 15:32:42 -08:00
|
|
|
logger.debug(`Claim.resolveClaim: ${name} ${claimId}`);
|
2017-10-31 16:44:32 -07:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this
|
|
|
|
.findAll({
|
|
|
|
where: { name, claimId },
|
|
|
|
})
|
2017-12-06 14:23:28 -08:00
|
|
|
.then(claimArray => {
|
|
|
|
switch (claimArray.length) {
|
|
|
|
case 0:
|
|
|
|
return resolve(null);
|
2017-10-31 16:44:32 -07:00
|
|
|
case 1:
|
2017-12-06 14:23:28 -08:00
|
|
|
return resolve(prepareClaimData(claimArray[0].dataValues));
|
2017-10-31 16:44:32 -07:00
|
|
|
default:
|
2018-03-07 18:57:35 -08:00
|
|
|
logger.error(`more than one record matches ${name}#${claimId} in db.Claim`);
|
2017-12-06 14:23:28 -08:00
|
|
|
return resolve(prepareClaimData(claimArray[0].dataValues));
|
2017-10-31 16:44:32 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-08-10 09:47:59 -07:00
|
|
|
return Claim;
|
|
|
|
};
|