Fix slash commands not being enabled in certain cases (#11090)

* Fix slash commands not being enabled in certain cases

* Fix import cycle
This commit is contained in:
Michael Telatynski
2023-06-14 13:49:18 +01:00
committed by GitHub
parent 9c48487d85
commit 6486255f54
4 changed files with 16 additions and 10 deletions

View File

@@ -69,6 +69,7 @@ import { htmlSerializeFromMdIfNeeded } from "./editor/serialize";
import { leaveRoomBehaviour } from "./utils/leave-behaviour";
import { isLocalRoom } from "./utils/localRoom/isLocalRoom";
import { SdkContextClass } from "./contexts/SDKContext";
import { MatrixClientPeg } from "./MatrixClientPeg";
// XXX: workaround for https://github.com/microsoft/TypeScript/issues/31816
interface HTMLInputEvent extends Event {
@@ -122,7 +123,7 @@ interface ICommandOpts {
runFn?: RunFn;
category: string;
hideCompletionAfterSpace?: boolean;
isEnabled?(matrixClient?: MatrixClient): boolean;
isEnabled?(matrixClient: MatrixClient | null): boolean;
renderingTypes?: TimelineRenderingType[];
}
@@ -136,7 +137,7 @@ export class Command {
public readonly hideCompletionAfterSpace: boolean;
public readonly renderingTypes?: TimelineRenderingType[];
public readonly analyticsName?: SlashCommandEvent["command"];
private readonly _isEnabled?: (matrixClient?: MatrixClient) => boolean;
private readonly _isEnabled?: (matrixClient: MatrixClient | null) => boolean;
public constructor(opts: ICommandOpts) {
this.command = opts.command;
@@ -189,7 +190,7 @@ export class Command {
return _t("Usage") + ": " + this.getCommandWithArgs();
}
public isEnabled(cli?: MatrixClient): boolean {
public isEnabled(cli: MatrixClient | null): boolean {
return this._isEnabled?.(cli) ?? true;
}
}
@@ -206,7 +207,7 @@ function successSync(value: any): RunResult {
return success(Promise.resolve(value));
}
const isCurrentLocalRoom = (cli?: MatrixClient): boolean => {
const isCurrentLocalRoom = (cli: MatrixClient | null): boolean => {
const roomId = SdkContextClass.instance.roomViewStore.getRoomId();
if (!roomId) return false;
const room = cli?.getRoom(roomId);
@@ -214,7 +215,7 @@ const isCurrentLocalRoom = (cli?: MatrixClient): boolean => {
return isLocalRoom(room);
};
const canAffectPowerlevels = (cli?: MatrixClient): boolean => {
const canAffectPowerlevels = (cli: MatrixClient | null): boolean => {
const roomId = SdkContextClass.instance.roomViewStore.getRoomId();
if (!cli || !roomId) return false;
const room = cli?.getRoom(roomId);
@@ -1425,7 +1426,7 @@ interface ICmd {
export function getCommand(input: string): ICmd {
const { cmd, args } = parseCommandString(input);
if (cmd && CommandMap.has(cmd) && CommandMap.get(cmd)!.isEnabled()) {
if (cmd && CommandMap.has(cmd) && CommandMap.get(cmd)!.isEnabled(MatrixClientPeg.get())) {
return {
cmd: CommandMap.get(cmd),
args,