Bug 1791615 - Pocket newtab configurable cache time. r=gvn

Differential Revision: https://phabricator.services.mozilla.com/D158059
This commit is contained in:
scott 2022-09-27 00:53:35 +00:00
Родитель a9ba6e830d
Коммит 2bb8defd02
3 изменённых файлов: 107 добавлений и 1 удалений

Просмотреть файл

@ -245,6 +245,7 @@ class DiscoveryStreamFeed {
const nimbusConfig = this.store.getState().Prefs.values?.pocketConfig || {};
const { region } = this.store.getState().Prefs.values;
this.setupSpocsCacheUpdateTime();
const saveToPocketCardRegions = nimbusConfig.saveToPocketCardRegions
?.split(",")
.map(s => s.trim());
@ -395,6 +396,34 @@ class DiscoveryStreamFeed {
return null;
}
get spocsCacheUpdateTime() {
if (this._spocsCacheUpdateTime) {
return this._spocsCacheUpdateTime;
}
this.setupSpocsCacheUpdateTime();
return this._spocsCacheUpdateTime;
}
setupSpocsCacheUpdateTime() {
const nimbusConfig = this.store.getState().Prefs.values?.pocketConfig || {};
const { spocsCacheTimeout } = nimbusConfig;
const MAX_TIMEOUT = 30;
const MIN_TIMEOUT = 5;
// We do a bit of min max checking the the configured value is between
// 5 and 30 minutes, to protect against unreasonable values.
if (
spocsCacheTimeout &&
spocsCacheTimeout <= MAX_TIMEOUT &&
spocsCacheTimeout >= MIN_TIMEOUT
) {
// This value is in minutes, but we want ms.
this._spocsCacheUpdateTime = spocsCacheTimeout * 60 * 1000;
} else {
// The const is already in ms.
this._spocsCacheUpdateTime = SPOCS_FEEDS_UPDATE_TIME;
}
}
/**
* Returns true if data in the cache for a particular key has expired or is missing.
* @param {object} cachedData data returned from cache.get()
@ -406,7 +435,7 @@ class DiscoveryStreamFeed {
const { layout, spocs, feeds } = cachedData;
const updateTimePerComponent = {
layout: LAYOUT_UPDATE_TIME,
spocs: SPOCS_FEEDS_UPDATE_TIME,
spocs: this.spocsCacheUpdateTime,
feed: COMPONENT_FEEDS_UPDATE_TIME,
};
const EXPIRATION_TIME = isStartup

Просмотреть файл

@ -715,6 +715,7 @@ describe("DiscoveryStreamFeed", () => {
let fakeDiscoveryStream;
beforeEach(() => {
fakeDiscoveryStream = {
Prefs: {},
DiscoveryStream: {
layout: [
{ components: [{ feed: { url: "foo.com" } }] },
@ -2457,6 +2458,76 @@ describe("DiscoveryStreamFeed", () => {
});
});
describe("#spocsCacheUpdateTime", () => {
it("should call setupSpocsCacheUpdateTime", () => {
const defaultCacheTime = 30 * 60 * 1000;
sandbox.spy(feed, "setupSpocsCacheUpdateTime");
const cacheTime = feed.spocsCacheUpdateTime;
assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime);
assert.equal(cacheTime, defaultCacheTime);
assert.calledOnce(feed.setupSpocsCacheUpdateTime);
});
it("should return _spocsCacheUpdateTime", () => {
sandbox.spy(feed, "setupSpocsCacheUpdateTime");
const testCacheTime = 123;
feed._spocsCacheUpdateTime = testCacheTime;
const cacheTime = feed.spocsCacheUpdateTime;
// Ensure _spocsCacheUpdateTime was not changed.
assert.equal(feed._spocsCacheUpdateTime, testCacheTime);
assert.equal(cacheTime, testCacheTime);
assert.notCalled(feed.setupSpocsCacheUpdateTime);
});
});
describe("#setupSpocsCacheUpdateTime", () => {
it("should set _spocsCacheUpdateTime with default value", () => {
const defaultCacheTime = 30 * 60 * 1000;
feed.setupSpocsCacheUpdateTime();
assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime);
});
it("should set _spocsCacheUpdateTime with min", () => {
const defaultCacheTime = 30 * 60 * 1000;
feed.store.getState = () => ({
Prefs: {
values: {
pocketConfig: {
spocsCacheTimeout: 1,
},
},
},
});
feed.setupSpocsCacheUpdateTime();
assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime);
});
it("should set _spocsCacheUpdateTime with max", () => {
const defaultCacheTime = 30 * 60 * 1000;
feed.store.getState = () => ({
Prefs: {
values: {
pocketConfig: {
spocsCacheTimeout: 31,
},
},
},
});
feed.setupSpocsCacheUpdateTime();
assert.equal(feed._spocsCacheUpdateTime, defaultCacheTime);
});
it("should set _spocsCacheUpdateTime with spocsCacheTimeout", () => {
feed.store.getState = () => ({
Prefs: {
values: {
pocketConfig: {
spocsCacheTimeout: 20,
},
},
},
});
feed.setupSpocsCacheUpdateTime();
assert.equal(feed._spocsCacheUpdateTime, 20 * 60 * 1000);
});
});
describe("#isExpired", () => {
it("should throw if the key is not valid", () => {
assert.throws(() => {

Просмотреть файл

@ -402,6 +402,12 @@ pocketNewtab:
browser.newtabpage.activity-stream.discoverystream.spocs.personalized
description: >-
Enables Pocket sponsored content personalization.
spocsCacheTimeout:
type: int
fallbackPref: >-
browser.newtabpage.activity-stream.discoverystream.spocs.cacheTimeout
description: >-
Set sponsored content cache timeout in minutes.
saveToPocket:
description: The save to Pocket feature
owner: sdowne@getpocket.com