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

Convert intent.js to TypeScript #185

Merged
merged 37 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ebf579d
Convert intent.js to TypeScript
Aug 3, 2020
277693b
Fix some linting issues
Aug 3, 2020
a953c5c
Don't always include visServers
Aug 3, 2020
fd1dff0
Refactor/fix ._joinGuard; use public explicitly and don't use arrow f…
Aug 4, 2020
3db2a95
Fix some ESLint warnings
Aug 4, 2020
9fe8d05
Fix more ESLint warnings
Aug 4, 2020
142d306
Don't use any
Aug 4, 2020
ff12e31
Silence ESLint errors in event-queue
Aug 3, 2020
4637c0b
linting
Aug 6, 2020
9443dde
Add newsfile
Aug 6, 2020
a2eba17
Merge remote-tracking branch 'origin/develop' into j94/typescript-intent
Aug 7, 2020
68dbcc8
Merge remote-tracking branch 'origin/develop' into j94/typescript-intent
Aug 7, 2020
2bc5f42
Remove Bluebird from intent.ts
Aug 7, 2020
fa52213
Change export of intent in index.ts
Aug 7, 2020
14ae101
Merge remote-tracking branch 'origin/develop' into j94/typescript-intent
Aug 7, 2020
cf80380
Add newsfile
Aug 7, 2020
979cf9d
Revert "Add newsfile"
Aug 7, 2020
099868b
Merge remote-tracking branch 'origin/develop' into j94/typescript-intent
Aug 7, 2020
17c3906
Fix export of Intent
Aug 7, 2020
6d7f70c
Merge remote-tracking branch 'origin/develop' into j94/typescript-intent
Aug 7, 2020
acaa0c6
Fix import in bridge.js
Aug 7, 2020
356cb20
Ingore changes to opts; await promise in _joinGuard
Aug 7, 2020
e10ef13
Clone opts.backingStore
Aug 7, 2020
ebe8128
Bump matrix-js-sdk to 8.0.1
Half-Shot Aug 10, 2020
3280097
changelog
Half-Shot Aug 10, 2020
01e0aa1
Fix intent parts, make client any again
Half-Shot Aug 10, 2020
66a37bd
Request factory should not be generic, but newRequest should
Half-Shot Aug 10, 2020
06d59ef
expose StateLookupEvent, make some parameters optional
Half-Shot Aug 10, 2020
596e0ea
Fix exports, publish declarations
Half-Shot Aug 10, 2020
e027f07
Validate event before insertion
Half-Shot Aug 10, 2020
12354a6
Don't assume the type of eventContent
Aug 10, 2020
9a756f3
Prefer Record over Object
Aug 10, 2020
84a322e
Merge pull request #196 from matrix-org/hs/typing-fixes
Aug 10, 2020
0567414
Merge remote-tracking branch 'origin/develop' into j94/typescript-intent
Aug 10, 2020
5739e65
Fix ESLint errors
Aug 10, 2020
deb2f38
Test for type and state_key being strings, not being truthy
Aug 10, 2020
198bf89
Refactor the syntax of state-lookup tests to use async/await
Aug 10, 2020
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
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