Skip to main content

ruma_client_api/message/
send_message_event.rs

1//! `PUT /_matrix/client/*/rooms/{roomId}/send/{eventType}/{txnId}`
2//!
3//! Send a message event to a room.
4
5pub mod v3 {
6    //! `/v3/` ([spec])
7    //!
8    //! [spec]: https://spec.matrix.org/v1.18/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
9
10    use ruma_common::{
11        MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedTransactionId,
12        api::{auth_scheme::AccessToken, request, response},
13        metadata,
14        serde::Raw,
15    };
16    use ruma_events::{AnyMessageLikeEventContent, MessageLikeEventContent, MessageLikeEventType};
17    use serde_json::value::to_raw_value as to_raw_json_value;
18
19    metadata! {
20        method: PUT,
21        rate_limited: false,
22        authentication: AccessToken,
23        history: {
24            1.0 => "/_matrix/client/r0/rooms/{room_id}/send/{event_type}/{txn_id}",
25            1.1 => "/_matrix/client/v3/rooms/{room_id}/send/{event_type}/{txn_id}",
26        }
27    }
28
29    /// Request type for the `create_message_event` endpoint.
30    #[request]
31    pub struct Request {
32        /// The room to send the event to.
33        #[ruma_api(path)]
34        pub room_id: OwnedRoomId,
35
36        /// The type of event to send.
37        #[ruma_api(path)]
38        pub event_type: MessageLikeEventType,
39
40        /// The transaction ID for this event.
41        ///
42        /// Clients should generate a unique ID across requests within the
43        /// same session. A session is identified by an access token, and
44        /// persists when the [access token is refreshed].
45        ///
46        /// It will be used by the server to ensure idempotency of requests.
47        ///
48        /// [access token is refreshed]: https://spec.matrix.org/v1.18/client-server-api/#refreshing-access-tokens
49        #[ruma_api(path)]
50        pub txn_id: OwnedTransactionId,
51
52        /// The event content to send.
53        #[ruma_api(body)]
54        #[serde(deserialize_with = "ruma_common::serde::deserialize_raw_object")]
55        pub body: Raw<AnyMessageLikeEventContent>,
56
57        /// Timestamp to use for the `origin_server_ts` of the event.
58        ///
59        /// This is called [timestamp massaging] and can only be used by Appservices.
60        ///
61        /// Note that this does not change the position of the event in the timeline.
62        ///
63        /// [timestamp massaging]: https://spec.matrix.org/v1.18/application-service-api/#timestamp-massaging
64        #[ruma_api(query)]
65        #[serde(skip_serializing_if = "Option::is_none", rename = "ts")]
66        pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
67    }
68
69    /// Response type for the `create_message_event` endpoint.
70    #[response]
71    pub struct Response {
72        /// A unique identifier for the event.
73        pub event_id: OwnedEventId,
74    }
75
76    impl Request {
77        /// Creates a new `Request` with the given room id, transaction id and event content.
78        ///
79        /// # Errors
80        ///
81        /// Since `Request` stores the request body in serialized form, this function can fail if
82        /// `T`s [`Serialize`][serde::Serialize] implementation can fail.
83        pub fn new<T>(
84            room_id: OwnedRoomId,
85            txn_id: OwnedTransactionId,
86            content: &T,
87        ) -> serde_json::Result<Self>
88        where
89            T: MessageLikeEventContent,
90        {
91            Ok(Self {
92                room_id,
93                txn_id,
94                event_type: content.event_type(),
95                body: Raw::from_json(to_raw_json_value(content)?),
96                timestamp: None,
97            })
98        }
99
100        /// Creates a new `Request` with the given room id, transaction id, event type and raw event
101        /// content.
102        pub fn new_raw(
103            room_id: OwnedRoomId,
104            txn_id: OwnedTransactionId,
105            event_type: MessageLikeEventType,
106            body: Raw<AnyMessageLikeEventContent>,
107        ) -> Self {
108            Self { room_id, event_type, txn_id, body, timestamp: None }
109        }
110    }
111
112    impl Response {
113        /// Creates a new `Response` with the given event id.
114        pub fn new(event_id: OwnedEventId) -> Self {
115            Self { event_id }
116        }
117    }
118}