1use super::{ExternalActor, MessagingAddress, MessagingModelError};
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6pub const RELATIONSHIP_RESOURCE: &str = "messaging.relationships";
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase", deny_unknown_fields)]
10pub struct MessagingCloudRequest {
11 pub tenant_id: String,
12 pub space_id: String,
13 pub operation: MessagingIngressOperation,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(tag = "operation", content = "input", rename_all = "snake_case")]
18pub enum MessagingIngressOperation {
19 Register(IngressRegister),
20 Redeem(IngressRedeem),
21 Unregister(IngressUnregister),
22 GrantCreate(IngressGrant),
23 GrantDelete(IngressGrant),
24 BindingUpdate(IngressBindingUpdate),
25 Unbind(IngressUnbind),
26}
27impl MessagingIngressOperation {
28 pub fn target(&self) -> &'static str {
29 match self {
30 Self::Register(_) => "/messaging/ingress/register",
31 Self::Redeem(_) => "/messaging/ingress/redeem",
32 Self::Unregister(_) => "/messaging/ingress/unregister",
33 Self::GrantCreate(_) => "/messaging/ingress/grant/create",
34 Self::GrantDelete(_) => "/messaging/ingress/grant/delete",
35 Self::BindingUpdate(_) => "/messaging/ingress/binding/update",
36 Self::Unbind(_) => "/messaging/ingress/unbind",
37 }
38 }
39 pub fn payload(&self) -> Result<String, serde_json::Error> {
40 match self {
41 Self::Register(input) => serde_json::to_string(input),
42 Self::Redeem(input) => serde_json::to_string(input),
43 Self::Unregister(input) => serde_json::to_string(input),
44 Self::GrantCreate(input) | Self::GrantDelete(input) => serde_json::to_string(input),
45 Self::BindingUpdate(input) => serde_json::to_string(input),
46 Self::Unbind(input) => serde_json::to_string(input),
47 }
48 }
49}
50#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(rename_all = "camelCase", deny_unknown_fields)]
52pub struct IngressRegister {
53 pub provider: String,
54 pub account_id: String,
55 pub instance_id: String,
56 pub owner_user_id: String,
57}
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase", deny_unknown_fields)]
60pub struct IngressRegistered {
61 pub resource_id: String,
62 pub instance_id: String,
63}
64#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
65#[serde(rename_all = "snake_case")]
66pub enum IdentityCodeKind {
67 Link,
68 Bind,
69}
70#[derive(Debug, Clone, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase", deny_unknown_fields)]
72pub struct IngressRedeem {
73 pub resource_id: String,
74 pub code: String,
75 pub kind: IdentityCodeKind,
76 pub context: IdentityRedemptionContext,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
82#[serde(rename_all = "camelCase", deny_unknown_fields)]
83pub struct IdentityRedemptionContext {
84 pub event_id: String,
85 pub address: MessagingAddress,
86 pub actor: ExternalActor,
87 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub conversation_display_name: Option<String>,
89}
90
91impl IdentityRedemptionContext {
92 pub fn validate(self) -> Result<Self, MessagingModelError> {
93 let event_id = super::model::segment(self.event_id, "messaging event id is invalid")?;
94 let address = self.address.validate()?.base_address();
95 let actor = self.actor.validate()?;
96 if actor.provider != address.provider || actor.account_id != address.account_id {
97 return Err(MessagingModelError(
98 "messaging actor and address account do not match",
99 ));
100 }
101 super::model::optional_ref(
102 &self.conversation_display_name,
103 "messaging conversation display name is invalid",
104 )?;
105 Ok(Self {
106 event_id,
107 address,
108 actor,
109 conversation_display_name: self.conversation_display_name,
110 })
111 }
112}
113#[derive(Debug, Clone, Serialize, Deserialize)]
114#[serde(rename_all = "camelCase", deny_unknown_fields)]
115pub struct IngressRedemption {
116 pub challenge_id: String,
117 pub state: String,
118 pub message: String,
119}
120#[derive(Debug, Clone, Serialize, Deserialize)]
121#[serde(rename_all = "camelCase", deny_unknown_fields)]
122pub struct IngressUnregister {
123 pub resource_id: String,
124 pub owner_user_id: String,
125}
126#[derive(Debug, Clone, Serialize, Deserialize)]
127#[serde(rename_all = "camelCase", deny_unknown_fields)]
128pub struct MessagingRelationshipApply {
129 pub tenant_id: String,
130 pub space_id: String,
131 pub resource_id: String,
132 pub snapshot: MessagingRelationshipsSnapshot,
133}
134#[derive(Debug, Clone, Serialize, Deserialize)]
135#[serde(rename_all = "camelCase", deny_unknown_fields)]
136pub struct MessagingRelationshipsSnapshot {
137 pub provider: String,
138 pub account_id: String,
139 pub instance_id: String,
140 pub revoked: bool,
141 pub links: Vec<MessagingLinkedIdentity>,
142 pub bindings: Vec<MessagingGroupBinding>,
143 pub grants: Vec<MessagingSpaceGrant>,
144}
145#[derive(Debug, Clone, Serialize, Deserialize)]
146#[serde(rename_all = "camelCase", deny_unknown_fields)]
147pub struct MessagingLinkedIdentity {
148 pub link_id: String,
149 pub external_user_id: String,
150 pub tenant_id: String,
151 pub user_id: String,
152 pub display_name: Option<String>,
153 pub created_at_ms: i64,
154 pub updated_at_ms: i64,
155}
156#[derive(Debug, Clone, Serialize, Deserialize)]
157#[serde(rename_all = "camelCase", deny_unknown_fields)]
158pub struct MessagingGroupBinding {
159 pub binding_id: String,
160 pub conversation_id: String,
161 pub tenant_id: String,
162 pub space_id: String,
163 pub bound_by_user_id: String,
164 pub display_name: Option<String>,
165 pub created_at_ms: i64,
166 pub projections: Value,
167}
168#[derive(Debug, Clone, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase", deny_unknown_fields)]
170pub struct MessagingSpaceGrant {
171 pub tenant_id: String,
172 pub space_id: String,
173 pub granted_by_user_id: String,
174 pub created_at_ms: i64,
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
178#[serde(rename_all = "camelCase", deny_unknown_fields)]
179pub struct IngressGrant {
180 pub resource_id: String,
181 pub space_id: String,
182 pub user_id: String,
183}
184#[derive(Debug, Clone, Serialize, Deserialize)]
185#[serde(rename_all = "camelCase", deny_unknown_fields)]
186pub struct IngressBindingUpdate {
187 pub resource_id: String,
188 pub space_id: String,
189 pub user_id: String,
190 pub conversation_id: String,
191 pub projections: Value,
192}
193#[derive(Debug, Clone, Serialize, Deserialize)]
194#[serde(rename_all = "camelCase", deny_unknown_fields)]
195pub struct IngressUnbind {
196 pub resource_id: String,
197 pub space_id: String,
198 pub user_id: String,
199 pub conversation_id: String,
200 pub binding_id: String,
201}