wechat-ilink 0.5.0

Stream-first stateless async WeChat iLink client for Rust: QR login, event-driven polling, automatic context refresh, and typed ret=-2 rate-limit backoff.
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
use hex;
use md5::{Digest, Md5};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::time::SystemTime;

/// Message sender type.
/// Uses serde_repr for integer (de)serialization: JSON `1` ↔ `MessageType::User`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum MessageType {
    None = 0,
    User = 1,
    Bot = 2,
}

impl Default for MessageType {
    fn default() -> Self {
        Self::None
    }
}

/// Message delivery state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum MessageState {
    New = 0,
    Generating = 1,
    Finish = 2,
}

impl Default for MessageState {
    fn default() -> Self {
        Self::New
    }
}

/// Content type of a message item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
#[repr(i32)]
pub enum MessageItemType {
    None = 0,
    Text = 1,
    Image = 2,
    Voice = 3,
    File = 4,
    Video = 5,
}

impl Default for MessageItemType {
    fn default() -> Self {
        Self::None
    }
}

/// Media type for upload requests.
#[derive(Debug, Clone, Copy)]
#[repr(i32)]
pub enum MediaType {
    Image = 1,
    Video = 2,
    File = 3,
    Voice = 4,
}

/// CDN media reference.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CDNMedia {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypt_query_param: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aes_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypt_type: Option<i32>,
    /// Complete download URL returned by server; when set, use directly.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_url: Option<String>,
}

/// Text content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
}

/// Image content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CDNMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_media: Option<CDNMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aeskey: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mid_size: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_size: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_width: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_height: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hd_size: Option<i64>,
}

/// Voice content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CDNMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encode_type: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bits_per_sample: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sample_rate: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub playtime: Option<i32>,
}

/// File content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CDNMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub md5: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub len: Option<String>,
}

/// Video content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VideoItem {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub media: Option<CDNMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_size: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub play_length: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_md5: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_media: Option<CDNMedia>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_size: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_height: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thumb_width: Option<i32>,
}

/// Referenced/quoted message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefMessage {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub msg_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_item: Option<Box<WireMessageItem>>,
}

/// A single content item in a message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WireMessageItem {
    #[serde(rename = "type")]
    #[serde(default)]
    pub item_type: MessageItemType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub create_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_completed: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub msg_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_item: Option<TextItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_item: Option<ImageItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub voice_item: Option<VoiceItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_item: Option<FileItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_item: Option<VideoItem>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ref_msg: Option<RefMessage>,
}

/// Raw wire message from the iLink API.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WireMessage {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seq: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_id: Option<i64>,
    #[serde(default)]
    pub from_user_id: String,
    #[serde(default)]
    pub to_user_id: String,
    #[serde(default)]
    pub client_id: String,
    #[serde(default)]
    pub create_time_ms: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delete_time_ms: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_id: Option<String>,
    #[serde(default)]
    pub message_type: MessageType,
    #[serde(default)]
    pub message_state: MessageState,
    #[serde(default)]
    pub context_token: String,
    #[serde(default)]
    pub item_list: Vec<WireMessageItem>,
}

/// Context information observed from an incoming message.
///
/// Carries the context token, user identity, and the account key that
/// was used to observe it. The [`Debug`] impl redacts the context token
/// to avoid leaking it into logs. Serialization includes the raw token;
/// do not log serialized contexts.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatContext {
    /// Account key used when this context was observed.
    pub account_key: String,
    /// WeChat user ID from whom this context was observed.
    pub user_id: String,
    /// Opaque context token for reply/send operations.
    pub context_token: String,
    /// Unix timestamp in milliseconds when the context was observed.
    pub observed_at_unix_ms: i64,
    /// Message ID of the message that produced this context, if any.
    pub source_message_id: Option<String>,
}

impl std::fmt::Debug for WechatContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WechatContext")
            .field("account_key", &self.account_key)
            .field("user_id", &self.user_id)
            .field("context_token", &"<redacted>")
            .field("observed_at_unix_ms", &self.observed_at_unix_ms)
            .field("source_message_id", &self.source_message_id)
            .finish()
    }
}

