Skip to content

Commit

Permalink
Merge pull request #185 from matrix-org/j94/typescript-intent
Browse files Browse the repository at this point in the history
Convert intent.js to TypeScript
  • Loading branch information
Christian Paul authored Aug 10, 2020
2 parents b0304f3 + 198bf89 commit 7df1372
Show file tree
Hide file tree
Showing 13 changed files with 955 additions and 932 deletions.
1 change: 1 addition & 0 deletions changelog.d/185.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert intent.js to TypeScript
1 change: 1 addition & 0 deletions changelog.d/194.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bump matrix-js-sdk to 8.0.1
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"is-my-json-valid": "^2.20.5",
"js-yaml": "^3.14.0",
"matrix-appservice": "^0.4.2",
"matrix-js-sdk": "^6.0.0",
"matrix-js-sdk": "^8.0.1",
"nedb": "^1.8.0",
"nopt": "^4.0.3",
"p-queue": "^6.6.0",
Expand Down
44 changes: 25 additions & 19 deletions spec/unit/intent.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"use strict";
var Intent = require("../..").Intent;
var log = require("../log");
const Intent = require("../..").Intent;
const log = require("../log");

describe("Intent", function() {
var intent, client, botClient;
var userId = "@alice:bar";
var botUserId = "@bot:user";
var roomId = "!foo:bar";
let intent, client, botClient;
const userId = "@alice:bar";
const botUserId = "@bot:user";
const roomId = "!foo:bar";
const alreadyRegistered = {
registered: true
};

beforeEach(
/** @this */
function() {
log.beforeEach(this);
var alreadyRegistered = {
registered: true
};
var clientFields = [
const clientFields = [
"credentials", "joinRoom", "invite", "leave", "ban", "unban",
"kick", "getStateEvent", "setPowerLevel", "sendTyping", "sendEvent",
"sendStateEvent", "setDisplayName", "setAvatarUrl",
Expand Down Expand Up @@ -150,9 +150,9 @@ describe("Intent", function() {
});

describe("sending state events", function() {
var validPowerLevels, invalidPowerLevels;
let validPowerLevels, invalidPowerLevels;

beforeEach(function() {
beforeEach(() => {
// not interested in joins, so no-op them.
intent.onEvent({
event_id: "test",
Expand All @@ -164,7 +164,7 @@ describe("Intent", function() {
}
});

var basePowerLevelEvent = {
const basePowerLevelEvent = {
content: {
"ban": 50,
"events": {
Expand Down Expand Up @@ -259,13 +259,16 @@ describe("Intent", function() {
});

describe("sending message events", function() {
var content = {
const content = {
body: "hello world",
msgtype: "m.text",
};

beforeEach(function() {
intent.opts.dontCheckPowerLevel = true;
intent = new Intent(client, botClient, {
...alreadyRegistered,
dontCheckPowerLevel: true,
});
// not interested in joins, so no-op them.
intent.onEvent({
event_id: "test",
Expand Down Expand Up @@ -304,7 +307,7 @@ describe("Intent", function() {
});

it("should try to join the room on M_FORBIDDEN then resend", function() {
var isJoined = false;
let isJoined = false;
client.sendEvent.and.callFake(function() {
if (isJoined) {
return Promise.resolve({
Expand Down Expand Up @@ -350,7 +353,7 @@ describe("Intent", function() {
});

it("should fail if the resend after M_FORBIDDEN fails", function() {
var isJoined = false;
let isJoined = false;
client.sendEvent.and.callFake(function() {
if (isJoined) {
return Promise.reject({
Expand Down Expand Up @@ -380,10 +383,13 @@ describe("Intent", function() {

describe("signaling bridge error", function() {
const reason = "m.event_not_handled"
var affectedUsers, eventId, bridge;
let affectedUsers, eventId, bridge;

beforeEach(function() {
intent.opts.dontCheckPowerLevel = true;
intent = new Intent(client, botClient, {
...alreadyRegistered,
dontCheckPowerLevel: true,
});
// not interested in joins, so no-op them.
intent.onEvent({
event_id: "test",
Expand Down
96 changes: 44 additions & 52 deletions spec/unit/state-lookup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,31 @@ describe("StateLookup", function() {
});
});

describe("trackRoom", function() {
describe("trackRoom", () => {
it("should return a Promise which is resolved after the HTTP call " +
"to /state returns", function(done) {
"to /state returns", async() => {
var statePromise = createStatePromise([]);
cli.roomState.and.returnValue(statePromise.promise);
var p = lookup.trackRoom("!foo:bar");
expect(p.isPending()).toBe(true); // not resolved HTTP call yet
Bluebird.delay(5).then(function() {
expect(p.isPending()).toBe(true); // still not resolved HTTP call
statePromise.resolve();
return p; // Should resolve now HTTP call is resolved
}).then(function() {
done();
});
await Bluebird.delay(5);
expect(p.isPending()).toBe(true); // still not resolved HTTP call
statePromise.resolve();
await p; // Should resolve now HTTP call is resolved
});

it("should return the same Promise if called multiple times with the " +
"same room ID", function() {
var statePromise = createStatePromise([]);
const statePromise = createStatePromise([]);
cli.roomState.and.returnValue(statePromise.promise);
var p = lookup.trackRoom("!foo:bar");
var q = lookup.trackRoom("!foo:bar");
const p = lookup.trackRoom("!foo:bar");
const q = lookup.trackRoom("!foo:bar");
expect(p).toBe(q);
});

it("should be able to have >1 in-flight track requests at once", function(done) {
var stateA = createStatePromise([]);
var stateB = createStatePromise([]);
it("should be able to have >1 in-flight track requests at once", async() => {
const stateA = createStatePromise([]);
const stateB = createStatePromise([]);
cli.roomState.and.callFake(function(roomId) {
if (roomId === "!a:foobar") {
return stateA.promise;
Expand All @@ -55,16 +52,13 @@ describe("StateLookup", function() {
}
throw new Error("Unexpected room ID: " + roomId);
});
var promiseA = lookup.trackRoom("!a:foobar");
var promiseB = lookup.trackRoom("!b:foobar");
const promiseA = lookup.trackRoom("!a:foobar");
const promiseB = lookup.trackRoom("!b:foobar");
stateA.resolve();
promiseA.then(function() {
expect(promiseB.isPending()).toBe(true);
stateB.resolve();
return promiseB;
}).then(function() {
done();
});
await promiseA;
expect(promiseB.isPending()).toBe(true);
stateB.resolve();
await promiseB;
});

it("should retry the HTTP call on non 4xx, 5xx errors", async function() {
Expand Down Expand Up @@ -109,28 +103,28 @@ describe("StateLookup", function() {
});
});

describe("onEvent", function() {
it("should update the state lookup map", function(done) {
cli.roomState.and.callFake(function(roomId) {
return Promise.resolve([
{type: "m.room.name", state_key: "", room_id: "!foo:bar",
content: { name: "Foo" }}
]);
describe("onEvent", () => {
it("should update the state lookup map", async() => {
cli.roomState.and.callFake(async(roomId) => {
return [{
type: "m.room.name",
state_key: "",
room_id: "!foo:bar",
content: { name: "Foo" },
}];
});

lookup.trackRoom("!foo:bar").then(function() {
expect(
lookup.getState("!foo:bar", "m.room.name", "").content.name
).toEqual("Foo");
lookup.onEvent(
{type: "m.room.name", state_key: "", room_id: "!foo:bar",
content: { name: "Bar" }}
);
expect(
lookup.getState("!foo:bar", "m.room.name", "").content.name
).toEqual("Bar");
done();
});
await lookup.trackRoom("!foo:bar")
expect(
lookup.getState("!foo:bar", "m.room.name", "").content.name
).toEqual("Foo");
lookup.onEvent(
{type: "m.room.name", state_key: "", room_id: "!foo:bar",
content: { name: "Bar" }}
);
expect(
lookup.getState("!foo:bar", "m.room.name", "").content.name
).toEqual("Bar");
});

it("should clobber events from in-flight track requests", async() => {
Expand All @@ -154,10 +148,10 @@ describe("StateLookup", function() {
});
});

describe("getState", function() {
beforeEach(function(done) {
cli.roomState.and.callFake(function(roomId) {
return Promise.resolve([
describe("getState", () => {
beforeEach(async() => {
cli.roomState.and.callFake(async(roomId) => {
return [
{type: "m.room.name", state_key: "", content: { name: "Foo" }},
{type: "m.room.topic", state_key: "", content: { name: "Bar" }},
{type: "m.room.member", state_key: "@alice:bar", content: {
Expand All @@ -168,12 +162,10 @@ describe("StateLookup", function() {
displayname: "Bob",
membership: "invite"
}},
]);
];
});

lookup.trackRoom("!foo:bar").then(function() {
done();
});
await lookup.trackRoom("!foo:bar");
});

it("should return null for no match with state_key", function() {
Expand Down
2 changes: 1 addition & 1 deletion src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const BridgeContext = require("./components/bridge-context");
const ClientFactory = require("./components/client-factory");
const AppServiceBot = require("./components/app-service-bot");
const RequestFactory = require("./components/request-factory").RequestFactory;
const Intent = require("./components/intent");
const Intent = require("./components/intent").Intent;
const RoomBridgeStore = require("./components/room-bridge-store");
const UserBridgeStore = require("./components/user-bridge-store");
const EventBridgeStore = require("./components/event-bridge-store");
Expand Down
Loading

0 comments on commit 7df1372

Please sign in to comment.