You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
Fix issues with /search and /context API handling for threads (#2261)
This commit is contained in:
committed by
GitHub
parent
bdc3da1fac
commit
85b8d4f83a
@@ -2,6 +2,7 @@ import * as utils from "../test-utils/test-utils";
|
||||
import { EventTimeline } from "../../src/matrix";
|
||||
import { logger } from "../../src/logger";
|
||||
import { TestClient } from "../TestClient";
|
||||
import { Thread, THREAD_RELATION_TYPE } from "../../src/models/thread";
|
||||
|
||||
const userId = "@alice:localhost";
|
||||
const userName = "Alice";
|
||||
@@ -69,6 +70,27 @@ const EVENTS = [
|
||||
}),
|
||||
];
|
||||
|
||||
const THREAD_ROOT = utils.mkMessage({
|
||||
room: roomId,
|
||||
user: userId,
|
||||
msg: "thread root",
|
||||
});
|
||||
|
||||
const 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: "io.element.thread",
|
||||
event_id: THREAD_ROOT.event_id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// start the client, and wait for it to initialise
|
||||
function startClient(httpBackend, client) {
|
||||
httpBackend.when("GET", "/versions").respond(200, {});
|
||||
@@ -116,9 +138,7 @@ describe("getEventTimeline support", function() {
|
||||
return startClient(httpBackend, client).then(function() {
|
||||
const room = client.getRoom(roomId);
|
||||
const timelineSet = room.getTimelineSets()[0];
|
||||
expect(function() {
|
||||
client.getEventTimeline(timelineSet, "event");
|
||||
}).toThrow();
|
||||
expect(client.getEventTimeline(timelineSet, "event")).rejects.toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -136,16 +156,12 @@ describe("getEventTimeline support", function() {
|
||||
return startClient(httpBackend, client).then(() => {
|
||||
const room = client.getRoom(roomId);
|
||||
const timelineSet = room.getTimelineSets()[0];
|
||||
expect(function() {
|
||||
client.getEventTimeline(timelineSet, "event");
|
||||
}).not.toThrow();
|
||||
expect(client.getEventTimeline(timelineSet, "event")).rejects.toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
it("scrollback should be able to scroll back to before a gappy /sync",
|
||||
function() {
|
||||
it("scrollback should be able to scroll back to before a gappy /sync", function() {
|
||||
// need a client with timelineSupport disabled to make this work
|
||||
|
||||
let room;
|
||||
|
||||
return startClient(httpBackend, client).then(function() {
|
||||
@@ -229,6 +245,7 @@ describe("MatrixClient event timelines", function() {
|
||||
afterEach(function() {
|
||||
httpBackend.verifyNoOutstandingExpectation();
|
||||
client.stopClient();
|
||||
Thread.setServerSideSupport(false);
|
||||
});
|
||||
|
||||
describe("getEventTimeline", function() {
|
||||
@@ -355,8 +372,7 @@ describe("MatrixClient event timelines", function() {
|
||||
]);
|
||||
});
|
||||
|
||||
it("should join timelines where they overlap a previous /context",
|
||||
function() {
|
||||
it("should join timelines where they overlap a previous /context", function() {
|
||||
const room = client.getRoom(roomId);
|
||||
const timelineSet = room.getTimelineSets()[0];
|
||||
|
||||
@@ -478,6 +494,50 @@ describe("MatrixClient event timelines", function() {
|
||||
httpBackend.flushAllExpected(),
|
||||
]);
|
||||
});
|
||||
|
||||
it("should handle thread replies with server support by fetching a contiguous thread timeline", async () => {
|
||||
Thread.setServerSideSupport(true);
|
||||
client.stopClient(); // we don't need the client to be syncing at this time
|
||||
const room = client.getRoom(roomId);
|
||||
const timelineSet = room.getTimelineSets()[0];
|
||||
|
||||
httpBackend.when("GET", "/rooms/!foo%3Abar/context/" + encodeURIComponent(THREAD_REPLY.event_id))
|
||||
.respond(200, function() {
|
||||
return {
|
||||
start: "start_token0",
|
||||
events_before: [],
|
||||
event: THREAD_REPLY,
|
||||
events_after: [],
|
||||
end: "end_token0",
|
||||
state: [],
|
||||
};
|
||||
});
|
||||
|
||||
httpBackend.when("GET", "/rooms/!foo%3Abar/event/" + encodeURIComponent(THREAD_ROOT.event_id))
|
||||
.respond(200, function() {
|
||||
return THREAD_ROOT;
|
||||
});
|
||||
|
||||
httpBackend.when("GET", "/rooms/!foo%3Abar/relations/" +
|
||||
encodeURIComponent(THREAD_ROOT.event_id) + "/" +
|
||||
encodeURIComponent(THREAD_RELATION_TYPE.name) + "?limit=20")
|
||||
.respond(200, function() {
|
||||
return {
|
||||
original_event: THREAD_ROOT,
|
||||
chunk: [THREAD_REPLY],
|
||||
next_batch: "next_batch_token0",
|
||||
prev_batch: "prev_batch_token0",
|
||||
};
|
||||
});
|
||||
|
||||
const timelinePromise = client.getEventTimeline(timelineSet, THREAD_REPLY.event_id);
|
||||
await httpBackend.flushAllExpected();
|
||||
|
||||
const timeline = await timelinePromise;
|
||||
|
||||
expect(timeline.getEvents().find(e => e.getId() === THREAD_ROOT.event_id));
|
||||
expect(timeline.getEvents().find(e => e.getId() === THREAD_REPLY.event_id));
|
||||
});
|
||||
});
|
||||
|
||||
describe("paginateEventTimeline", function() {
|
||||
|
@@ -3,6 +3,7 @@ import { CRYPTO_ENABLED } from "../../src/client";
|
||||
import { MatrixEvent } from "../../src/models/event";
|
||||
import { Filter, MemoryStore, Room } from "../../src/matrix";
|
||||
import { TestClient } from "../TestClient";
|
||||
import { THREAD_RELATION_TYPE } from "../../src/models/thread";
|
||||
|
||||
describe("MatrixClient", function() {
|
||||
let client = null;
|
||||
@@ -14,9 +15,7 @@ describe("MatrixClient", function() {
|
||||
beforeEach(function() {
|
||||
store = new MemoryStore();
|
||||
|
||||
const testClient = new TestClient(userId, "aliceDevice", accessToken, undefined, {
|
||||
store: store,
|
||||
});
|
||||
const testClient = new TestClient(userId, "aliceDevice", accessToken, undefined, { store });
|
||||
httpBackend = testClient.httpBackend;
|
||||
client = testClient.client;
|
||||
});
|
||||
@@ -244,14 +243,15 @@ describe("MatrixClient", function() {
|
||||
});
|
||||
|
||||
describe("searching", function() {
|
||||
const response = {
|
||||
search_categories: {
|
||||
room_events: {
|
||||
count: 24,
|
||||
results: {
|
||||
"$flibble:localhost": {
|
||||
it("searchMessageText should perform a /search for room_events", function() {
|
||||
const response = {
|
||||
search_categories: {
|
||||
room_events: {
|
||||
count: 24,
|
||||
results: [{
|
||||
rank: 0.1,
|
||||
result: {
|
||||
event_id: "$flibble:localhost",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
@@ -260,13 +260,11 @@ describe("MatrixClient", function() {
|
||||
msgtype: "m.text",
|
||||
},
|
||||
},
|
||||
},
|
||||
}],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
it("searchMessageText should perform a /search for room_events", function(done) {
|
||||
client.searchMessageText({
|
||||
query: "monkeys",
|
||||
});
|
||||
@@ -280,8 +278,171 @@ describe("MatrixClient", function() {
|
||||
});
|
||||
}).respond(200, response);
|
||||
|
||||
httpBackend.flush().then(function() {
|
||||
done();
|
||||
return httpBackend.flush();
|
||||
});
|
||||
|
||||
describe("should filter out context from different timelines (threads)", () => {
|
||||
it("filters out thread replies when result is in the main timeline", async () => {
|
||||
const response = {
|
||||
search_categories: {
|
||||
room_events: {
|
||||
count: 24,
|
||||
results: [{
|
||||
rank: 0.1,
|
||||
result: {
|
||||
event_id: "$flibble:localhost",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
body: "main timeline",
|
||||
msgtype: "m.text",
|
||||
},
|
||||
},
|
||||
context: {
|
||||
events_after: [{
|
||||
event_id: "$ev-after:server",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
"body": "thread reply",
|
||||
"msgtype": "m.text",
|
||||
"m.relates_to": {
|
||||
"event_id": "$some-thread:server",
|
||||
"rel_type": THREAD_RELATION_TYPE.name,
|
||||
},
|
||||
},
|
||||
}],
|
||||
events_before: [{
|
||||
event_id: "$ev-before:server",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
body: "main timeline again",
|
||||
msgtype: "m.text",
|
||||
},
|
||||
}],
|
||||
},
|
||||
}],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const data = {
|
||||
results: [],
|
||||
highlights: [],
|
||||
};
|
||||
client.processRoomEventsSearch(data, response);
|
||||
|
||||
expect(data.results).toHaveLength(1);
|
||||
expect(data.results[0].context.timeline).toHaveLength(2);
|
||||
expect(data.results[0].context.timeline.find(e => e.getId() === "$ev-after:server")).toBeFalsy();
|
||||
});
|
||||
|
||||
it("filters out thread replies from threads other than the thread the result replied to", () => {
|
||||
const response = {
|
||||
search_categories: {
|
||||
room_events: {
|
||||
count: 24,
|
||||
results: [{
|
||||
rank: 0.1,
|
||||
result: {
|
||||
event_id: "$flibble:localhost",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
"body": "thread 1 reply 1",
|
||||
"msgtype": "m.text",
|
||||
"m.relates_to": {
|
||||
"event_id": "$thread1:server",
|
||||
"rel_type": THREAD_RELATION_TYPE.name,
|
||||
},
|
||||
},
|
||||
},
|
||||
context: {
|
||||
events_after: [{
|
||||
event_id: "$ev-after:server",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
"body": "thread 2 reply 2",
|
||||
"msgtype": "m.text",
|
||||
"m.relates_to": {
|
||||
"event_id": "$thread2:server",
|
||||
"rel_type": THREAD_RELATION_TYPE.name,
|
||||
},
|
||||
},
|
||||
}],
|
||||
events_before: [],
|
||||
},
|
||||
}],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const data = {
|
||||
results: [],
|
||||
highlights: [],
|
||||
};
|
||||
client.processRoomEventsSearch(data, response);
|
||||
|
||||
expect(data.results).toHaveLength(1);
|
||||
expect(data.results[0].context.timeline).toHaveLength(1);
|
||||
expect(data.results[0].context.timeline.find(e => e.getId() === "$flibble:localhost")).toBeTruthy();
|
||||
});
|
||||
|
||||
it("filters out main timeline events when result is a thread reply", () => {
|
||||
const response = {
|
||||
search_categories: {
|
||||
room_events: {
|
||||
count: 24,
|
||||
results: [{
|
||||
rank: 0.1,
|
||||
result: {
|
||||
event_id: "$flibble:localhost",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
"body": "thread 1 reply 1",
|
||||
"msgtype": "m.text",
|
||||
"m.relates_to": {
|
||||
"event_id": "$thread1:server",
|
||||
"rel_type": THREAD_RELATION_TYPE.name,
|
||||
},
|
||||
},
|
||||
},
|
||||
context: {
|
||||
events_after: [{
|
||||
event_id: "$ev-after:server",
|
||||
type: "m.room.message",
|
||||
user_id: "@alice:localhost",
|
||||
room_id: "!feuiwhf:localhost",
|
||||
content: {
|
||||
"body": "main timeline",
|
||||
"msgtype": "m.text",
|
||||
},
|
||||
}],
|
||||
events_before: [],
|
||||
},
|
||||
}],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const data = {
|
||||
results: [],
|
||||
highlights: [],
|
||||
};
|
||||
client.processRoomEventsSearch(data, response);
|
||||
|
||||
expect(data.results).toHaveLength(1);
|
||||
expect(data.results[0].context.timeline).toHaveLength(1);
|
||||
expect(data.results[0].context.timeline.find(e => e.getId() === "$flibble:localhost")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user