Files
tubestation/browser/components/firefoxview/firefox-view-places-query.sys.mjs
Jonathan Sudiaman e1670cced8 Bug 1828669 - Dedupe URLs in the history view r=mkaply
In Firefox View, remove duplicate history visits per container. When sorting by date, a container is a single day. When sorting by site, a container is a single host (which, in this case, effectively means that //every// single visit listed must have a unique URL).

We want to get a bit smarter about determining which visits are "duplicates". This is a bit more involved than simply comparing exact URLs. [This doc](https://docs.google.com/document/d/1t13dAW6yMyxKys2Emthe0tZE1Q9j-CTsjRb2fa-LNL0/edit?usp=sharing) explains in further detail, but the summary is that visits should be considered duplicates **if multiple documents have the same title, but have the same base URL (URL possibly without query (?) or fragment (#))**.

SQL is leveraged a bit to handle the straightforward case, i.e. matching exact URL. Frontend is used to further dedupe by "Base URL" + document title.

Some of the caching in `FirefoxViewPlacesQuery` was replaced in favor of getters which utilize existing caches from the base class. Perhaps there's no need for this extension class, will consider refactoring in the future.

https://treeherder.mozilla.org/jobs?repo=try&revision=d8f618ad7c16f3254e871b53725cf039f85d6bc3

Differential Revision: https://phabricator.services.mozilla.com/D185315
2023-08-14 22:27:26 +00:00

179 lines
4.7 KiB
JavaScript

/* 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/. */
import { PlacesQuery } from "resource://gre/modules/PlacesQuery.sys.mjs";
/**
* Extension of PlacesQuery which provides additional caches for Firefox View.
*/
export class FirefoxViewPlacesQuery extends PlacesQuery {
/** @type {Date} */
#todaysDate = null;
/** @type {Date} */
#yesterdaysDate = null;
get visitsFromToday() {
if (this.cachedHistory == null || this.#todaysDate == null) {
return [];
}
const mapKey = this.getStartOfDayTimestamp(this.#todaysDate);
return this.cachedHistory.has(mapKey) ? this.cachedHistory.get(mapKey) : [];
}
get visitsFromYesterday() {
if (this.cachedHistory == null || this.#yesterdaysDate == null) {
return [];
}
const mapKey = this.getStartOfDayTimestamp(this.#yesterdaysDate);
return this.cachedHistory.has(mapKey) ? this.cachedHistory.get(mapKey) : [];
}
/**
* Get a list of visits per day for each day on this month, excluding today
* and yesterday.
*
* @returns {HistoryVisit[][]}
* A list of visits for each day.
*/
get visitsByDay() {
const visitsPerDay = [];
for (const [time, visits] of this.cachedHistory.entries()) {
const date = new Date(time);
if (
this.#isSameDate(date, this.#todaysDate) ||
this.#isSameDate(date, this.#yesterdaysDate)
) {
continue;
} else if (!this.#isSameMonth(date, this.#todaysDate)) {
break;
} else {
visitsPerDay.push(visits);
}
}
return visitsPerDay;
}
/**
* Get a list of visits per month for each month, excluding this one, and
* excluding yesterday's visits if yesterday happens to fall on the previous
* month.
*
* @returns {HistoryVisit[][]}
* A list of visits for each month.
*/
get visitsByMonth() {
const visitsPerMonth = [];
let previousMonth = null;
for (const [time, visits] of this.cachedHistory.entries()) {
const date = new Date(time);
if (
this.#isSameDate(date, this.#yesterdaysDate) ||
this.#isSameMonth(date, this.#todaysDate)
) {
continue;
}
const month = this.getStartOfMonthTimestamp(date);
if (month !== previousMonth) {
visitsPerMonth.push(visits);
} else {
visitsPerMonth[visitsPerMonth.length - 1] =
visitsPerMonth[visitsPerMonth.length - 1].concat(visits);
}
previousMonth = month;
}
return visitsPerMonth;
}
appendToCache(visit) {
super.appendToCache(visit);
this.#normalizeVisit(visit);
}
insertSortedIntoCache(visit) {
super.insertSortedIntoCache(visit);
this.#normalizeVisit(visit);
}
/**
* Normalize data for fxview-tabs-list.
*
* @param {HistoryVisit} visit
* The visit to format.
*/
#normalizeVisit(visit) {
visit.time = visit.date.getTime();
visit.title = visit.title || visit.url;
visit.icon = `page-icon:${visit.url}`;
visit.primaryL10nId = "fxviewtabrow-tabs-list-tab";
visit.primaryL10nArgs = JSON.stringify({
targetURI: visit.url,
});
visit.secondaryL10nId = "fxviewtabrow-options-menu-button";
visit.secondaryL10nArgs = JSON.stringify({
tabTitle: visit.title || visit.url,
});
}
async fetchHistory() {
await super.fetchHistory();
if (this.cachedHistoryOptions.sortBy === "date") {
this.#setTodaysDate();
}
}
handlePageVisited(event) {
const visit = super.handlePageVisited(event);
if (!visit) {
return;
}
if (this.cachedHistoryOptions.sortBy === "date") {
this.#setTodaysDate();
}
}
#setTodaysDate() {
const now = new Date();
this.#todaysDate = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate()
);
this.#yesterdaysDate = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() - 1
);
}
/**
* Given two date instances, check if their dates are equivalent.
*
* @param {Date} dateToCheck
* @param {Date} date
* @returns {boolean}
* Whether both date instances have equivalent dates.
*/
#isSameDate(dateToCheck, date) {
return (
dateToCheck.getDate() === date.getDate() &&
this.#isSameMonth(dateToCheck, date)
);
}
/**
* Given two date instances, check if their months are equivalent.
*
* @param {Date} dateToCheck
* @param {Date} month
* @returns {boolean}
* Whether both date instances have equivalent months.
*/
#isSameMonth(dateToCheck, month) {
return (
dateToCheck.getMonth() === month.getMonth() &&
dateToCheck.getFullYear() === month.getFullYear()
);
}
}