return canonicalUrl on publish/update; fixes

This commit is contained in:
Travis Eden 2018-11-06 17:40:38 -05:00
parent ffe8aaba47
commit 936ac2f439
5 changed files with 149 additions and 38 deletions
client/src/sagas
server
chainquery
controllers/api/claim
publish
update

View file

@ -73,7 +73,7 @@ function * publishFile (action) {
}); });
} }
if (success.data.claimId) { if (success.data.claimId) {
return history.push(`/${success.data.claimId}/${success.data.name}`); return history.push(success.data.pushTo);
} else { } else {
// this returns to the homepage, needs work // this returns to the homepage, needs work
return yield put(updatePublishStatus(publishStates.FAILED, 'ERROR')); return yield put(updatePublishStatus(publishStates.FAILED, 'ERROR'));

View file

@ -850,7 +850,7 @@ var claimQueries = (db, table, sequelize) => ({
}); });
}, },
getShortClaimIdFromLongClaimId: async (claimId, claimName) => { getShortClaimIdFromLongClaimId: async (claimId, claimName, pendingClaim) => {
logger$1.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); logger$1.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`);
return await table.findAll({ return await table.findAll({
where: { name: claimName }, where: { name: claimName },
@ -860,7 +860,12 @@ var claimQueries = (db, table, sequelize) => ({
throw new Error('No claim(s) found with that claim name'); throw new Error('No claim(s) found with that claim name');
} }
return returnShortId(result, claimId); let list = result.map(claim => claim.dataValues);
if (pendingClaim) {
list = list.concat(pendingClaim);
}
return returnShortId(list, claimId);
}); });
}, },
@ -981,6 +986,24 @@ var claimQueries = (db, table, sequelize) => ({
}); });
}, },
resolveClaimInChannel: async (claimName, channelId) => {
logger$1.debug(`Claim.resolveClaimByNames: ${claimName} in ${channelId}`);
return table.findAll({
where: {
name: claimName,
publisher_id: channelId,
},
}).then(claimArray => {
if (claimArray.length === 0) {
return null;
} else if (claimArray.length !== 1) {
logger$1.warn(`more than one record matches ${claimName} in ${channelId}`);
}
return claimArray[0];
});
},
getOutpoint: async (name, claimId) => { getOutpoint: async (name, claimId) => {
logger$1.debug(`finding outpoint for ${name}#${claimId}`); logger$1.debug(`finding outpoint for ${name}#${claimId}`);

View file

@ -49,7 +49,7 @@ export default (db, table, sequelize) => ({
}); });
}, },
getShortClaimIdFromLongClaimId: async (claimId, claimName) => { getShortClaimIdFromLongClaimId: async (claimId, claimName, pendingClaim) => {
logger.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); logger.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`);
return await table.findAll({ return await table.findAll({
where: { name: claimName }, where: { name: claimName },
@ -59,7 +59,12 @@ export default (db, table, sequelize) => ({
throw new Error('No claim(s) found with that claim name'); throw new Error('No claim(s) found with that claim name');
} }
return returnShortId(result, claimId); let list = result.map(claim => claim.dataValues);
if (pendingClaim) {
list = list.concat(pendingClaim);
}
return returnShortId(list, claimId);
}); });
}, },
@ -180,6 +185,24 @@ export default (db, table, sequelize) => ({
}); });
}, },
resolveClaimInChannel: async (claimName, channelId) => {
logger.debug(`Claim.resolveClaimByNames: ${claimName} in ${channelId}`);
return table.findAll({
where: {
name: claimName,
publisher_id: channelId,
},
}).then(claimArray => {
if (claimArray.length === 0) {
return null;
} else if (claimArray.length !== 1) {
logger.warn(`more than one record matches ${claimName} in ${channelId}`);
}
return claimArray[0];
});
},
getOutpoint: async (name, claimId) => { getOutpoint: async (name, claimId) => {
logger.debug(`finding outpoint for ${name}#${claimId}`); logger.debug(`finding outpoint for ${name}#${claimId}`);

View file

@ -17,6 +17,9 @@ const parsePublishApiRequestBody = require('./parsePublishApiRequestBody.js');
const parsePublishApiRequestFiles = require('./parsePublishApiRequestFiles.js'); const parsePublishApiRequestFiles = require('./parsePublishApiRequestFiles.js');
const authenticateUser = require('./authentication.js'); const authenticateUser = require('./authentication.js');
const chainquery = require('chainquery');
const createCanonicalLink = require('../../../../../utils/createCanonicalLink');
const CLAIM_TAKEN = 'CLAIM_TAKEN'; const CLAIM_TAKEN = 'CLAIM_TAKEN';
const UNAPPROVED_CHANNEL = 'UNAPPROVED_CHANNEL'; const UNAPPROVED_CHANNEL = 'UNAPPROVED_CHANNEL';
@ -42,7 +45,25 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res)
}); });
} }
// define variables // define variables
let channelName, channelId, channelPassword, description, fileName, filePath, fileExtension, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title; let channelName,
channelId,
channelPassword,
description,
fileName,
filePath,
fileExtension,
fileType,
gaStartTime,
license,
name,
nsfw,
thumbnail,
thumbnailFileName,
thumbnailFilePath,
thumbnailFileType,
title,
claimData,
claimId;
// record the start time of the request // record the start time of the request
gaStartTime = Date.now(); gaStartTime = Date.now();
// validate the body and files of the request // validate the body and files of the request
@ -64,6 +85,7 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res)
}; };
throw error; throw error;
} }
return Promise.all([ return Promise.all([
checkClaimAvailability(name), checkClaimAvailability(name),
createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName, channelClaimId), createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName, channelClaimId),
@ -85,17 +107,37 @@ const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res)
// publish the asset // publish the asset
return publish(publishParams, fileName, fileType, filePath); return publish(publishParams, fileName, fileType, filePath);
}) })
.then(claimData => { .then(publishResults => {
logger.debug('Publish success >', claimData); logger.info('Publish success >', publishResults);
claimData = publishResults;
({claimId} = claimData);
if (channelName) {
return chainquery.claim.queries.getShortClaimIdFromLongClaimId(claimData.certificateId, channelName);
} else {
return chainquery.claim.queries.getShortClaimIdFromLongClaimId(claimId, name, claimData).catch(error => {
return claimId.slice(0, 1);
});
}
})
.then(shortId => {
let canonicalUrl;
if (channelName) {
canonicalUrl = createCanonicalLink({ asset: { ...claimData, channelShortId: shortId } });
} else {
canonicalUrl = createCanonicalLink({ asset: { ...claimData, shortId } })
}
res.status(200).json({ res.status(200).json({
success: true, success: true,
message: 'publish completed successfully', message: 'publish completed successfully',
data : { data : {
name, name,
claimId : claimData.claimId, claimId,
url : `${host}/${claimData.claimId}/${name}`, // for backwards compatability with app url : `${host}${canonicalUrl}`, // for backwards compatability with app
showUrl : `${host}/${claimData.claimId}/${name}`, showUrl : `${host}${canonicalUrl}`,
serveUrl: `${host}/${claimData.claimId}/${name}${fileExtension}`, serveUrl: `${host}${canonicalUrl}${fileExtension}`,
pushTo : canonicalUrl,
claimData, claimData,
}, },
}); });

View file

@ -9,6 +9,8 @@ const parsePublishApiRequestBody = require('../publish/parsePublishApiRequestBod
const parsePublishApiRequestFiles = require('../publish/parsePublishApiRequestFiles.js'); const parsePublishApiRequestFiles = require('../publish/parsePublishApiRequestFiles.js');
const authenticateUser = require('../publish/authentication.js'); const authenticateUser = require('../publish/authentication.js');
const createThumbnailPublishParams = require('../publish/createThumbnailPublishParams.js'); const createThumbnailPublishParams = require('../publish/createThumbnailPublishParams.js');
const chainquery = require('chainquery');
const createCanonicalLink = require('../../../../../utils/createCanonicalLink');
/* /*
route to update a claim through the daemon route to update a claim through the daemon
@ -42,7 +44,7 @@ const claimUpdate = ({ body, files, headers, ip, originalUrl, user, tor }, res)
} }
// define variables // define variables
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, thumbnail, fileExtension, license, name, nsfw, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title, claimRecord, metadata; let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, thumbnail, fileExtension, license, name, nsfw, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title, claimRecord, metadata, publishResult;
// record the start time of the request // record the start time of the request
gaStartTime = Date.now(); gaStartTime = Date.now();
@ -57,19 +59,22 @@ const claimUpdate = ({ body, files, headers, ip, originalUrl, user, tor }, res)
// check channel authorization // check channel authorization
authenticateUser(channelName, channelId, channelPassword, user) authenticateUser(channelName, channelId, channelPassword, user)
.then(({ channelName, channelClaimId }) => { .then(({ channelName, channelClaimId }) => {
return db.Claim.findOne({ return chainquery.claim.queries.resolveClaimInChannel(name, channelClaimId).then(claim => claim.dataValues);
where: {
name,
channelName,
},
});
}) })
.then(claim => { .then(claim => {
return resolveUri(`${claim.name}#${claim.claimId}`); claimRecord = claim;
if (!files.file) {
return Promise.all([
db.File.findOne({ where: { name, claimId: claim.claim_id } }),
resolveUri(`${claim.name}#${claim.claim_id}`),
]);
}
return [null, null];
}) })
.then(fullClaim => { .then(([fileResult, resolution]) => {
claimRecord = fullClaim;
logger.info('fullClaim', fullClaim);
metadata = Object.assign({}, { metadata = Object.assign({}, {
title : claimRecord.title, title : claimRecord.title,
description: claimRecord.description, description: claimRecord.description,
@ -89,9 +94,9 @@ const claimUpdate = ({ body, files, headers, ip, originalUrl, user, tor }, res)
if (files.file) { if (files.file) {
publishParams['file_path'] = filePath; publishParams['file_path'] = filePath;
} else { } else {
fileName = fullClaim.file_name; fileName = fileResult.fileName;
fileType = fullClaim.mime_type; fileType = fileResult.fileType;
publishParams['sources'] = fullClaim.claim.value.stream.source; publishParams['sources'] = resolution.claim.value.stream.source;
} }
// publish the thumbnail, if one exists // publish the thumbnail, if one exists
if (thumbnailFileName) { if (thumbnailFileName) {
@ -103,26 +108,44 @@ const claimUpdate = ({ body, files, headers, ip, originalUrl, user, tor }, res)
const fp = files && files.file && files.file.path ? files.file.path : undefined; const fp = files && files.file && files.file.path ? files.file.path : undefined;
return publish(publishParams, fileName, fileType, fp); return publish(publishParams, fileName, fileType, fp);
}) })
.then(claimData => { .then(result => {
// this may need to happen in ../publish/index.js as well publishResult = result;
if (claimData.error) {
res.status(400).json({ if (channelName) {
success: false, return chainquery.claim.queries.getShortClaimIdFromLongClaimId(result.certificateId, channelName);
message: claimData.message, } else {
return chainquery.claim.queries.getShortClaimIdFromLongClaimId(result.claimId, name, result).catch(error => {
return result.claimId.slice(0, 1);
}); });
} }
const {claimId} = claimData; })
.then(shortId => {
let canonicalUrl;
if (channelName) {
canonicalUrl = createCanonicalLink({ asset: { ...publishResult, channelShortId: shortId } });
} else {
canonicalUrl = createCanonicalLink({ asset: { ...publishResult, shortId } })
}
if (publishResult.error) {
res.status(400).json({
success: false,
message: publishResult.message,
});
}
const {claimId} = publishResult;
res.status(200).json({ res.status(200).json({
success: true, success: true,
message: 'update successful', message: 'update successful',
data : { data : {
name, name,
channelName,
channelId: claimData.certificateId,
claimId, claimId,
url : `${details.host}/${claimId}/${name}`, // for backwards compatability with app url : `${details.host}${canonicalUrl}`, // for backwards compatability with app
showUrl : `${details.host}/${claimId}/${name}`, showUrl : `${details.host}${canonicalUrl}`,
claimData, serveUrl: `${details.host}${canonicalUrl}${fileExtension}`,
pushTo : canonicalUrl,
claimData: publishResult,
}, },
}); });
// record the publish end time and send to google analytics // record the publish end time and send to google analytics