From 8b9c94bd6624b37018f387ca58c347b41d6cb446 Mon Sep 17 00:00:00 2001 From: Wei-Jun Hung Date: Wed, 23 Nov 2022 17:38:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A5=85=20Filter=20failed=20Tx=20and?= =?UTF-8?q?=20don't=20write=20into=20DB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/likernft/sell.js | 2 ++ src/routes/likernft/transfer.js | 2 ++ src/util/api/likernft/transfer.js | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/routes/likernft/sell.js b/src/routes/likernft/sell.js index 5b27b500e..7e75f0b9b 100644 --- a/src/routes/likernft/sell.js +++ b/src/routes/likernft/sell.js @@ -29,10 +29,12 @@ router.post( const info = await getNFTTransferInfo(txHash, classId, nftId); if (!info) throw new ValidationError('NO_MATCHING_TX_HASH_AND_NFT_ID'); const { + code, fromAddress, toAddress, txTimestamp, } = info; + if (code) throw new ValidationError(`TX_FAILED_WITH_CODE_${code}`); if (toAddress !== LIKER_NFT_TARGET_ADDRESS) throw new ValidationError('INVALID_TX_RECEIVER'); const iscnPrefix = await getISCNPrefixByClassId(classId); const owner = await getNFTOwner(classId, nftId); diff --git a/src/routes/likernft/transfer.js b/src/routes/likernft/transfer.js index 07db517b7..b023576a8 100644 --- a/src/routes/likernft/transfer.js +++ b/src/routes/likernft/transfer.js @@ -21,10 +21,12 @@ router.post( const info = await getNFTTransferInfo(txHash, classId, nftId); if (!info) throw new ValidationError('NO_MATCHING_TX_HASH_AND_NFT_ID'); const { + code, fromAddress, toAddress, txTimestamp, } = info; + if (code) throw new ValidationError(`TX_FAILED_WITH_CODE_${code}`); const iscnPrefix = await getISCNPrefixByClassId(classId); await processNFTTransfer({ fromAddress, diff --git a/src/util/api/likernft/transfer.js b/src/util/api/likernft/transfer.js index 1eb2dafba..fd2001467 100644 --- a/src/util/api/likernft/transfer.js +++ b/src/util/api/likernft/transfer.js @@ -16,8 +16,9 @@ export async function getNFTTransferInfo(txHash, classId, nftId) { sender: fromAddress, receiver: toAddress, } = message; - const { timestamp } = data.tx_response; + const { code, timestamp } = data.tx_response; return { + code, fromAddress, toAddress, txTimestamp: Date.parse(timestamp), From 606a442a9bd84965e553254075b698a16612baec Mon Sep 17 00:00:00 2001 From: William Chong Date: Wed, 23 Nov 2022 14:25:33 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E2=9C=A8=20Add=20update=20hash=20to=20inva?= =?UTF-8?q?lidate=20avatar=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/users/registerLogin.js | 10 ++++++++-- src/util/api/users/getPublicInfo.js | 6 ++++-- src/util/api/users/register.js | 11 +++++++++-- src/util/fileupload.js | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/routes/users/registerLogin.js b/src/routes/users/registerLogin.js index 118199c8b..bc5a25f7c 100644 --- a/src/routes/users/registerLogin.js +++ b/src/routes/users/registerLogin.js @@ -357,9 +357,13 @@ router.post( const { avatarSHA256 } = req.body; const { file } = req; let avatarUrl; + let avatarHash; if (!file) throw new ValidationError('MISSING_AVATAR_FILE'); try { - avatarUrl = await handleAvatarUploadAndGetURL(user, file, avatarSHA256); + ({ + url: avatarUrl, + hash: avatarHash, + } = await handleAvatarUploadAndGetURL(user, file, avatarSHA256)); } catch (err) { // eslint-disable-next-line no-console console.error('Avatar file handling error:'); @@ -368,7 +372,9 @@ router.post( throw new ValidationError('INVALID_AVATAR'); } - await dbRef.doc(user).update({ avatar: avatarUrl }); + const payload = { avatar: avatarUrl }; + if (avatarHash) payload.avatarHash = avatarHash; + await dbRef.doc(user).update(payload); res.json({ avatar: avatarUrl }); const oldUserObj = await dbRef.doc(user).get(); diff --git a/src/util/api/users/getPublicInfo.js b/src/util/api/users/getPublicInfo.js index 39b447be9..f2d0348b1 100644 --- a/src/util/api/users/getPublicInfo.js +++ b/src/util/api/users/getPublicInfo.js @@ -29,10 +29,12 @@ function isValidUserDoc(userDoc) { function formatUserCivicLikerProperies(userDoc) { const { id } = userDoc; const data = userDoc.data(); - const { civicLiker } = data; + const { civicLiker, avatarHash } = data; const payload = data; payload.user = id; - payload.avatar = `https://${API_EXTERNAL_HOSTNAME}/users/id/${id}/avatar?size=${DEFAULT_AVATAR_SIZE}`; + let avatarUrl = `https://${API_EXTERNAL_HOSTNAME}/users/id/${id}/avatar?size=${DEFAULT_AVATAR_SIZE}`; + if (avatarHash) avatarUrl += `&hash=${avatarHash}`; + payload.avatar = avatarUrl; if (civicLiker) { const { diff --git a/src/util/api/users/register.js b/src/util/api/users/register.js index 3823abaf2..d82dba411 100644 --- a/src/util/api/users/register.js +++ b/src/util/api/users/register.js @@ -129,9 +129,13 @@ export async function handleUserRegistration({ // upload avatar const { file } = req; let avatarURL; + let avatarHash; try { if (file) { - avatarURL = await handleAvatarUploadAndGetURL(user, file, avatarSHA256); + ({ + url: avatarURL, + hash: avatarHash, + } = await handleAvatarUploadAndGetURL(user, file, avatarSHA256)); } else if (avatarURLInput) { avatarURL = await handleAvatarLinkAndGetURL(user, avatarURLInput); } @@ -170,7 +174,10 @@ export async function handleUserRegistration({ locale, }; - if (avatarURL) createObj.avatar = avatarURL; + if (avatarURL) { + createObj.avatar = avatarURL; + if (avatarHash) createObj.avatarHash = avatarHash; + } if (likeWallet) createObj.likeWallet = likeWallet; if (hasReferrer) createObj.referrer = referrer; if (description) createObj.description = description; diff --git a/src/util/fileupload.js b/src/util/fileupload.js index e17cc4b69..b5454dabb 100644 --- a/src/util/fileupload.js +++ b/src/util/fileupload.js @@ -48,8 +48,8 @@ export async function handleAvatarUploadAndGetURL(user, file, avatarSHA256) { throw new ValidationError(`unsupported file format! ${(type || {}).ext || JSON.stringify(type)}`); } + const hash256 = sha256(file.buffer); if (avatarSHA256) { - const hash256 = sha256(file.buffer); if (hash256 !== avatarSHA256) throw new ValidationError('avatar sha not match'); } @@ -60,7 +60,7 @@ export async function handleAvatarUploadAndGetURL(user, file, avatarSHA256) { mimetype: file.mimetype, }); const versionHash = md5(file.buffer).substring(0, 7); - return `${avatarUrl}&${versionHash}`; + return { url: `${avatarUrl}&${versionHash}`, hash: hash256 }; } export async function handleAvatarLinkAndGetURL(user, url) { From 4535e73c6811592b2c27d9f960426ffd553dc43c Mon Sep 17 00:00:00 2001 From: William Chong Date: Wed, 23 Nov 2022 15:19:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E2=9C=85=20Fix=20fileupload=20stub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/stub/util/fileupload.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/stub/util/fileupload.js b/test/stub/util/fileupload.js index 1db4bbc8c..fabf55636 100644 --- a/test/stub/util/fileupload.js +++ b/test/stub/util/fileupload.js @@ -22,8 +22,8 @@ export async function handleAvatarUploadAndGetURL(user, file, avatarSHA256) { throw new ValidationError(`unsupported file format! ${(type || {}).ext || JSON.stringify(type)}`); } + const hash256 = sha256(file.buffer); if (avatarSHA256) { - const hash256 = sha256(file.buffer); if (hash256 !== avatarSHA256) throw new ValidationError('avatar sha not match'); } @@ -34,7 +34,7 @@ export async function handleAvatarUploadAndGetURL(user, file, avatarSHA256) { mimetype: file.mimetype, }); const versionHash = md5(file.buffer).substring(0, 7); - return `${avatarUrl}&${versionHash}`; + return { url: `${avatarUrl}&${versionHash}`, hash: hash256 }; } export async function handleAvatarLinkAndGetURL(user, url) {