Skip to content

Commit

Permalink
Better fallback for unavailable event age (#3854)
Browse files Browse the repository at this point in the history
* Age fallback using origin_server_ts instead of 0

Signed-off-by: Timo K <toger5@hotmail.de>

* use getMsUntilExpiry for isExpired

Signed-off-by: Timo K <toger5@hotmail.de>

* fix tests
tests now also rely on localTimestamp. So this need to be mocked as well

Signed-off-by: Timo K <toger5@hotmail.de>

* better fallback for unavailable unsigned

Signed-off-by: Timo K <toger5@hotmail.de>

---------

Signed-off-by: Timo K <toger5@hotmail.de>
  • Loading branch information
toger5 committed Nov 6, 2023
1 parent a25ba7b commit 84bd8ab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 3 additions & 1 deletion spec/unit/matrixrtc/CallMembership.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ describe("CallMembership", () => {

it("considers memberships expired when local age large", () => {
const fakeEvent = makeMockEvent(1000);
fakeEvent.getLocalAge = jest.fn().mockReturnValue(6000);
const evAge = 6000;
fakeEvent.getLocalAge = jest.fn().mockReturnValue(evAge);
fakeEvent.localTimestamp = Date.now() - evAge;
const membership = new CallMembership(fakeEvent, membershipTemplate);
expect(membership.isExpired()).toEqual(true);
});
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/matrixrtc/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function mockRTCEvent(
getSender: jest.fn().mockReturnValue("@mock:user.example"),
getTs: jest.fn().mockReturnValue(1000),
getLocalAge: getLocalAgeFn,
localTimestamp: Date.now(),
localTimestamp: Date.now() - getLocalAgeFn(),
getRoomId: jest.fn().mockReturnValue(roomId),
sender: {
userId: "@mock:user.example",
Expand Down
2 changes: 1 addition & 1 deletion src/matrixrtc/CallMembership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class CallMembership {
}

public isExpired(): boolean {
return this.getAbsoluteExpiry() < this.parentEvent.getTs() + this.parentEvent.getLocalAge();
return this.getMsUntilExpiry() <= 0;
}

public getActiveFoci(): Focus[] {
Expand Down
19 changes: 17 additions & 2 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
});

this.txnId = event.txn_id;
this.localTimestamp = Date.now() - (this.getAge() ?? 0);
this.localTimestamp = Date.now() - (this.getAge() ?? this.fallbackAge());
this.reEmitter = new TypedReEmitter(this);
}

Expand Down Expand Up @@ -663,6 +663,21 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
return this.getUnsigned().age || this.event.age; // v2 / v1
}

/**
* The fallbackAge is computed by using the origin_server_ts. So it is not adjusted
* to the local device clock. It should never be used.
* If there is no unsigned field in the event this is a better fallback then 0.
* It is supposed to only be used like this: `ev.getAge() ?? ev.fallbackAge()`
*/
private fallbackAge(): number {
if (!this.getAge()) {
logger.warn(
"Age for event was not available, using `now - origin_server_ts` as a fallback. If the device clock is not correct issues might occur.",
);
}
return Math.max(Date.now() - this.getTs(), 0);
}

/**
* Get the age of the event when this function was called.
* This is the 'age' field adjusted according to how long this client has
Expand Down Expand Up @@ -1384,7 +1399,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
this.emit(MatrixEventEvent.LocalEventIdReplaced, this);
}

this.localTimestamp = Date.now() - this.getAge()!;
this.localTimestamp = Date.now() - (this.getAge() ?? this.fallbackAge());
}

/**
Expand Down

0 comments on commit 84bd8ab

Please sign in to comment.