From 44b23d219102833926862a0c336c02ae74133ce0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 16 Oct 2024 17:38:22 +0100 Subject: [PATCH 1/5] Make Sonar happier Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/AddThreepid.ts | 4 +-- src/Avatar.ts | 2 +- src/ContentMessages.ts | 16 +++------ src/IdentityAuthClient.tsx | 2 +- src/Lifecycle.ts | 2 +- src/NodeAnimator.tsx | 3 -- src/Notifier.ts | 4 +-- src/PosthogAnalytics.ts | 8 ++--- src/RoomInvite.tsx | 4 +-- src/SlashCommands.tsx | 4 +-- src/SlidingSyncManager.ts | 36 +++++++++---------- src/Terms.ts | 9 ++--- src/actions/RoomListActions.ts | 6 ++-- src/components/structures/MatrixChat.tsx | 2 +- .../views/dialogs/RoomUpgradeDialog.tsx | 3 +- src/components/views/right_panel/UserInfo.tsx | 2 +- .../views/settings/AddRemoveThreepids.tsx | 2 +- 17 files changed, 44 insertions(+), 65 deletions(-) diff --git a/src/AddThreepid.ts b/src/AddThreepid.ts index 7e53bd79a2d..34ba9d51ed7 100644 --- a/src/AddThreepid.ts +++ b/src/AddThreepid.ts @@ -201,7 +201,7 @@ export default class AddThreepid { // implemented it without, so this may just succeed and that's OK. return [true]; } catch (err) { - if (!(err instanceof MatrixError) || err.httpStatus !== 401 || !err.data || !err.data.flows) { + if (!(err instanceof MatrixError) || err.httpStatus !== 401 || !err.data?.flows) { // doesn't look like an interactive-auth failure throw err; } @@ -300,7 +300,7 @@ export default class AddThreepid { // implemented it without, so this may just succeed and that's OK. return [true]; } catch (err) { - if (!(err instanceof MatrixError) || err.httpStatus !== 401 || !err.data || !err.data.flows) { + if (!(err instanceof MatrixError) || err.httpStatus !== 401 || !err.data?.flows) { // doesn't look like an interactive-auth failure throw err; } diff --git a/src/Avatar.ts b/src/Avatar.ts index 5821647ea88..a78aa4d03b3 100644 --- a/src/Avatar.ts +++ b/src/Avatar.ts @@ -68,7 +68,7 @@ function isValidHexColor(color: string): boolean { return ( typeof color === "string" && (color.length === 7 || color.length === 9) && - color.charAt(0) === "#" && + color.startsWith("#") && !color .slice(1) .split("") diff --git a/src/ContentMessages.ts b/src/ContentMessages.ts index 07c5491f51d..b7f67321c13 100644 --- a/src/ContentMessages.ts +++ b/src/ContentMessages.ts @@ -396,11 +396,7 @@ export default class ContentMessages { } public getUploadLimit(): number | null { - if (this.mediaConfig !== null && this.mediaConfig["m.upload.size"] !== undefined) { - return this.mediaConfig["m.upload.size"]; - } else { - return null; - } + return this.mediaConfig?.["m.upload.size"] ?? null; } public async sendContentListToRoom( @@ -578,7 +574,7 @@ export default class ContentMessages { logger.error(e); content.msgtype = MsgType.File; } - } else if (file.type.indexOf("audio/") === 0) { + } else if (file.type.startsWith("audio/")) { content.msgtype = MsgType.Audio; try { const audioInfo = await infoForAudioFile(file); @@ -588,7 +584,7 @@ export default class ContentMessages { logger.error(e); content.msgtype = MsgType.File; } - } else if (file.type.indexOf("video/") === 0) { + } else if (file.type.startsWith("video/")) { content.msgtype = MsgType.Video; try { const videoInfo = await infoForVideoFile(matrixClient, roomId, file); @@ -648,11 +644,7 @@ export default class ContentMessages { } private isFileSizeAcceptable(file: File): boolean { - if ( - this.mediaConfig !== null && - this.mediaConfig["m.upload.size"] !== undefined && - file.size > this.mediaConfig["m.upload.size"] - ) { + if (this.mediaConfig?.["m.upload.size"] !== undefined && file.size > this.mediaConfig["m.upload.size"]) { return false; } return true; diff --git a/src/IdentityAuthClient.tsx b/src/IdentityAuthClient.tsx index 3b78d9e5706..8963b6bcf25 100644 --- a/src/IdentityAuthClient.tsx +++ b/src/IdentityAuthClient.tsx @@ -170,7 +170,7 @@ export default class IdentityAuthClient { // XXX: The spec is `token`, but we used `access_token` for a Sydent release. const { access_token: accessToken, token } = await this.identityClient.registerWithIdentityServer(hsOpenIdToken); - const identityAccessToken = token ? token : accessToken; + const identityAccessToken = token || accessToken; if (check) await this.checkToken(identityAccessToken); return identityAccessToken; } diff --git a/src/Lifecycle.ts b/src/Lifecycle.ts index 39d93dec119..89faa9e0e9a 100644 --- a/src/Lifecycle.ts +++ b/src/Lifecycle.ts @@ -661,7 +661,7 @@ export async function setLoggedIn(credentials: IMatrixClientCreds): Promise string | null> = { [MsgType.KeyVerificationRequest]: (event: MatrixEvent) => { - const name = (event.sender || {}).name; + const name = event.sender?.name; return _t("notifier|m.key.verification.request", { name }); }, [M_LOCATION.name]: (event: MatrixEvent) => { @@ -233,7 +233,7 @@ class NotifierClass extends TypedEventEmitter void, ): Promise { const inviter = new MultiInviter(client, roomId, progressCallback); - return inviter.invite(addresses, undefined).then((states) => Promise.resolve({ states, inviter })); + return inviter.invite(addresses).then((states) => Promise.resolve({ states, inviter })); } export function showStartChatInviteDialog(initialText = ""): void { @@ -104,7 +104,7 @@ export function inviteUsersToRoom( logger.error(err.stack); Modal.createDialog(ErrorDialog, { title: _t("invite|failed_title"), - description: err && err.message ? err.message : _t("invite|failed_generic"), + description: err?.message ?? _t("invite|failed_generic"), }); }); } diff --git a/src/SlashCommands.tsx b/src/SlashCommands.tsx index 7375cba568e..0215d5d6891 100644 --- a/src/SlashCommands.tsx +++ b/src/SlashCommands.tsx @@ -450,7 +450,7 @@ export const Commands = [ const matches = args.match(/^(\S+)$/); if (matches) { let roomAlias = matches[1]; - if (roomAlias[0] !== "#") return reject(this.getUsage()); + if (!roomAlias.startsWith("#")) return reject(this.getUsage()); if (!roomAlias.includes(":")) { roomAlias += ":" + cli.getDomain(); @@ -994,7 +994,7 @@ Commands.forEach((cmd) => { export function parseCommandString(input: string): { cmd?: string; args?: string } { // trim any trailing whitespace, as it can confuse the parser for IRC-style commands input = input.trimEnd(); - if (input[0] !== "/") return {}; // not a command + if (input.startsWith("/")) return {}; // not a command const bits = input.match(/^(\S+?)(?:[ \n]+((.|\n)*))?$/); let cmd: string; diff --git a/src/SlidingSyncManager.ts b/src/SlidingSyncManager.ts index 578a1757490..8137ecd15ec 100644 --- a/src/SlidingSyncManager.ts +++ b/src/SlidingSyncManager.ts @@ -72,27 +72,23 @@ const DEFAULT_ROOM_SUBSCRIPTION_INFO = { }; // lazy load room members so rooms like Matrix HQ don't take forever to load const UNENCRYPTED_SUBSCRIPTION_NAME = "unencrypted"; -const UNENCRYPTED_SUBSCRIPTION = Object.assign( - { - required_state: [ - [MSC3575_WILDCARD, MSC3575_WILDCARD], // all events - [EventType.RoomMember, MSC3575_STATE_KEY_ME], // except for m.room.members, get our own membership - [EventType.RoomMember, MSC3575_STATE_KEY_LAZY], // ...and lazy load the rest. - ], - }, - DEFAULT_ROOM_SUBSCRIPTION_INFO, -); +const UNENCRYPTED_SUBSCRIPTION = { + required_state: [ + [MSC3575_WILDCARD, MSC3575_WILDCARD], // all events + [EventType.RoomMember, MSC3575_STATE_KEY_ME], // except for m.room.members, get our own membership + [EventType.RoomMember, MSC3575_STATE_KEY_LAZY], // ...and lazy load the rest. + ], + ...DEFAULT_ROOM_SUBSCRIPTION_INFO, +}; // we need all the room members in encrypted rooms because we need to know which users to encrypt // messages for. -const ENCRYPTED_SUBSCRIPTION = Object.assign( - { - required_state: [ - [MSC3575_WILDCARD, MSC3575_WILDCARD], // all events - ], - }, - DEFAULT_ROOM_SUBSCRIPTION_INFO, -); +const ENCRYPTED_SUBSCRIPTION = { + required_state: [ + [MSC3575_WILDCARD, MSC3575_WILDCARD], // all events + ], + ...DEFAULT_ROOM_SUBSCRIPTION_INFO, +}; export type PartialSlidingSyncRequest = { filters?: MSC3575Filter; @@ -199,10 +195,10 @@ export class SlidingSyncManager { [EventType.RoomMember, MSC3575_STATE_KEY_ME], // lets the client calculate that we are in fact in the room ], }, + ...updateArgs, }; - list = Object.assign(list, updateArgs); } else { - const updatedList = Object.assign({}, list, updateArgs); + const updatedList = { ...list, ...updateArgs }; // cannot use objectHasDiff as we need to do deep diff checking if (JSON.stringify(list) === JSON.stringify(updatedList)) { logger.debug("list matches, not sending, update => ", updateArgs); diff --git a/src/Terms.ts b/src/Terms.ts index ee5ddfd8540..868e2428c67 100644 --- a/src/Terms.ts +++ b/src/Terms.ts @@ -102,13 +102,8 @@ export async function startTermsFlow( }); // fetch the set of agreed policy URLs from account data - const currentAcceptedTerms = await client.getAccountData("m.accepted_terms"); - let agreedUrlSet: Set; - if (!currentAcceptedTerms || !currentAcceptedTerms.getContent() || !currentAcceptedTerms.getContent().accepted) { - agreedUrlSet = new Set(); - } else { - agreedUrlSet = new Set(currentAcceptedTerms.getContent().accepted); - } + const currentAcceptedTerms = client.getAccountData("m.accepted_terms")?.getContent(); + const agreedUrlSet = new Set(currentAcceptedTerms?.accepted || []); // remove any policies the user has already agreed to and any services where // they've already agreed to all the policies diff --git a/src/actions/RoomListActions.ts b/src/actions/RoomListActions.ts index 1b693ba4dd6..578226986d7 100644 --- a/src/actions/RoomListActions.ts +++ b/src/actions/RoomListActions.ts @@ -79,7 +79,7 @@ export default class RoomListActions { logger.error("Failed to set DM tag " + err); Modal.createDialog(ErrorDialog, { title: _t("room_list|failed_set_dm_tag"), - description: err && err.message ? err.message : _t("invite|failed_generic"), + description: err?.message ?? _t("invite|failed_generic"), }); }); } @@ -95,7 +95,7 @@ export default class RoomListActions { logger.error("Failed to remove tag " + oldTag + " from room: " + err); Modal.createDialog(ErrorDialog, { title: _t("room_list|failed_remove_tag", { tagName: oldTag }), - description: err && err.message ? err.message : _t("invite|failed_generic"), + description: err?.message ?? _t("invite|failed_generic"), }); }); @@ -108,7 +108,7 @@ export default class RoomListActions { logger.error("Failed to add tag " + newTag + " to room: " + err); Modal.createDialog(ErrorDialog, { title: _t("room_list|failed_add_tag", { tagName: newTag }), - description: err && err.message ? err.message : _t("invite|failed_generic"), + description: err?.message ?? _t("invite|failed_generic"), }); throw err; diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 8b23455967b..fc1b5d970dd 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -1317,7 +1317,7 @@ export default class MatrixChat extends React.PureComponent { const errCode = err.errcode || _td("error|unknown_error_code"); Modal.createDialog(ErrorDialog, { title: _t("error_dialog|forget_room_failed", { errCode }), - description: err && err.message ? err.message : _t("invite|failed_generic"), + description: err?.message ?? _t("invite|failed_generic"), }); }); } diff --git a/src/components/views/dialogs/RoomUpgradeDialog.tsx b/src/components/views/dialogs/RoomUpgradeDialog.tsx index 0935fca19f7..337c1a334ca 100644 --- a/src/components/views/dialogs/RoomUpgradeDialog.tsx +++ b/src/components/views/dialogs/RoomUpgradeDialog.tsx @@ -52,8 +52,7 @@ export default class RoomUpgradeDialog extends React.Component { .catch((err) => { Modal.createDialog(ErrorDialog, { title: _t("room_settings|advanced|error_upgrade_title"), - description: - err && err.message ? err.message : _t("room_settings|advanced|error_upgrade_description"), + description: err?.message ?? _t("room_settings|advanced|error_upgrade_description"), }); }) .finally(() => { diff --git a/src/components/views/right_panel/UserInfo.tsx b/src/components/views/right_panel/UserInfo.tsx index 73bb818bb58..e81f667da96 100644 --- a/src/components/views/right_panel/UserInfo.tsx +++ b/src/components/views/right_panel/UserInfo.tsx @@ -725,7 +725,7 @@ export const RoomKickButton = ({ logger.error("Kick error: " + err); Modal.createDialog(ErrorDialog, { title: _t("user_info|error_kicking_user"), - description: err && err.message ? err.message : "Operation failed", + description: err?.message ?? "Operation failed", }); }, ) diff --git a/src/components/views/settings/AddRemoveThreepids.tsx b/src/components/views/settings/AddRemoveThreepids.tsx index 4535400c333..8b2b61bb9ac 100644 --- a/src/components/views/settings/AddRemoveThreepids.tsx +++ b/src/components/views/settings/AddRemoveThreepids.tsx @@ -74,7 +74,7 @@ const ExistingThreepid: React.FC = ({ mode, threepid, onC logger.error("Unable to remove contact information: " + err); Modal.createDialog(ErrorDialog, { title: _t("settings|general|error_remove_3pid"), - description: err && err.message ? err.message : _t("invite|failed_generic"), + description: err?.message ?? _t("invite|failed_generic"), }); }); }, From 0105f566c9af2c2abb0f1e5938a8bc1eea207fd0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 18 Oct 2024 13:28:44 +0100 Subject: [PATCH 2/5] Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlashCommands.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SlashCommands.tsx b/src/SlashCommands.tsx index 0215d5d6891..0df53d02ae3 100644 --- a/src/SlashCommands.tsx +++ b/src/SlashCommands.tsx @@ -994,7 +994,7 @@ Commands.forEach((cmd) => { export function parseCommandString(input: string): { cmd?: string; args?: string } { // trim any trailing whitespace, as it can confuse the parser for IRC-style commands input = input.trimEnd(); - if (input.startsWith("/")) return {}; // not a command + if (!input.startsWith("/")) return {}; // not a command const bits = input.match(/^(\S+?)(?:[ \n]+((.|\n)*))?$/); let cmd: string; From e09e15bb525265cde07d8e6dfd2b0e3d676e4b57 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 18 Oct 2024 13:38:25 +0100 Subject: [PATCH 3/5] Fix test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- test/unit-tests/Lifecycle-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit-tests/Lifecycle-test.ts b/test/unit-tests/Lifecycle-test.ts index 19bf93cdd6d..67ac7c76373 100644 --- a/test/unit-tests/Lifecycle-test.ts +++ b/test/unit-tests/Lifecycle-test.ts @@ -630,7 +630,7 @@ describe("Lifecycle", () => { deviceId, freshLogin: true, guest: false, - pickleKey: null, + pickleKey: undefined, }, undefined, ); From 4d9b40c49ae85c7c7fd9f71e9a84e9b860142dac Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 18 Oct 2024 15:17:58 +0100 Subject: [PATCH 4/5] Update src/RoomInvite.tsx Co-authored-by: Florian Duros --- src/RoomInvite.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RoomInvite.tsx b/src/RoomInvite.tsx index 86eb31b9d07..ee90b67c385 100644 --- a/src/RoomInvite.tsx +++ b/src/RoomInvite.tsx @@ -42,7 +42,7 @@ export function inviteMultipleToRoom( progressCallback?: () => void, ): Promise { const inviter = new MultiInviter(client, roomId, progressCallback); - return inviter.invite(addresses).then((states) => Promise.resolve({ states, inviter })); + return { states: await inviter.invite(addresses), inviter }; } export function showStartChatInviteDialog(initialText = ""): void { From 51c308f2ec595603809111b0c57a596456de0dd9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 18 Oct 2024 15:38:49 +0100 Subject: [PATCH 5/5] Update RoomInvite.tsx --- src/RoomInvite.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RoomInvite.tsx b/src/RoomInvite.tsx index ee90b67c385..07a24dd384c 100644 --- a/src/RoomInvite.tsx +++ b/src/RoomInvite.tsx @@ -35,7 +35,7 @@ export interface IInviteResult { * @param {function} progressCallback optional callback, fired after each invite. * @returns {Promise} Promise */ -export function inviteMultipleToRoom( +export async function inviteMultipleToRoom( client: MatrixClient, roomId: string, addresses: string[],