Make thread replies trigger a room list re-ordering (#9510)

pull/28788/head^2
Germain 2022-10-27 13:50:05 +01:00 committed by GitHub
parent af47b74992
commit 07003a5bc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 2 deletions

View File

@ -73,7 +73,7 @@ export const sortRooms = (rooms: Room[]): Room[] => {
};
const getLastTs = (r: Room, userId: string) => {
const ts = (() => {
const mainTimelineLastTs = (() => {
// Apparently we can have rooms without timelines, at least under testing
// environments. Just return MAX_INT when this happens.
if (!r?.timeline) {
@ -108,7 +108,13 @@ const getLastTs = (r: Room, userId: string) => {
// This is better than just assuming the last event was forever ago.
return r.timeline[0]?.getTs() ?? Number.MAX_SAFE_INTEGER;
})();
return ts;
const threadLastEventTimestamps = r.getThreads().map(thread => {
const event = thread.replyToEvent ?? thread.rootEvent;
return event!.getTs();
});
return Math.max(mainTimelineLastTs, ...threadLastEventTimestamps);
};
/**

View File

@ -21,6 +21,7 @@ import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import "../../../../src/stores/room-list/RoomListStore";
import { RecentAlgorithm } from "../../../../src/stores/room-list/algorithms/tag-sorting/RecentAlgorithm";
import { EffectiveMembership } from "../../../../src/utils/membership";
import { makeThreadEvent, mkThread } from "../../../test-utils/threads";
describe("RecentAlgorithm", () => {
let algorithm;
@ -122,6 +123,60 @@ describe("RecentAlgorithm", () => {
room1.addLiveEvents([evt]);
expect(algorithm.sortRooms([room2, room1])).toEqual([room2, room1]);
const { events } = mkThread({
room: room1,
client: cli,
authorId: "@bob:matrix.org",
participantUserIds: ["@bob:matrix.org"],
ts: 12,
});
room1.addLiveEvents(events);
});
it("orders rooms based on thread replies too", () => {
const room1 = new Room("room1", cli, "@bob:matrix.org");
const room2 = new Room("room2", cli, "@bob:matrix.org");
room1.getMyMembership = () => "join";
room2.getMyMembership = () => "join";
const { rootEvent, events: events1 } = mkThread({
room: room1,
client: cli,
authorId: "@bob:matrix.org",
participantUserIds: ["@bob:matrix.org"],
ts: 12,
length: 5,
});
room1.addLiveEvents(events1);
const { events: events2 } = mkThread({
room: room2,
client: cli,
authorId: "@bob:matrix.org",
participantUserIds: ["@bob:matrix.org"],
ts: 14,
length: 10,
});
room2.addLiveEvents(events2);
expect(algorithm.sortRooms([room1, room2])).toEqual([room2, room1]);
const threadReply = makeThreadEvent({
user: "@bob:matrix.org",
room: room1.roomId,
event: true,
msg: `hello world`,
rootEventId: rootEvent.getId(),
replyToEventId: rootEvent.getId(),
// replies are 1ms after each other
ts: 50,
});
room1.addLiveEvents([threadReply]);
expect(algorithm.sortRooms([room1, room2])).toEqual([room1, room2]);
});
});
});