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 #12135 from brave/woi-ref-params
Browse files Browse the repository at this point in the history
Send week of installation and ref parameter to updater
  • Loading branch information
bsclifton committed Dec 4, 2017
1 parent d1d35df commit 72b934e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
12 changes: 12 additions & 0 deletions app/dates.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

// The iso week is defined as the week number starting on January 4th indexed to the first Thursday
var isoWeek = function () {
var date = new Date()
Expand Down Expand Up @@ -30,3 +34,11 @@ exports.todayWOY = () => {
exports.todayMonth = () => {
return (new Date()).getMonth() + 1
}

const MILLISECONDS_IN_ONE_DAY = 60 * 60 * 24 * 1000

// return YYYY-MM-DD of closest Monday in the past to current date
exports.lastMonday = (d) => {
var monday = new Date(d.getTime() - ((d.getDay() - 1) * MILLISECONDS_IN_ONE_DAY))
return localYMD(monday)
}
15 changes: 12 additions & 3 deletions app/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const appActions = require('../js/actions/appActions')
const Immutable = require('immutable')
const dates = require('./dates')
const Channel = require('./channel')
const buildConfig = require('../js/constants/buildConfig')

const fs = require('fs')
const path = require('path')
Expand Down Expand Up @@ -117,12 +118,14 @@ exports.init = (platform, arch, ver, updateToPreview) => {
// This is a privacy preserving policy. Instead of passing personally identifying
// information, the browser will pass thefour boolean values indicating when the last
// update check occurred.
var paramsFromLastCheckDelta = (lastCheckYMD, lastCheckWOY, lastCheckMonth, firstCheckMade) => {
var paramsFromLastCheckDelta = (lastCheckYMD, lastCheckWOY, lastCheckMonth, firstCheckMade, weekOfInstallation, ref) => {
return {
daily: !lastCheckYMD || (dates.todayYMD() > lastCheckYMD),
weekly: !lastCheckWOY || (dates.todayWOY() !== lastCheckWOY),
monthly: !lastCheckMonth || (dates.todayMonth() !== lastCheckMonth),
first: !firstCheckMade
first: !firstCheckMade,
woi: weekOfInstallation,
ref: ref || null
}
}

Expand All @@ -143,12 +146,18 @@ var requestVersionInfo = (done, pingOnly) => {
const firstCheckMade = state.getIn(['updates', 'firstCheckMade'], false)
debug(`firstCheckMade = ${firstCheckMade}`)

// The previous Monday from the installation date
const weekOfInstallation = state.getIn(['updates', 'weekOfInstallation'], null)
debug(`weekOfInstallation= ${weekOfInstallation}`)

// Build query string based on the last date an update request was made
const query = paramsFromLastCheckDelta(
lastCheckYMD,
lastCheckWOY,
lastCheckMonth,
firstCheckMade
firstCheckMade,
weekOfInstallation,
buildConfig.ref || null
)
query.accept_preview = updateToPreviewReleases ? 'true' : 'false'
const queryString = `${platformBaseUrl}?${querystring.stringify(query)}`
Expand Down
3 changes: 3 additions & 0 deletions js/stores/appStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ const handleAppAction = (action) => {
appState = appState.setIn(['updates', 'lastCheckWOY'], dates.todayWOY())
appState = appState.setIn(['updates', 'lastCheckMonth'], dates.todayMonth())
appState = appState.setIn(['updates', 'firstCheckMade'], true)
if (!appState.getIn(['updates', 'weekOfInstallation'])) {
appState = appState.setIn(['updates', 'weekOfInstallation'], dates.lastMonday(new Date()))
}
break
case appConstants.APP_SET_UPDATE_STATUS:
if (action.status !== undefined) {
Expand Down
49 changes: 49 additions & 0 deletions test/unit/app/browser/datesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* global describe, it, before, after */

const assert = require('assert')
const sinon = require('sinon')
let dates

require('../../braveUnit')

describe('update date handling', function () {
const exampleDate = 1510708981887 // Tuesday November 14th 2017, 6:23:01 PM
const exampleDate2 = 1512304291746 // Monday December 04th 2017, 9:18:11 AM
let fakeClock
before(function () {
fakeClock = sinon.useFakeTimers(exampleDate)
dates = require('../../../../app/dates')
})
after(function () {
fakeClock.restore()
})

describe('todayYMD', function () {
it('returns YYYY-MM-DD of today', function () {
assert.equal(dates.todayYMD(), '2017-11-14', 'today')
})
})

describe('todayWOY', function () {
it('returns the ISO week number for today', function () {
assert.equal(dates.todayWOY(), 314, 'ISO week number')
})
})

describe('todayMonth', function () {
it('returns the month of today as a number', function () {
assert.equal(dates.todayMonth(), 11, 'Month of today')
})
})

describe('lastMonday', function () {
it('returns YYYY-MM-DD of closest Monday in the past to current date', function () {
const d = new Date(exampleDate)
assert.equal(dates.lastMonday(d), '2017-11-13', 'previous Monday')
})
it('returns YYYY-MM-DD of today if today is monday', function () {
const d = new Date(exampleDate2)
assert.equal(dates.lastMonday(d), '2017-12-04', 'today is Monday')
})
})
})
6 changes: 4 additions & 2 deletions tools/buildPackage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const buildDir = 'Brave-' + process.platform + '-' + arch

var env = {
NODE_ENV: 'production',
CHANNEL: process.env.CHANNEL
CHANNEL: process.env.CHANNEL,
REF: process.env.REF || null
}

var channels = { dev: true, beta: true, stable: true, nightly: true, developer: true }
Expand All @@ -42,7 +43,8 @@ config.writeBuildConfig(
{
channel: env.CHANNEL,
BROWSER_LAPTOP_REV: require('git-rev-sync').long(),
nodeEnv: env.NODE_ENV
nodeEnv: env.NODE_ENV,
ref: env.REF || null
},
'buildConfig.js'
)
Expand Down

0 comments on commit 72b934e

Please sign in to comment.