1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

sonarcloud

This commit is contained in:
Kegan Dougal
2022-11-28 10:58:38 +00:00
parent 74147b9943
commit 6592b2c205
2 changed files with 53 additions and 59 deletions

View File

@ -932,6 +932,27 @@ describe("SlidingSyncSdk", () => {
describe("ExtensionReceipts", () => {
let ext: Extension;
const generateReceiptResponse = (
userId: string, roomId: string, eventId: string, recType: string, ts: number,
) => {
return {
rooms: {
[roomId]: {
type: EventType.Receipt,
content: {
[eventId]: {
[recType]: {
[userId]: {
ts: ts,
},
},
},
},
},
},
};
};
beforeAll(async () => {
await setupClient();
const hasSynced = sdk!.sync();
@ -973,22 +994,9 @@ describe("SlidingSyncSdk", () => {
const room = client!.getRoom(roomId)!;
expect(room).toBeDefined();
expect(room.getReadReceiptForUserId(alice, true)).toBeNull();
ext.onResponse({
rooms: {
[roomId]: {
type: EventType.Receipt,
content: {
[lastEvent.event_id]: {
"m.read": {
[alice]: {
ts: 1234567,
},
},
},
},
},
},
});
ext.onResponse(
generateReceiptResponse(alice, roomId, lastEvent.event_id, "m.read", 1234567),
);
const receipt = room.getReadReceiptForUserId(alice);
expect(receipt).toBeDefined();
expect(receipt?.eventId).toEqual(lastEvent.event_id);
@ -1000,22 +1008,9 @@ describe("SlidingSyncSdk", () => {
const roomId = "!room:id";
const alice = "@alice:alice";
const eventId = "$something";
ext.onResponse({
rooms: {
[roomId]: {
type: EventType.Receipt,
content: {
[eventId]: {
"m.read": {
[alice]: {
ts: 1234567,
},
},
},
},
},
},
});
ext.onResponse(
generateReceiptResponse(alice, roomId, eventId, "m.read", 1234567),
);
// we expect it not to crash
});
});

View File

@ -253,22 +253,15 @@ class ExtensionTyping implements Extension {
};
}
public onResponse(data: {rooms: Record<string, Event[]>}): void {
public onResponse(data: {rooms: Record<string, IMinimalEvent>}): void {
if (!data || !data.rooms) {
return;
}
for (const roomId in data.rooms) {
const ephemeralEvents = mapEvents(this.client, roomId, [data.rooms[roomId]]);
const room = this.client.getRoom(roomId);
if (!room) {
logger.warn("got typing events for room but room doesn't exist on client:", roomId);
continue;
}
room.addEphemeralEvents(ephemeralEvents);
ephemeralEvents.forEach((e) => {
this.client.emit(ClientEvent.Event, e);
});
processEphemeralEvents(
this.client, roomId, [data.rooms[roomId]],
);
}
}
}
@ -285,30 +278,23 @@ class ExtensionReceipts implements Extension {
}
public onRequest(isInitial: boolean): object | undefined {
if (!isInitial) {
return undefined;
}
if (isInitial) {
return {
enabled: true,
};
}
return undefined;
}
public onResponse(data: {rooms: Record<string, Event>}): void {
public onResponse(data: {rooms: Record<string, IMinimalEvent>}): void {
if (!data || !data.rooms) {
return;
}
for (const roomId in data.rooms) {
const ephemeralEvents = mapEvents(this.client, roomId, [data.rooms[roomId]]);
const room = this.client.getRoom(roomId);
if (!room) {
logger.warn("got receipts for room but room doesn't exist on client:", roomId);
continue;
}
room.addEphemeralEvents(ephemeralEvents);
ephemeralEvents.forEach((e) => {
this.client.emit(ClientEvent.Event, e);
});
processEphemeralEvents(
this.client, roomId, [data.rooms[roomId]],
);
}
}
}
@ -970,3 +956,16 @@ function mapEvents(client: MatrixClient, roomId: string | undefined, events: obj
return mapper(e);
});
}
function processEphemeralEvents(client: MatrixClient, roomId: string, ephEvents: IMinimalEvent[]) {
const ephemeralEvents = mapEvents(client, roomId, ephEvents);
const room = client.getRoom(roomId);
if (!room) {
logger.warn("got ephemeral events for room but room doesn't exist on client:", roomId);
return;
}
room.addEphemeralEvents(ephemeralEvents);
ephemeralEvents.forEach((e) => {
client.emit(ClientEvent.Event, e);
});
}