mhome-messaging-api 0.9.0

Typed control and normalized data-plane contracts for mHome messaging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use conversation_api::ConversationSurface;
use serde::{Deserialize, Serialize};
use std::fmt;

pub const NORMALIZED_INBOUND_SCHEMA_VERSION: u16 = 3;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConversationAudience {
    Personal,
    Shared,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MessagingAddress {
    pub provider: String,
    pub account_id: String,
    pub conversation_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lane_id: Option<String>,
    pub audience: ConversationAudience,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessagingModelError(&'static str);

impl fmt::Display for MessagingModelError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.0)
    }
}

impl std::error::Error for MessagingModelError {}

impl MessagingAddress {
    pub fn new(
        provider: impl Into<String>,
        account_id: impl Into<String>,
        conversation_id: impl Into<String>,
        lane_id: Option<String>,
        audience: ConversationAudience,
    ) -> Result<Self, MessagingModelError> {
        Ok(Self {
            provider: provider_id(provider.into())?,
            account_id: segment(account_id.into(), "messaging account id is invalid")?,
            conversation_id: segment(
                conversation_id.into(),
                "messaging conversation id is invalid",
            )?,
            lane_id: lane_id
                .map(|value| segment(value, "messaging lane id is invalid"))
                .transpose()?,
            audience,
        })
    }

    #[must_use]
    pub fn base_address(&self) -> Self {
        let mut base = self.clone();
        base.lane_id = None;
        base
    }

    pub fn conversation_surface(&self) -> Result<ConversationSurface, MessagingModelError> {
        let result = match self.audience {
            ConversationAudience::Personal => ConversationSurface::messaging_personal(
                &self.provider,
                &self.account_id,
                &self.conversation_id,
                self.lane_id.clone(),
            ),
            ConversationAudience::Shared => ConversationSurface::messaging_group(
                &self.provider,
                &self.account_id,
                &self.conversation_id,
                self.lane_id.clone(),
            ),
        };
        result.map_err(|_| MessagingModelError("messaging address cannot form a surface"))
    }

