You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-30 04:23:07 +03:00
Ensure we don't overinflate the total notification count (#3634)
* Ensure we don't overinflate the total notification count By correctly comparing push rules before & after decryption * DRY the code * Testsssss * Update tests
This commit is contained in:
committed by
GitHub
parent
615f7f9e72
commit
fd0c4a7f56
@ -303,8 +303,13 @@ describe("EventTimelineSet", () => {
|
||||
messageEventIsDecryptionFailureSpy.mockReturnValue(true);
|
||||
replyEventIsDecryptionFailureSpy.mockReturnValue(true);
|
||||
|
||||
messageEvent.emit(MatrixEventEvent.Decrypted, messageEvent);
|
||||
replyEvent.emit(MatrixEventEvent.Decrypted, replyEvent);
|
||||
messageEvent.emit(
|
||||
MatrixEventEvent.Decrypted,
|
||||
messageEvent,
|
||||
undefined,
|
||||
messageEvent.getPushDetails(),
|
||||
);
|
||||
replyEvent.emit(MatrixEventEvent.Decrypted, replyEvent, undefined, replyEvent.getPushDetails());
|
||||
|
||||
// simulate decryption
|
||||
messageEventIsDecryptionFailureSpy.mockReturnValue(false);
|
||||
@ -313,8 +318,13 @@ describe("EventTimelineSet", () => {
|
||||
messageEventShouldAttemptDecryptionSpy.mockReturnValue(false);
|
||||
replyEventShouldAttemptDecryptionSpy.mockReturnValue(false);
|
||||
|
||||
messageEvent.emit(MatrixEventEvent.Decrypted, messageEvent);
|
||||
replyEvent.emit(MatrixEventEvent.Decrypted, replyEvent);
|
||||
messageEvent.emit(
|
||||
MatrixEventEvent.Decrypted,
|
||||
messageEvent,
|
||||
undefined,
|
||||
messageEvent.getPushDetails(),
|
||||
);
|
||||
replyEvent.emit(MatrixEventEvent.Decrypted, replyEvent, undefined, replyEvent.getPushDetails());
|
||||
});
|
||||
|
||||
itShouldReturnTheRelatedEvents();
|
||||
|
@ -54,7 +54,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
mockClient = getMockClientWithEventEmitter({
|
||||
...mockClientMethodsUser(),
|
||||
isInitialSyncComplete: jest.fn().mockReturnValue(false),
|
||||
getPushActionsForEvent: jest.fn().mockReturnValue(mkPushAction(true, true)),
|
||||
getPushActionsForEvent: jest.fn((ev) => event.getPushActions() ?? mkPushAction(true, true)),
|
||||
getRoom: jest.fn().mockImplementation(() => room),
|
||||
decryptEventIfNeeded: jest.fn().mockResolvedValue(void 0),
|
||||
supportsThreads: jest.fn().mockReturnValue(true),
|
||||
@ -125,15 +125,15 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 1);
|
||||
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0);
|
||||
|
||||
event.getPushActions = jest.fn().mockReturnValue(mkPushAction(false, false));
|
||||
threadEvent.getPushActions = jest.fn().mockReturnValue(mkPushAction(false, false));
|
||||
event.getPushActions = jest.fn().mockReturnValue(mkPushAction(true, true));
|
||||
threadEvent.getPushActions = jest.fn().mockReturnValue(mkPushAction(true, true));
|
||||
});
|
||||
|
||||
it("changes the room count to highlight on decryption", () => {
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(2);
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0);
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, event);
|
||||
fixNotificationCountOnDecryption(mockClient, event, {});
|
||||
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(3);
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(1);
|
||||
@ -143,7 +143,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
room.setUnreadNotificationCount(NotificationCountType.Total, 0);
|
||||
room.setUnreadNotificationCount(NotificationCountType.Highlight, 0);
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, event);
|
||||
fixNotificationCountOnDecryption(mockClient, event, {});
|
||||
|
||||
expect(room.getRoomUnreadNotificationCount(NotificationCountType.Total)).toBe(1);
|
||||
expect(room.getRoomUnreadNotificationCount(NotificationCountType.Highlight)).toBe(1);
|
||||
@ -153,7 +153,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(1);
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(0);
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, threadEvent);
|
||||
fixNotificationCountOnDecryption(mockClient, threadEvent, {});
|
||||
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(2);
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(1);
|
||||
@ -163,7 +163,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total, 0);
|
||||
room.setThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight, 0);
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, event);
|
||||
fixNotificationCountOnDecryption(mockClient, event, {});
|
||||
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(0);
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(0);
|
||||
@ -187,7 +187,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
event: true,
|
||||
});
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, unknownThreadEvent);
|
||||
fixNotificationCountOnDecryption(mockClient, unknownThreadEvent, {});
|
||||
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(0);
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(0);
|
||||
@ -201,7 +201,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
event.getPushActions = jest.fn().mockReturnValue(mkPushAction(true, false));
|
||||
mockClient.getPushActionsForEvent = jest.fn().mockReturnValue(mkPushAction(false, false));
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, event);
|
||||
fixNotificationCountOnDecryption(mockClient, event, {});
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(0);
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0);
|
||||
});
|
||||
@ -213,7 +213,7 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
threadEvent.getPushActions = jest.fn().mockReturnValue(mkPushAction(true, false));
|
||||
mockClient.getPushActionsForEvent = jest.fn().mockReturnValue(mkPushAction(false, false));
|
||||
|
||||
fixNotificationCountOnDecryption(mockClient, event);
|
||||
fixNotificationCountOnDecryption(mockClient, event, {});
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(0);
|
||||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(0);
|
||||
});
|
||||
@ -231,4 +231,15 @@ describe("fixNotificationCountOnDecryption", () => {
|
||||
room.setThreadUnreadNotificationCount("$123", NotificationCountType.Highlight, 5);
|
||||
expect(cb).toHaveBeenLastCalledWith({ highlight: 5 }, "$123");
|
||||
});
|
||||
|
||||
it("should not increment notification count where event was already contributing notification", () => {
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(2);
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0);
|
||||
|
||||
event.getPushActions = jest.fn().mockReturnValue(mkPushAction(true, false));
|
||||
fixNotificationCountOnDecryption(mockClient, event, { actions: { notify: true, tweaks: {} } });
|
||||
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(2);
|
||||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0);
|
||||
});
|
||||
});
|
||||
|
@ -1037,7 +1037,12 @@ describe("RoomState", function () {
|
||||
|
||||
// this event is a message after decryption
|
||||
decryptingRelatedEvent.event.type = EventType.RoomMessage;
|
||||
decryptingRelatedEvent.emit(MatrixEventEvent.Decrypted, decryptingRelatedEvent);
|
||||
decryptingRelatedEvent.emit(
|
||||
MatrixEventEvent.Decrypted,
|
||||
decryptingRelatedEvent,
|
||||
undefined,
|
||||
decryptingRelatedEvent.getPushDetails(),
|
||||
);
|
||||
|
||||
expect(addLocationsSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
@ -3428,13 +3428,13 @@ describe("Room", function () {
|
||||
expect(room.polls.get(pollStartEventId)).toBeUndefined();
|
||||
|
||||
// now emit a Decrypted event but keep the decryption failure
|
||||
pollStartEvent.emit(MatrixEventEvent.Decrypted, pollStartEvent);
|
||||
pollStartEvent.emit(MatrixEventEvent.Decrypted, pollStartEvent, undefined, pollStartEvent.getPushDetails());
|
||||
// still do not expect a poll to show up for the room
|
||||
expect(room.polls.get(pollStartEventId)).toBeUndefined();
|
||||
|
||||
// clear decryption failure and emit a Decrypted event again
|
||||
isDecryptionFailureSpy.mockRestore();
|
||||
pollStartEvent.emit(MatrixEventEvent.Decrypted, pollStartEvent);
|
||||
pollStartEvent.emit(MatrixEventEvent.Decrypted, pollStartEvent, undefined, pollStartEvent.getPushDetails());
|
||||
|
||||
// the poll should now show up in the room's polls
|
||||
const poll = room.polls.get(pollStartEventId);
|
||||
|
Reference in New Issue
Block a user