Skip to content

Commit

Permalink
Make changes as requested by reviewers, cleaning up the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Oct 4, 2022
1 parent 2a3fc53 commit fbb39af
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 160 deletions.
95 changes: 22 additions & 73 deletions spec/integ/matrix-client-event-timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,44 +144,6 @@ const THREAD_REPLY = utils.mkEvent({

THREAD_ROOT.unsigned["m.relations"]["io.element.thread"].latest_event = THREAD_REPLY;

const STABLE_THREAD_ROOT = utils.mkEvent({
room: roomId,
user: userId,
type: "m.room.message",
content: {
"body": "thread root",
"msgtype": "m.text",
},
unsigned: {
"m.relations": {
"m.thread": {
//"latest_event": undefined,
"count": 1,
"current_user_participated": true,
},
},
},
event: false,
});

const STABLE_THREAD_REPLY = utils.mkEvent({
room: roomId,
user: userId,
type: "m.room.message",
content: {
"body": "thread reply",
"msgtype": "m.text",
"m.relates_to": {
// We can't use the const here because we change server support mode for test
rel_type: "m.thread",
event_id: THREAD_ROOT.event_id,
},
},
event: false,
});

STABLE_THREAD_ROOT.unsigned["m.relations"]["m.thread"].latest_event = STABLE_THREAD_REPLY;

const SYNC_THREAD_ROOT = withoutRoomId(THREAD_ROOT);
const SYNC_THREAD_REPLY = withoutRoomId(THREAD_REPLY);
SYNC_THREAD_ROOT.unsigned = {
Expand Down Expand Up @@ -980,33 +942,40 @@ describe("MatrixClient event timelines", function() {
}

describe("with server compatibility", function() {
beforeEach(() => {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.Stable);
});

async function testPagination(timelineSet: EventTimelineSet, direction: Direction) {
const RANDOM_TOKEN = "7280349c7bee430f91defe2a38a0a08c";
function respondToThreads() {
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/threads", {
$roomId: roomId,
})).respond(200, {
chunk: [STABLE_THREAD_ROOT],
chunk: [THREAD_ROOT],
state: [],
next_batch: RANDOM_TOKEN,
});
}
function respondToContext() {
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/context/$eventId", {
$roomId: roomId,
$eventId: STABLE_THREAD_ROOT.event_id!,
$eventId: THREAD_ROOT.event_id!,
})).respond(200, {
end: "",
start: "",
state: [],
events_before: [],
events_after: [],
event: STABLE_THREAD_ROOT,
event: THREAD_ROOT,
});
}

respondToContext();
await flushHttp(client.getEventTimeline(timelineSet, STABLE_THREAD_ROOT.event_id!));
await flushHttp(client.getEventTimeline(timelineSet, THREAD_ROOT.event_id!));
respondToThreads();
const timeline = await flushHttp(client.getLatestTimeline(timelineSet));
expect(timeline).not.toBeNull();
Expand All @@ -1016,17 +985,11 @@ describe("MatrixClient event timelines", function() {
backwards: direction === Direction.Backward,
}));
expect(success).toBeTruthy();
expect(timeline!.getEvents().length).toEqual(1);
expect(timeline!.getEvents()[0].event).toEqual(STABLE_THREAD_ROOT);
expect(timeline!.getEvents().map(it => it.event)).toEqual([THREAD_ROOT]);
expect(timeline!.getPaginationToken(direction)).toEqual(RANDOM_TOKEN);
}

it("should allow you to paginate all threads backwards", async function() {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.Stable);

const room = client.getRoom(roomId);
const timelineSets = await (room?.createThreadsTimelineSets());
expect(timelineSets).not.toBeNull();
Expand All @@ -1036,11 +999,6 @@ describe("MatrixClient event timelines", function() {
});

it("should allow you to paginate all threads forwards", async function() {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.Stable);

const room = client.getRoom(roomId);
const timelineSets = await (room?.createThreadsTimelineSets());
expect(timelineSets).not.toBeNull();
Expand All @@ -1051,17 +1009,12 @@ describe("MatrixClient event timelines", function() {
});

it("should allow fetching all threads", async function() {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.Stable);

const RANDOM_TOKEN = "7280349c7bee430f91defe2a38a0a08c";
function respondToThreads() {
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/threads", {
$roomId: roomId,
})).respond(200, {
chunk: [STABLE_THREAD_ROOT],
chunk: [THREAD_ROOT],
state: [],
next_batch: RANDOM_TOKEN,
});
Expand All @@ -1077,6 +1030,13 @@ describe("MatrixClient event timelines", function() {
});

describe("without server compatibility", function() {
beforeEach(() => {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.None);
});

async function testPagination(timelineSet: EventTimelineSet, direction: Direction) {
const RANDOM_TOKEN = "7280349c7bee430f91defe2a38a0a08c";
function respondToMessagesRequest() {
Expand Down Expand Up @@ -1120,17 +1080,11 @@ describe("MatrixClient event timelines", function() {
}));

expect(success).toBeTruthy();
expect(timeline!.getEvents().length).toEqual(1);
expect(timeline!.getEvents()[0].event).toEqual(THREAD_ROOT);
expect(timeline!.getEvents().map(it => it.event)).toEqual([THREAD_ROOT]);
expect(timeline!.getPaginationToken(direction)).toEqual(`${direction}${RANDOM_TOKEN}2`);
}

it("should allow you to paginate all threads", async function() {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.None);

function respondToFilter() {
httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
}
Expand All @@ -1156,19 +1110,14 @@ describe("MatrixClient event timelines", function() {
});

it("should allow fetching all threads", async function() {
// @ts-ignore
client.clientOpts.experimentalThreadSupport = true;
Thread.setServerSideSupport(FeatureSupport.Experimental);
Thread.setServerSideListSupport(FeatureSupport.None);

const room = client.getRoom(roomId);

const RANDOM_TOKEN = "7280349c7bee430f91defe2a38a0a08c";
function respondToMessagesRequest() {
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/messages", {
$roomId: roomId,
})).respond(200, {
chunk: [STABLE_THREAD_ROOT],
chunk: [THREAD_ROOT],
state: [],
start: `${Direction.Forward}${RANDOM_TOKEN}2`,
end: `${Direction.Backward}${RANDOM_TOKEN}2`,
Expand Down
8 changes: 3 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5357,7 +5357,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
" parameter to true when creating MatrixClient to enable it.");
}

if (!timelineSet.room) {
if (!timelineSet?.room) {
throw new Error("getEventTimeline only supports room timelines");
}

Expand Down Expand Up @@ -5477,7 +5477,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}

let res: IMessagesResponse;
const roomId = timelineSet.room?.roomId;
const roomId = timelineSet.room.roomId;
if (timelineSet.isThreadTimeline) {
res = await this.createThreadListMessagesRequest(
roomId,
Expand Down Expand Up @@ -5731,7 +5731,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
if (backwards && res.end == res.start) {
eventTimeline.setPaginationToken(null, dir);
}
return res.end != res.start;
return res.end !== res.start;
}).finally(() => {
eventTimeline.paginationRequests[dir] = null;
});
Expand Down Expand Up @@ -6866,8 +6866,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
list: determineFeatureSupport(listStable, listUnstable),
};
} catch (e) {
// Assume server support and stability aren't available: null/no data return.
// XXX: This should just return an object with `false` booleans instead.
return {
threads: FeatureSupport.None,
list: FeatureSupport.None,
Expand Down
Loading

0 comments on commit fbb39af

Please sign in to comment.