Skip to content

Commit

Permalink
Merge pull request #137 from Half-Shot/hs/msc3316
Browse files Browse the repository at this point in the history
Implement MSC3316 (Timestamp massaging)
  • Loading branch information
turt2live authored Aug 18, 2021
2 parents d474a6d + decab56 commit 9e9d623
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
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);
});
});
});

0 comments on commit 9e9d623

Please sign in to comment.