/ruma-common/src/thirdparty.rs

https://github.com/ruma/ruma · Rust · 200 lines · 100 code · 37 blank · 63 comment · 3 complexity · a290d260d140c9c9075e7dcf345eb84d MD5 · raw file

  1. //! Common types for the [third party networks module][thirdparty]
  2. //!
  3. //! [thirdparty]: https://matrix.org/docs/spec/client_server/r0.6.1#id153
  4. use std::collections::BTreeMap;
  5. use ruma_identifiers::{RoomAliasId, UserId};
  6. use serde::{Deserialize, Serialize};
  7. /// Metadata about a third party protocol.
  8. ///
  9. /// To create an instance of this type, first create a `ProtocolInit` and convert it via
  10. /// `Protocol::from` / `.into()`.
  11. #[derive(Clone, Debug, Deserialize, Serialize)]
  12. #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
  13. pub struct Protocol {
  14. /// Fields which may be used to identify a third party user.
  15. pub user_fields: Vec<String>,
  16. /// Fields which may be used to identify a third party location.
  17. pub location_fields: Vec<String>,
  18. /// A content URI representing an icon for the third party protocol.
  19. #[cfg(not(feature = "unstable-synapse-quirks"))]
  20. pub icon: String,
  21. /// A content URI representing an icon for the third party protocol.
  22. #[cfg(feature = "unstable-synapse-quirks")]
  23. pub icon: Option<String>,
  24. /// The type definitions for the fields defined in `user_fields` and `location_fields`.
  25. pub field_types: BTreeMap<String, FieldType>,
  26. /// A list of objects representing independent instances of configuration.
  27. pub instances: Vec<ProtocolInstance>,
  28. }
  29. /// Initial set of fields of `Protocol`.
  30. ///
  31. /// This struct will not be updated even if additional fields are added to `Prococol` in a new
  32. /// (non-breaking) release of the Matrix specification.
  33. #[derive(Debug)]
  34. pub struct ProtocolInit {
  35. /// Fields which may be used to identify a third party user.
  36. pub user_fields: Vec<String>,
  37. /// Fields which may be used to identify a third party location.
  38. pub location_fields: Vec<String>,
  39. /// A content URI representing an icon for the third party protocol.
  40. #[cfg(not(feature = "unstable-synapse-quirks"))]
  41. pub icon: String,
  42. /// A content URI representing an icon for the third party protocol.
  43. #[cfg(feature = "unstable-synapse-quirks")]
  44. pub icon: Option<String>,
  45. /// The type definitions for the fields defined in `user_fields` and `location_fields`.
  46. pub field_types: BTreeMap<String, FieldType>,
  47. /// A list of objects representing independent instances of configuration.
  48. pub instances: Vec<ProtocolInstance>,
  49. }
  50. impl From<ProtocolInit> for Protocol {
  51. fn from(init: ProtocolInit) -> Self {
  52. let ProtocolInit { user_fields, location_fields, icon, field_types, instances } = init;
  53. Self { user_fields, location_fields, icon, field_types, instances }
  54. }
  55. }
  56. /// Metadata about an instance of a third party protocol.
  57. ///
  58. /// To create an instance of this type, first create a `ProtocolInstanceInit` and convert it via
  59. /// `ProtocolInstance::from` / `.into()`.
  60. #[derive(Clone, Debug, Deserialize, Serialize)]
  61. #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
  62. pub struct ProtocolInstance {
  63. /// A human-readable description for the protocol, such as the name.
  64. pub desc: String,
  65. /// An optional content URI representing the protocol.
  66. #[serde(skip_serializing_if = "Option::is_none")]
  67. pub icon: Option<String>,
  68. /// Preset values for `fields` the client may use to search by.
  69. pub fields: BTreeMap<String, String>,
  70. /// A unique identifier across all instances.
  71. pub network_id: String,
  72. }
  73. /// Initial set of fields of `Protocol`.
  74. ///
  75. /// This struct will not be updated even if additional fields are added to `Prococol` in a new
  76. /// (non-breaking) release of the Matrix specification.
  77. #[derive(Debug)]
  78. pub struct ProtocolInstanceInit {
  79. /// A human-readable description for the protocol, such as the name.
  80. pub desc: String,
  81. /// Preset values for `fields` the client may use to search by.
  82. pub fields: BTreeMap<String, String>,
  83. /// A unique identifier across all instances.
  84. pub network_id: String,
  85. }
  86. impl From<ProtocolInstanceInit> for ProtocolInstance {
  87. fn from(init: ProtocolInstanceInit) -> Self {
  88. let ProtocolInstanceInit { desc, fields, network_id } = init;
  89. Self { desc, icon: None, fields, network_id }
  90. }
  91. }
  92. /// A type definition for a field used to identify third party users or locations.
  93. ///
  94. /// To create an instance of this type, first create a `FieldTypeInit` and convert it via
  95. /// `FieldType::from` / `.into()`.
  96. #[derive(Clone, Debug, Deserialize, Serialize)]
  97. #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
  98. pub struct FieldType {
  99. /// A regular expression for validation of a field's value.
  100. pub regexp: String,
  101. /// A placeholder serving as a valid example of the field value.
  102. pub placeholder: String,
  103. }
  104. /// Initial set of fields of `FieldType`.
  105. ///
  106. /// This struct will not be updated even if additional fields are added to `FieldType` in a new
  107. /// (non-breaking) release of the Matrix specification.
  108. #[derive(Debug)]
  109. pub struct FieldTypeInit {
  110. /// A regular expression for validation of a field's value.
  111. pub regexp: String,
  112. /// A placeholder serving as a valid example of the field value.
  113. pub placeholder: String,
  114. }
  115. impl From<FieldTypeInit> for FieldType {
  116. fn from(init: FieldTypeInit) -> Self {
  117. let FieldTypeInit { regexp, placeholder } = init;
  118. Self { regexp, placeholder }
  119. }
  120. }
  121. /// A third party network location.
  122. #[derive(Clone, Debug, Deserialize, Serialize)]
  123. #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
  124. pub struct Location {
  125. /// An alias for a matrix room.
  126. pub alias: RoomAliasId,
  127. /// The protocol ID that the third party location is a part of.
  128. pub protocol: String,
  129. /// Information used to identify this third party location.
  130. pub fields: BTreeMap<String, String>,
  131. }
  132. impl Location {
  133. /// Creates a new `Location` with the given alias, protocol and fields.
  134. pub fn new(alias: RoomAliasId, protocol: String, fields: BTreeMap<String, String>) -> Self {
  135. Self { alias, protocol, fields }
  136. }
  137. }
  138. /// A third party network user.
  139. #[derive(Clone, Debug, Deserialize, Serialize)]
  140. pub struct User {
  141. /// A matrix user ID representing a third party user.
  142. pub userid: UserId,
  143. /// The protocol ID that the third party user is a part of.
  144. pub protocol: String,
  145. /// Information used to identify this third party user.
  146. pub fields: BTreeMap<String, String>,
  147. }
  148. impl User {
  149. /// Creates a new `User` with the given userid, protocol and fields.
  150. pub fn new(userid: UserId, protocol: String, fields: BTreeMap<String, String>) -> Self {
  151. Self { userid, protocol, fields }
  152. }
  153. }
  154. /// The medium of a third party identifier.
  155. #[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
  156. #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
  157. #[serde(rename_all = "lowercase")]
  158. pub enum Medium {
  159. /// Email address identifier
  160. Email,
  161. /// Phone number identifier
  162. MSISDN,
  163. }