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

Commit 4d1885c

Browse files
authored
Merge pull request #8240 from brave/fix/8217
fix sync error when default newtab site is visited
2 parents ab85093 + f302960 commit 4d1885c

File tree

6 files changed

+69
-17
lines changed

6 files changed

+69
-17
lines changed

app/renderer/reducers/urlBarSuggestionsReducer.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const windowConstants = require('../../../js/constants/windowConstants')
88
const getSetting = require('../../../js/settings').getSetting
9+
const {isDefaultEntry} = require('../../../js/state/siteUtil')
910
const fetchSearchSuggestions = require('../fetchSearchSuggestions')
1011
const {activeFrameStatePath, frameStatePath, getFrameByKey, getFrameKeyByTabId, getActiveFrame} = require('../../../js/state/frameStateUtil')
1112
const searchProviders = require('../../../js/data/searchProviders')
@@ -128,11 +129,7 @@ const generateNewSuggestionsList = (state) => {
128129
let sites = appStoreRenderer.state.get('sites')
129130
if (sites) {
130131
// Filter out Brave default newtab sites and sites with falsey location
131-
sites = sites.filterNot((site) =>
132-
(Immutable.is(site.get('tags'), new Immutable.List(['default'])) &&
133-
site.get('lastAccessedTime') === 1) ||
134-
!site.get('location')
135-
)
132+
sites = sites.filter((site) => !isDefaultEntry(site) && site.get('location'))
136133
}
137134
const searchResults = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']))
138135
const frameSearchDetail = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'searchDetail']))