impl WechatContext {
    /// Returns a stable fingerprint of the context token.
    ///
    /// Two contexts with the same token always produce the same fingerprint,
    /// regardless of other fields. Useful for detecting token rotation without
    /// comparing the raw token.
    pub fn token_fingerprint(&self) -> String {
        let digest = Md5::digest(self.context_token.as_bytes());
        format!("md5:{}", hex::encode(digest))
    }

    /// Returns the observation time as a [`SystemTime`].
    pub fn observed_at(&self) -> SystemTime {
        std::time::UNIX_EPOCH + std::time::Duration::from_millis(self.observed_at_unix_ms as u64)
    }
}

/// Parsed incoming message — user-friendly.
#[derive(Debug, Clone)]
pub struct IncomingMessage {
    pub message_id: Option<String>,
    pub wire_message_id: Option<i64>,
    pub client_id: String,
    pub user_id: String,
    pub text: String,
    pub content_type: ContentType,
    pub timestamp: SystemTime,
    pub images: Vec<ImageContent>,
    pub voices: Vec<VoiceContent>,
    pub files: Vec<FileContent>,
    pub videos: Vec<VideoContent>,
    pub quoted: Option<QuotedMessage>,
    pub raw: WireMessage,
    pub(crate) context_token: String,
    /// Context observed from this message, including the account key.
    pub context: Option<WechatContext>,
}

impl IncomingMessage {
    /// Opaque reply token bound to this message.
    ///
    /// Prefer the full [`context`](Self::context) with
    /// [`WechatIlinkClient::send_text_with_context`](crate::WechatIlinkClient::send_text_with_context)
    /// or [`WechatIlinkClient::reply`](crate::WechatIlinkClient::reply), which uses it automatically.
    /// Use this getter only when constructing low-level protocol payloads with
    /// [`protocol::build_text_message`](crate::protocol::build_text_message) /
    /// [`protocol::build_media_message`](crate::protocol::build_media_message)
    /// for [`ILinkClient::send_message`](crate::protocol::ILinkClient::send_message).
    pub fn context_token(&self) -> &str {
        &self.context_token
    }

    /// Parse a raw [`WireMessage`] into a user-friendly [`IncomingMessage`],
    /// associating the context with the given account key.
    ///
    /// Prefer this over [`from_wire`](Self::from_wire) when the account key
    /// is known, so that [`context`](Self::context) carries correct
    /// [`WechatContext::account_key`] information.
    ///
    /// Returns `None` if the wire message is not a user-originated message
    /// (e.g. it was sent by the bot itself).
    pub fn from_wire_for_account(wire: &WireMessage, account_key: &str) -> Option<Self> {
        Self::from_wire_inner(wire, account_key)
    }

    /// Parse a raw [`WireMessage`] into a user-friendly [`IncomingMessage`].
    ///
    /// Uses a fallback account key derived from the wire message. When the
    /// account key is known, prefer [`from_wire_for_account`](Self::from_wire_for_account).
    ///
    /// Returns `None` if the wire message is not a user-originated message
    /// (e.g. it was sent by the bot itself).
    ///
    /// This is the stable entry point for consumers who drive
    /// [`ILinkClient::get_updates`](crate::protocol::ILinkClient::get_updates)
    /// themselves instead of using [`WechatIlinkClient`](crate::WechatIlinkClient)'s
    /// dispatcher.
    pub fn from_wire(wire: &WireMessage) -> Option<Self> {
        let fallback_account_key = if wire.to_user_id.is_empty() {
            wire.from_user_id.as_str()
        } else {
            wire.to_user_id.as_str()
        };
        Self::from_wire_inner(wire, fallback_account_key)
    }

