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

Implement MSC3316 (Timestamp massaging) #137

Merged
merged 6 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions src/appservice/UnstableAppserviceApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { MSC2716BatchSendResponse } from "../models/MSC2176";
* @category Unstable APIs
*/
export class UnstableAppserviceApis {
private requestId = 0;

constructor(private client: MatrixClient) {
}

Expand All @@ -30,4 +32,32 @@ export class UnstableAppserviceApis {
}
);
}

/**
* Sends an event to the given room with a given timestamp.
* @param {string} roomId the room ID to send the event to
* @param {string} eventType the type of event to send
* @param {string} content the event body to send
* @param {number} ts The origin_server_ts of the new event
* @returns {Promise<string>} resolves to the event ID that represents the event
*/
public async sendEventWithTimestamp(roomId: string, eventType: string, content: any, ts: number) {
const txnId = `${(new Date().getTime())}__inc_appts${++this.requestId}`;
const response = await this.client.doRequest("PUT", `/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/${encodeURIComponent(txnId)}`, {ts}, content);
return response.event_id;
}

/**
* Sends a state event to the given room with a given timestamp.
* @param {string} roomId the room ID to send the event to
* @param {string} type the event type to send
* @param {string} stateKey the state key to send, should not be null
* @param {string} content the event body to send
* @param {number} ts The origin_server_ts of the new event
* @returns {Promise<string>} resolves to the event ID that represents the message
*/
public async sendStateEventWithTimestamp(roomId: string, type: string, stateKey: string, content: any, ts: number): Promise<string> {
const response = await this.client.doRequest("PUT", `/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(type)}/${encodeURIComponent(stateKey)}`, {ts}, content);
return response.event_id;
}
}
56 changes: 56 additions & 0 deletions test/appservice/UnstableAppserviceApisTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,60 @@ describe('UnstableAppserviceApis', () => {
expect(result).toEqual(expectedResponse);
});
});

describe('sendEventWithTimestamp', () => {
it('should call the right endpoint with a timestamp', async () => {
const {client, http, hsUrl} = createTestUnstableClient();

const roomId = "!testing:example.org";
const eventId = "$something:example.org";
const eventType = "io.t2bot.test";
const eventContent = {
testing: "hello world",
sample: true,
};
const ts = 5000;

http.when("PUT", "/_matrix/client/r0/rooms").respond(200, (path, content, {opts}) => {
const idx = path.indexOf(`${hsUrl}/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/send/${encodeURIComponent(eventType)}/`);
expect(idx).toBe(0);
expect(content).toMatchObject(eventContent);
expect(opts.qs).toMatchObject({ts});
return {event_id: eventId};
});

http.flushAllExpected();
const result = await client.sendEventWithTimestamp(roomId, eventType, eventContent, ts);
expect(result).toEqual(eventId);
});
});

describe('sendStateEvent', () => {
it('should call the right endpoint with a timestamp', async () => {
const {client, http, hsUrl} = createTestUnstableClient();

const roomId = "!testing:example.org";
const eventId = "$something:example.org";
const stateKey = "testing";
const eventType = "m.room.message";
const eventContent = {
body: "Hello World",
msgtype: "m.text",
sample: true,
};
const ts = 5000;

http.when("PUT", "/_matrix/client/r0/rooms").respond(200, (path, content, {opts}) => {
const idx = path.indexOf(`${hsUrl}/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/state/${encodeURIComponent(eventType)}/`);
expect(idx).toBe(0);
expect(content).toMatchObject(eventContent);
expect(opts.qs).toMatchObject({ts});
return {event_id: eventId};
});

http.flushAllExpected();
const result = await client.sendStateEventWithTimestamp(roomId, eventType, stateKey, eventContent, ts);
expect(result).toEqual(eventId);
});
});
});