ruma_events/call/hangup.rs
1//! Types for the [`m.call.hangup`] event.
2//!
3//! [`m.call.hangup`]: https://spec.matrix.org/latest/client-server-api/#mcallhangup
4
5use ruma_common::{serde::StringEnum, OwnedVoipId, VoipVersionId};
6use ruma_macros::EventContent;
7use serde::{Deserialize, Serialize};
8
9use crate::PrivOwnedStr;
10
11/// The content of an `m.call.hangup` event.
12///
13/// Sent by either party to signal their termination of the call.
14///
15/// In VoIP version 0, this can be sent either once the call has been established or before to abort
16/// the call.
17///
18/// If the call is using VoIP version 1, this should only be sent by the caller after sending the
19/// invite or by the callee after answering the invite. To reject an invite, send an
20/// [`m.call.reject`] event.
21///
22/// [`m.call.reject`]: super::reject::CallRejectEventContent
23#[derive(Clone, Debug, Deserialize, Serialize, EventContent)]
24#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
25#[ruma_event(type = "m.call.hangup", kind = MessageLike)]
26pub struct CallHangupEventContent {
27 /// A unique identifier for the call.
28 pub call_id: OwnedVoipId,
29
30 /// **Required in VoIP version 1.** A unique ID for this session for the duration of the call.
31 ///
32 /// Must be the same as the one sent by the previous invite or answer from
33 /// this session.
34 #[serde(skip_serializing_if = "Option::is_none")]
35 pub party_id: Option<OwnedVoipId>,
36
37 /// The version of the VoIP specification this messages adheres to.
38 pub version: VoipVersionId,
39
40 /// Error reason for the hangup.
41 ///
42 /// Defaults to `Reason::UserHangup`.
43 #[serde(default)]
44 pub reason: Reason,
45}
46
47impl CallHangupEventContent {
48 /// Creates a new `CallHangupEventContent` with the given call ID and VoIP version.
49 pub fn new(call_id: OwnedVoipId, version: VoipVersionId) -> Self {
50 Self { call_id, party_id: None, version, reason: Default::default() }
51 }
52
53 /// Convenience method to create a VoIP version 0 `CallHangupEventContent` with all the required
54 /// fields.
55 pub fn version_0(call_id: OwnedVoipId) -> Self {
56 Self::new(call_id, VoipVersionId::V0)
57 }
58
59 /// Convenience method to create a VoIP version 1 `CallHangupEventContent` with all the required
60 /// fields.
61 pub fn version_1(call_id: OwnedVoipId, party_id: OwnedVoipId, reason: Reason) -> Self {
62 Self { call_id, party_id: Some(party_id), version: VoipVersionId::V1, reason }
63 }
64}
65
66/// A reason for a hangup.
67///
68/// Should not be provided when the user naturally ends or rejects the call. When there was an error
69/// in the call negotiation, this should be `ice_failed` for when ICE negotiation fails or
70/// `invite_timeout` for when the other party did not answer in time.
71#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
72#[derive(Clone, Default, PartialEq, Eq, StringEnum)]
73#[ruma_enum(rename_all = "snake_case")]
74#[non_exhaustive]
75pub enum Reason {
76 /// ICE negotiation failure.
77 IceFailed,
78
79 /// Party did not answer in time.
80 InviteTimeout,
81
82 /// The connection failed after some media was exchanged.
83 ///
84 /// Note that, in the case of an ICE renegotiation, a client should be sure to send
85 /// `ice_timeout` rather than `ice_failed` if media had previously been received successfully,
86 /// even if the ICE renegotiation itself failed.
87 IceTimeout,
88
89 /// The user chose to end the call.
90 #[default]
91 UserHangup,
92
93 /// The client was unable to start capturing media in such a way as it is unable to continue
94 /// the call.
95 UserMediaFailed,
96
97 /// The user is busy.
98 UserBusy,
99
100 /// Some other failure occurred that meant the client was unable to continue the call rather
101 /// than the user choosing to end it.
102 UnknownError,
103
104 #[doc(hidden)]
105 _Custom(PrivOwnedStr),
106}