From cb9a4365574c771c024f4082821d6cd6e92b1a41 Mon Sep 17 00:00:00 2001 From: Aubrey Keus Date: Fri, 29 Jan 2016 10:16:33 -0500 Subject: [PATCH] Send updater query parameters based on calendar date * Compare calendar dates when sending daily flag instead of 86400 seconds * Send first=true if first update request * Use moment.js for date handling --- app/dates.js | 5 ++++ app/updater.js | 56 +++++++++++++++++-------------------------- js/stores/appStore.js | 3 +++ package.json | 1 + 4 files changed, 31 insertions(+), 34 deletions(-) create mode 100644 app/dates.js diff --git a/app/dates.js b/app/dates.js new file mode 100644 index 00000000000..cde74a97715 --- /dev/null +++ b/app/dates.js @@ -0,0 +1,5 @@ +const moment = require('moment') + +exports.todayYMD = () => { + return moment().format('YYYY-MM-DD') +} diff --git a/app/updater.js b/app/updater.js index 673914e0481..2ce2df4bb62 100644 --- a/app/updater.js +++ b/app/updater.js @@ -14,6 +14,7 @@ const querystring = require('querystring') const AppStore = require('../js/stores/appStore') const AppActions = require('../js/actions/appActions') const Immutable = require('immutable') +const dates = require('./dates') const fs = require('fs') const path = require('path') @@ -86,47 +87,34 @@ exports.init = (platform, ver) => { } } -const secondsPerDay = 24 * 60 * 60 -const secondsPerWeek = secondsPerDay * 7 -const secondsPerMonth = secondsPerDay * 30 - // Build a set of three params providing flags determining when the last update occurred // This is a privacy preserving policy. Instead of passing personally identifying // information, the browser will pass the three boolean values indicating when the last // update check occurred. -var paramsFromLastCheckDelta = (seconds) => { +var paramsFromLastCheckDelta = (lastCheckYMD, firstCheckMade) => { // Default params var params = { daily: false, weekly: false, - monthly: false + monthly: false, + first: false } - // First ever check - if (seconds === 0) { + // If the first flag has NOT been set then we will send it + // with the update params to help with daily new users + if (!firstCheckMade || !lastCheckYMD) { + params.first = true params.daily = true return params } - // More than one today - if (seconds < secondsPerDay) { - return params - } - - // If we have not checked today, but we have since last week (first check as well) - if (seconds === 0 || (seconds > secondsPerDay && seconds < secondsPerWeek)) { + // If we have not checked today + if (dates.todayYMD() > lastCheckYMD) { params.daily = true return params } - // If we have not checked this week, but have this month - if (seconds >= secondsPerWeek && seconds < secondsPerMonth) { - params.weekly = true - return params - } - - params.monthly = true - + // Safety return (in case code above changes) return params } @@ -134,19 +122,19 @@ var paramsFromLastCheckDelta = (seconds) => { var requestVersionInfo = (done) => { if (!platformBaseUrl) throw new Error('platformBaseUrl not set') - // Get the timestamp of the last update request - var lastCheckTimestamp = AppStore.getState().toJS().updates['lastCheckTimestamp'] || 0 - debug(`lastCheckTimestamp = ${lastCheckTimestamp}`) + // Get the YMD of the last update request + var lastCheckYMD = AppStore.getState().toJS().updates['lastCheckYMD'] || null + debug(`lastCheckYMD = ${lastCheckYMD}`) - // Calculate the number of seconds since the last update - var secondsSinceLastCheck = 0 - if (lastCheckTimestamp) { - secondsSinceLastCheck = Math.round(((new Date()).getTime() - lastCheckTimestamp) / 1000) - } - debug(`secondsSinceLastCheck = ${secondsSinceLastCheck}`) + // Has the browser ever asked for an update + var firstCheckMade = AppStore.getState().toJS().updates['firstCheckMade'] || false + debug(`firstCheckMade = ${firstCheckMade}`) - // Build query string based on the number of seconds since last check - var query = paramsFromLastCheckDelta(secondsSinceLastCheck) + // Build query string based on the last date an update request was made + var query = paramsFromLastCheckDelta( + lastCheckYMD, + firstCheckMade + ) var queryString = `${platformBaseUrl}?${querystring.stringify(query)}` debug(queryString) diff --git a/js/stores/appStore.js b/js/stores/appStore.js index effb2a4a126..78369956ce4 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -13,6 +13,7 @@ const LocalShortcuts = require('../../app/localShortcuts') const AppActions = require('../actions/appActions') const firstDefinedValue = require('../lib/functional').firstDefinedValue const Serializer = require('../dispatcher/serializer') +const dates = require('../../app/dates') let appState @@ -240,6 +241,8 @@ const handleAppAction = (action) => { break case AppConstants.APP_UPDATE_LAST_CHECK: appState = appState.setIn(['updates', 'lastCheckTimestamp'], (new Date()).getTime()) + appState = appState.setIn(['updates', 'lastCheckYMD'], dates.todayYMD()) + appState = appState.setIn(['updates', 'firstCheckMade'], true) appStore.emitChange() break case AppConstants.APP_SET_UPDATE_STATUS: diff --git a/package.json b/package.json index 170d2a41279..ffefed62c8c 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "font-awesome": "^4.5.0", "font-awesome-webpack": "0.0.4", "immutable": "^3.7.5", + "moment": "^2.11.1", "react": "^0.14.3", "react-dom": "^0.14.3", "sqlite3": "^3.1.0",