2022-02-23 21:13:42 +03:00
|
|
|
const {MemberPageViewEvent} = require('@tryghost/member-events');
|
2022-03-01 12:28:45 +03:00
|
|
|
const moment = require('moment-timezone');
|
2022-05-02 21:07:30 +03:00
|
|
|
const {IncorrectUsageError} = require('@tryghost/errors');
|
2022-02-23 21:13:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for `MemberViewEvent` to update the `member.last_seen_at` timestamp
|
|
|
|
*/
|
|
|
|
class LastSeenAtUpdater {
|
|
|
|
/**
|
|
|
|
* Initializes the event subscriber
|
|
|
|
* @param {Object} deps dependencies
|
2022-03-01 12:28:45 +03:00
|
|
|
* @param {Object} deps.services The list of service dependencies
|
2022-03-01 19:19:34 +03:00
|
|
|
* @param {any} deps.services.domainEvents The DomainEvents service
|
2022-03-01 12:28:45 +03:00
|
|
|
* @param {any} deps.services.settingsCache The settings service
|
2022-05-02 21:07:30 +03:00
|
|
|
* @param {() => object} deps.getMembersApi - A function which returns an instance of members-api
|
2022-02-23 21:13:42 +03:00
|
|
|
*/
|
2022-03-01 12:28:45 +03:00
|
|
|
constructor({
|
|
|
|
services: {
|
2022-03-01 19:19:34 +03:00
|
|
|
domainEvents,
|
2022-03-01 12:28:45 +03:00
|
|
|
settingsCache
|
2022-05-02 21:07:30 +03:00
|
|
|
},
|
|
|
|
getMembersApi
|
2022-03-01 12:28:45 +03:00
|
|
|
}) {
|
2022-05-02 21:07:30 +03:00
|
|
|
if (!getMembersApi) {
|
|
|
|
throw new IncorrectUsageError({message: 'Missing option getMembersApi'});
|
|
|
|
}
|
|
|
|
|
|
|
|
this._getMembersApi = getMembersApi;
|
2022-03-01 19:19:34 +03:00
|
|
|
this._domainEventsService = domainEvents;
|
2022-03-01 12:28:45 +03:00
|
|
|
this._settingsCacheService = settingsCache;
|
2022-03-01 19:12:59 +03:00
|
|
|
|
|
|
|
this._domainEventsService.subscribe(MemberPageViewEvent, async (event) => {
|
2022-02-23 21:13:42 +03:00
|
|
|
await this.updateLastSeenAt(event.data.memberId, event.data.memberLastSeenAt, event.timestamp);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-01 19:35:02 +03:00
|
|
|
* Updates the member.last_seen_at field if it wasn't updated in the current day yet (in the publication timezone)
|
|
|
|
* Example: current time is 2022-02-28 18:00:00
|
|
|
|
* - memberLastSeenAt is 2022-02-27 23:00:00, timestamp is current time, then `last_seen_at` is set to the current time
|
|
|
|
* - memberLastSeenAt is 2022-02-28 01:00:00, timestamp is current time, then `last_seen_at` isn't changed
|
2022-02-23 21:13:42 +03:00
|
|
|
* @param {string} memberId The id of the member to be udpated
|
|
|
|
* @param {string} memberLastSeenAt The previous last_seen_at property value for the current member
|
|
|
|
* @param {Date} timestamp The event timestamp
|
|
|
|
*/
|
|
|
|
async updateLastSeenAt(memberId, memberLastSeenAt, timestamp) {
|
2022-03-01 12:28:45 +03:00
|
|
|
const timezone = this._settingsCacheService.get('timezone');
|
2022-03-01 15:07:37 +03:00
|
|
|
if (memberLastSeenAt === null || moment(moment.utc(timestamp).tz(timezone).startOf('day')).isAfter(memberLastSeenAt)) {
|
2022-05-02 21:07:30 +03:00
|
|
|
const membersApi = await this._getMembersApi();
|
|
|
|
await membersApi.members.update({
|
2022-02-28 16:36:58 +03:00
|
|
|
last_seen_at: moment.utc(timestamp).format('YYYY-MM-DD HH:mm:ss')
|
2022-02-23 21:13:42 +03:00
|
|
|
}, {
|
|
|
|
id: memberId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LastSeenAtUpdater;
|