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

Port BridgeContext to Typescript #210

Merged
merged 4 commits into from
Sep 1, 2020
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
1 change: 1 addition & 0 deletions changelog.d/210.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Typescriptify BridgeContext
2 changes: 1 addition & 1 deletion src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const AppServiceRegistration = require("matrix-appservice").AppServiceRegistrati
const AppService = require("matrix-appservice").AppService;
const MatrixScheduler = require("matrix-js-sdk").MatrixScheduler;

const BridgeContext = require("./components/bridge-context");
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;
Expand Down
108 changes: 0 additions & 108 deletions src/components/bridge-context.js

This file was deleted.

115 changes: 115 additions & 0 deletions src/components/bridge-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright 2020 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { MatrixUser } from "../models/users/matrix";
import { MatrixRoom } from "../models/rooms/matrix";
import { RoomBridgeStore } from "./room-bridge-store";
import { UserBridgeStore } from "./user-bridge-store";
import { RemoteUser } from "../models/users/remote";
import { RemoteRoom } from "../models/rooms/remote";
import { unstable } from "../errors";

interface BridgeContextSenders {
matrix: MatrixUser;
remote: RemoteUser|null;
remotes: RemoteUser[];
}

interface BridgeContextTargets {
matrix: MatrixUser|null;
remote: RemoteUser|null;
remotes: RemoteUser[];
}

interface BridgeContextRoom {
matrix: MatrixRoom;
remote: RemoteRoom|null;
remotes: RemoteRoom[];
}

export class BridgeContext {
public readonly senders: BridgeContextSenders;
public readonly targets: BridgeContextTargets;
public readonly rooms: BridgeContextRoom;
/**
* @param ctx Event related data
* @param ctx.sender Matrix user ID of the sender.
* @param ctx.target Matrix user ID of the target.
* @param ctx.room Matrix room ID.
*/
constructor(private ctx: { sender: string, target: string|undefined, room: string}) {
this.senders = {
matrix: new MatrixUser(this.ctx.sender),
remote: null,
remotes: [],
};
this.targets = {
matrix: this.ctx.target ? new MatrixUser(this.ctx.target) : null,
remote: null,
remotes: [],
};
this.rooms = {
matrix: new MatrixRoom(this.ctx.room),
remote: null,
remotes: [],
};
}

/**
* Returns this instance after its initialization.
*
* @param {RoomBridgeStore} roomStore
* @param {UserBridgeStore} userStore
* @returns {Promise<BridgeContext>}
*/
async get(roomStore: RoomBridgeStore, userStore: UserBridgeStore) {
try {
const results = await Promise.all([
roomStore.getLinkedRemoteRooms(this.ctx.room),
userStore.getRemoteUsersFromMatrixId(this.ctx.sender),
(this.ctx.target ?
userStore.getRemoteUsersFromMatrixId(this.ctx.target) :
Promise.resolve([])
),
roomStore.getMatrixRoom(this.ctx.room),
userStore.getMatrixUser(this.ctx.sender),
]);
const [remoteRooms, remoteSenders, remoteTargets, mxRoom, mxSender] = results;
if (remoteRooms.length) {
this.rooms.remotes = remoteRooms;
this.rooms.remote = remoteRooms[0];
}
if (remoteSenders.length) {
this.senders.remotes = remoteSenders;
this.senders.remote = remoteSenders[0];
}
if (remoteTargets.length) {
this.targets.remotes = remoteTargets;
this.targets.remote = remoteTargets[0];
}
if (mxRoom) {
this.rooms.matrix = mxRoom;
}
if (mxSender) {
this.senders.matrix = mxSender;
}
}
catch (ex) {
throw unstable.wrapError(ex, Error, "Could not retrieve bridge context");
}
return this;
}
}
2 changes: 1 addition & 1 deletion src/components/room-bridge-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class RoomBridgeStore extends BridgeStore {
return Boolean(e.remote);
}).map(function(e) {
return e.remote;
});
}) as RemoteRoom[];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export * from "./models/users/remote";
export * from "./models/events/event";

module.exports.Bridge = require("./bridge");
module.exports.BridgeContext = require("./components/bridge-context");
export * from "./components/bridge-context";

export * from "matrix-appservice";

Expand Down