Use MatrixClientPeg::safeGet for strict typing (#10989)
This commit is contained in:
committed by
GitHub
parent
d64018ce26
commit
9b5b053148
@@ -25,6 +25,7 @@ import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
||||
import { M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
|
||||
import { isSupportedReceiptType } from "matrix-js-sdk/src/utils";
|
||||
import { Optional } from "matrix-events-sdk";
|
||||
import { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import shouldHideEvent from "../../shouldHideEvent";
|
||||
import { wantsDateSeparator } from "../../DateUtils";
|
||||
@@ -73,6 +74,7 @@ const groupedStateEvents = [
|
||||
export function shouldFormContinuation(
|
||||
prevEvent: MatrixEvent | null,
|
||||
mxEvent: MatrixEvent,
|
||||
matrixClient: MatrixClient,
|
||||
showHiddenEvents: boolean,
|
||||
timelineRenderingType?: TimelineRenderingType,
|
||||
): boolean {
|
||||
@@ -110,7 +112,7 @@ export function shouldFormContinuation(
|
||||
}
|
||||
|
||||
// if we don't have tile for previous event then it was shown by showHiddenEvents and has no SenderProfile
|
||||
if (!haveRendererForEvent(prevEvent, showHiddenEvents)) return false;
|
||||
if (!haveRendererForEvent(prevEvent, matrixClient, showHiddenEvents)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -481,7 +483,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!haveRendererForEvent(mxEv, this.showHiddenEvents)) {
|
||||
if (!haveRendererForEvent(mxEv, MatrixClientPeg.safeGet(), this.showHiddenEvents)) {
|
||||
return false; // no tile = no show
|
||||
}
|
||||
|
||||
@@ -736,6 +738,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
|
||||
ret.push(dateSeparator);
|
||||
}
|
||||
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
let lastInSection = true;
|
||||
if (nextEventWithTile) {
|
||||
const nextEv = nextEventWithTile;
|
||||
@@ -743,14 +746,14 @@ export default class MessagePanel extends React.Component<IProps, IState> {
|
||||
lastInSection =
|
||||
willWantDateSeparator ||
|
||||
mxEv.getSender() !== nextEv.getSender() ||
|
||||
getEventDisplayInfo(MatrixClientPeg.safeGet(), nextEv, this.showHiddenEvents).isInfoMessage ||
|
||||
!shouldFormContinuation(mxEv, nextEv, this.showHiddenEvents, this.context.timelineRenderingType);
|
||||
getEventDisplayInfo(cli, nextEv, this.showHiddenEvents).isInfoMessage ||
|
||||
!shouldFormContinuation(mxEv, nextEv, cli, this.showHiddenEvents, this.context.timelineRenderingType);
|
||||
}
|
||||
|
||||
// is this a continuation of the previous message?
|
||||
const continuation =
|
||||
!wantsDateSeparator &&
|
||||
shouldFormContinuation(prevEvent, mxEv, this.showHiddenEvents, this.context.timelineRenderingType);
|
||||
shouldFormContinuation(prevEvent, mxEv, cli, this.showHiddenEvents, this.context.timelineRenderingType);
|
||||
|
||||
const eventId = mxEv.getId()!;
|
||||
const highlight = eventId === this.props.highlightedEventId;
|
||||
|
||||
@@ -237,7 +237,7 @@ export const RoomSearchView = forwardRef<ScrollPanel, Props>(
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!haveRendererForEvent(mxEv, roomContext.showHiddenEvents)) {
|
||||
if (!haveRendererForEvent(mxEv, client, roomContext.showHiddenEvents)) {
|
||||
// XXX: can this ever happen? It will make the result count
|
||||
// not match the displayed count.
|
||||
continue;
|
||||
|
||||
@@ -1965,7 +1965,8 @@ class TimelinePanel extends React.Component<IProps, IState> {
|
||||
!!ev.status || // local echo
|
||||
(ignoreOwn && ev.getSender() === myUserId); // own message
|
||||
const isWithoutTile =
|
||||
!haveRendererForEvent(ev, this.context?.showHiddenEvents) || shouldHideEvent(ev, this.context);
|
||||
!haveRendererForEvent(ev, MatrixClientPeg.safeGet(), this.context?.showHiddenEvents) ||
|
||||
shouldHideEvent(ev, this.context);
|
||||
|
||||
if (isWithoutTile || !node) {
|
||||
// don't start counting if the event should be ignored,
|
||||
|
||||
@@ -75,7 +75,7 @@ export function createRedactEventDialog({
|
||||
onFinished: async (proceed, reason): Promise<void> => {
|
||||
if (!proceed) return;
|
||||
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
const withRelTypes: Pick<IRedactOpts, "with_rel_types"> = {};
|
||||
|
||||
// redact related events if this is a voice broadcast started event and
|
||||
|
||||
@@ -37,7 +37,7 @@ export function ManualDeviceKeyVerificationDialog({
|
||||
device,
|
||||
onFinished,
|
||||
}: IManualDeviceKeyVerificationDialogProps): JSX.Element {
|
||||
const mxClient = MatrixClientPeg.get();
|
||||
const mxClient = MatrixClientPeg.safeGet();
|
||||
|
||||
const onLegacyFinished = useCallback(
|
||||
(confirm: boolean) => {
|
||||
|
||||
@@ -203,9 +203,9 @@ export default class VerificationPanel extends React.PureComponent<IProps, IStat
|
||||
|
||||
private getDevice(): DeviceInfo | null {
|
||||
const deviceId = this.props.request?.otherDeviceId;
|
||||
const userId = MatrixClientPeg.get().getUserId();
|
||||
const userId = MatrixClientPeg.safeGet().getUserId();
|
||||
if (deviceId && userId) {
|
||||
return MatrixClientPeg.get().getStoredDevice(userId, deviceId);
|
||||
return MatrixClientPeg.safeGet().getStoredDevice(userId, deviceId);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1166,7 +1166,7 @@ export class UnwrappedEventTile extends React.Component<EventTileProps, IState>
|
||||
|
||||
let replyChain: JSX.Element | undefined;
|
||||
if (
|
||||
haveRendererForEvent(this.props.mxEvent, this.context.showHiddenEvents) &&
|
||||
haveRendererForEvent(this.props.mxEvent, MatrixClientPeg.safeGet(), this.context.showHiddenEvents) &&
|
||||
shouldDisplayReply(this.props.mxEvent)
|
||||
) {
|
||||
replyChain = (
|
||||
|
||||
@@ -27,6 +27,7 @@ import { shouldFormContinuation } from "../../structures/MessagePanel";
|
||||
import { wantsDateSeparator } from "../../../DateUtils";
|
||||
import LegacyCallEventGrouper, { buildLegacyCallEventGroupers } from "../../structures/LegacyCallEventGrouper";
|
||||
import { haveRendererForEvent } from "../../../events/EventTileFactory";
|
||||
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||
|
||||
interface IProps {
|
||||
// a list of strings to be highlighted in the results
|
||||
@@ -69,6 +70,7 @@ export default class SearchResultTile extends React.Component<IProps> {
|
||||
const isTwelveHour = SettingsStore.getValue("showTwelveHourTimestamps");
|
||||
const alwaysShowTimestamps = SettingsStore.getValue("alwaysShowTimestamps");
|
||||
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
for (let j = 0; j < timeline.length; j++) {
|
||||
const mxEv = timeline[j];
|
||||
let highlights: string[] | undefined;
|
||||
@@ -77,14 +79,20 @@ export default class SearchResultTile extends React.Component<IProps> {
|
||||
highlights = this.props.searchHighlights;
|
||||
}
|
||||
|
||||
if (haveRendererForEvent(mxEv, this.context?.showHiddenEvents)) {
|
||||
if (haveRendererForEvent(mxEv, cli, this.context?.showHiddenEvents)) {
|
||||
// do we need a date separator since the last event?
|
||||
const prevEv = timeline[j - 1];
|
||||
// is this a continuation of the previous message?
|
||||
const continuation =
|
||||
prevEv &&
|
||||
!wantsDateSeparator(prevEv.getDate() || undefined, mxEv.getDate() || undefined) &&
|
||||
shouldFormContinuation(prevEv, mxEv, this.context?.showHiddenEvents, TimelineRenderingType.Search);
|
||||
shouldFormContinuation(
|
||||
prevEv,
|
||||
mxEv,
|
||||
cli,
|
||||
this.context?.showHiddenEvents,
|
||||
TimelineRenderingType.Search,
|
||||
);
|
||||
|
||||
let lastInSection = true;
|
||||
const nextEv = timeline[j + 1];
|
||||
@@ -99,6 +107,7 @@ export default class SearchResultTile extends React.Component<IProps> {
|
||||
!shouldFormContinuation(
|
||||
mxEv,
|
||||
nextEv,
|
||||
cli,
|
||||
this.context?.showHiddenEvents,
|
||||
TimelineRenderingType.Search,
|
||||
);
|
||||
|
||||
@@ -51,7 +51,7 @@ export default class CrossSigningPanel extends React.PureComponent<{}, IState> {
|
||||
}
|
||||
|
||||
public componentDidMount(): void {
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
cli.on(ClientEvent.AccountData, this.onAccountData);
|
||||
cli.on(CryptoEvent.UserTrustStatusChanged, this.onStatusChanged);
|
||||
cli.on(CryptoEvent.KeysChanged, this.onStatusChanged);
|
||||
@@ -89,7 +89,7 @@ export default class CrossSigningPanel extends React.PureComponent<{}, IState> {
|
||||
};
|
||||
|
||||
private async getUpdatedStatus(): Promise<void> {
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
const crypto = cli.getCrypto();
|
||||
if (!crypto) return;
|
||||
|
||||
@@ -127,7 +127,7 @@ export default class CrossSigningPanel extends React.PureComponent<{}, IState> {
|
||||
private bootstrapCrossSigning = async ({ forceReset = false }): Promise<void> => {
|
||||
this.setState({ error: undefined });
|
||||
try {
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
await cli.bootstrapCrossSigning({
|
||||
authUploadDeviceSigningKeys: async (makeRequest): Promise<void> => {
|
||||
const { finished } = Modal.createDialog(InteractiveAuthDialog, {
|
||||
|
||||
@@ -89,7 +89,7 @@ export default class MjolnirUserSettingsTab extends React.Component<{}, IState>
|
||||
|
||||
this.setState({ busy: true });
|
||||
try {
|
||||
const room = await MatrixClientPeg.get().joinRoom(this.state.newList);
|
||||
const room = await MatrixClientPeg.safeGet().joinRoom(this.state.newList);
|
||||
await Mjolnir.sharedInstance().subscribeToList(room.roomId);
|
||||
this.setState({ newList: "" }); // this will also cause the new rule to be rendered
|
||||
} catch (e) {
|
||||
@@ -125,7 +125,7 @@ export default class MjolnirUserSettingsTab extends React.Component<{}, IState>
|
||||
this.setState({ busy: true });
|
||||
try {
|
||||
await Mjolnir.sharedInstance().unsubscribeFromList(list.roomId);
|
||||
await MatrixClientPeg.get().leave(list.roomId);
|
||||
await MatrixClientPeg.safeGet().leave(list.roomId);
|
||||
} catch (e) {
|
||||
logger.error(e);
|
||||
|
||||
@@ -139,7 +139,7 @@ export default class MjolnirUserSettingsTab extends React.Component<{}, IState>
|
||||
}
|
||||
|
||||
private viewListRules(list: BanList): void {
|
||||
const room = MatrixClientPeg.get().getRoom(list.roomId);
|
||||
const room = MatrixClientPeg.safeGet().getRoom(list.roomId);
|
||||
const name = room ? room.name : list.roomId;
|
||||
|
||||
const renderRules = (rules: ListRule[]): JSX.Element => {
|
||||
@@ -210,7 +210,7 @@ export default class MjolnirUserSettingsTab extends React.Component<{}, IState>
|
||||
|
||||
const tiles: JSX.Element[] = [];
|
||||
for (const list of lists) {
|
||||
const room = MatrixClientPeg.get().getRoom(list.roomId);
|
||||
const room = MatrixClientPeg.safeGet().getRoom(list.roomId);
|
||||
const name = room ? (
|
||||
<span>
|
||||
{room.name} (<code>{list.roomId}</code>)
|
||||
|
||||
@@ -93,7 +93,7 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||
const invitedRoomIds = new Set(this.getInvitedRooms().map((room) => room.roomId));
|
||||
|
||||
this.state = {
|
||||
ignoredUserIds: MatrixClientPeg.get().getIgnoredUsers(),
|
||||
ignoredUserIds: MatrixClientPeg.safeGet().getIgnoredUsers(),
|
||||
waitingUnignored: [],
|
||||
managingInvites: false,
|
||||
invitedRoomIds,
|
||||
@@ -102,7 +102,7 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||
|
||||
private onAction = ({ action }: ActionPayload): void => {
|
||||
if (action === "ignore_state_changed") {
|
||||
const ignoredUserIds = MatrixClientPeg.get().getIgnoredUsers();
|
||||
const ignoredUserIds = MatrixClientPeg.safeGet().getIgnoredUsers();
|
||||
const newWaitingUnignored = this.state.waitingUnignored.filter((e) => ignoredUserIds.includes(e));
|
||||
this.setState({ ignoredUserIds, waitingUnignored: newWaitingUnignored });
|
||||
}
|
||||
@@ -110,15 +110,15 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||
|
||||
public componentDidMount(): void {
|
||||
this.dispatcherRef = dis.register(this.onAction);
|
||||
MatrixClientPeg.get().on(RoomEvent.MyMembership, this.onMyMembership);
|
||||
MatrixClientPeg.get()
|
||||
MatrixClientPeg.safeGet().on(RoomEvent.MyMembership, this.onMyMembership);
|
||||
MatrixClientPeg.safeGet()
|
||||
.getVersions()
|
||||
.then((versions) => this.setState({ versions }));
|
||||
}
|
||||
|
||||
public componentWillUnmount(): void {
|
||||
if (this.dispatcherRef) dis.unregister(this.dispatcherRef);
|
||||
MatrixClientPeg.get().removeListener(RoomEvent.MyMembership, this.onMyMembership);
|
||||
MatrixClientPeg.safeGet().removeListener(RoomEvent.MyMembership, this.onMyMembership);
|
||||
}
|
||||
|
||||
private onMyMembership = (room: Room, membership: string): void => {
|
||||
@@ -159,15 +159,15 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||
if (index !== -1) {
|
||||
currentlyIgnoredUserIds.splice(index, 1);
|
||||
this.setState(({ waitingUnignored }) => ({ waitingUnignored: [...waitingUnignored, userId] }));
|
||||
MatrixClientPeg.get().setIgnoredUsers(currentlyIgnoredUserIds);
|
||||
MatrixClientPeg.safeGet().setIgnoredUsers(currentlyIgnoredUserIds);
|
||||
}
|
||||
};
|
||||
|
||||
private getInvitedRooms = (): Room[] => {
|
||||
return MatrixClientPeg.get()
|
||||
return MatrixClientPeg.safeGet()
|
||||
.getRooms()
|
||||
.filter((r) => {
|
||||
return r.hasMembershipState(MatrixClientPeg.get().getUserId()!, "invite");
|
||||
return r.hasMembershipState(MatrixClientPeg.safeGet().getUserId()!, "invite");
|
||||
});
|
||||
};
|
||||
|
||||
@@ -180,7 +180,7 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||
const invitedRoomIdsValues = Array.from(this.state.invitedRoomIds);
|
||||
|
||||
// Execute all acceptances/rejections sequentially
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
const action = accept ? cli.joinRoom.bind(cli) : cli.leave.bind(cli);
|
||||
for (let i = 0; i < invitedRoomIdsValues.length; i++) {
|
||||
const roomId = invitedRoomIdsValues[i];
|
||||
@@ -298,7 +298,7 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
|
||||
);
|
||||
|
||||
let warning;
|
||||
if (!privateShouldBeEncrypted(MatrixClientPeg.get())) {
|
||||
if (!privateShouldBeEncrypted(MatrixClientPeg.safeGet())) {
|
||||
warning = (
|
||||
<div className="mx_SecurityUserSettingsTab_warning">
|
||||
{_t(
|
||||
|
||||
@@ -75,7 +75,7 @@ export default class VerificationRequestToast extends React.PureComponent<IProps
|
||||
this.checkRequestIsPending();
|
||||
|
||||
if (request.isSelfVerification) {
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
const device = request.otherDeviceId ? await cli.getDevice(request.otherDeviceId) : null;
|
||||
const ip = device?.last_seen_ip;
|
||||
this.setState({
|
||||
@@ -113,7 +113,7 @@ export default class VerificationRequestToast extends React.PureComponent<IProps
|
||||
ToastStore.sharedInstance().dismissToast(this.props.toastKey);
|
||||
const { request } = this.props;
|
||||
// no room id for to_device requests
|
||||
const cli = MatrixClientPeg.get();
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
try {
|
||||
if (request.roomId) {
|
||||
dis.dispatch<ViewRoomPayload>({
|
||||
@@ -165,12 +165,12 @@ export default class VerificationRequestToast extends React.PureComponent<IProps
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const client = MatrixClientPeg.safeGet();
|
||||
const userId = request.otherUserId;
|
||||
const roomId = request.roomId;
|
||||
description = roomId ? userLabelForEventRoom(MatrixClientPeg.get(), userId, roomId) : userId;
|
||||
description = roomId ? userLabelForEventRoom(client, userId, roomId) : userId;
|
||||
// for legacy to_device verification requests
|
||||
if (description === userId) {
|
||||
const client = MatrixClientPeg.get();
|
||||
const user = client.getUser(userId);
|
||||
if (user && user.displayName) {
|
||||
description = _t("%(name)s (%(userId)s)", { name: user.displayName, userId });
|
||||
|
||||
Reference in New Issue
Block a user