Improve types for sendEvent (#12335)

This commit is contained in:
Michael Telatynski 2024-03-25 12:48:48 +00:00 committed by GitHub
parent 4941327c78
commit ef2bd7ae04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 209 additions and 99 deletions

View file

@ -15,7 +15,7 @@ limitations under the License.
*/
import React from "react";
import { MatrixEvent, MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixEvent, MatrixClient, TimelineEvents } from "matrix-js-sdk/src/matrix";
import { PollEndEvent } from "matrix-js-sdk/src/extensible_events_v1/PollEndEvent";
import { _t } from "../../../languageHandler";
@ -51,7 +51,11 @@ export default class EndPollDialog extends React.Component<IProps> {
const endEvent = PollEndEvent.from(this.props.event.getId()!, message).serialize();
await this.props.matrixClient.sendEvent(this.props.event.getRoomId()!, endEvent.type, endEvent.content);
await this.props.matrixClient.sendEvent(
this.props.event.getRoomId()!,
endEvent.type as keyof TimelineEvents,
endEvent.content as TimelineEvents[keyof TimelineEvents],
);
} catch (e) {
console.error("Failed to submit poll response event:", e);
Modal.createDialog(ErrorDialog, {

View file

@ -28,6 +28,7 @@ import {
LocationAssetType,
M_TIMESTAMP,
M_BEACON,
TimelineEvents,
} from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
@ -80,10 +81,10 @@ interface IProps {
onFinished(): void;
}
interface IEntryProps {
interface IEntryProps<K extends keyof TimelineEvents> {
room: Room;
type: EventType | string;
content: IContent;
type: K;
content: TimelineEvents[K];
matrixClient: MatrixClient;
onFinished(success: boolean): void;
}
@ -95,7 +96,7 @@ enum SendState {
Failed,
}
const Entry: React.FC<IEntryProps> = ({ room, type, content, matrixClient: cli, onFinished }) => {
const Entry: React.FC<IEntryProps<any>> = ({ room, type, content, matrixClient: cli, onFinished }) => {
const [sendState, setSendState] = useState<SendState>(SendState.CanSend);
const [onFocus, isActive, ref] = useRovingTabIndex<HTMLDivElement>();

View file

@ -32,6 +32,12 @@ import Field from "../elements/Field";
import Spinner from "../elements/Spinner";
import LabelledCheckbox from "../elements/LabelledCheckbox";
declare module "matrix-js-sdk/src/types" {
interface TimelineEvents {
[ABUSE_EVENT_TYPE]: AbuseEventContent;
}
}
interface IProps {
mxEvent: MatrixEvent;
onFinished(report?: boolean): void;
@ -56,7 +62,16 @@ const MODERATED_BY_STATE_EVENT_TYPE = [
*/
];
const ABUSE_EVENT_TYPE = "org.matrix.msc3215.abuse.report";
export const ABUSE_EVENT_TYPE = "org.matrix.msc3215.abuse.report";
interface AbuseEventContent {
event_id: string;
room_id: string;
moderated_by_id: string;
nature?: ExtendedNature;
reporter: string;
comment: string;
}
// Standard abuse natures.
enum Nature {
@ -250,13 +265,13 @@ export default class ReportEventDialog extends React.Component<IProps, IState> {
}
await client.sendEvent(dmRoomId, ABUSE_EVENT_TYPE, {
event_id: ev.getId(),
room_id: ev.getRoomId(),
event_id: ev.getId()!,
room_id: ev.getRoomId()!,
moderated_by_id: this.moderation.moderationRoomId,
nature,
reporter: client.getUserId(),
reporter: client.getUserId()!,
comment: this.state.reason.trim(),
});
} satisfies AbuseEventContent);
} else {
// Report to homeserver admin through the dedicated Matrix API.
await client.reportEvent(ev.getRoomId()!, ev.getId()!, -100, this.state.reason.trim());

View file

@ -16,7 +16,7 @@ limitations under the License.
*/
import React, { ChangeEvent, ReactNode, useContext, useMemo, useRef, useState } from "react";
import { IContent, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { IContent, MatrixEvent, TimelineEvents } from "matrix-js-sdk/src/matrix";
import { _t, _td, TranslationKey } from "../../../../languageHandler";
import Field from "../../elements/Field";
@ -32,7 +32,7 @@ export const stringify = (object: object): string => {
interface IEventEditorProps extends Pick<IDevtoolsProps, "onBack"> {
fieldDefs: IFieldDef[]; // immutable
defaultContent?: string;
onSend(fields: string[], content?: IContent): Promise<unknown>;
onSend(fields: string[], content: IContent): Promise<unknown>;
}
interface IFieldDef {
@ -180,8 +180,8 @@ export const TimelineEventEditor: React.FC<IEditorProps> = ({ mxEvent, onBack })
const fields = useMemo(() => [eventTypeField(mxEvent?.getType())], [mxEvent]);
const onSend = ([eventType]: string[], content?: IContent): Promise<unknown> => {
return cli.sendEvent(context.room.roomId, eventType, content || {});
const onSend = ([eventType]: string[], content: TimelineEvents[keyof TimelineEvents]): Promise<unknown> => {
return cli.sendEvent(context.room.roomId, eventType as keyof TimelineEvents, content);
};
let defaultContent: string | undefined;