-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for unread thread notifications (#2726)
- Loading branch information
Germain
authored
Oct 5, 2022
1 parent
ff720e3
commit 21a6f61
Showing
16 changed files
with
551 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { MethodKeysOf, mocked, MockedObject } from "jest-mock"; | ||
|
||
import { ClientEventHandlerMap, EmittedEvents, MatrixClient } from "../../src/client"; | ||
import { TypedEventEmitter } from "../../src/models/typed-event-emitter"; | ||
import { User } from "../../src/models/user"; | ||
|
||
/** | ||
* Mock client with real event emitter | ||
* useful for testing code that listens | ||
* to MatrixClient events | ||
*/ | ||
export class MockClientWithEventEmitter extends TypedEventEmitter<EmittedEvents, ClientEventHandlerMap> { | ||
constructor(mockProperties: Partial<Record<MethodKeysOf<MatrixClient>, unknown>> = {}) { | ||
super(); | ||
Object.assign(this, mockProperties); | ||
} | ||
} | ||
|
||
/** | ||
* - make a mock client | ||
* - cast the type to mocked(MatrixClient) | ||
* - spy on MatrixClientPeg.get to return the mock | ||
* eg | ||
* ``` | ||
* const mockClient = getMockClientWithEventEmitter({ | ||
getUserId: jest.fn().mockReturnValue(aliceId), | ||
}); | ||
* ``` | ||
*/ | ||
export const getMockClientWithEventEmitter = ( | ||
mockProperties: Partial<Record<MethodKeysOf<MatrixClient>, unknown>>, | ||
): MockedObject<MatrixClient> => { | ||
const mock = mocked(new MockClientWithEventEmitter(mockProperties) as unknown as MatrixClient); | ||
return mock; | ||
}; | ||
|
||
/** | ||
* Returns basic mocked client methods related to the current user | ||
* ``` | ||
* const mockClient = getMockClientWithEventEmitter({ | ||
...mockClientMethodsUser('@mytestuser:domain'), | ||
}); | ||
* ``` | ||
*/ | ||
export const mockClientMethodsUser = (userId = '@alice:domain') => ({ | ||
getUserId: jest.fn().mockReturnValue(userId), | ||
getUser: jest.fn().mockReturnValue(new User(userId)), | ||
isGuest: jest.fn().mockReturnValue(false), | ||
mxcUrlToHttp: jest.fn().mockReturnValue('mock-mxcUrlToHttp'), | ||
credentials: { userId }, | ||
getThreePids: jest.fn().mockResolvedValue({ threepids: [] }), | ||
getAccessToken: jest.fn(), | ||
}); | ||
|
||
/** | ||
* Returns basic mocked client methods related to rendering events | ||
* ``` | ||
* const mockClient = getMockClientWithEventEmitter({ | ||
...mockClientMethodsUser('@mytestuser:domain'), | ||
}); | ||
* ``` | ||
*/ | ||
export const mockClientMethodsEvents = () => ({ | ||
decryptEventIfNeeded: jest.fn(), | ||
getPushActionsForEvent: jest.fn(), | ||
}); | ||
|
||
/** | ||
* Returns basic mocked client methods related to server support | ||
*/ | ||
export const mockClientMethodsServer = (): Partial<Record<MethodKeysOf<MatrixClient>, unknown>> => ({ | ||
doesServerSupportSeparateAddAndBind: jest.fn(), | ||
getIdentityServerUrl: jest.fn(), | ||
getHomeserverUrl: jest.fn(), | ||
getCapabilities: jest.fn().mockReturnValue({}), | ||
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(false), | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { | ||
EventType, | ||
fixNotificationCountOnDecryption, | ||
MatrixClient, | ||
MatrixEvent, | ||
MsgType, | ||
NotificationCountType, | ||
RelationType, | ||
Room, | ||
} from "../../src/matrix"; | ||
import { IActionsObject } from "../../src/pushprocessor"; | ||
import { ReEmitter } from "../../src/ReEmitter"; | ||
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../test-utils/client"; | ||
import { mkEvent, mock } from "../test-utils/test-utils"; | ||
|
||
let mockClient: MatrixClient; | ||
let room: Room; | ||
let event: MatrixEvent; | ||
let threadEvent: MatrixEvent; | ||
|
||
const ROOM_ID = "!roomId:example.org"; | ||
let THREAD_ID; | ||
|
||
function mkPushAction(notify, highlight): IActionsObject { | ||
return { | ||
notify, | ||
tweaks: { | ||
highlight, | ||
}, | ||
}; | ||
} | ||
|
||
describe("fixNotificationCountOnDecryption", () => { | ||
beforeEach(() => { | ||
mockClient = getMockClientWithEventEmitter({ | ||
...mockClientMethodsUser(), | ||
getPushActionsForEvent: jest.fn().mockReturnValue(mkPushAction(true, true)), | ||
getRoom: jest.fn().mockImplementation(() => room), | ||
decryptEventIfNeeded: jest.fn().mockResolvedValue(void 0), | ||
supportsExperimentalThreads: jest.fn().mockReturnValue(true), | ||
}); | ||
mockClient.reEmitter = mock(ReEmitter, 'ReEmitter'); | ||
|
||
room = new Room(ROOM_ID, mockClient, mockClient.getUserId()); | ||
room.setUnreadNotificationCount(NotificationCountType.Total, 1); | ||
room.setUnreadNotificationCount(NotificationCountType.Highlight, 0); | ||
|
||
event = mkEvent({ | ||
type: EventType.RoomMessage, | ||
content: { | ||
msgtype: MsgType.Text, | ||
body: "Hello world!", | ||
}, | ||
event: true, | ||
}, mockClient); | ||
|
||
THREAD_ID = event.getId(); | ||
threadEvent = mkEvent({ | ||
type: EventType.RoomMessage, | ||
content: { | ||
"m.relates_to": { | ||
rel_type: RelationType.Thread, | ||
event_id: THREAD_ID, | ||
}, | ||
"msgtype": MsgType.Text, | ||
"body": "Thread reply", | ||
}, | ||
event: true, | ||
}); | ||
room.createThread(THREAD_ID, event, [threadEvent], false); | ||
|
||
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)); | ||
}); | ||
|
||
it("changes the room count to highlight on decryption", () => { | ||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(1); | ||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(0); | ||
|
||
fixNotificationCountOnDecryption(mockClient, event); | ||
|
||
expect(room.getUnreadNotificationCount(NotificationCountType.Total)).toBe(1); | ||
expect(room.getUnreadNotificationCount(NotificationCountType.Highlight)).toBe(1); | ||
}); | ||
|
||
it("changes the thread count to highlight on decryption", () => { | ||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(1); | ||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(0); | ||
|
||
fixNotificationCountOnDecryption(mockClient, threadEvent); | ||
|
||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Total)).toBe(1); | ||
expect(room.getThreadUnreadNotificationCount(THREAD_ID, NotificationCountType.Highlight)).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.