    fn from_wire_inner(wire: &WireMessage, account_key: &str) -> Option<Self> {
        if wire.message_type != MessageType::User {
            return None;
        }

        let source_message_id = current_message_id(wire);

        let mut msg = IncomingMessage {
            message_id: source_message_id.clone(),
            wire_message_id: wire.message_id,
            client_id: wire.client_id.clone(),
            user_id: wire.from_user_id.clone(),
            text: extract_text(&wire.item_list),
            content_type: detect_type(&wire.item_list),
            timestamp: std::time::UNIX_EPOCH
                + std::time::Duration::from_millis(wire.create_time_ms as u64),
            images: Vec::new(),
            voices: Vec::new(),
            files: Vec::new(),
            videos: Vec::new(),
            quoted: None,
            raw: wire.clone(),
            context_token: wire.context_token.clone(),
            context: (!wire.context_token.is_empty()).then(|| WechatContext {
                account_key: account_key.to_string(),
                user_id: wire.from_user_id.clone(),
                context_token: wire.context_token.clone(),
                observed_at_unix_ms: wire.create_time_ms,
                source_message_id: source_message_id.clone(),
            }),
        };

        for item in &wire.item_list {
            if let Some(ref img) = item.image_item {
                msg.images.push(ImageContent {
                    media: img.media.clone(),
                    thumb_media: img.thumb_media.clone(),
                    aes_key: img.aeskey.clone(),
                    url: img.url.clone(),
                    width: img.thumb_width,
                    height: img.thumb_height,
                });
            }
            if let Some(ref voice) = item.voice_item {
                msg.voices.push(VoiceContent {
                    media: voice.media.clone(),
                    text: voice.text.clone(),
                    duration_ms: voice.playtime,
                    encode_type: voice.encode_type,
                });
            }
            if let Some(ref file) = item.file_item {
                msg.files.push(FileContent {
                    media: file.media.clone(),
                    file_name: file.file_name.clone(),
                    md5: file.md5.clone(),
                    size: file.len.as_ref().and_then(|s| s.parse().ok()),
                });
            }
            if let Some(ref video) = item.video_item {
                msg.videos.push(VideoContent {
                    media: video.media.clone(),
                    thumb_media: video.thumb_media.clone(),
                    duration_ms: video.play_length,
                });
            }
            if let Some(ref refm) = item.ref_msg {
                msg.quoted = Some(QuotedMessage {
                    message_id: quoted_message_id(refm),
                    title: refm.title.clone(),
                    text: refm
                        .message_item
                        .as_ref()
                        .and_then(|i| i.text_item.as_ref())
                        .and_then(|t| t.text.clone()),
                });
            }
        }

        Some(msg)
    }
}

fn quoted_message_id(refm: &RefMessage) -> Option<String> {
    refm.message_item
        .as_ref()
        .and_then(|item| item.msg_id.clone())
        .or_else(|| refm.msg_id.clone())
        .or_else(|| refm.message_id.map(|id| id.to_string()))
        .or_else(|| refm.client_id.clone())
}

fn current_message_id(wire: &WireMessage) -> Option<String> {
    wire.item_list
        .iter()
        .find_map(|item| item.msg_id.clone())
        .or_else(|| wire.message_id.map(|id| id.to_string()))
        .or_else(|| (!wire.client_id.is_empty()).then(|| wire.client_id.clone()))
}

fn detect_type(items: &[WireMessageItem]) -> ContentType {
    items
        .first()
        .map_or(ContentType::Text, |item| match item.item_type {
            MessageItemType::Image => ContentType::Image,
            MessageItemType::Voice => ContentType::Voice,
            MessageItemType::File => ContentType::File,
            MessageItemType::Video => ContentType::Video,
            _ => ContentType::Text,
        })
}

fn extract_text(items: &[WireMessageItem]) -> String {
    items
        .iter()
        .filter_map(|item| match item.item_type {
            MessageItemType::None => None,
            MessageItemType::Text => item.text_item.as_ref().and_then(|t| t.text.clone()),
            MessageItemType::Image => Some(
                item.image_item
                    .as_ref()
                    .and_then(|i| i.url.clone())
                    .unwrap_or_else(|| "[image]".to_string()),
            ),
            MessageItemType::Voice => Some(
                item.voice_item
                    .as_ref()
                    .and_then(|v| v.text.clone())
                    .unwrap_or_else(|| "[voice]".to_string()),
            ),
            MessageItemType::File => Some(
                item.file_item
                    .as_ref()
                    .and_then(|f| f.file_name.clone())
                    .unwrap_or_else(|| "[file]".to_string()),
            ),
            MessageItemType::Video => Some("[video]".to_string()),
        })
        .collect::<Vec<_>>()
        .join("\n")
}

