diff --git a/app/dates.js b/app/dates.js new file mode 100644 index 00000000000..b9767c663a6 --- /dev/null +++ b/app/dates.js @@ -0,0 +1,15 @@ +const moment = require('moment') + +exports.todayYMD = () => { + return moment().format('YYYY-MM-DD') +} + +exports.todayWOY = () => { + return moment().isoWeek() +} + +// Months a returned zero based from moment +// We add 1 to make sure January does not fail a truth test +exports.todayMonth = () => { + return moment().month() + 1 +} diff --git a/app/updater.js b/app/updater.js index 712335c5119..1e9cdeda215 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') @@ -23,6 +24,7 @@ const updateLogPath = path.join(app.getPath('userData'), 'updateLog.log') // in built mode console.log output is not emitted to the terminal // in prod mode we pipe to a file var debug = function (contents) { + console.log(contents) fs.appendFile(updateLogPath, new Date().toISOString() + ' - ' + contents + '\n') } @@ -86,67 +88,44 @@ 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 +// information, the browser will pass thefour boolean values indicating when the last // update check occurred. -var paramsFromLastCheckDelta = (seconds) => { - // Default params - var params = { - daily: false, - weekly: false, - monthly: false - } - - // First ever check - if (seconds === 0) { - 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)) { - params.daily = true - return params +var paramsFromLastCheckDelta = (lastCheckYMD, lastCheckWOY, lastCheckMonth, firstCheckMade) => { + return { + daily: !lastCheckYMD || (dates.todayYMD() > lastCheckYMD), + weekly: !lastCheckWOY || (dates.todayWOY() !== lastCheckWOY), + monthly: !lastCheckMonth || (dates.todayMonth() !== lastCheckMonth), + first: !firstCheckMade } - - // If we have not checked this week, but have this month - if (seconds >= secondsPerWeek && seconds < secondsPerMonth) { - params.weekly = true - return params - } - - params.monthly = true - - return params } // Make a request to the update server to retrieve meta data 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 daily, week of year and month update checks + 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}`) + var lastCheckWOY = AppStore.getState().toJS().updates['lastCheckWOY'] || null + debug(`lastCheckWOY = ${lastCheckWOY}`) + + var lastCheckMonth = AppStore.getState().toJS().updates['lastCheckMonth'] || null + debug(`lastCheckMonth = ${lastCheckMonth}`) + + // 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, + lastCheckWOY, + lastCheckMonth, + firstCheckMade + ) var queryString = `${platformBaseUrl}?${querystring.stringify(query)}` debug(queryString) diff --git a/js/stores/appStore.js b/js/stores/appStore.js index 9c2f3b14e89..f46a11e9dbf 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -15,6 +15,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') const path = require('path') let appState @@ -250,6 +251,10 @@ 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', 'lastCheckWOY'], dates.todayWOY()) + appState = appState.setIn(['updates', 'lastCheckMonth'], dates.todayMonth()) + appState = appState.setIn(['updates', 'firstCheckMade'], true) appStore.emitChange() break case AppConstants.APP_SET_UPDATE_STATUS: diff --git a/package.json b/package.json index 814a99b22eb..77b67e388d3 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,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",