Skip to content

Commit

Permalink
Merge pull request #212 from matrix-org/hs/appservice-bot
Browse files Browse the repository at this point in the history
Typescriptify AppserviceBot
  • Loading branch information
Half-Shot authored Aug 24, 2020
2 parents f951b6b + 1f80854 commit 88e9bef
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 155 deletions.
1 change: 1 addition & 0 deletions changelog.d/212.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Typescriptify AppserviceBot
2 changes: 1 addition & 1 deletion src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const MatrixScheduler = require("matrix-js-sdk").MatrixScheduler;

const BridgeContext = require("./components/bridge-context");
const { ClientFactory } = require("./components/client-factory");
const AppServiceBot = require("./components/app-service-bot");
const { AppServiceBot } = require("./components/app-service-bot");
const RequestFactory = require("./components/request-factory").RequestFactory;
const Intent = require("./components/intent").Intent;
const RoomBridgeStore = require("./components/room-bridge-store");
Expand Down
150 changes: 0 additions & 150 deletions src/components/app-service-bot.js

This file was deleted.

115 changes: 115 additions & 0 deletions src/components/app-service-bot.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 { AppServiceRegistration } from "matrix-appservice";
import { MembershipCache } from "./membership-cache";
import { StateLookupEvent } from "..";

/**
* Construct an AS bot user which has various helper methods.
* @constructor
* @param {MatrixClient} client The client instance configured for the AS bot.
* @param registration The registration that the bot
* is following. Used to determine which user IDs it is controlling.
* @param memberCache The bridges membership cache instance,
* for storing membership the bot has discovered.
*/
export class AppServiceBot {
private exclusiveUserRegexes: RegExp[];
constructor (private client: any, registration: AppServiceRegistration, private memberCache: MembershipCache) {
// yank out the exclusive user ID regex strings
this.exclusiveUserRegexes = [];
const regOut = registration.getOutput();
if (regOut.namespaces.users) {
regOut.namespaces.users.forEach((userEntry) => {
if (!userEntry.exclusive) {
return;
}
this.exclusiveUserRegexes.push(new RegExp(userEntry.regex));
});
}
}

public getClient() {
return this.client;
}

public getUserId(): string {
return this.client.credentials.userId;
}

/**
* Get a list of joined room IDs for the AS bot.
* @return Resolves to a list of room IDs.
*/
public async getJoinedRooms(): Promise<string[]> {
return (await this.client.getJoinedRooms()).joined_rooms || [];
}

/**
* Get a map of joined user IDs for the given room ID. The values in the map are objects
* with a 'display_name' and 'avatar_url' properties. These properties may be null.
* @param roomId The room to get a list of joined user IDs in.
* @return Resolves to a map of user ID => display_name avatar_url
*/
public async getJoinedMembers(roomId: string) {
const res: {joined: string[]} = await this.client.getJoinedRoomMembers(roomId);
if (!res.joined) {
return {};
}
for (const member in res.joined) {
if (this.isRemoteUser(member)) {
this.memberCache.setMemberEntry(roomId, member, "join");
}
}
return res.joined;
}

public async getRoomInfo(roomId: string, joinedRoom: {state?: { events: StateLookupEvent[]}} = {}) {
const stateEvents = joinedRoom.state ? joinedRoom.state.events : [];
const roomInfo: {id: string, state: StateLookupEvent[], realJoinedUsers: string[], remoteJoinedUsers: string[]}
= {
id: roomId,
state: stateEvents,
realJoinedUsers: [],
remoteJoinedUsers: [],
};
stateEvents.forEach((event) => {
if (event.type !== "m.room.member" || (event.content as {membership: string}).membership !== "join") {
return;
}
const userId = event.state_key;
if (userId === this.getUserId()) {
return;
}
if (this.isRemoteUser(userId)) {
roomInfo.remoteJoinedUsers.push(userId);
}
else {
roomInfo.realJoinedUsers.push(userId);
}
});
return roomInfo;
}

/**
* Test a userId to determine if it's a user within the exclusive regexes of the bridge.
* @return True if it is a remote user, false otherwise.
*/
public isRemoteUser(userId: string) {
return this.exclusiveUserRegexes.some((r) => r.test(userId));
}
}
2 changes: 1 addition & 1 deletion src/components/room-link-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
* The room link validator is used to determine if a room can be bridged.
*/
import util from "util";
import AppServiceBot from "./app-service-bot";
import { AppServiceBot } from "./app-service-bot";
import ConfigValidator from "./config-validator";
import logging from "./logging";
const log = logging.get("room-link-validator");
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ export * from "./components/request-factory";
export * from "./components/client-factory";
export * from "./components/intent";

/* eslint-disable @typescript-eslint/no-var-requires */

module.exports.AppServiceBot = require("./components/app-service-bot");
export * from "./components/app-service-bot";
export * from "./components/state-lookup";

// Config and CLI
export * from "./components/cli";
export * from "./components/config-validator";
/* eslint-disable @typescript-eslint/no-var-requires */
module.exports.ConfigValidator = require("./components/config-validator");

// Store
Expand Down

0 comments on commit 88e9bef

Please sign in to comment.