/// Content type of an incoming message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContentType {
    Text,
    Image,
    Voice,
    File,
    Video,
}

#[derive(Debug, Clone)]
pub struct ImageContent {
    pub media: Option<CDNMedia>,
    pub thumb_media: Option<CDNMedia>,
    pub aes_key: Option<String>,
    pub url: Option<String>,
    pub width: Option<i32>,
    pub height: Option<i32>,
}

#[derive(Debug, Clone)]
pub struct VoiceContent {
    pub media: Option<CDNMedia>,
    pub text: Option<String>,
    pub duration_ms: Option<i32>,
    pub encode_type: Option<i32>,
}

#[derive(Debug, Clone)]
pub struct FileContent {
    pub media: Option<CDNMedia>,
    pub file_name: Option<String>,
    pub md5: Option<String>,
    pub size: Option<i64>,
}

#[derive(Debug, Clone)]
pub struct VideoContent {
    pub media: Option<CDNMedia>,
    pub thumb_media: Option<CDNMedia>,
    pub duration_ms: Option<i32>,
}

#[derive(Debug, Clone)]
pub struct QuotedMessage {
    pub message_id: Option<String>,
    pub title: Option<String>,
    pub text: Option<String>,
}

/// Result of downloading media from a message.
#[derive(Debug, Clone)]
pub struct DownloadedMedia {
    pub data: Vec<u8>,
    /// "image", "file", "video", "voice"
    pub media_type: String,
    pub file_name: Option<String>,
    pub format: Option<String>,
}

/// Result of uploading media to CDN.
#[derive(Debug, Clone)]
pub struct UploadResult {
    pub media: CDNMedia,
    pub aes_key: [u8; 16],
    pub encrypted_file_size: usize,
}

