Skip to content

Commit

Permalink
Typescriptify Membership cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Aug 20, 2020
1 parent 49ba2d0 commit 65af140
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
1 change: 1 addition & 0 deletions changelog.d/203.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Typescriptify MembershipCache
2 changes: 1 addition & 1 deletion src/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
11 changes: 5 additions & 6 deletions src/components/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | undefined,
setMembership: (roomId: string, userId: string, membership: MembershipState) => void,
setMembership: (roomId: string, userId: string, membership: UserMembership) => void,
setPowerLevelContent: (roomId: string, content: Record<string, unknown>) => void,
};

Expand Down Expand Up @@ -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<string, MembershipState> = {};
private readonly _membershipStates: Record<string, UserMembership> = {};
private readonly _powerLevels: Record<string, PowerLevelContent> = {};

/**
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();

/**
* Get's the *cached* state of a user's membership for a room.
Expand All @@ -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];
}

/**
Expand All @@ -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;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 65af140

Please sign in to comment.