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

@ -16,6 +16,7 @@ limitations under the License.
import { richToPlain, plainToRich } from "@matrix-org/matrix-wysiwyg";
import { IContent, IEventRelation, MatrixEvent, MsgType } from "matrix-js-sdk/src/matrix";
import { ReplacementEvent, RoomMessageEventContent, RoomMessageTextEventContent } from "matrix-js-sdk/src/types";
import SettingsStore from "../../../../../settings/SettingsStore";
import { parsePermalink, RoomPermalinkCreator } from "../../../../../utils/permalinks/Permalinks";
@ -76,7 +77,7 @@ export async function createMessageContent(
includeReplyLegacyFallback = true,
editedEvent,
}: CreateMessageContentParams,
): Promise<IContent> {
): Promise<RoomMessageEventContent> {
const isEditing = isMatrixEvent(editedEvent);
const isReply = isEditing ? Boolean(editedEvent.replyEventId) : isMatrixEvent(replyToEvent);
const isReplyAndEditing = isEditing && isReply;
@ -100,10 +101,10 @@ export async function createMessageContent(
const bodyPrefix = (isReplyAndEditing && getTextReplyFallback(editedEvent)) || "";
const formattedBodyPrefix = (isReplyAndEditing && getHtmlReplyFallback(editedEvent)) || "";
const content: IContent = {
const content = {
msgtype: isEmote ? MsgType.Emote : MsgType.Text,
body: isEditing ? `${bodyPrefix} * ${body}` : body,
};
} as RoomMessageTextEventContent & ReplacementEvent<RoomMessageTextEventContent>;
// TODO markdown support

View file

@ -14,18 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { IContent } from "matrix-js-sdk/src/matrix";
import { RoomMessageEventContent, RoomMessageTextEventContent } from "matrix-js-sdk/src/types";
import EditorStateTransfer from "../../../../../utils/EditorStateTransfer";
export function isContentModified(newContent: IContent, editorStateTransfer: EditorStateTransfer): boolean {
export function isContentModified(
newContent: RoomMessageEventContent,
editorStateTransfer: EditorStateTransfer,
): boolean {
// if nothing has changed then bail
const oldContent = editorStateTransfer.getEvent().getContent();
const oldContent = editorStateTransfer.getEvent().getContent<RoomMessageEventContent>();
if (
oldContent["msgtype"] === newContent["msgtype"] &&
oldContent["body"] === newContent["body"] &&
oldContent["format"] === newContent["format"] &&
oldContent["formatted_body"] === newContent["formatted_body"]
(<RoomMessageTextEventContent>oldContent)["format"] === (<RoomMessageTextEventContent>newContent)["format"] &&
(<RoomMessageTextEventContent>oldContent)["formatted_body"] ===
(<RoomMessageTextEventContent>newContent)["formatted_body"]
) {
return false;
}

View file

@ -16,13 +16,13 @@ limitations under the License.
import { Composer as ComposerEvent } from "@matrix-org/analytics-events/types/typescript/Composer";
import {
IContent,
IEventRelation,
MatrixEvent,
ISendEventResponse,
MatrixClient,
THREAD_RELATION_TYPE,
} from "matrix-js-sdk/src/matrix";
import { RoomMessageEventContent } from "matrix-js-sdk/src/types";
import { PosthogAnalytics } from "../../../../../PosthogAnalytics";
import SettingsStore from "../../../../../settings/SettingsStore";
@ -82,7 +82,7 @@ export async function sendMessage(
}*/
PosthogAnalytics.instance.trackEvent<ComposerEvent>(posthogEvent);
let content: IContent | null = null;
let content: RoomMessageEventContent | null = null;
// Slash command handling here approximates what can be found in SendMessageComposer.sendMessage()
// but note that the /me and // special cases are handled by the call to createMessageContent
@ -145,7 +145,7 @@ export async function sendMessage(
const prom = doMaybeLocalRoomAction(
roomId,
(actualRoomId: string) => mxClient.sendMessage(actualRoomId, threadId, content as IContent),
(actualRoomId: string) => mxClient.sendMessage(actualRoomId, threadId, content!),
mxClient,
);
@ -218,7 +218,7 @@ export async function editMessage(
this.editorRef.current?.replaceEmoticon(position, REGEX_EMOTICON);
}*/
const editContent = await createMessageContent(html, true, { editedEvent });
const newContent = editContent["m.new_content"];
const newContent = editContent["m.new_content"]!;
const shouldSend = true;