matrix-js-sdk

    Class MatrixEvent

    Typed Event Emitter class which can act as a Base Model for all our model and communication events. This makes it much easier for us to distinguish between events, as we now need to properly type this, so that our events are not stringly-based and prone to silly typos.

    Type parameters:

    • Events - List of all events emitted by this TypedEventEmitter. Normally an enum type.
    • Arguments - A ListenerMap type providing mappings from event names to listener types.
    • SuperclassArguments - TODO: not really sure. Alternative listener mappings, I think? But only honoured for .emit?

    Hierarchy (View Summary, Expand)

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    Properties

    error: null | MatrixError = null

    most recent error associated with sending the event, if any

    event: Partial<IEvent> = {}

    The raw (possibly encrypted) event. Do not access this property directly unless you absolutely have to. Prefer the getter methods defined on this class. Using the getter methods shields your app from changes to event JSON between Matrix versions.

    forwardLooking: boolean = true

    True if this event is 'forward looking', meaning that getDirectionalContent() will return event.content and not event.prev_content. Only state events may be backwards looking Default: true. This property is experimental and may change.

    localTimestamp: number
    sender: null | RoomMember = null

    The room member who sent this event, or null e.g. this is a presence event. This is only guaranteed to be set for events that appear in a timeline, ie. do not guarantee that it will be set on state events.

    status: null | EventStatus = null

    The sending status of the event.

    target: null | RoomMember = null

    The room member who is the target of this event, e.g. the invitee, the person being banned, etc.

    Accessors

    • get isEncryptedDisabledForUnverifiedDevices(): boolean

      True if this event is an encrypted event which we failed to decrypt, the receiver's device is unverified and the sender has disabled encrypting to unverified devices.

      @deprecated: Prefer event.decryptionFailureReason === DecryptionFailureCode.MEGOLM_KEY_WITHHELD_FOR_UNVERIFIED_DEVICE.

      Returns boolean

    • get isThreadRoot(): boolean

      A helper to check if an event is a thread's head or not

      Returns boolean

    • get threadRootId(): undefined | string

      Get the event ID of the thread head

      Returns undefined | string

    • get unstableExtensibleEvent(): Optional<ExtensibleEvent<object>>

      Unstable getter to try and get an extensible event. Note that this might return a falsy value if the event could not be parsed as an extensible event.

      Returns Optional<ExtensibleEvent<object>>

      Use stable functions where possible.

    Methods

    • Flags an event as cancelled due to future conditions. For example, a verification request event in the same sync transaction may be flagged as cancelled to warn listeners that a cancellation event is coming down the same pipe shortly.

      Parameters

      • cancelled: boolean = true

        Whether the event is to be cancelled or not.

      Returns void

    • Get the age of this event. This represents the age of the event when the event arrived at the device, and not the age of the event when this function was called. Can only be returned once the server has echo'ed back

      Returns undefined | number

      The age of this event in milliseconds.

    • For relations and redactions, returns the event_id this event is referring to.

      Returns undefined | string

    • Returns the status of any associated edit or redaction (not for reactions/annotations as their local echo doesn't affect the original event), or else the status of the event.

      Returns null | EventStatus

    • Get the ed25519 the sender of this event claims to own.

      For Olm messages, this claim is encoded directly in the plaintext of the event itself. For megolm messages, it is implied by the m.room_key event which established the megolm session.

      Until we download the device list of the sender, it's just a claim: the device list gives a proof that the owner of the curve25519 key used for this event (and returned by #getSenderKey) also owns the ed25519 key by signing the public curve25519 key with the ed25519 key.

      In general, applications should not use this method directly, but should instead use crypto-api!CryptoApi#getEncryptionInfoForEvent.

      Returns null | string

    • Gets the cleartext content for this event. If the event is not encrypted, or encryption has not been completed, this will return null.

      Returns null | IContent

      The cleartext (decrypted) content for the event

    • Get the (decrypted, if necessary) event content JSON, or the content from the replacing event, if any. See makeReplaced.

      Type Parameters

      Returns T

      The event content JSON, or an empty object.

    • Get the timestamp of this event, as a Date object.

      Returns null | Date

      The event date, e.g. new Date(1433502692297)

    • Get a string containing details of this event

      This is intended for logging, to help trace errors. Example output:

      Returns string

      id=$HjnOHV646n0SjLDAqFrgIjim7RCpB7cdMXFrekWYAn type=m.room.encrypted
      sender=@user:example.com room=!room:example.com ts=2022-10-25T17:30:28.404Z
    • Get either 'content' or 'prev_content' depending on if this event is 'forward-looking' or not. This can be modified via event.forwardLooking. In practice, this means we get the chronologically earlier content value for this event (this method should surely be called getEarlierContent) This method is experimental and may change.

      Returns IContent

      event.content if this event is forward-looking, else event.prev_content.

    • Gets the event as it would appear if it had been sent unencrypted.

      If the event is encrypted, we attempt to mock up an event as it would have looked had the sender not encrypted it. If the event is not encrypted, a copy of it is simply returned as-is.

      Returns IEvent

      A shallow copy of the event, in wire format, as it would have been had it not been encrypted.

    • Get the curve25519 keys of the devices which were involved in telling us about the claimedEd25519Key and sender curve25519 key.

      Normally this will be empty, but in the case of a forwarded megolm session, the sender keys are sent to us by another device (the forwarding device), which we need to trust to do this. In that case, the result will be a list consisting of one entry.

      If the device that sent us the key (A) got it from another device which it wasn't prepared to vouch for (B), the result will be [A, B]. And so on.

      Returns string[]

      base64-encoded curve25519 keys, from oldest to newest.

    • Get the event_id for this event.

      Returns undefined | string

      The event ID, e.g. $143350589368169JsLZx:localhost

    • The additional keys the sender of this encrypted event claims to possess.

      Just a wrapper for #getClaimedEd25519Key (q.v.)

      Returns Partial<Record<"ed25519", string>>

    • Get the age of the event when this function was called. This is the 'age' field adjusted according to how long this client has had the event.

      Returns number

      The age of this event in milliseconds.

    • Get the user's room membership at the time the event was sent, as reported by the server. This uses MSC4115.

      Returns Optional<string>

      The user's room membership, or undefined if the server does not report it.

    • Get the (decrypted, if necessary) event content JSON, even if the event was replaced by another event.

      Type Parameters

      Returns T

      The event content JSON, or an empty object.

    • Get the previous event content JSON. This will only return something for state events which exist in the timeline.

      Returns IContent

      The previous event content JSON, or an empty object.

    • Get the room_id for this event. This will return undefined for m.presence events.

      Returns undefined | string

      The room ID, e.g. !cURbafjkfsMDVwdRDQ:matrix.org

    • Get the user_id for this event.

      Returns undefined | string

      The user ID, e.g. @alice:matrix.org

    • The curve25519 key for the device that we think sent this event

      For an Olm-encrypted event, this is inferred directly from the DH exchange at the start of the session: the curve25519 key is involved in the DH exchange, so only a device which holds the private part of that key can establish such a session.

      For a megolm-encrypted event, it is inferred from the Olm message which established the megolm session

      Returns null | string

    • Type Parameters

      • T

      Parameters

      • relType: string

      Returns undefined | T

    • Get the event state_key if it has one. This will return undefined for message events.

      Returns undefined | string

      The event's state_key.

    • Get the timestamp of this event.

      Returns number

      The event timestamp, e.g. 1433502692297

    • Get the (decrypted, if necessary) type of event.

      Returns string

      The event type, e.g. m.room.message

    • Get the (possibly encrypted) event content JSON that will be sent to the homeserver.

      Returns IContent

      The event content JSON, or an empty object.

    • Get the (possibly encrypted) type of the event that will be sent to the homeserver.

      Returns string

      The event type.

    • Replace the event property and recalculate any properties based on it.

      Parameters

      • event: object

        the object to assign to the event property

      Returns void

    • Checks if this event is associated with another event. See getAssociatedId.

      Returns boolean

    • Check if this event is currently being decrypted.

      Returns boolean

      True if this event is currently being decrypted, else false.

    • Gets whether or not the event is flagged as cancelled. See flagCancelled() for more information.

      Returns boolean

      True if the event is cancelled, false otherwise.

    • Check if this event is an encrypted event which we failed to decrypt

      (This implies that we might retry decryption at some point in the future)

      Returns boolean

      True if this event is an encrypted event which we couldn't decrypt.

    • Check if the event is encrypted.

      Returns boolean

      True if this event is encrypted.

    • Determines if this event is equivalent to the given event. This only checks the event object itself, not the other properties of the event. Intended for use with toSnapshot() to identify events changing.

      Parameters

      • OptionalotherEvent: MatrixEvent

        The other event to check against.

      Returns boolean

      True if the events are the same, false otherwise.

    • Whether the decryption key was obtained from an untrusted source. If so, we cannot verify the authenticity of the message.

      Returns undefined | boolean

    • Check if this event has been redacted

      Returns boolean

      True if this event has been redacted

    • Check if this event is a redaction of another event

      Returns boolean

      True if this event is a redaction

    • Get whether the event is a relation event, and of a given type if relType is passed in. State events cannot be relation events

      Parameters

      • OptionalrelType: string

        if given, checks that the relation is of the given type

      Returns boolean

    • Whether the event is in any phase of sending, send failure, waiting for remote echo, etc.

      Returns boolean

    • Check if this event is a state event.

      Returns boolean

      True if this is a state event.

    • Internal

      Replace the content of this event with encrypted versions. (This is used when sending an event; it should not be used by applications).

      Parameters

      • cryptoType: string

        type of the encrypted event - typically "m.room.encrypted"

      • cryptoContent: object

        raw 'content' for the encrypted event.

      • senderCurve25519Key: string

        curve25519 key to record for the sender of this event. See MatrixEvent#getSenderKey.

      • claimedEd25519Key: string

        claimed ed25519 key to record for the sender if this event. See MatrixEvent#getClaimedEd25519Key

      Returns void

    • Update the content of an event in the same way it would be by the server if it were redacted before it was sent to us

      Parameters

      • redactionEvent: MatrixEvent

        event causing the redaction

      • room: Room

        the room in which the event exists

      Returns void

    • Returns the event replacing the content of this event, if any. Replacements are aggregated on the server, so this would only return an event in case it came down the sync, or for local echo of edits.

      Returns null | MatrixEvent

    • Returns the origin_server_ts of the event replacing the content of this event, if any.

      Returns undefined | Date

    • Returns the event ID of the event replacing the content of this event, if any.

      Returns undefined | string

    • Internal

      Update the sentinels and forwardLooking flag for this event.

      Parameters

      • stateContext: RoomState

        the room state to be queried

      • toStartOfTimeline: boolean

        if true the event's forwardLooking flag is set false

      Returns void

    • Set the instance of a thread associated with the current event

      Parameters

      • Optionalthread: Thread

        the thread

      Returns void

    • Summarise the event as JSON.

      If encrypted, include both the decrypted and encrypted view of the event.

      This is named toJSON for use with JSON.stringify which checks objects for functions named toJSON and will call them to customise the output if they are defined.

      WARNING Do not log the result of this method; otherwise, it will end up in rageshakes, leading to a privacy violation.

      Returns object

      Prefer to use MatrixEvent#getEffectiveEvent or similar. This method will be removed soon; it is too easy to use it accidentally and cause a privacy violation (cf https://github.com/vector-im/element-web/issues/26380). In any case, the value it returns is not a faithful serialization of the object.

    • Get a copy/snapshot of this event. The returned copy will be loosely linked back to this instance, though will have "frozen" event information. Other properties of this MatrixEvent instance will be copied verbatim, which can mean they are in reference to this instance despite being on the copy too. The reference the snapshot uses does not change, however members aside from the underlying event will not be deeply cloned, thus may be mutated internally. For example, the sender profile will be copied over at snapshot time, and the sender profile internally may mutate without notice to the consumer.

      This is meant to be used to snapshot the event details themselves, not the features (such as sender) surrounding the event.

      Returns MatrixEvent

      A snapshot of this event.

    • Update the related id with a new one.

      Used to replace a local id with remote one before sending an event with a related id.

      Parameters

      • eventId: string

        the new event id

      Returns void

    MMNEPVFCICPMFPCPTTAAATR