app/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const sendSyncRecords = (sender, action, data) => {
5353
if (!deviceId) {
5454
throw new Error('Cannot build a sync record because deviceId is not set')
5555
}
56-
if (!data || !data.length) {
56+
if (!data || !data.length || !data[0]) {
5757
return
5858
}
5959
const category = CATEGORY_MAP[data[0].name]

js/constants/siteTags.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const mapValuesByKeys = require('../lib/functional').mapValuesByKeys
66

77
const _ = null
88
const siteTags = {
9+
DEFAULT: _,
910
BOOKMARK: _,
1011
BOOKMARK_FOLDER: _,
1112
PINNED: _,

js/state/siteUtil.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const UrlUtil = require('../lib/urlutil')
1111
const urlParse = require('../../app/common/urlParse')
1212
const {makeImmutable} = require('../../app/common/state/immutableUtil')
1313

14+
const defaultTags = new Immutable.List([siteTags.DEFAULT])
15+
1416
const isBookmark = (tags) => {
1517
if (!tags) {
1618
return false
@@ -604,8 +606,7 @@ module.exports.isHistoryEntry = function (siteDetail) {
604606
if (siteDetail.get('location').startsWith('about:')) {
605607
return false
606608
}
607-
if (Immutable.is(siteDetail.get('tags'), new Immutable.List(['default'])) &&
608-
siteDetail.get('lastAccessedTime') === 1) {
609+
if (module.exports.isDefaultEntry(siteDetail)) {
609610
// This is a Brave default newtab site
610611
return false
611612
}
@@ -614,6 +615,16 @@ module.exports.isHistoryEntry = function (siteDetail) {
614615
return false
615616
}
616617

618+
/**
619+
* Determines if the site detail is one of default sites in about:newtab.
620+
* @param {Immutable.Map} siteDetail The site detail to check.
621+
* @returns {boolean} if the site detail is a default newtab entry.
622+
*/
623+
module.exports.isDefaultEntry = function (siteDetail) {
624+
return Immutable.is(siteDetail.get('tags'), defaultTags) &&
625+
siteDetail.get('lastAccessedTime') === 1
626+
}
627+
617628
/**
618629
* Get a folder by folderId
619630
* @returns {Immutable.List.<Immutable.Map>} sites

js/state/syncUtil.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,8 @@ module.exports.now = () => {
388388
* @returns {boolean}
389389
*/
390390
module.exports.isSyncable = (type, item) => {
391-
if (type === 'bookmark' && item.get('tags')) {
392-
return (item.get('tags').includes('bookmark') ||
393-
item.get('tags').includes('bookmark-folder'))
391+
if (type === 'bookmark') {
392+
return siteUtil.isBookmark(item) || siteUtil.isFolder(item)
394393
} else if (type === 'siteSetting') {
395394
for (let field in siteSettingDefaults) {
396395
if (item.has(field)) {
@@ -457,11 +456,12 @@ module.exports.createSiteData = (site, appState) => {
457456
siteData[field] = site[field]
458457
}
459458
}
460-
const siteKey = siteUtil.getSiteKey(Immutable.fromJS(site)) || siteUtil.getSiteKey(Immutable.fromJS(siteData))
459+
const immutableSite = Immutable.fromJS(site)
460+
const siteKey = siteUtil.getSiteKey(immutableSite) || siteUtil.getSiteKey(Immutable.fromJS(siteData))
461461
if (siteKey === null) {
462462
throw new Error('Sync could not create siteKey')
463463
}
464-
if (module.exports.isSyncable('bookmark', Immutable.fromJS(site))) {
464+
if (module.exports.isSyncable('bookmark', immutableSite)) {
465465
const objectId = site.objectId || module.exports.newObjectId(['sites', siteKey])
466466
const parentFolderObjectId = site.parentFolderObjectId ||
467467
(site.parentFolderId && findOrCreateFolderObjectId(site.parentFolderId, appState))
@@ -470,18 +470,18 @@ module.exports.createSiteData = (site, appState) => {
470470
objectId,
471471
value: {
472472
site: siteData,
473-
isFolder: site.tags.includes('bookmark-folder'),
473+
isFolder: siteUtil.isFolder(immutableSite),
474474
parentFolderObjectId
475475
}
476476
}
477-
} else if (!site.tags || !site.tags.length || site.tags.includes('pinned')) {
477+
} else if (siteUtil.isHistoryEntry(immutableSite)) {
478478
return {
479479
name: 'historySite',
480480
objectId: site.objectId || module.exports.newObjectId(['sites', siteKey]),
481481
value: siteData
482482
}
483483
}
484-
console.log(`Warning: Can't create site data: ${site}`)
484+
console.log(`Warning: Can't create site data: ${JSON.stringify(site)}`)
485485
}
486486

487487
/**

test/unit/state/siteUtilTest.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,14 @@ describe('siteUtil', function () {
12591259
})
12601260
assert.equal(siteUtil.isHistoryEntry(siteDetail), true)
12611261
})
1262+
it('returns false for a default site', function () {
1263+
const siteDetail = Immutable.fromJS({
1264+
location: testUrl1,
1265+
tags: [siteTags.DEFAULT],
1266+
lastAccessedTime: 1
1267+
})
1268+
assert.equal(siteUtil.isHistoryEntry(siteDetail), false)
1269+
})
12621270
it('returns false for a bookmark entry with falsey lastAccessedTime', function () {
12631271
const siteDetail = Immutable.fromJS({
12641272
location: testUrl1,
@@ -1277,7 +1285,7 @@ describe('siteUtil', function () {
12771285
it('returns false for a brave default site', function () {
12781286
const siteDetail = Immutable.fromJS({
12791287
location: testUrl1,
1280-
tags: ['default'],
1288+
tags: [siteTags.DEFAULT],
12811289
lastAccessedTime: 1
12821290
})
12831291
assert.equal(siteUtil.isHistoryEntry(siteDetail), false)
@@ -1295,6 +1303,41 @@ describe('siteUtil', function () {
12951303
})
12961304
})
12971305

1306+
describe('isDefaultEntry', function () {
1307+
it('returns false for history entry which has lastAccessedTime', function () {
1308+
const siteDetail = Immutable.fromJS({
1309+
location: 'https://brave.com/',
1310+
tags: [siteTags.DEFAULT],
1311+
lastAccessedTime: 123
1312+
})
1313+
assert.equal(siteUtil.isDefaultEntry(siteDetail), false)
1314+
})
1315+
it('returns false for bookmark entry', function () {
1316+
const siteDetail = Immutable.fromJS({
1317+
location: 'https://brave.com/',
1318+
tags: [siteTags.BOOKMARK],
1319+
lastAccessedTime: 1
1320+
})
1321+
assert.equal(siteUtil.isDefaultEntry(siteDetail), false)
1322+
})
1323+
it('returns false for entry without lastAccessedTime', function () {
1324+
const siteDetail = Immutable.fromJS({
1325+
location: 'https://brave.com/',
1326+
tags: [siteTags.DEFAULT]
1327+
})
1328+
assert.equal(siteUtil.isDefaultEntry(siteDetail), false)
1329+
})
1330+
it('returns true for default entry', function () {
1331+
const siteDetail = Immutable.fromJS({
1332+
tags: [siteTags.DEFAULT],
1333+
lastAccessedTime: 1,
1334+
objectId: [210, 115, 31, 176, 57, 212, 167, 120, 104, 88, 88, 27, 141, 36, 235, 226],
1335+
location: testUrl1
1336+
})
1337+
assert.equal(siteUtil.isDefaultEntry(siteDetail), true)
1338+
})
1339+
})
1340+
12981341
describe('getFolder', function () {
12991342
const folder = Immutable.fromJS({
13001343
customTitle: 'folder1',

0 commit comments

Comments
 (0)