-
-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes unwanted highlight notifications with encrypted threads (#2862)
- Loading branch information
Germain
committed
Nov 11, 2022
1 parent
54d11e1
commit a7f0ba9
Showing
4 changed files
with
216 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
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 { RelationType } from "../../src/@types/event"; | ||
import { MatrixClient } from "../../src/client"; | ||
import { MatrixEvent, MatrixEventEvent } from "../../src/models/event"; | ||
import { Room } from "../../src/models/room"; | ||
import { Thread } from "../../src/models/thread"; | ||
import { mkMessage } from "./test-utils"; | ||
|
||
export const makeThreadEvent = ({ rootEventId, replyToEventId, ...props }: any & { | ||
rootEventId: string; replyToEventId: string; event?: boolean; | ||
}): MatrixEvent => mkMessage({ | ||
...props, | ||
relatesTo: { | ||
event_id: rootEventId, | ||
rel_type: "m.thread", | ||
['m.in_reply_to']: { | ||
event_id: replyToEventId, | ||
}, | ||
}, | ||
}); | ||
|
||
type MakeThreadEventsProps = { | ||
roomId: Room["roomId"]; | ||
// root message user id | ||
authorId: string; | ||
// user ids of thread replies | ||
// cycled through until thread length is fulfilled | ||
participantUserIds: string[]; | ||
// number of messages in the thread, root message included | ||
// optional, default 2 | ||
length?: number; | ||
ts?: number; | ||
// provide to set current_user_participated accurately | ||
currentUserId?: string; | ||
}; | ||
|
||
export const makeThreadEvents = ({ | ||
roomId, authorId, participantUserIds, length = 2, ts = 1, currentUserId, | ||
}: MakeThreadEventsProps): { rootEvent: MatrixEvent, events: MatrixEvent[] } => { | ||
const rootEvent = mkMessage({ | ||
user: authorId, | ||
room: roomId, | ||
msg: 'root event message ' + Math.random(), | ||
ts, | ||
event: true, | ||
}); | ||
|
||
const rootEventId = rootEvent.getId(); | ||
const events = [rootEvent]; | ||
|
||
for (let i = 1; i < length; i++) { | ||
const prevEvent = events[i - 1]; | ||
const replyToEventId = prevEvent.getId(); | ||
const user = participantUserIds[i % participantUserIds.length]; | ||
events.push(makeThreadEvent({ | ||
user, | ||
room: roomId, | ||
event: true, | ||
msg: `reply ${i} by ${user}`, | ||
rootEventId, | ||
replyToEventId, | ||
// replies are 1ms after each other | ||
ts: ts + i, | ||
})); | ||
} | ||
|
||
rootEvent.setUnsigned({ | ||
"m.relations": { | ||
[RelationType.Thread]: { | ||
latest_event: events[events.length - 1], | ||
count: length, | ||
current_user_participated: [...participantUserIds, authorId].includes(currentUserId ?? ""), | ||
}, | ||
}, | ||
}); | ||
|
||
return { rootEvent, events }; | ||
}; | ||
|
||
type MakeThreadProps = { | ||
room: Room; | ||
client: MatrixClient; | ||
authorId: string; | ||
participantUserIds: string[]; | ||
length?: number; | ||
ts?: number; | ||
}; | ||
|
||
export const mkThread = ({ | ||
room, | ||
client, | ||
authorId, | ||
participantUserIds, | ||
length = 2, | ||
ts = 1, | ||
}: MakeThreadProps): { thread: Thread, rootEvent: MatrixEvent, events: MatrixEvent[] } => { | ||
const { rootEvent, events } = makeThreadEvents({ | ||
roomId: room.roomId, | ||
authorId, | ||
participantUserIds, | ||
length, | ||
ts, | ||
currentUserId: client.getUserId() ?? "", | ||
}); | ||
expect(rootEvent).toBeTruthy(); | ||
|
||
for (const evt of events) { | ||
room?.reEmitter.reEmit(evt, [ | ||
MatrixEventEvent.BeforeRedaction, | ||
]); | ||
} | ||
|
||
const thread = room.createThread(rootEvent.getId() ?? "", rootEvent, events, true); | ||
// So that we do not have to mock the thread loading | ||
thread.initialEventsFetched = true; | ||
thread.addEvents(events, true); | ||
|
||
return { thread, rootEvent, events }; | ||
}; |
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