Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Fix getRelationsForEvent under TypeScript strict mode (#9558)
Browse files Browse the repository at this point in the history
* Fix getRelationsForEvent tsc strictness

* Use shared type for GetRelationsForEvent

* Fix lint

* Add alternative type

* getRelationsForEvent is not required

* Relations are optional

* Reactions are optional

* We expect relations in these tests

* Add more protection if the eventID is not defined

* Allow null too

* Better test typing

* User ID is not necessary unless something is selected

* It's okay to [].includes(null)

* Null is as good as undefined here

* Null or undefined is good here

* We have some expectations for the tests

* The room and user can be undefined too

* Protec

* Reactions are optional

* Try match signatures

* Null or undefined

* More null or undefined

* Protec

* Fix typo (wrong variable)

* Remove optional params

See #9558 (comment)

* Fix up last maaaaybe relevant lint

Co-authored-by: Michael Telatynski <[email protected]>
  • Loading branch information
2 people authored and Amy Walker committed Nov 28, 2022
1 parent a0f3d19 commit 09db26d
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 127 deletions.
5 changes: 2 additions & 3 deletions src/components/structures/MessagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import classNames from 'classnames';
import { Room } from 'matrix-js-sdk/src/models/room';
import { EventType } from 'matrix-js-sdk/src/@types/event';
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { Relations } from "matrix-js-sdk/src/models/relations";
import { logger } from 'matrix-js-sdk/src/logger';
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { M_BEACON_INFO } from 'matrix-js-sdk/src/@types/beacon';
Expand All @@ -35,7 +34,7 @@ import SettingsStore from '../../settings/SettingsStore';
import RoomContext, { TimelineRenderingType } from "../../contexts/RoomContext";
import { Layout } from "../../settings/enums/Layout";
import { _t } from "../../languageHandler";
import EventTile, { UnwrappedEventTile, IReadReceiptProps } from "../views/rooms/EventTile";
import EventTile, { UnwrappedEventTile, GetRelationsForEvent, IReadReceiptProps } from "../views/rooms/EventTile";
import { hasText } from "../../TextForEvent";
import IRCTimelineProfileResizer from "../views/elements/IRCTimelineProfileResizer";
import DMRoomMap from "../../utils/DMRoomMap";
Expand Down Expand Up @@ -186,7 +185,7 @@ interface IProps {
// helper function to access relations for an event
onUnfillRequest?(backwards: boolean, scrollToken: string): void;

getRelationsForEvent?(eventId: string, relationType: string, eventType: string): Relations;
getRelationsForEvent?: GetRelationsForEvent;

hideThreadedMessages?: boolean;
disableGrouping?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ class TimelinePanel extends React.Component<IProps, IState> {

private getRelationsForEvent = (
eventId: string,
relationType: RelationType,
relationType: RelationType | string,
eventType: EventType | string,
) => this.props.timelineSet.relations?.getChildEventsForEvent(eventId, relationType, eventType);

Expand Down
2 changes: 1 addition & 1 deletion src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ interface IProps extends IPosition {
// True if the menu is being used as a right click menu
rightClick?: boolean;
// The Relations model from the JS SDK for reactions to `mxEvent`
reactions?: Relations;
reactions?: Relations | null | undefined;
// A permalink to this event or an href of an anchor element the user has clicked
link?: string;

Expand Down
8 changes: 2 additions & 6 deletions src/components/views/dialogs/EndPollDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
import React from "react";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { MatrixClient } from "matrix-js-sdk/src/client";
import { Relations } from "matrix-js-sdk/src/models/relations";
import { PollEndEvent } from "matrix-events-sdk";

import { _t } from "../../../languageHandler";
Expand All @@ -26,16 +25,13 @@ import QuestionDialog from "./QuestionDialog";
import { findTopAnswer } from "../messages/MPollBody";
import Modal from "../../../Modal";
import ErrorDialog from "./ErrorDialog";
import { GetRelationsForEvent } from "../rooms/EventTile";

interface IProps extends IDialogProps {
matrixClient: MatrixClient;
event: MatrixEvent;
onFinished: (success: boolean) => void;
getRelationsForEvent?: (
eventId: string,
relationType: string,
eventType: string
) => Relations;
getRelationsForEvent?: GetRelationsForEvent;
}

export default class EndPollDialog extends React.Component<IProps> {
Expand Down
6 changes: 2 additions & 4 deletions src/components/views/elements/ReplyChain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import React from 'react';
import classNames from 'classnames';
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { Room } from 'matrix-js-sdk/src/models/room';
import { Relations } from 'matrix-js-sdk/src/models/relations';
import { MatrixClient } from 'matrix-js-sdk/src/client';

import { _t } from '../../../languageHandler';
Expand All @@ -36,6 +35,7 @@ import AccessibleButton, { ButtonEvent } from './AccessibleButton';
import { getParentEventId, shouldDisplayReply } from '../../../utils/Reply';
import RoomContext from "../../../contexts/RoomContext";
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import { GetRelationsForEvent } from "../rooms/EventTile";

/**
* This number is based on the previous behavior - if we have message of height
Expand All @@ -56,9 +56,7 @@ interface IProps {
forExport?: boolean;
isQuoteExpanded?: boolean;
setQuoteExpanded: (isExpanded: boolean) => void;
getRelationsForEvent?: (
(eventId: string, relationType: string, eventType: string) => Relations
);
getRelationsForEvent?: GetRelationsForEvent;
}

interface IState {
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/emojipicker/ReactionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { FocusComposerPayload } from '../../../dispatcher/payloads/FocusComposer

interface IProps {
mxEvent: MatrixEvent;
reactions?: Relations;
reactions?: Relations | null | undefined;
onFinished(): void;
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/views/messages/IBodyProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ limitations under the License.

import React, { LegacyRef } from "react";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { Relations } from "matrix-js-sdk/src/models/relations";

import { MediaEventHelper } from "../../../utils/MediaEventHelper";
import EditorStateTransfer from "../../../utils/EditorStateTransfer";
import { RoomPermalinkCreator } from "../../../utils/permalinks/Permalinks";
import { GetRelationsForEvent } from "../rooms/EventTile";

export interface IBodyProps {
mxEvent: MatrixEvent;
Expand Down Expand Up @@ -52,7 +52,7 @@ export interface IBodyProps {
isSeeingThroughMessageHiddenForModeration?: boolean;

// helper function to access relations for this event
getRelationsForEvent?: (eventId: string, relationType: string, eventType: string) => Relations;
getRelationsForEvent?: GetRelationsForEvent;

ref?: React.RefObject<any> | LegacyRef<any>;
}
10 changes: 5 additions & 5 deletions src/components/views/messages/MBeaconBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,23 @@ const useUniqueId = (eventId: string): string => {
// remove related beacon locations on beacon redaction
const useHandleBeaconRedaction = (
event: MatrixEvent,
getRelationsForEvent: GetRelationsForEvent,
cli: MatrixClient,
matrixClient: MatrixClient,
getRelationsForEvent?: GetRelationsForEvent,
): void => {
const onBeforeBeaconInfoRedaction = useCallback((_event: MatrixEvent, redactionEvent: MatrixEvent) => {
const relations = getRelationsForEvent ?
getRelationsForEvent(event.getId(), RelationType.Reference, M_BEACON.name) :
undefined;

relations?.getRelations()?.forEach(locationEvent => {
cli.redactEvent(
matrixClient.redactEvent(
locationEvent.getRoomId(),
locationEvent.getId(),
undefined,
redactionEvent.getContent(),
);
});
}, [event, getRelationsForEvent, cli]);
}, [event, matrixClient, getRelationsForEvent]);

useEffect(() => {
event.addListener(MatrixEventEvent.BeforeRedaction, onBeforeBeaconInfoRedaction);
Expand Down Expand Up @@ -151,7 +151,7 @@ const MBeaconBody: React.FC<IBodyProps> = React.forwardRef(({ mxEvent, getRelati
const markerRoomMember = isSelfLocation(mxEvent.getContent()) ? mxEvent.sender : undefined;
const isOwnBeacon = beacon?.beaconInfoOwner === matrixClient.getUserId();

useHandleBeaconRedaction(mxEvent, getRelationsForEvent, matrixClient);
useHandleBeaconRedaction(mxEvent, matrixClient, getRelationsForEvent);

const onClick = () => {
if (displayStatus !== BeaconDisplayStatus.Active) {
Expand Down
Loading

0 comments on commit 09db26d

Please sign in to comment.