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/latest/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid
9
10 use ruma_common::{
11 api::{request, response, Metadata},
12 metadata,
13 serde::Raw,
14 MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedTransactionId,
15 };
16 use ruma_events::{AnyMessageLikeEventContent, MessageLikeEventContent, MessageLikeEventType};
17 use serde_json::value::to_raw_value as to_raw_json_value;
18
19 const METADATA: Metadata = 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(error = crate::Error)]
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/latest/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 pub body: Raw<AnyMessageLikeEventContent>,
55
56 /// Timestamp to use for the `origin_server_ts` of the event.
57 ///
58 /// This is called [timestamp massaging] and can only be used by Appservices.
59 ///
60 /// Note that this does not change the position of the event in the timeline.
61 ///
62 /// [timestamp massaging]: https://spec.matrix.org/latest/application-service-api/#timestamp-massaging
63 #[ruma_api(query)]
64 #[serde(skip_serializing_if = "Option::is_none", rename = "ts")]
65 pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
66 }
67
68 /// Response type for the `create_message_event` endpoint.
69 #[response(error = crate::Error)]
70 pub struct Response {
71 /// A unique identifier for the event.
72 pub event_id: OwnedEventId,
73 }
74
75 impl Request {
76 /// Creates a new `Request` with the given room id, transaction id and event content.
77 ///
78 /// # Errors
79 ///
80 /// Since `Request` stores the request body in serialized form, this function can fail if
81 /// `T`s [`Serialize`][serde::Serialize] implementation can fail.
82 pub fn new<T>(
83 room_id: OwnedRoomId,
84 txn_id: OwnedTransactionId,
85 content: &T,
86 ) -> serde_json::Result<Self>
87 where
88 T: MessageLikeEventContent,
89 {
90 Ok(Self {
91 room_id,
92 txn_id,
93 event_type: content.event_type(),
94 body: Raw::from_json(to_raw_json_value(content)?),
95 timestamp: None,
96 })
97 }
98
99 /// Creates a new `Request` with the given room id, transaction id, event type and raw event
100 /// content.
101 pub fn new_raw(
102 room_id: OwnedRoomId,
103 txn_id: OwnedTransactionId,
104 event_type: MessageLikeEventType,
105 body: Raw<AnyMessageLikeEventContent>,
106 ) -> Self {
107 Self { room_id, event_type, txn_id, body, timestamp: None }
108 }
109 }
110
111 impl Response {
112 /// Creates a new `Response` with the given event id.
113 pub fn new(event_id: OwnedEventId) -> Self {
114 Self { event_id }
115 }
116 }
117}