Conform more code to strict null checking (#10153)

* Conform more code to strict null checking

* Conform more code to strict null checking

* Iterate

* Iterate
This commit is contained in:
Michael Telatynski 2023-02-15 13:36:22 +00:00 committed by GitHub
parent a4ff959aa1
commit 145a5a8a8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
89 changed files with 520 additions and 551 deletions

View file

@ -81,7 +81,7 @@ const singleMxcUpload = async (): Promise<string | null> => {
const fileSelector = document.createElement("input");
fileSelector.setAttribute("type", "file");
fileSelector.onchange = (ev: HTMLInputEvent) => {
const file = ev.target.files[0];
const file = ev.target.files?.[0];
Modal.createDialog(UploadConfirmDialog, {
file,
@ -111,7 +111,7 @@ export const CommandCategories = {
export type RunResult = XOR<{ error: Error | ITranslatableError }, { promise: Promise<IContent | undefined> }>;
type RunFn = (this: Command, roomId: string, args: string) => RunResult;
type RunFn = (this: Command, roomId: string, args?: string) => RunResult;
interface ICommandOpts {
command: string;
@ -159,7 +159,7 @@ export class Command {
return this.getCommand() + " " + this.args;
}
public run(roomId: string, threadId: string, args: string): RunResult {
public run(roomId: string, threadId: string | null, args?: string): RunResult {
// if it has no runFn then its an ignored/nop command (autocomplete only) e.g `/me`
if (!this.runFn) {
return reject(newTranslatableError("Command error: Unable to handle slash command."));
@ -395,12 +395,12 @@ export const Commands = [
runFn: function (roomId, args) {
if (args) {
const cli = MatrixClientPeg.get();
const ev = cli.getRoom(roomId).currentState.getStateEvents("m.room.member", cli.getUserId());
const ev = cli.getRoom(roomId)?.currentState.getStateEvents("m.room.member", cli.getUserId()!);
const content = {
...(ev ? ev.getContent() : { membership: "join" }),
displayname: args,
};
return success(cli.sendStateEvent(roomId, "m.room.member", content, cli.getUserId()));
return success(cli.sendStateEvent(roomId, "m.room.member", content, cli.getUserId()!));
}
return reject(this.getUsage());
},
@ -413,7 +413,7 @@ export const Commands = [
description: _td("Changes the avatar of the current room"),
isEnabled: () => !isCurrentLocalRoom(),
runFn: function (roomId, args) {
let promise = Promise.resolve(args);
let promise = Promise.resolve(args ?? null);
if (!args) {
promise = singleMxcUpload();
}
@ -436,9 +436,9 @@ export const Commands = [
runFn: function (roomId, args) {
const cli = MatrixClientPeg.get();
const room = cli.getRoom(roomId);
const userId = cli.getUserId();
const userId = cli.getUserId()!;
let promise = Promise.resolve(args);
let promise = Promise.resolve(args ?? null);
if (!args) {
promise = singleMxcUpload();
}
@ -446,7 +446,7 @@ export const Commands = [
return success(
promise.then((url) => {
if (!url) return;
const ev = room.currentState.getStateEvents("m.room.member", userId);
const ev = room?.currentState.getStateEvents("m.room.member", userId);
const content = {
...(ev ? ev.getContent() : { membership: "join" }),
avatar_url: url,
@ -463,7 +463,7 @@ export const Commands = [
args: "[<mxc_url>]",
description: _td("Changes your avatar in all rooms"),
runFn: function (roomId, args) {
let promise = Promise.resolve(args);
let promise = Promise.resolve(args ?? null);
if (!args) {
promise = singleMxcUpload();
}
@ -496,7 +496,7 @@ export const Commands = [
);
}
const content: MRoomTopicEventContent = room.currentState.getStateEvents("m.room.topic", "")?.getContent();
const content = room.currentState.getStateEvents("m.room.topic", "")?.getContent<MRoomTopicEventContent>();
const topic = !!content
? ContentHelpers.parseTopicContent(content)
: { text: _t("This room has no topic.") };
@ -874,7 +874,8 @@ export const Commands = [
const cli = MatrixClientPeg.get();
const room = cli.getRoom(SdkContextClass.instance.roomViewStore.getRoomId());
return (
room?.currentState.maySendStateEvent(EventType.RoomPowerLevels, cli.getUserId()) && !isLocalRoom(room)
!!room?.currentState.maySendStateEvent(EventType.RoomPowerLevels, cli.getUserId()!) &&
!isLocalRoom(room)
);
},
runFn: function (roomId, args) {
@ -916,7 +917,8 @@ export const Commands = [
const cli = MatrixClientPeg.get();
const room = cli.getRoom(SdkContextClass.instance.roomViewStore.getRoomId());
return (
room?.currentState.maySendStateEvent(EventType.RoomPowerLevels, cli.getUserId()) && !isLocalRoom(room)
!!room?.currentState.maySendStateEvent(EventType.RoomPowerLevels, cli.getUserId()!) &&
!isLocalRoom(room)
);
},
runFn: function (roomId, args) {
@ -932,7 +934,7 @@ export const Commands = [
}
const powerLevelEvent = room.currentState.getStateEvents("m.room.power_levels", "");
if (!powerLevelEvent.getContent().users[args]) {
if (!powerLevelEvent?.getContent().users[args]) {
return reject(newTranslatableError("Could not find user in room"));
}
return success(cli.setPowerLevel(roomId, args, undefined, powerLevelEvent));
@ -1113,9 +1115,9 @@ export const Commands = [
MatrixClientPeg.get().forceDiscardSession(roomId);
return success(
room.getEncryptionTargetMembers().then((members) => {
room?.getEncryptionTargetMembers().then((members) => {
// noinspection JSIgnoredPromiseFromCall
MatrixClientPeg.get().crypto.ensureOlmSessionsForUsers(
MatrixClientPeg.get().crypto?.ensureOlmSessionsForUsers(
members.map((m) => m.userId),
true,
);
@ -1167,7 +1169,7 @@ export const Commands = [
return reject(this.getUsage());
}
const member = MatrixClientPeg.get().getRoom(roomId).getMember(userId);
const member = MatrixClientPeg.get().getRoom(roomId)?.getMember(userId);
dis.dispatch<ViewUserPayload>({
action: Action.ViewUser,
// XXX: We should be using a real member object and not assuming what the receiver wants.
@ -1412,7 +1414,7 @@ interface ICmd {
export function getCommand(input: string): ICmd {
const { cmd, args } = parseCommandString(input);
if (CommandMap.has(cmd) && CommandMap.get(cmd).isEnabled()) {
if (CommandMap.has(cmd) && CommandMap.get(cmd)!.isEnabled()) {
return {
cmd: CommandMap.get(cmd),
args,