Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sliding sync: add support for typing extension #2893

Merged
merged 6 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions spec/integ/sliding-sync-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,106 @@ describe("SlidingSyncSdk", () => {
});
});
});
describe("ExtensionTyping", () => {
kegsay marked this conversation as resolved.
Show resolved Hide resolved
let ext: Extension;
beforeAll(async () => {
await setupClient();
const hasSynced = sdk!.sync();
await httpBackend!.flushAllExpected();
await hasSynced;
ext = findExtension("typing");
});
it("gets enabled on the initial request only", () => {
expect(ext.onRequest(true)).toEqual({
enabled: true,
});
expect(ext.onRequest(false)).toEqual(undefined);
});
it("processes typing notifications", async () => {
const roomId = "!room:id";
mockSlidingSync!.emit(SlidingSyncEvent.RoomData, roomId, {
name: "Room with typing",
required_state: [],
timeline: [
mkOwnStateEvent(EventType.RoomCreate, { creator: selfUserId }, ""),
mkOwnStateEvent(EventType.RoomMember, { membership: "join" }, selfUserId),
mkOwnStateEvent(EventType.RoomPowerLevels, { users: { [selfUserId]: 100 } }, ""),
mkOwnEvent(EventType.RoomMessage, { body: "hello" }),
],
initial: true,
});
const room = client!.getRoom(roomId)!;
expect(room).toBeDefined();
expect(room.getMember(selfUserId)?.typing).toEqual(false);
ext.onResponse({
rooms: {
[roomId]: [
{
type: EventType.Typing,
content: {
user_ids: [selfUserId],
},
},
],
},
});
expect(room.getMember(selfUserId)?.typing).toEqual(true);
ext.onResponse({
rooms: {
[roomId]: [
{
type: EventType.Typing,
content: {
user_ids: [],
},
},
],
},
});
expect(room.getMember(selfUserId)?.typing).toEqual(false);
});
it("gracefully handles missing rooms and members when typing", async () => {
const roomId = "!room:id";
mockSlidingSync!.emit(SlidingSyncEvent.RoomData, roomId, {
name: "Room with typing",
required_state: [],
timeline: [
mkOwnStateEvent(EventType.RoomCreate, { creator: selfUserId }, ""),
mkOwnStateEvent(EventType.RoomMember, { membership: "join" }, selfUserId),
mkOwnStateEvent(EventType.RoomPowerLevels, { users: { [selfUserId]: 100 } }, ""),
mkOwnEvent(EventType.RoomMessage, { body: "hello" }),
],
initial: true,
});
const room = client!.getRoom(roomId)!;
expect(room).toBeDefined();
expect(room.getMember(selfUserId)?.typing).toEqual(false);
ext.onResponse({
rooms: {
[roomId]: [
{
type: EventType.Typing,
content: {
user_ids: ["@someone:else"],
},
},
],
},
});
expect(room.getMember(selfUserId)?.typing).toEqual(false);
ext.onResponse({
rooms: {
"!something:else": [
{
type: EventType.Typing,
content: {
user_ids: [selfUserId],
},
},
],
},
});
expect(room.getMember(selfUserId)?.typing).toEqual(false);
});
});
});
41 changes: 41 additions & 0 deletions src/sliding-sync-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,46 @@ class ExtensionAccountData implements Extension {
}
}

class ExtensionTyping implements Extension {
public constructor(private readonly client: MatrixClient) {}

public name(): string {
return "typing";
}

public when(): ExtensionState {
return ExtensionState.PostProcess;
}

public onRequest(isInitial: boolean): object | undefined {
if (!isInitial) {
return undefined;
}
return {
enabled: true,
};
}

public onResponse(data: {rooms: Record<string, Event[]>}): 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);
});
}
}
}

/**
* A copy of SyncApi such that it can be used as a drop-in replacement for sync v2. For the actual
* sliding sync API, see sliding-sync.ts or the class SlidingSync.
Expand Down Expand Up @@ -273,6 +313,7 @@ export class SlidingSyncSdk {
const extensions: Extension[] = [
new ExtensionToDevice(this.client),
new ExtensionAccountData(this.client),
new ExtensionTyping(this.client),
];
if (this.opts.crypto) {
extensions.push(
Expand Down