Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #433 from brave/update-calendar-date
Browse files Browse the repository at this point in the history
Send updater query parameters based on calendar date
  • Loading branch information
bbondy committed Feb 1, 2016
2 parents 22a6ea3 + f51127b commit 1cda6ee
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
15 changes: 15 additions & 0 deletions app/dates.js
Original file line number Diff line number Diff line change
@@ -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
}
77 changes: 28 additions & 49 deletions app/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
}

Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1cda6ee

Please sign in to comment.