Navigation Menu

Skip to content

Commit

Permalink
fix: Compute guest expiration only once server time diff have been co…
Browse files Browse the repository at this point in the history
…mputed (#5569)
  • Loading branch information
atomrc committed Jan 8, 2019
1 parent 8026846 commit 64c00e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
13 changes: 8 additions & 5 deletions app/script/time/ServerTimeRepository.js
Expand Up @@ -17,26 +17,29 @@
*
*/

import ko from 'knockout';

window.z = window.z || {};
window.z.time = z.time || {};

z.time.ServerTimeRepository = class ServerTimeRepository {
constructor() {
this.logger = new z.util.Logger('z.time.ServerTimeRepository', z.config.LOGGER.OPTIONS);
this._timeOffset = undefined;
this.timeOffset = ko.observable(undefined);
}

computeTimeOffset(serverTimeString) {
this._timeOffset = new Date() - new Date(serverTimeString);
this.logger.info(`Current backend time is '${serverTimeString}'. Time offset updated to '${this._timeOffset}' ms`);
const timeOffset = new Date() - new Date(serverTimeString);
this.timeOffset(timeOffset);
this.logger.info(`Current backend time is '${serverTimeString}'. Time offset updated to '${this.timeOffset()}' ms`);
}

getTimeOffset() {
if (this._timeOffset === undefined) {
if (this.timeOffset() === undefined) {
this.logger.warn('Trying to get server/client time offset, but no server time has been set.');
return 0;
}
return this._timeOffset;
return this.timeOffset();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions app/script/user/UserMapper.js
Expand Up @@ -129,8 +129,11 @@ z.user.UserMapper = class UserMapper {

if (expirationDate) {
userEntity.isTemporaryGuest(true);
const adjustedTimestamp = this.serverTimeRepository.toLocalTimestamp(new Date(expirationDate).getTime());
userEntity.setGuestExpiration(adjustedTimestamp);
const timeOffsetSubscription = this.serverTimeRepository.timeOffset.subscribe(() => {
const adjustedTimestamp = this.serverTimeRepository.toLocalTimestamp(new Date(expirationDate).getTime());
userEntity.setGuestExpiration(adjustedTimestamp);
timeOffsetSubscription.dispose();
});
}

if (handle) {
Expand Down
3 changes: 3 additions & 0 deletions test/unit_tests/user/UserMapperSpec.js
Expand Up @@ -147,6 +147,9 @@ describe('User Mapper', () => {
const data = {expires_at: expirationDate.toISOString(), id: userEntity.id};
mapper.updateUserFromObject(userEntity, data);

expect(mapper.serverTimeRepository.toLocalTimestamp).not.toHaveBeenCalledWith();
mapper.serverTimeRepository.timeOffset(10);

expect(mapper.serverTimeRepository.toLocalTimestamp).toHaveBeenCalledWith(expirationDate.getTime());
});

Expand Down

0 comments on commit 64c00e4

Please sign in to comment.