/// Stored login credentials.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Credentials {
    pub token: String,
    #[serde(rename = "baseUrl")]
    pub base_url: String,
    #[serde(rename = "accountId")]
    pub account_id: String,
    #[serde(rename = "userId")]
    pub user_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub saved_at: Option<String>,
}

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

    macro_rules! wire_message {
        ($($field:tt)*) => {
            WireMessage {
                seq: None,
                message_id: None,
                update_time_ms: None,
                delete_time_ms: None,
                session_id: None,
                group_id: None,
                $($field)*
            }
        };
    }

    macro_rules! wire_item {
        ($($field:tt)*) => {
            WireMessageItem {
                create_time_ms: None,
                update_time_ms: None,
                is_completed: None,
                msg_id: None,
                $($field)*
            }
        };
    }

    #[test]
    fn message_type_values() {
        assert_eq!(MessageType::None as i32, 0);
        assert_eq!(MessageType::User as i32, 1);
        assert_eq!(MessageType::Bot as i32, 2);
    }

    #[test]
    fn message_state_values() {
        assert_eq!(MessageState::New as i32, 0);
        assert_eq!(MessageState::Generating as i32, 1);
        assert_eq!(MessageState::Finish as i32, 2);
    }

    #[test]
    fn message_item_type_values() {
        assert_eq!(MessageItemType::None as i32, 0);
        assert_eq!(MessageItemType::Text as i32, 1);
        assert_eq!(MessageItemType::Image as i32, 2);
        assert_eq!(MessageItemType::Voice as i32, 3);
        assert_eq!(MessageItemType::File as i32, 4);
        assert_eq!(MessageItemType::Video as i32, 5);
    }

    #[test]
    fn wire_message_json_round_trip() {
        let wire = wire_message! {
            from_user_id: "user1".to_string(),
            to_user_id: "bot1".to_string(),
            client_id: "c1".to_string(),
            create_time_ms: 1700000000000,
            message_type: MessageType::User,
            message_state: MessageState::Finish,
            context_token: "ctx".to_string(),
            item_list: vec![wire_item! {
                item_type: MessageItemType::Text,
                text_item: Some(TextItem {
                    text: Some("hello".to_string()),
                }),
                image_item: None,
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            }],
        };
        let json = serde_json::to_string(&wire).unwrap();
        let decoded: WireMessage = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.from_user_id, "user1");
        assert_eq!(decoded.message_type, MessageType::User);
        assert_eq!(decoded.item_list.len(), 1);
        assert_eq!(
            decoded.item_list[0]
                .text_item
                .as_ref()
                .unwrap()
                .text
                .as_deref(),
            Some("hello")
        );
    }

    #[test]
    fn wire_message_accepts_missing_optional_typescript_fields() {
        let wire: WireMessage = serde_json::from_value(serde_json::json!({}))
            .expect("wire message with optional fields");

        assert_eq!(wire.from_user_id, "");
        assert_eq!(wire.to_user_id, "");
        assert_eq!(wire.client_id, "");
        assert_eq!(wire.create_time_ms, 0);
        assert_eq!(wire.message_type, MessageType::None);
        assert_eq!(wire.message_state, MessageState::New);
        assert_eq!(wire.context_token, "");
        assert!(wire.item_list.is_empty());
    }

    #[test]
    fn wire_message_item_defaults_missing_type_to_none() {
        let item: WireMessageItem =
            serde_json::from_value(serde_json::json!({ "text_item": { "text": "hello" } }))
                .expect("message item with optional type");

        assert_eq!(item.item_type, MessageItemType::None);
        assert_eq!(
            item.text_item
                .as_ref()
                .and_then(|text| text.text.as_deref()),
            Some("hello")
        );
    }

    #[test]
    fn credentials_json_camel_case() {
        let creds = Credentials {
            token: "tok".to_string(),
            base_url: "https://api.example.com".to_string(),
            account_id: "acc1".to_string(),
            user_id: "uid1".to_string(),
            saved_at: Some("2024-01-01T00:00:00Z".to_string()),
        };
        let json = serde_json::to_string(&creds).unwrap();
        assert!(json.contains("\"baseUrl\""), "expected camelCase baseUrl");
        assert!(
            json.contains("\"accountId\""),
            "expected camelCase accountId"
        );
        assert!(json.contains("\"userId\""), "expected camelCase userId");

        let decoded: Credentials = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.token, "tok");
        assert_eq!(decoded.base_url, "https://api.example.com");
    }

    #[test]
    fn credentials_omits_none_saved_at() {
        let creds = Credentials {
            token: "tok".to_string(),
            base_url: "https://api.example.com".to_string(),
            account_id: "acc1".to_string(),
            user_id: "uid1".to_string(),
            saved_at: None,
        };
        let json = serde_json::to_string(&creds).unwrap();
        assert!(!json.contains("saved_at"), "should omit None saved_at");
    }

    #[test]
    fn cdn_media_json() {
        let media = CDNMedia {
            encrypt_query_param: Some("param=abc".to_string()),
            aes_key: Some("key123".to_string()),
            encrypt_type: Some(1),
            full_url: None,
        };
        let json = serde_json::to_string(&media).unwrap();
        let decoded: CDNMedia = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.encrypt_query_param.as_deref(), Some("param=abc"));
        assert_eq!(decoded.aes_key.as_deref(), Some("key123"));
        assert_eq!(decoded.encrypt_type, Some(1));
    }

    #[test]
    fn wire_message_with_image() {
        let wire = wire_message! {
            from_user_id: "user1".to_string(),
            to_user_id: "bot1".to_string(),
            client_id: "c1".to_string(),
            create_time_ms: 1700000000000,
            message_type: MessageType::User,
            message_state: MessageState::Finish,
            context_token: "ctx".to_string(),
            item_list: vec![wire_item! {
                item_type: MessageItemType::Image,
                text_item: None,
                image_item: Some(ImageItem {
                    media: None,
                    thumb_media: None,
                    aeskey: Some("key".to_string()),
                    url: Some("http://img.jpg".to_string()),
                    mid_size: Some(1024),
                    thumb_size: None,
                    thumb_width: Some(100),
                    thumb_height: Some(200),
                    hd_size: None,
                }),
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            }],
        };
        let json = serde_json::to_string(&wire).unwrap();
        let decoded: WireMessage = serde_json::from_str(&json).unwrap();
        let img = decoded.item_list[0].image_item.as_ref().unwrap();
        assert_eq!(img.url, Some("http://img.jpg".to_string()));
        assert_eq!(img.thumb_width, Some(100));
    }

    #[test]
    fn content_type_equality() {
        assert_eq!(ContentType::Text, ContentType::Text);
        assert_ne!(ContentType::Text, ContentType::Image);
    }

    #[test]
    fn detect_type_text() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Text,
            text_item: Some(TextItem {
                text: Some("hi".to_string()),
            }),
            image_item: None,
            voice_item: None,
            file_item: None,
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(detect_type(&items), ContentType::Text);
    }

    #[test]
    fn detect_type_image() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Image,
            text_item: None,
            image_item: Some(ImageItem {
                media: None,
                thumb_media: None,
                aeskey: None,
                url: Some("http://img".to_string()),
                mid_size: None,
                thumb_size: None,
                thumb_width: None,
                thumb_height: None,
                hd_size: None,
            }),
            voice_item: None,
            file_item: None,
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(detect_type(&items), ContentType::Image);
    }

    #[test]
    fn detect_type_empty() {
        assert_eq!(detect_type(&[]), ContentType::Text);
    }

    #[test]
    fn extract_text_single() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Text,
            text_item: Some(TextItem {
                text: Some("hello world".to_string()),
            }),
            image_item: None,
            voice_item: None,
            file_item: None,
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(extract_text(&items), "hello world");
    }

    #[test]
    fn extract_text_multi() {
        let items = vec![
            wire_item! {
                item_type: MessageItemType::Text,
                text_item: Some(TextItem {
                    text: Some("line1".to_string()),
                }),
                image_item: None,
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            },
            wire_item! {
                item_type: MessageItemType::Text,
                text_item: Some(TextItem {
                    text: Some("line2".to_string()),
                }),
                image_item: None,
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            },
        ];
        assert_eq!(extract_text(&items), "line1\nline2");
    }

    #[test]
    fn extract_text_image_url() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Image,
            text_item: None,
            image_item: Some(ImageItem {
                media: None,
                thumb_media: None,
                aeskey: None,
                url: Some("http://img.jpg".to_string()),
                mid_size: None,
                thumb_size: None,
                thumb_width: None,
                thumb_height: None,
                hd_size: None,
            }),
            voice_item: None,
            file_item: None,
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(extract_text(&items), "http://img.jpg");
    }

    #[test]
    fn extract_text_image_placeholder() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Image,
            text_item: None,
            image_item: Some(ImageItem {
                media: None,
                thumb_media: None,
                aeskey: None,
                url: None,
                mid_size: None,
                thumb_size: None,
                thumb_width: None,
                thumb_height: None,
                hd_size: None,
            }),
            voice_item: None,
            file_item: None,
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(extract_text(&items), "[image]");
    }

    #[test]
    fn extract_text_voice_with_text() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Voice,
            text_item: None,
            image_item: None,
            voice_item: Some(VoiceItem {
                media: None,
                encode_type: None,
                bits_per_sample: None,
                sample_rate: None,
                text: Some("hello".to_string()),
                playtime: None,
            }),
            file_item: None,
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(extract_text(&items), "hello");
    }

    #[test]
    fn extract_text_file_name() {
        let items = vec![wire_item! {
            item_type: MessageItemType::File,
            text_item: None,
            image_item: None,
            voice_item: None,
            file_item: Some(FileItem {
                media: None,
                file_name: Some("doc.pdf".to_string()),
                md5: None,
                len: None,
            }),
            video_item: None,
            ref_msg: None,
        }];
        assert_eq!(extract_text(&items), "doc.pdf");
    }

    #[test]
    fn extract_text_video() {
        let items = vec![wire_item! {
            item_type: MessageItemType::Video,
            text_item: None,
            image_item: None,
            voice_item: None,
            file_item: None,
            video_item: Some(VideoItem {
                media: None,
                video_size: None,
                play_length: None,
                video_md5: None,
                thumb_media: None,
                thumb_size: None,
                thumb_height: None,
                thumb_width: None,
            }),
            ref_msg: None,
        }];
        assert_eq!(extract_text(&items), "[video]");
    }

    #[test]
    fn from_wire_user_text() {
        let wire = wire_message! {
            from_user_id: "user123".to_string(),
            to_user_id: "bot456".to_string(),
            client_id: "c1".to_string(),
            create_time_ms: 1700000000000,
            message_type: MessageType::User,
            message_state: MessageState::Finish,
            context_token: "ctx-abc".to_string(),
            item_list: vec![wire_item! {
                item_type: MessageItemType::Text,
                text_item: Some(TextItem {
                    text: Some("hello".to_string()),
                }),
                image_item: None,
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            }],
        };
        let msg = IncomingMessage::from_wire(&wire).unwrap();
        assert_eq!(msg.user_id, "user123");
        assert_eq!(msg.text, "hello");
        assert_eq!(msg.content_type, ContentType::Text);
        assert_eq!(msg.context_token(), "ctx-abc");
    }

    #[test]
    fn from_wire_skips_bot() {
        let wire = wire_message! {
            from_user_id: "bot456".to_string(),
            to_user_id: "user123".to_string(),
            client_id: "c1".to_string(),
            create_time_ms: 1700000000000,
            message_type: MessageType::Bot,
            message_state: MessageState::Finish,
            context_token: "ctx".to_string(),
            item_list: vec![wire_item! {
                item_type: MessageItemType::Text,
                text_item: Some(TextItem {
                    text: Some("reply".to_string()),
                }),
                image_item: None,
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            }],
        };
        assert!(IncomingMessage::from_wire(&wire).is_none());
    }

    #[test]
    fn from_wire_with_image() {
        let wire = wire_message! {
            from_user_id: "user123".to_string(),
            to_user_id: "bot456".to_string(),
            client_id: "c1".to_string(),
            create_time_ms: 1700000000000,
            message_type: MessageType::User,
            message_state: MessageState::Finish,
            context_token: "ctx".to_string(),
            item_list: vec![wire_item! {
                item_type: MessageItemType::Image,
                text_item: None,
                image_item: Some(ImageItem {
                    media: None,
                    thumb_media: None,
                    aeskey: Some("key".to_string()),
                    url: Some("http://img.jpg".to_string()),
                    mid_size: None,
                    thumb_size: None,
                    thumb_width: Some(100),
                    thumb_height: Some(200),
                    hd_size: None,
                }),
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: None,
            }],
        };
        let msg = IncomingMessage::from_wire(&wire).unwrap();
        assert_eq!(msg.images.len(), 1);
        assert_eq!(msg.images[0].url, Some("http://img.jpg".to_string()));
        assert_eq!(msg.images[0].width, Some(100));
        assert_eq!(msg.images[0].height, Some(200));
    }

    #[test]
    fn from_wire_with_quoted() {
        let wire = wire_message! {
            from_user_id: "user123".to_string(),
            to_user_id: "bot456".to_string(),
            client_id: "c1".to_string(),
            create_time_ms: 1700000000000,
            message_type: MessageType::User,
            message_state: MessageState::Finish,
            context_token: "ctx".to_string(),
            item_list: vec![wire_item! {
                item_type: MessageItemType::Text,
                text_item: Some(TextItem {
                    text: Some("replying".to_string()),
                }),
                image_item: None,
                voice_item: None,
                file_item: None,
                video_item: None,
                ref_msg: Some(RefMessage {
                    msg_id: None,
                    message_id: None,
                    client_id: None,
                    title: Some("Original".to_string()),
                    message_item: Some(Box::new(wire_item! {
                        item_type: MessageItemType::Text,
                        text_item: Some(TextItem {
                            text: Some("original text".to_string()),
                        }),
                        image_item: None,
                        voice_item: None,
                        file_item: None,
                        video_item: None,
                        ref_msg: None,
                    })),
                }),
            }],
        };
        let msg = IncomingMessage::from_wire(&wire).unwrap();
        let quoted = msg.quoted.as_ref().unwrap();
        assert_eq!(quoted.title, Some("Original".to_string()));
        assert_eq!(quoted.text.as_deref(), Some("original text"));
    }
}