Skip to content

Commit bc209e6

Browse files
committed
Deprecate Room.lastThread
1 parent 43a5406 commit bc209e6

File tree

2 files changed

+104
-13
lines changed

2 files changed

+104
-13
lines changed

spec/unit/room.spec.ts

+67-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
*/
2020

2121
import { mocked } from "jest-mock";
22-
import { M_POLL_KIND_DISCLOSED, M_POLL_RESPONSE, M_POLL_START, PollStartEvent } from "matrix-events-sdk";
22+
import { M_POLL_KIND_DISCLOSED, M_POLL_RESPONSE, M_POLL_START, Optional, PollStartEvent } from "matrix-events-sdk";
2323

2424
import * as utils from "../test-utils/test-utils";
2525
import { emitPromise } from "../test-utils/test-utils";
@@ -188,7 +188,41 @@ describe("Room", function () {
188188
participantUserIds: ["@bob:example.org"],
189189
});
190190
result.threadEvent = mkThreadResponse(rootEvent, { ts: tsThread });
191-
thread.liveTimeline.addEvent(result.threadEvent, true);
191+
thread.liveTimeline.addEvent(result.threadEvent, { toStartOfTimeline: true });
192+
}
193+
194+
return result;
195+
};
196+
197+
const addRoomThreads = (
198+
room: Room,
199+
thread1EventTs: Optional<number>,
200+
thread2EventTs: Optional<number>,
201+
): { thread1?: Thread; thread2?: Thread } => {
202+
const result: { thread1?: Thread; thread2?: Thread } = {};
203+
204+
if (thread1EventTs !== null) {
205+
const { rootEvent: thread1RootEvent, thread: thread1 } = mkThread({
206+
room,
207+
client: new TestClient().client,
208+
authorId: "@bob:example.org",
209+
participantUserIds: ["@bob:example.org"],
210+
});
211+
const thread1Event = mkThreadResponse(thread1RootEvent, { ts: thread1EventTs });
212+
thread1.liveTimeline.addEvent(thread1Event, { toStartOfTimeline: true });
213+
result.thread1 = thread1;
214+
}
215+
216+
if (thread2EventTs !== null) {
217+
const { rootEvent: thread2RootEvent, thread: thread2 } = mkThread({
218+
room,
219+
client: new TestClient().client,
220+
authorId: "@bob:example.org",
221+
participantUserIds: ["@bob:example.org"],
222+
});
223+
const thread2Event = mkThreadResponse(thread2RootEvent, { ts: thread2EventTs });
224+
thread2.liveTimeline.addEvent(thread2Event, { toStartOfTimeline: true });
225+
result.thread2 = thread2;
192226
}
193227

194228
return result;
@@ -3556,4 +3590,35 @@ describe("Room", function () {
35563590
});
35573591
});
35583592
});
3593+
3594+
describe("getLastThread", () => {
3595+
it("when there is no thread, it should return undefined", () => {
3596+
expect(room.getLastThread()).toBeUndefined();
3597+
});
3598+
3599+
it("when there is only one thread, it should return this one", () => {
3600+
const { thread1 } = addRoomThreads(room, 23, null);
3601+
expect(room.getLastThread()).toBe(thread1);
3602+
});
3603+
3604+
it("when there are tho threads, it should return the one with the recent event I", () => {
3605+
const { thread2 } = addRoomThreads(room, 23, 42);
3606+
expect(room.getLastThread()).toBe(thread2);
3607+
});
3608+
3609+
it("when there are tho threads, it should return the one with the recent event II", () => {
3610+
const { thread1 } = addRoomThreads(room, 42, 23);
3611+
expect(room.getLastThread()).toBe(thread1);
3612+
});
3613+
3614+
it("when there is a thread with the last event ts undefined, it should return the thread with the defined event ts", () => {
3615+
const { thread2 } = addRoomThreads(room, undefined, 23);
3616+
expect(room.getLastThread()).toBe(thread2);
3617+
});
3618+
3619+
it("when the last event ts of all threads is undefined, it should return the last added thread", () => {
3620+
const { thread2 } = addRoomThreads(room, undefined, undefined);
3621+
expect(room.getLastThread()).toBe(thread2);
3622+
});
3623+
});
35593624
});

src/models/room.ts

+37-11
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
397397
*/
398398
private threads = new Map<string, Thread>();
399399

400+
/**
401+
* @deprecated use {@link Room.getLastThread} instead
402+
*/
403+
public lastThread?: Thread;
404+
400405
/**
401406
* A mapping of eventId to all visibility changes to apply
402407
* to the event, by chronological order, as per
@@ -794,20 +799,33 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
794799
public getLastLiveEvent(): MatrixEvent | undefined {
795800
const roomEvents = this.getLiveTimeline().getEvents();
796801
const lastRoomEvent = roomEvents[roomEvents.length - 1] as MatrixEvent | undefined;
802+
const lastThread = this.getLastThread();
797803

798-
return this.getThreads().reduce<MatrixEvent | undefined>(
799-
(lastEventSoFar: MatrixEvent | undefined, thread: Thread) => {
800-
const lastThreadEvent = thread.events[thread.events.length - 1];
804+
if (!lastThread) return lastRoomEvent;
801805

802-
if (!lastEventSoFar || (lastThreadEvent?.getTs() ?? 0) > (lastEventSoFar?.getTs() ?? 0)) {
803-
// no last-event-so-far or last thread event is newer than the last-event-so-far
804-
return lastThreadEvent;
805-
}
806+
const lastThreadEvent = lastThread.events[lastThread.events.length - 1];
806807

807-
return lastEventSoFar;
808-
},
809-
lastRoomEvent,
810-
);
808+
return (lastRoomEvent?.getTs() ?? 0) > (lastThreadEvent.getTs() ?? 0) ? lastRoomEvent : lastThreadEvent;
809+
}
810+
811+
/**
812+
* @returns the thread with the most recent event in its live time line. undefined if there is no thread.
813+
*/
814+
public getLastThread(): Thread | undefined {
815+
return this.getThreads().reduce<Thread | undefined>((lastThread: Thread | undefined, thread: Thread) => {
816+
if (!lastThread) return thread;
817+
818+
const threadEvent = thread.events[thread.events.length - 1];
819+
const lastThreadEvent = lastThread.events[lastThread.events.length - 1];
820+
821+
if ((threadEvent?.getTs() ?? 0) >= (lastThreadEvent?.getTs() ?? 0)) {
822+
// Last message of current thread is newer → new last thread.
823+
// Equal also means newer, because it was added to the thread map later.
824+
return thread;
825+
}
826+
827+
return lastThread;
828+
}, undefined);
811829
}
812830

813831
/**
@@ -2237,6 +2255,14 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
22372255
RoomEvent.Timeline,
22382256
RoomEvent.TimelineReset,
22392257
]);
2258+
const isNewer =
2259+
this.lastThread?.rootEvent &&
2260+
rootEvent?.localTimestamp &&
2261+
this.lastThread.rootEvent?.localTimestamp < rootEvent?.localTimestamp;
2262+
2263+
if (!this.lastThread || isNewer) {
2264+
this.lastThread = thread;
2265+
}
22402266

22412267
if (this.threadsReady) {
22422268
this.updateThreadRootEvents(thread, toStartOfTimeline, false);

0 commit comments

Comments
 (0)