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

Commit

Permalink
fix sync error when default newtab site is visited
Browse files Browse the repository at this point in the history
fix #8217 and refactors syncUtil/urlBarSuggestions to use common siteUtil methods

Test Plan:
1. enable sync in a clean profile
2. go to https://brave.com
3. you should not see any console errors
  • Loading branch information
diracdeltas authored and bsclifton committed Apr 12, 2017
1 parent ea5a69c commit 0fb7fb1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 17 deletions.
7 changes: 2 additions & 5 deletions app/renderer/reducers/urlBarSuggestionsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

const windowConstants = require('../../../js/constants/windowConstants')
const getSetting = require('../../../js/settings').getSetting
const {isDefaultEntry} = require('../../../js/state/siteUtil')
const fetchSearchSuggestions = require('../fetchSearchSuggestions')
const {activeFrameStatePath, frameStatePath, getFrameByKey, getFrameKeyByTabId, getActiveFrame} = require('../../../js/state/frameStateUtil')
const searchProviders = require('../../../js/data/searchProviders')
Expand Down Expand Up @@ -128,11 +129,7 @@ const generateNewSuggestionsList = (state) => {
let sites = appStoreRenderer.state.get('sites')
if (sites) {
// Filter out Brave default newtab sites and sites with falsey location
sites = sites.filterNot((site) =>
(Immutable.is(site.get('tags'), new Immutable.List(['default'])) &&
site.get('lastAccessedTime') === 1) ||
!site.get('location')
)
sites = sites.filter((site) => !isDefaultEntry(site) && site.get('location'))
}
const searchResults = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']))
const frameSearchDetail = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'searchDetail']))
Expand Down
2 changes: 1 addition & 1 deletion app/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const sendSyncRecords = (sender, action, data) => {
if (!deviceId) {
throw new Error('Cannot build a sync record because deviceId is not set')
}
if (!data || !data.length) {
if (!data || !data.length || !data[0]) {
return
}
const category = CATEGORY_MAP[data[0].name]
Expand Down
1 change: 1 addition & 0 deletions js/constants/siteTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mapValuesByKeys = require('../lib/functional').mapValuesByKeys

const _ = null
const siteTags = {
DEFAULT: _,
BOOKMARK: _,
BOOKMARK_FOLDER: _,
PINNED: _,
Expand Down
15 changes: 13 additions & 2 deletions js/state/siteUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const UrlUtil = require('../lib/urlutil')
const urlParse = require('../../app/common/urlParse')
const {makeImmutable} = require('../../app/common/state/immutableUtil')

const defaultTags = new Immutable.List([siteTags.DEFAULT])

const isBookmark = (tags) => {
if (!tags) {
return false
Expand Down Expand Up @@ -604,8 +606,7 @@ module.exports.isHistoryEntry = function (siteDetail) {
if (siteDetail.get('location').startsWith('about:')) {
return false
}
if (Immutable.is(siteDetail.get('tags'), new Immutable.List(['default'])) &&
siteDetail.get('lastAccessedTime') === 1) {
if (module.exports.isDefaultEntry(siteDetail)) {
// This is a Brave default newtab site
return false
}
Expand All @@ -614,6 +615,16 @@ module.exports.isHistoryEntry = function (siteDetail) {
return false
}

/**
* Determines if the site detail is one of default sites in about:newtab.
* @param {Immutable.Map} siteDetail The site detail to check.
* @returns {boolean} if the site detail is a default newtab entry.
*/
module.exports.isDefaultEntry = function (siteDetail) {
return Immutable.is(siteDetail.get('tags'), defaultTags) &&
siteDetail.get('lastAccessedTime') === 1
}

/**
* Get a folder by folderId
* @returns {Immutable.List.<Immutable.Map>} sites
Expand Down
16 changes: 8 additions & 8 deletions js/state/syncUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,8 @@ module.exports.now = () => {
* @returns {boolean}
*/
module.exports.isSyncable = (type, item) => {
if (type === 'bookmark' && item.get('tags')) {
return (item.get('tags').includes('bookmark') ||
item.get('tags').includes('bookmark-folder'))
if (type === 'bookmark') {
return siteUtil.isBookmark(item) || siteUtil.isFolder(item)
} else if (type === 'siteSetting') {
for (let field in siteSettingDefaults) {
if (item.has(field)) {
Expand Down Expand Up @@ -465,11 +464,12 @@ module.exports.createSiteData = (site, appState) => {
siteData[field] = site[field]
}
}
const siteKey = siteUtil.getSiteKey(Immutable.fromJS(site)) || siteUtil.getSiteKey(Immutable.fromJS(siteData))
const immutableSite = Immutable.fromJS(site)
const siteKey = siteUtil.getSiteKey(immutableSite) || siteUtil.getSiteKey(Immutable.fromJS(siteData))
if (siteKey === null) {
throw new Error('Sync could not create siteKey')
}
if (module.exports.isSyncable('bookmark', Immutable.fromJS(site))) {
if (module.exports.isSyncable('bookmark', immutableSite)) {
const objectId = site.objectId || module.exports.newObjectId(['sites', siteKey])
const parentFolderObjectId = site.parentFolderObjectId ||
(site.parentFolderId && findOrCreateFolderObjectId(site.parentFolderId, appState))
Expand All @@ -478,18 +478,18 @@ module.exports.createSiteData = (site, appState) => {
objectId,
value: {
site: siteData,
isFolder: site.tags.includes('bookmark-folder'),
isFolder: siteUtil.isFolder(immutableSite),
parentFolderObjectId
}
}
} else if (!site.tags || !site.tags.length || site.tags.includes('pinned')) {
} else if (siteUtil.isHistoryEntry(immutableSite)) {
return {
name: 'historySite',
objectId: site.objectId || module.exports.newObjectId(['sites', siteKey]),
value: siteData
}
}
console.log(`Warning: Can't create site data: ${site}`)
console.log(`Warning: Can't create site data: ${JSON.stringify(site)}`)
}

/**
Expand Down
45 changes: 44 additions & 1 deletion test/unit/state/siteUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,14 @@ describe('siteUtil', function () {
})
assert.equal(siteUtil.isHistoryEntry(siteDetail), true)
})
it('returns false for a default site', function () {
const siteDetail = Immutable.fromJS({
location: testUrl1,
tags: [siteTags.DEFAULT],
lastAccessedTime: 1
})
assert.equal(siteUtil.isHistoryEntry(siteDetail), false)
})
it('returns false for a bookmark entry with falsey lastAccessedTime', function () {
const siteDetail = Immutable.fromJS({
location: testUrl1,
Expand All @@ -1277,7 +1285,7 @@ describe('siteUtil', function () {
it('returns false for a brave default site', function () {
const siteDetail = Immutable.fromJS({
location: testUrl1,
tags: ['default'],
tags: [siteTags.DEFAULT],
lastAccessedTime: 1
})
assert.equal(siteUtil.isHistoryEntry(siteDetail), false)
Expand All @@ -1295,6 +1303,41 @@ describe('siteUtil', function () {
})
})

describe('isDefaultEntry', function () {
it('returns false for history entry which has lastAccessedTime', function () {
const siteDetail = Immutable.fromJS({
location: 'https://brave.com/',
tags: [siteTags.DEFAULT],
lastAccessedTime: 123
})
assert.equal(siteUtil.isDefaultEntry(siteDetail), false)
})
it('returns false for bookmark entry', function () {
const siteDetail = Immutable.fromJS({
location: 'https://brave.com/',
tags: [siteTags.BOOKMARK],
lastAccessedTime: 1
})
assert.equal(siteUtil.isDefaultEntry(siteDetail), false)
})
it('returns false for entry without lastAccessedTime', function () {
const siteDetail = Immutable.fromJS({
location: 'https://brave.com/',
tags: [siteTags.DEFAULT]
})
assert.equal(siteUtil.isDefaultEntry(siteDetail), false)
})
it('returns true for default entry', function () {
const siteDetail = Immutable.fromJS({
tags: [siteTags.DEFAULT],
lastAccessedTime: 1,
objectId: [210, 115, 31, 176, 57, 212, 167, 120, 104, 88, 88, 27, 141, 36, 235, 226],
location: testUrl1
})
assert.equal(siteUtil.isDefaultEntry(siteDetail), true)
})
})

describe('getFolder', function () {
const folder = Immutable.fromJS({
customTitle: 'folder1',
Expand Down

0 comments on commit 0fb7fb1

Please sign in to comment.