- Notifications
You must be signed in to change notification settings - Fork 1
Message event zod types#30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
58dbe79273a3d30f6c26301fcbc0778e539d79840367d8ee4c132437File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import z from "zod" | ||
| import { MatrixEvent } from "matrix-js-sdk" | ||
| import ChatMessageBase from "./ChatMessageBase" | ||
| import { markdownToHtml } from "../../md" | ||
| import { UNKNOWN_EVENT_KEY } from "../../schemas/ClientEvent" | ||
| import { ZodError } from "zod" | ||
| export default class ErrorEvent extends ChatMessageBase< | ||
| typeof UNKNOWN_EVENT_KEY | ||
| > { | ||
| private readonly errorabc: unknown | ||
| constructor(msg: MatrixEvent, error: unknown) { | ||
| super(msg) | ||
| this.element.dataset.eventType = "$error" | ||
| this.errorabc = error | ||
| } | ||
| async reset(): Promise<void> { | ||
| let errorMessage: string | ||
| if (this.errorabc instanceof ZodError) { | ||
| errorMessage = z.prettifyError(this.errorabc) | ||
| } else if (this.errorabc instanceof Error) { | ||
| errorMessage = this.errorabc.message | ||
| } else { | ||
| errorMessage = String(this.errorabc) | ||
| } | ||
| this.contentElement.innerHTML = markdownToHtml( | ||
| ` | ||
| Error parsing content of ${this.message.getType()} message: | ||
| \`\`\`json | ||
| ${JSON.stringify(this.message.getContent(), undefined, " ")} | ||
| \`\`\` | ||
| Error: | ||
| \`\`\`text | ||
| ${errorMessage} | ||
| \`\`\` | ||
| `.trim() | ||
| ) | ||
| await super.reset() | ||
| } | ||
| get type() { | ||
| return UNKNOWN_EVENT_KEY | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,11 +9,13 @@ import RoomAudioMessage from "./RoomAudioMessage" | ||
| import RoomFileMessage from "./RoomFileMessage" | ||
| import UnknownEvent from "./UnknownEvent" | ||
| import RoomNameEvent from "./RoomNameEvent" | ||
| import { SupportedStateEvents } from "../../schemas/ClientEvent" | ||
| import ChatMessageBase from "./ChatMessageBase" | ||
| import ErrorEvent from "./ErrorEvent" | ||
| const eventTypes = { | ||
| "m.room.message": RoomTextMessage, | ||
| "m.room.name": RoomNameEvent, | ||
| } satisfies Record<string, typeof EventBase> | ||
| } satisfies Record<string, typeof EventBase<SupportedStateEvents>> | ||
| const msgTypes = { | ||
| "m.text": RoomTextMessage, | ||
| @@ -23,7 +25,7 @@ const msgTypes = { | ||
| "m.audio": RoomAudioMessage, | ||
| "m.video": RoomVideoMessage, | ||
| "m.file": RoomFileMessage, | ||
| } satisfies Record<string, typeof EventBase> | ||
| } satisfies Record<string, typeof ChatMessageBase<"m.room.message">> | ||
| type EventClasses = | ||
| | (typeof eventTypes)[keyof typeof eventTypes] | ||
| @@ -45,5 +47,9 @@ export function renderEvent(event: MatrixEvent) { | ||
| : RoomTextMessage | ||
| } | ||
| return new EventClass(event) | ||
| try { | ||
| return new EventClass(event) | ||
| } catch (e) { | ||
| return new ErrorEvent(event, e) | ||
| } | ||
cfpwastaken marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { MatrixEvent } from "matrix-js-sdk" | ||
| import ChatMessageBase from "./ChatMessageBase" | ||
| import { getMXCData } from "../../matrix" | ||
| import { parseEventContent } from "../../events" | ||
| import { Audio } from "../../schemas/msgtypes/m/Audio" | ||
| export default class RoomAudioMessage extends ChatMessageBase { | ||
| constructor(msg: MatrixEvent) { | ||
| @@ -11,7 +11,7 @@ export default class RoomAudioMessage extends ChatMessageBase { | ||
| async reset(): Promise<void> { | ||
| this.contentElement.innerHTML = "" | ||
| const content = parseEventContent(this.message.getContent()) | ||
| const content = this.content as Audio | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this guaranteed to be of type Audio already/type checked? Can the cast be avoided using some typescript magic? OwnerAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is (in practice) by only calling that constructor with an audio message. | ||
| const blobUrl = | ||
| typeof content.url == "string" ? await getMXCData(content.url) : null | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the message content or error message contain ``` it would mess up the message.
Maybe do the UI for this manually?