diff --git a/changelog.d/203.misc b/changelog.d/203.misc new file mode 100644 index 00000000..3b7bf3bd --- /dev/null +++ b/changelog.d/203.misc @@ -0,0 +1 @@ +Typescriptify MembershipCache \ No newline at end of file diff --git a/src/bridge.js b/src/bridge.js index 6f33a562..9d93d789 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -34,7 +34,7 @@ const EventBridgeStore = require("./components/event-bridge-store"); const MatrixUser = require("./models/users/matrix").MatrixUser; const MatrixRoom = require("./models/rooms/matrix").MatrixRoom; const { PrometheusMetrics } = require("./components/prometheusmetrics"); -const MembershipCache = require("./components/membership-cache"); +const { MembershipCache } = require("./components/membership-cache"); const RoomLinkValidator = require("./components/room-link-validator").RoomLinkValidator; const RLVStatus = require("./components/room-link-validator").validationStatuses; const RoomUpgradeHandler = require("./components/room-upgrade-handler"); diff --git a/src/components/intent.ts b/src/components/intent.ts index be9d258f..402b2509 100644 --- a/src/components/intent.ts +++ b/src/components/intent.ts @@ -20,17 +20,16 @@ import JsSdk from "matrix-js-sdk"; const { MatrixEvent, RoomMember } = JsSdk as any; import ClientRequestCache from "./client-request-cache"; import { defer } from "../utils/promiseutil"; +import { UserMembership } from "./membership-cache"; type BridgeErrorReason = "m.event_not_handled" | "m.event_too_old" | "m.internal_error" | "m.foreign_network_error" | "m.event_unknown"; -type MembershipState = "join" | "invite" | "leave" | null; // null = unknown - type BackingStore = { - getMembership: (roomId: string, userId: string) => MembershipState, + getMembership: (roomId: string, userId: string) => UserMembership, getPowerLevelContent: (roomId: string) => Record | undefined, - setMembership: (roomId: string, userId: string, membership: MembershipState) => void, + setMembership: (roomId: string, userId: string, membership: UserMembership) => void, setPowerLevelContent: (roomId: string, content: Record) => void, }; @@ -98,7 +97,7 @@ export class Intent { registered?: boolean; } // These two are only used if no opts.backingStore is provided to the constructor. - private readonly _membershipStates: Record = {}; + private readonly _membershipStates: Record = {}; private readonly _powerLevels: Record = {}; /** @@ -167,7 +166,7 @@ export class Intent { getPowerLevelContent: (roomId: string) => { return this._powerLevels[roomId]; }, - setMembership: (roomId: string, userId: string, membership: MembershipState) => { + setMembership: (roomId: string, userId: string, membership: UserMembership) => { if (userId !== this.client.credentials.userId) { return; } diff --git a/src/components/membership-cache.js b/src/components/membership-cache.ts similarity index 55% rename from src/components/membership-cache.js rename to src/components/membership-cache.ts index 90a3a2c6..0e443cbf 100644 --- a/src/components/membership-cache.js +++ b/src/components/membership-cache.ts @@ -17,13 +17,10 @@ limitations under the License. * Caches membership of virtual users to rooms in memory * and also stores the state of whether users are registered. */ -class MembershipCache { - constructor () { - this._membershipMap = { - // room_id: { user_id: "join|invite|leave|ban|null" } null=unknown - }; - this._registeredUsers = new Set(); - } +export type UserMembership = "join"|"invite"|"leave"|"ban"|null; +export class MembershipCache { + private membershipMap: {[roomId: string]: { [userId: string]: UserMembership }} = {}; + private registeredUsers = new Set(); /** * Get's the *cached* state of a user's membership for a room. @@ -32,15 +29,15 @@ class MembershipCache { * * This only caches users from the appservice. * - * @param {string}} roomId Room id to check the state of. - * @param {string} userId The userid to check the state of. - * @returns {string} The membership state of the user, e.g. "joined" + * @param roomId Room id to check the state of. + * @param userId The userid to check the state of. + * @returns The membership state of the user, e.g. "joined" */ - getMemberEntry(roomId, userId) { - if (this._membershipMap[roomId] === undefined) { + getMemberEntry(roomId: string, userId: string): UserMembership { + if (this.membershipMap[roomId] === undefined) { return null; } - return this._membershipMap[roomId][userId]; + return this.membershipMap[roomId][userId]; } /** @@ -50,27 +47,29 @@ class MembershipCache { * This DOES NOT set the actual membership of the room. * * This only caches users from the appservice. - * @param {string} roomId Room id to set the state of. - * @param {string} userId The userid to set the state of. - * @param {string} membership The membership value to set for the user + * @param roomId Room id to set the state of. + * @param userId The userid to set the state of. + * @param membership The membership value to set for the user * e.g joined. */ - setMemberEntry(roomId, userId, membership) { - if (this._membershipMap[roomId] === undefined) { - this._membershipMap[roomId] = {}; + setMemberEntry(roomId: string, userId: string, membership: UserMembership) { + if (this.membershipMap[roomId] === undefined) { + this.membershipMap[roomId] = {}; } // Bans and invites do not mean the user exists. if (membership === "join" || membership === "leave") { - this._registeredUsers.add(userId); + this.registeredUsers.add(userId); } - this._membershipMap[roomId][userId] = membership; + this.membershipMap[roomId][userId] = membership; } - isUserRegistered(userId) { - return this._registeredUsers.has(userId); + /** + * Is a user considered registered with the homeserver. + * @param userId A Matrix userId + */ + isUserRegistered(userId: string) { + return this.registeredUsers.has(userId); } } - -module.exports = MembershipCache; diff --git a/src/index.ts b/src/index.ts index 1cac6014..49bfe6d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,7 +58,7 @@ export * from "./components/prometheusmetrics"; module.exports.PrometheusMetrics.AgeCounters = require("./components/agecounters").AgeCounters; // Caches -module.exports.MembershipCache = require("./components/membership-cache"); +export * from "./components/membership-cache"; // Logging module.exports.Logging = require("./components/logging");