    pub fn validate(self) -> Result<Self, MessagingModelError> {
        Self::new(
            self.provider,
            self.account_id,
            self.conversation_id,
            self.lane_id,
            self.audience,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalActor {
    pub provider: String,
    pub account_id: String,
    pub external_user_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
}

impl ExternalActor {
    pub fn new(
        provider: impl Into<String>,
        account_id: impl Into<String>,
        external_user_id: impl Into<String>,
        display_name: Option<String>,
    ) -> Result<Self, MessagingModelError> {
        Ok(Self {
            provider: provider_id(provider.into())?,
            account_id: segment(account_id.into(), "messaging actor account id is invalid")?,
            external_user_id: segment(
                external_user_id.into(),
                "messaging external user id is invalid",
            )?,
            display_name: display_name.map(optional_segment).transpose()?,
        })
    }

    pub fn validate(self) -> Result<Self, MessagingModelError> {
        Self::new(
            self.provider,
            self.account_id,
            self.external_user_id,
            self.display_name,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ActionOption {
    pub label: String,
    pub token: String,
}

impl ActionOption {
    pub fn new(
        label: impl Into<String>,
        token: impl Into<String>,
    ) -> Result<Self, MessagingModelError> {
        Ok(Self {
            label: segment(label.into(), "action label is invalid")?,
            token: segment(token.into(), "action token is invalid")?,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ActionSet {
    pub options: Vec<ActionOption>,
}

impl ActionSet {
    pub fn new(options: Vec<ActionOption>) -> Result<Self, MessagingModelError> {
        if options.is_empty() {
            return Err(MessagingModelError("action set cannot be empty"));
        }
        for option in &options {
            segment_ref(&option.label, "action label is invalid")?;
            segment_ref(&option.token, "action token is invalid")?;
        }
        Ok(Self { options })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProviderMediaKind {
    Image,
    Audio,
    Video,
    File,
}

/// Provider-scoped, short-lived media handle. It must be materialized before entering Conversation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ProviderMediaRef {
    pub handle: String,
    pub kind: ProviderMediaKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width_px: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub height_px: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub caption: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub transcript: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
    tag = "type",
    rename_all = "snake_case",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum InboundMessagePart {
    Text { text: String },
    Media { reference: ProviderMediaRef },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "type",
    rename_all = "snake_case",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum NormalizedInboundContent {
    Message {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        provider_message_id: Option<String>,
        parts: Vec<InboundMessagePart>,
    },
    ActionSelected {
        token: String,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NormalizedInbound {
    pub schema_version: u16,
    pub event_id: String,
    pub address: MessagingAddress,
    pub actor: ExternalActor,
    pub content: NormalizedInboundContent,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conversation_display_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub occurred_at_ms: Option<i64>,
}

impl NormalizedInbound {
    pub fn validate(self) -> Result<Self, MessagingModelError> {
        if self.schema_version != NORMALIZED_INBOUND_SCHEMA_VERSION {
            return Err(MessagingModelError(
                "unsupported normalized messaging schema version",
            ));
        }
        let event_id = segment(self.event_id, "messaging event id is invalid")?;
        let address = self.address.validate()?;
        let actor = self.actor.validate()?;
        if actor.provider != address.provider || actor.account_id != address.account_id {
            return Err(MessagingModelError(
                "messaging actor and address account do not match",
            ));
        }
        if self.occurred_at_ms.is_some_and(|value| value < 0) {
            return Err(MessagingModelError(
                "messaging occurrence time cannot be negative",
            ));
        }
        validate_content(&self.content)?;
        optional_ref(
            &self.conversation_display_name,
            "messaging conversation display name is invalid",
        )?;
        Ok(Self {
            schema_version: self.schema_version,
            event_id,
            address,
            actor,
            content: self.content,
            conversation_display_name: self.conversation_display_name,
            occurred_at_ms: self.occurred_at_ms,
        })
    }
}

fn validate_content(content: &NormalizedInboundContent) -> Result<(), MessagingModelError> {
    match content {
        NormalizedInboundContent::Message {
            provider_message_id,
            parts,
        } => {
            if parts.is_empty() {
                return Err(MessagingModelError("messaging message has no parts"));
            }
            optional_ref(provider_message_id, "provider message id is invalid")?;
            for part in parts {
                match part {
                    InboundMessagePart::Text { text } if text.is_empty() => {
                        return Err(MessagingModelError("messaging text is invalid"));
                    }
                    InboundMessagePart::Text { .. } => {}
                    InboundMessagePart::Media { reference } => validate_media(reference)?,
                }
            }
        }
        NormalizedInboundContent::ActionSelected { token } => {
            segment_ref(token, "action token is invalid")?;
        }
    }
    Ok(())
}

fn validate_media(reference: &ProviderMediaRef) -> Result<(), MessagingModelError> {
    segment_ref(&reference.handle, "provider media handle is invalid")?;
    optional_ref(&reference.mime_type, "provider media MIME type is invalid")?;
    optional_ref(&reference.file_name, "provider media file name is invalid")?;
    optional_ref(&reference.caption, "provider media caption is invalid")?;
    optional_ref(
        &reference.transcript,
        "provider media transcript is invalid",
    )?;
    if reference.size_bytes == Some(0)
        || reference.duration_ms == Some(0)
        || reference.width_px == Some(0)
        || reference.height_px == Some(0)
    {
        return Err(MessagingModelError("provider media metadata is invalid"));
    }
    if reference.width_px.is_some() != reference.height_px.is_some() {
        return Err(MessagingModelError(
            "provider media dimensions must be complete",
        ));
    }
    Ok(())
}

fn provider_id(value: String) -> Result<String, MessagingModelError> {
    let normalized = value.trim().to_ascii_lowercase();
    if normalized.is_empty()
        || !normalized
            .as_bytes()
            .first()
            .is_some_and(u8::is_ascii_lowercase)
        || !normalized
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
    {
        return Err(MessagingModelError("messaging provider id is invalid"));
    }
    Ok(normalized)
}

fn segment(value: String, error: &'static str) -> Result<String, MessagingModelError> {
    segment_ref(&value, error)?;
    Ok(value)
}

fn optional_segment(value: String) -> Result<String, MessagingModelError> {
    segment(value, "messaging display name is invalid")
}

fn segment_ref(value: &str, error: &'static str) -> Result<(), MessagingModelError> {
    if value.is_empty() || value.trim() != value {
        return Err(MessagingModelError(error));
    }
    Ok(())
}

fn optional_ref(value: &Option<String>, error: &'static str) -> Result<(), MessagingModelError> {
    if let Some(value) = value {
        segment_ref(value, error)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn address_owns_lane_and_canonical_surface_mapping() {
        let address = MessagingAddress::new(
            "Telegram",
            "bot:1",
            "chat:2",
            Some("topic:3".to_string()),
            ConversationAudience::Shared,
        )
        .unwrap();
        assert_eq!(address.provider, "telegram");
        assert_eq!(address.base_address().lane_id, None);
        let surface = address.conversation_surface().unwrap();
        let route = surface.messaging_route().unwrap();
        assert_eq!(route.lane_id, Some("topic:3"));
        assert!(route.group);
    }

    #[test]
    fn inbound_rejects_actor_from_another_provider_account() {
        let inbound = NormalizedInbound {
            schema_version: NORMALIZED_INBOUND_SCHEMA_VERSION,
            event_id: "event".to_string(),
            address: MessagingAddress::new(
                "telegram",
                "bot-a",
                "chat",
                None,
                ConversationAudience::Personal,
            )
            .unwrap(),
            actor: ExternalActor::new("telegram", "bot-b", "user", None).unwrap(),
            content: NormalizedInboundContent::Message {
                provider_message_id: None,
                parts: vec![InboundMessagePart::Text {
                    text: "hello".to_string(),
                }],
            },
            conversation_display_name: None,
            occurred_at_ms: None,
        };
        assert!(inbound.validate().is_err());
    }

    #[test]
    fn actions_only_expose_labels_and_opaque_tokens() {
        let actions = ActionSet::new(vec![
            ActionOption::new("Approve", "route-approve").unwrap(),
            ActionOption::new("Reject", "route-reject").unwrap(),
        ])
        .unwrap();
        assert_eq!(actions.options.len(), 2);
        assert!(ActionSet::new(Vec::new()).is_err());
        assert!(ActionOption::new("Approve", " ").is_err());
    }

    #[test]
    fn inbound_text_preserves_whitespace_allowed_by_the_schema() {
        let inbound = NormalizedInbound {
            schema_version: NORMALIZED_INBOUND_SCHEMA_VERSION,
            event_id: "event".to_string(),
            address: MessagingAddress::new(
                "telegram",
                "bot",
                "chat",
                None,
                ConversationAudience::Personal,
            )
            .unwrap(),
            actor: ExternalActor::new("telegram", "bot", "user", None).unwrap(),
            content: NormalizedInboundContent::Message {
                provider_message_id: None,
                parts: vec![InboundMessagePart::Text {
                    text: " message with spacing ".to_string(),
                }],
            },
            conversation_display_name: None,
            occurred_at_ms: None,
        };
        assert!(inbound.validate().is_ok());
    }
}