mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
Moved the last-seen-at-updater to use the publication timezone
refs https://github.com/TryGhost/Team/issues/1306
- This removes the limitation described in commit ff46449ad6
- The only edge case is that when a publication changes their timezone, it will have maximum 24 hours where the member last_seen_at could be incorrect
This commit is contained in:
parent
d2d7fb3fe7
commit
bc5b8109e6
@ -1,6 +1,6 @@
|
|||||||
const DomainEvents = require('@tryghost/domain-events');
|
const DomainEvents = require('@tryghost/domain-events');
|
||||||
const {MemberPageViewEvent} = require('@tryghost/member-events');
|
const {MemberPageViewEvent} = require('@tryghost/member-events');
|
||||||
const moment = require('moment');
|
const moment = require('moment-timezone');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listen for `MemberViewEvent` to update the `member.last_seen_at` timestamp
|
* Listen for `MemberViewEvent` to update the `member.last_seen_at` timestamp
|
||||||
@ -9,10 +9,21 @@ class LastSeenAtUpdater {
|
|||||||
/**
|
/**
|
||||||
* Initializes the event subscriber
|
* Initializes the event subscriber
|
||||||
* @param {Object} deps dependencies
|
* @param {Object} deps dependencies
|
||||||
* @param {any} deps.memberModel The member model
|
* @param {Object} deps.models The list of model dependencies
|
||||||
|
* @param {any} deps.models.Member The Member model
|
||||||
|
* @param {Object} deps.services The list of service dependencies
|
||||||
|
* @param {any} deps.services.settingsCache The settings service
|
||||||
*/
|
*/
|
||||||
constructor({memberModel}) {
|
constructor({
|
||||||
this._memberModel = memberModel;
|
models: {
|
||||||
|
Member
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
settingsCache
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
this._memberModel = Member;
|
||||||
|
this._settingsCacheService = settingsCache;
|
||||||
DomainEvents.subscribe(MemberPageViewEvent, async (event) => {
|
DomainEvents.subscribe(MemberPageViewEvent, async (event) => {
|
||||||
await this.updateLastSeenAt(event.data.memberId, event.data.memberLastSeenAt, event.timestamp);
|
await this.updateLastSeenAt(event.data.memberId, event.data.memberLastSeenAt, event.timestamp);
|
||||||
});
|
});
|
||||||
@ -28,7 +39,8 @@ class LastSeenAtUpdater {
|
|||||||
* @param {Date} timestamp The event timestamp
|
* @param {Date} timestamp The event timestamp
|
||||||
*/
|
*/
|
||||||
async updateLastSeenAt(memberId, memberLastSeenAt, timestamp) {
|
async updateLastSeenAt(memberId, memberLastSeenAt, timestamp) {
|
||||||
if (memberLastSeenAt === null || moment(moment.utc(timestamp).startOf('day')).isAfter(moment.utc(memberLastSeenAt).startOf('day'))) {
|
const timezone = this._settingsCacheService.get('timezone');
|
||||||
|
if (memberLastSeenAt === null || moment(moment.utc(timestamp).tz(timezone).startOf('day')).isAfter(moment.utc(memberLastSeenAt).tz(timezone).startOf('day'))) {
|
||||||
await this._memberModel.update({
|
await this._memberModel.update({
|
||||||
last_seen_at: moment.utc(timestamp).format('YYYY-MM-DD HH:mm:ss')
|
last_seen_at: moment.utc(timestamp).format('YYYY-MM-DD HH:mm:ss')
|
||||||
}, {
|
}, {
|
||||||
|
@ -27,6 +27,6 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tryghost/domain-events": "^0.1.7",
|
"@tryghost/domain-events": "^0.1.7",
|
||||||
"@tryghost/member-events": "^0.3.5",
|
"@tryghost/member-events": "^0.3.5",
|
||||||
"moment": "^2.29.1"
|
"moment-timezone": "^0.5.34"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,64 @@ describe('LastSeenAtUpdater', function () {
|
|||||||
const now = moment('2022-02-28T18:00:00Z').utc();
|
const now = moment('2022-02-28T18:00:00Z').utc();
|
||||||
const previousLastSeen = moment('2022-02-27T23:00:00Z').toISOString();
|
const previousLastSeen = moment('2022-02-27T23:00:00Z').toISOString();
|
||||||
const spy = sinon.spy();
|
const spy = sinon.spy();
|
||||||
|
const settingsCache = sinon.stub().returns('Etc/UTC');
|
||||||
new LastSeenAtUpdater({
|
new LastSeenAtUpdater({
|
||||||
memberModel: {
|
models: {
|
||||||
|
Member: {
|
||||||
update: spy
|
update: spy
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
settingsCache: {
|
||||||
|
get: settingsCache
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
|
||||||
|
assert(spy.calledOnceWithExactly({
|
||||||
|
last_seen_at: now.format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
}, {
|
||||||
|
id: '1'
|
||||||
|
}), 'The LastSeenAtUpdater should attempt a member update with the current date.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works correctly on another timezone (not updating last_seen_at)', async function () {
|
||||||
|
const now = moment('2022-02-28T04:00:00Z').utc();
|
||||||
|
const previousLastSeen = moment('2022-02-27T20:00:00Z').toISOString();
|
||||||
|
const spy = sinon.spy();
|
||||||
|
const settingsCache = sinon.stub().returns('Asia/Bangkok');
|
||||||
|
new LastSeenAtUpdater({
|
||||||
|
models: {
|
||||||
|
Member: {
|
||||||
|
update: spy
|
||||||
|
}
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
settingsCache: {
|
||||||
|
get: settingsCache
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
|
||||||
|
assert(spy.notCalled, 'The LastSeenAtUpdater should attempt a member update when the new timestamp is within the same day in the publication timezone.');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works correctly on another timezone (updating last_seen_at)', async function () {
|
||||||
|
const now = moment('2022-02-28T04:00:00Z').utc();
|
||||||
|
const previousLastSeen = moment('2022-02-27T20:00:00Z').toISOString();
|
||||||
|
const spy = sinon.spy();
|
||||||
|
const settingsCache = sinon.stub().returns('Europe/Paris');
|
||||||
|
new LastSeenAtUpdater({
|
||||||
|
models: {
|
||||||
|
Member: {
|
||||||
|
update: spy
|
||||||
|
}
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
settingsCache: {
|
||||||
|
get: settingsCache
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
|
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
|
||||||
assert(spy.calledOnceWithExactly({
|
assert(spy.calledOnceWithExactly({
|
||||||
@ -31,10 +85,18 @@ describe('LastSeenAtUpdater', function () {
|
|||||||
const now = moment('2022-02-28T18:00:00Z');
|
const now = moment('2022-02-28T18:00:00Z');
|
||||||
const previousLastSeen = moment('2022-02-28T00:00:00Z').toISOString();
|
const previousLastSeen = moment('2022-02-28T00:00:00Z').toISOString();
|
||||||
const spy = sinon.spy();
|
const spy = sinon.spy();
|
||||||
|
const settingsCache = sinon.stub().returns('Etc/UTC');
|
||||||
new LastSeenAtUpdater({
|
new LastSeenAtUpdater({
|
||||||
memberModel: {
|
models: {
|
||||||
|
Member: {
|
||||||
update: spy
|
update: spy
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
settingsCache: {
|
||||||
|
get: settingsCache
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
|
DomainEvents.dispatch(MemberPageViewEvent.create({memberId: '1', memberLastSeenAt: previousLastSeen, url: '/'}, now.toDate()));
|
||||||
assert(spy.notCalled, 'The LastSeenAtUpdater should\'t update a member when the previous last_seen_at is close to the event timestamp.');
|
assert(spy.notCalled, 'The LastSeenAtUpdater should\'t update a member when the previous last_seen_at is close to the event timestamp.');
|
||||||
@ -43,10 +105,18 @@ describe('LastSeenAtUpdater', function () {
|
|||||||
it('Doesn\'t fire on other events', async function () {
|
it('Doesn\'t fire on other events', async function () {
|
||||||
const now = moment('2022-02-28T18:00:00Z');
|
const now = moment('2022-02-28T18:00:00Z');
|
||||||
const spy = sinon.spy();
|
const spy = sinon.spy();
|
||||||
|
const settingsCache = sinon.stub().returns('Etc/UTC');
|
||||||
new LastSeenAtUpdater({
|
new LastSeenAtUpdater({
|
||||||
memberModel: {
|
models: {
|
||||||
|
Member: {
|
||||||
update: spy
|
update: spy
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
services: {
|
||||||
|
settingsCache: {
|
||||||
|
get: settingsCache
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
DomainEvents.dispatch(MemberSubscribeEvent.create({memberId: '1', source: 'api'}, now.toDate()));
|
DomainEvents.dispatch(MemberSubscribeEvent.create({memberId: '1', source: 'api'}, now.toDate()));
|
||||||
assert(spy.notCalled, 'The LastSeenAtUpdater should never fire on MemberPageViewEvent events.');
|
assert(spy.notCalled, 'The LastSeenAtUpdater should never fire on MemberPageViewEvent events.');
|
||||||
|
Loading…
Reference in New Issue
Block a user