From 46381fd516b06202cace16ddb6bc7c45d2085025 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 00:35:53 -0400 Subject: [PATCH 1/7] Fix glitchy UI dropdown select for max downloads and expiration --- app/ui/selectbox.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/ui/selectbox.js b/app/ui/selectbox.js index cf1d1a4f..ce3e3ef8 100644 --- a/app/ui/selectbox.js +++ b/app/ui/selectbox.js @@ -1,32 +1,28 @@ const html = require('choo/html'); module.exports = function(selected, options, translate, changed, htmlId) { - let x = selected; - + function choose(event) { + if (event.target.value != selected) { + console.log('Selected new value from dropdown', htmlId, ':', selected, '->', event.target.value) + changed(event.target.value); + } + } + return html` `; - - function choose(event) { - const target = event.target; - const value = +target.value; - - if (x !== value) { - x = value; - changed(value); - } - } }; From 1e7efe3d989eb1eaf86a1b1091175c690f3be359 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 00:43:08 -0400 Subject: [PATCH 2/7] fix signup-ctas blocking render --- app/ui/expiryOptions.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/ui/expiryOptions.js b/app/ui/expiryOptions.js index 494093f4..149e2636 100644 --- a/app/ui/expiryOptions.js +++ b/app/ui/expiryOptions.js @@ -31,12 +31,12 @@ module.exports = function(state, emit) { counts, num => state.translate('downloadCount', { num }), value => { - const max = state.user.maxDownloads; - state.archive.dlimit = Math.min(value, max); - if (value > max) { + const selected = parseInt(value); + state.archive.dlimit = selected; + emit('render'); + if (selected > parseInt(state.user.maxDownloads || '0')) { + console.log('Chosen max download count is larger than the allowed limit for anonymous users', selected) emit('signup-cta', 'count'); - } else { - emit('render'); } }, 'expire-after-dl-count-select' @@ -58,12 +58,12 @@ module.exports = function(state, emit) { return state.translate(l10n.id, l10n); }, value => { - const max = state.user.maxExpireSeconds; - state.archive.timeLimit = Math.min(value, max); - if (value > max) { + const selected = parseInt(value); + state.archive.timeLimit = selected; + emit('render'); + if (selected > parseInt(state.user.maxExpireSeconds || '0')) { + console.log('Chosen download expiration is larger than the allowed limit for anonymous users', selected) emit('signup-cta', 'time'); - } else { - emit('render'); } }, 'expire-after-time-select' From 4a6a3dfc36bbbdf8601997715d227f78e160d45a Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 01:39:14 -0400 Subject: [PATCH 3/7] coerce DOWNLOAD_COUNTS and EXPIRE_TIMES_SECONDS into positive integer arrays --- server/config.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/server/config.js b/server/config.js index d530610f..18f99b2f 100644 --- a/server/config.js +++ b/server/config.js @@ -3,6 +3,18 @@ const { tmpdir } = require('os'); const path = require('path'); const { randomBytes } = require('crypto'); +convict.addFormat({ + name: 'positive-int-array', + validate: (ints, schema) => { + ints.forEach(int => { + if (isNaN(int) || int < 0) + throw new Error('must be a comma-separated list of positive integers') + }) + }, + coerce: (ints_str, schema) => + ints_str.trim().split(',').map(int_str => parseInt(int_str.trim(), 10)) +}); + const conf = convict({ s3_bucket: { format: String, @@ -25,7 +37,7 @@ const conf = convict({ env: 'GCS_BUCKET' }, expire_times_seconds: { - format: Array, + format: 'positive-int-array', default: [300, 3600, 86400, 604800], env: 'EXPIRE_TIMES_SECONDS' }, @@ -40,7 +52,7 @@ const conf = convict({ env: 'MAX_EXPIRE_SECONDS' }, download_counts: { - format: Array, + format: 'positive-int-array', default: [1, 2, 3, 4, 5, 20, 50, 100], env: 'DOWNLOAD_COUNTS' }, From a6162f7142a03a6e42d1f9ae9bed5219a0f1866d Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 01:41:22 -0400 Subject: [PATCH 4/7] fix indentation --- server/config.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/config.js b/server/config.js index 18f99b2f..971ede73 100644 --- a/server/config.js +++ b/server/config.js @@ -6,13 +6,14 @@ const { randomBytes } = require('crypto'); convict.addFormat({ name: 'positive-int-array', validate: (ints, schema) => { - ints.forEach(int => { - if (isNaN(int) || int < 0) + for (const int of ints) { + if (isNaN(int) || int < 0) throw new Error('must be a comma-separated list of positive integers') - }) + } + }, + coerce: (ints_str, schema) => { + return ints_str.trim().split(',').map(int_str => parseInt(int_str.trim(), 10)) }, - coerce: (ints_str, schema) => - ints_str.trim().split(',').map(int_str => parseInt(int_str.trim(), 10)) }); const conf = convict({ From 77ea05a23339110d2f63c04c6709c185b8c9805e Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 01:46:12 -0400 Subject: [PATCH 5/7] also handle arrays of strings --- server/config.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/config.js b/server/config.js index 971ede73..0cea3996 100644 --- a/server/config.js +++ b/server/config.js @@ -11,8 +11,9 @@ convict.addFormat({ throw new Error('must be a comma-separated list of positive integers') } }, - coerce: (ints_str, schema) => { - return ints_str.trim().split(',').map(int_str => parseInt(int_str.trim(), 10)) + coerce: (ints, schema) => { + const ints_arr = Array.isArray(ints_str) ? ints : ints.trim().split(',') + return ints_arr.map(int => (typeof int === 'number') ? int : parseInt(int.trim(), 10)) }, }); From 0ffc960523b9f1855fc69b70be2eda3bd7bb69a0 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 01:52:37 -0400 Subject: [PATCH 6/7] add comments --- server/config.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/server/config.js b/server/config.js index 0cea3996..833ddf10 100644 --- a/server/config.js +++ b/server/config.js @@ -5,16 +5,19 @@ const { randomBytes } = require('crypto'); convict.addFormat({ name: 'positive-int-array', - validate: (ints, schema) => { + coerce: (ints, schema) => { // can take: int[] | string[] | string (csv), returns -> int[] + const ints_arr = Array.isArray(ints) ? ints : ints.trim().split(',') + return ints_arr.map(int => + (typeof int === 'number') + ? int + : parseInt(int.trim(), 10)) + }, + validate: (ints, schema) => { // takes: int[], errors if any NaNs, negatives, or floats present for (const int of ints) { - if (isNaN(int) || int < 0) + if (typeof(int) !== 'number' || isNaN(int) || int < 0 || int % 1 > 0) throw new Error('must be a comma-separated list of positive integers') } }, - coerce: (ints, schema) => { - const ints_arr = Array.isArray(ints_str) ? ints : ints.trim().split(',') - return ints_arr.map(int => (typeof int === 'number') ? int : parseInt(int.trim(), 10)) - }, }); const conf = convict({ From d6ac469e1a9a5b8645744e8248515319143fe62c Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 19 May 2021 05:13:47 -0400 Subject: [PATCH 7/7] remove signup-cta and tweak console log wording to remove anon user references --- app/ui/expiryOptions.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/ui/expiryOptions.js b/app/ui/expiryOptions.js index 149e2636..2ee489f7 100644 --- a/app/ui/expiryOptions.js +++ b/app/ui/expiryOptions.js @@ -35,8 +35,7 @@ module.exports = function(state, emit) { state.archive.dlimit = selected; emit('render'); if (selected > parseInt(state.user.maxDownloads || '0')) { - console.log('Chosen max download count is larger than the allowed limit for anonymous users', selected) - emit('signup-cta', 'count'); + console.log('Chosen max download count is larger than the allowed limit', selected) } }, 'expire-after-dl-count-select' @@ -62,8 +61,7 @@ module.exports = function(state, emit) { state.archive.timeLimit = selected; emit('render'); if (selected > parseInt(state.user.maxExpireSeconds || '0')) { - console.log('Chosen download expiration is larger than the allowed limit for anonymous users', selected) - emit('signup-cta', 'time'); + console.log('Chosen download expiration is larger than the allowed limit', selected) } }, 'expire-after-time-select'