From f8f611905be1186bc030df37594d513294c526a6 Mon Sep 17 00:00:00 2001 From: Shawn Date: Tue, 9 Oct 2018 19:11:06 -0500 Subject: [PATCH] Fix channel names not showing on view pages --- server/chainquery/bundle.js | 15 +++++++ server/chainquery/queries/claimQueries.js | 15 +++++++ .../api/channel/claims/getChannelClaims.js | 2 +- server/controllers/api/claim/data/index.js | 42 +++++++++---------- server/controllers/api/claim/get/index.js | 2 +- server/utils/getClaimData.js | 35 +++++++++++----- 6 files changed, 76 insertions(+), 35 deletions(-) diff --git a/server/chainquery/bundle.js b/server/chainquery/bundle.js index 7661f9f9..95b1b589 100644 --- a/server/chainquery/bundle.js +++ b/server/chainquery/bundle.js @@ -835,6 +835,21 @@ const isShortClaimId = (claimId) => { var claimQueries = (db, table, sequelize) => ({ + getClaimChannelName: async (publisher_id) => { + return await table.findAll({ + where : { claim_id: publisher_id }, + attributes: ['name'], + }).then(result => { + if(result.length === 0) { + throw new Error(`no record found for ${claimId}`); + } else if(result.length !== 1) { + logger$1.warn(`more than one record matches ${claimId} in db.Claim`); + } + + return result[0].name; + }); + }, + getShortClaimIdFromLongClaimId: async (claimId, claimName) => { logger$1.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); return await table.findAll({ diff --git a/server/chainquery/queries/claimQueries.js b/server/chainquery/queries/claimQueries.js index 8972aaa3..27f2ea93 100644 --- a/server/chainquery/queries/claimQueries.js +++ b/server/chainquery/queries/claimQueries.js @@ -34,6 +34,21 @@ const isShortClaimId = (claimId) => { export default (db, table, sequelize) => ({ + getClaimChannelName: async (publisher_id) => { + return await table.findAll({ + where : { claim_id: publisher_id }, + attributes: ['name'], + }).then(result => { + if(result.length === 0) { + throw new Error(`no record found for ${claimId}`); + } else if(result.length !== 1) { + logger.warn(`more than one record matches ${claimId} in db.Claim`); + } + + return result[0].name; + }); + }, + getShortClaimIdFromLongClaimId: async (claimId, claimName) => { logger.debug(`claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); return await table.findAll({ diff --git a/server/controllers/api/channel/claims/getChannelClaims.js b/server/controllers/api/channel/claims/getChannelClaims.js index 213b769e..f0252b53 100644 --- a/server/controllers/api/channel/claims/getChannelClaims.js +++ b/server/controllers/api/channel/claims/getChannelClaims.js @@ -7,7 +7,7 @@ const getChannelClaims = async (channelName, channelShortId, page) => { const channelId = await chainquery.claim.queries.getLongClaimId(channelName, channelShortId); const channelClaims = await chainquery.claim.queries.getAllChannelClaims(channelId); - const processedChannelClaims = channelClaims.map((claim) => getClaimData(claim)); + const processedChannelClaims = await channelClaims.map((claim) => getClaimData(claim)); return returnPaginatedChannelClaims(channelName, channelId, processedChannelClaims, page); }; diff --git a/server/controllers/api/claim/data/index.js b/server/controllers/api/claim/data/index.js index 5909abcb..482be972 100644 --- a/server/controllers/api/claim/data/index.js +++ b/server/controllers/api/claim/data/index.js @@ -9,34 +9,32 @@ const db = require('../../../../models'); */ -const claimData = ({ ip, originalUrl, body, params }, res) => { +const claimData = async ({ ip, originalUrl, body, params }, res) => { const claimName = params.claimName; let claimId = params.claimId; if (claimId === 'none') claimId = null; - chainquery.claim.queries.resolveClaim(claimName, claimId).catch(() => {}) - .then(claimInfo => { - if (!claimInfo) { - // Not found remote, try local - return db.Claim.resolveClaim(claimName, claimId) - } - return claimInfo - }) - .then(claimInfo => { - if (!claimInfo) { - return res.status(404).json({ - success: false, - message: 'No claim could be found', - }); - } - res.status(200).json({ - success: true, - data : getClaimData(claimInfo), + try { + let resolvedClaim = await chainquery.claim.queries.resolveClaim(claimName, claimId).catch(() => {}); + + if(!resolvedClaim) { + resolvedClaim = await db.Claim.resolveClaim(claimName, claimId); + } + + if (!resolvedClaim) { + return res.status(404).json({ + success: false, + message: 'No claim could be found', }); - }) - .catch(error => { - handleErrorResponse(originalUrl, ip, error, res); + } + + res.status(200).json({ + success: true, + data : await getClaimData(resolvedClaim), }); + } catch(error) { + handleErrorResponse(originalUrl, ip, error, res); + } }; module.exports = claimData; diff --git a/server/controllers/api/claim/get/index.js b/server/controllers/api/claim/get/index.js index 4223c473..5af4e9a5 100644 --- a/server/controllers/api/claim/get/index.js +++ b/server/controllers/api/claim/get/index.js @@ -31,7 +31,7 @@ const claimGet = async ({ ip, originalUrl, params }, res) => { throw new Error(`Unable to Get ${name}#${claimId}`); } - let fileData = await createFileRecordDataAfterGet(getClaimData(claimData), lbrynetResult); + let fileData = await createFileRecordDataAfterGet(await getClaimData(claimData), lbrynetResult); const upsertCriteria = { name, claimId }; await db.upsert(db.File, fileData, upsertCriteria, 'File'); diff --git a/server/utils/getClaimData.js b/server/utils/getClaimData.js index 8b84b3f7..68c367e2 100644 --- a/server/utils/getClaimData.js +++ b/server/utils/getClaimData.js @@ -1,13 +1,26 @@ const { details: { host } } = require('@config/siteConfig'); +const chainquery = require('chainquery'); -module.exports = (data) => ({ - name: data.name, - title: data.title, - contentType: data.content_type || data.contentType, - claimId: data.claim_id || data.claimId, - fileExt: data.generated_extension || data.fileExt, - description: data.description, - thumbnail: data.generated_thumbnail || data.thumbnail, - outpoint: data.transaction_hash_id || data.outpoint, - host, -}) +module.exports = async (data) => { + // TODO: Refactor getching the channel name out; requires invasive changes. + const certificateId = data.publisher_id || data.certificateId; + let channelName = data.channelName; + + if(certificateId && !channelName) { + channelName = await chainquery.claim.queries.getClaimChannelName(certificateId).catch(()=>{}); + } + + return ({ + name: data.name, + title: data.title, + certificateId, + channelName, + contentType: data.content_type || data.contentType, + claimId: data.claim_id || data.claimId, + fileExt: data.generated_extension || data.fileExt, + description: data.description, + thumbnail: data.generated_thumbnail || data.thumbnail, + outpoint: data.transaction_hash_id || data.outpoint, + host, + }) +}