skystreamer 0.2.2

Idiomatic Rust library for the AT Firehose streaming API
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
use crate::{util::datetime_to_chrono, Result};
use atrium_api::{
    app::bsky::{
        embed::{
            images::ImageData, record_with_media::MainMediaRefs, video::MainData as VideoData,
        },
        feed::post::{Record as PostRecord, RecordEmbedRefs},
    },
    types::{string::Did, BlobRef, CidLink, TypedBlobRef, UnTypedBlobRef},
};
use cid::multihash::Multihash;
use cid::Cid;
use ipld_core::ipld::Ipld;
use serde::{Deserialize, Serialize};
use std::io::Cursor;
pub mod actor;
pub mod commit;
pub mod feed;
pub mod graph;
pub mod operation;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Media {
    // Image(ImageData),
    Image(Image),
    // note: weird naming
    // Video(VideoData),
    Video(Video),
}

// We have to define our own wrapper types because
// BlobRef union type are kinda weird
// todo: Figure out how to fix this

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Video {
    /// Alt text
    pub alt: Option<String>,
    pub blob: Blob,
    pub aspect_ratio: Option<(u32, u32)>,
}

impl From<VideoData> for Video {
    fn from(value: VideoData) -> Self {
        Self {
            alt: value.alt,
            blob: value.video.into(),
            aspect_ratio: value
                .aspect_ratio
                .map(|ar| (ar.width.get() as u32, ar.height.get() as u32)),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Image {
    pub alt: String,
    pub blob: Blob,
    pub aspect_ratio: Option<(u32, u32)>,
}

impl From<ImageData> for Image {
    fn from(value: ImageData) -> Self {
        Self {
            alt: value.alt,
            blob: value.image.into(),
            aspect_ratio: value
                .aspect_ratio
                .map(|ar| (ar.width.get() as u32, ar.height.get() as u32)),
        }
    }
}
// pub struct ExternalData {
//     pub description: String,
//     #[serde(skip_serializing_if = "core::option::Option::is_none")]
//     pub thumb: core::option::Option<crate::types::BlobRef>,
//     pub title: String,
//     pub uri: String,
// }
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ExternalLink {
    pub description: String,
    pub thumb: Option<Blob>,
    pub title: String,
    pub uri: String,
}

impl From<atrium_api::app::bsky::embed::external::ExternalData> for ExternalLink {
    fn from(value: atrium_api::app::bsky::embed::external::ExternalData) -> Self {
        Self {
            description: value.description,
            thumb: value.thumb.map(|b| b.into()),
            title: value.title,
            uri: value.uri,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Blob {
    pub cid: String,
    pub mime_type: String,
    pub size: Option<usize>,
}

impl From<BlobRef> for Blob {
    fn from(value: BlobRef) -> Self {
        match value {
            BlobRef::Typed(TypedBlobRef::Blob(blob)) => Self {
                cid: blob.r#ref.0.to_string(),
                mime_type: blob.mime_type,
                size: Some(blob.size),
            },
            BlobRef::Untyped(UnTypedBlobRef { cid, mime_type }) => Self {
                cid,
                mime_type,
                size: None,
            },
        }
    }
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ReplyRef {
    pub parent: Cid,
    pub root: Cid,
}

impl From<atrium_api::app::bsky::feed::post::ReplyRef> for ReplyRef {
    fn from(value: atrium_api::app::bsky::feed::post::ReplyRef) -> Self {
        Self {
            parent: value.parent.data.cid.as_ref().to_owned(),
            root: value.root.data.cid.as_ref().to_owned(),
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum Embed {
    Media(Vec<Media>),
    External(ExternalLink),
    Record(Cid),
    RecordWithMedia(Cid, Box<Vec<Media>>),
    Unknown,
}

impl From<RecordEmbedRefs> for Embed {
    fn from(value: RecordEmbedRefs) -> Self {
        match value {
            RecordEmbedRefs::AppBskyEmbedImagesMain(m) => Embed::Media(
                m.images
                    .clone()
                    .into_iter()
                    .map(|i| Media::Image(i.data.into()))
                    .collect(),
            ),
            RecordEmbedRefs::AppBskyEmbedExternalMain(m) => {
                Embed::External(m.data.external.data.into())
            }
            RecordEmbedRefs::AppBskyEmbedVideoMain(m) => {
                Embed::Media(vec![Media::Video(m.data.clone().into())])
            }
            RecordEmbedRefs::AppBskyEmbedRecordMain(m) => {
                Embed::Record(m.record.data.cid.as_ref().to_owned())
            }
            RecordEmbedRefs::AppBskyEmbedRecordWithMediaMain(m) => {
                let media = match &m.media {
                    atrium_api::types::Union::Refs(MainMediaRefs::AppBskyEmbedImagesMain(m)) => m
                        .images
                        .clone()
                        .into_iter()
                        .map(|i| Media::Image(i.data.into()))
                        .collect::<Vec<Media>>(),
                    atrium_api::types::Union::Refs(MainMediaRefs::AppBskyEmbedVideoMain(m)) => {
                        vec![Media::Video(m.data.clone().into())]
                    }
                    _ => return Embed::Unknown,
                };
                // let media = match m.media {
                //     atrium_api::types::Union::Refs(MainMediaRefs::AppBskyEmbedImagesMain(m)) => {
                //         // Media::Image(
                //             m.images
                //                 .into_iter()
                //                 .map(|i| i.data.into())
                //                 .collect::<Vec<Image>>(),
                //         // )
                //     }
                //     MainMediaRefs::AppBskyEmbedVideoMain(m) => Media::Video(m.data.into()),
                //     _ => return Embed::Unknown,
                // };
                Embed::RecordWithMedia(
                    m.record.data.record.cid.as_ref().to_owned(),
                    Box::new(media),
                )
            } // _ => Embed::Unknown,
        }
    }
}

/// A post on the network
///
/// An idiomatic wrapper type around [`atrium_api::app::bsky::feed::post::Record`]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Post {
    pub author: Did,
    pub created_at: chrono::DateTime<chrono::FixedOffset>,
    pub text: String,
    pub id: Cid,
    // pub cid: String,
    pub language: Vec<String>,
    pub reply: Option<ReplyRef>,
    pub tags: Vec<String>,
    pub labels: Vec<String>,
    pub embed: Option<Embed>,
}

impl Post {
    /// Directly create a Post from a Record type by automatically creating a new PostData
    pub fn from_record(author: Did, cid: CidLink, record: PostRecord) -> Self {
        Self::from(PostData::new(author, cid, record))
    }

    /// Get media associated with the post, if any
    ///
    /// Before this was implemented as a method,
    /// you were required to match each variant of the Embed union type from
    /// ATrium to get the media associated with the post.
    ///
    /// This function abstracts that away and returns a vector of Media types
    pub fn get_post_media(&self) -> Vec<Media> {
        let mut media = vec![];
        if let Some(embed) = self.embed.as_ref() {
            match embed {
                crate::types::Embed::RecordWithMedia(_, m) => {
                    media.extend(m.iter().cloned());
                }
                crate::types::Embed::Media(m) => {
                    media.extend(m.iter().cloned());
                }
                _ => {}
            }
        }
        media
    }
}

impl From<PostData> for Post {
    fn from(value: PostData) -> Self {
        let record = value.record.data;
        Self {
            author: value.author,
            // because for some reason we can't access the inner chrono::DateTime
            // we will have to reparse it from string
            created_at: datetime_to_chrono(&record.created_at),
            text: record.text,
            id: value.cid,
            language: record
                .langs
                .as_ref()
                .map(|langs| langs.iter().map(|lang| lang.as_ref().to_string()).collect())
                .unwrap_or_default(),
            reply: record.reply.as_ref().map(|reply| (*reply).clone().into()),
            tags: record.tags.unwrap_or_default().iter().map(|tag| tag.to_string()).collect(),
            labels: record.labels.as_ref().map_or_else(Vec::new, |labels| {
                match labels {
                    atrium_api::types::Union::Refs(
                        atrium_api::app::bsky::feed::post::RecordLabelsRefs::ComAtprotoLabelDefsSelfLabels(
                            values,
                        ),
                    ) => values.data.values.iter().map(|label| label.val.as_str().to_string()).collect(),
                    _ => Vec::new(),
                }
            }),
            embed: record.embed.as_ref().map(|embed| match embed {
                atrium_api::types::Union::Refs(refs) => refs.clone().into(),
                _ => Embed::Unknown,
            }),
        }
    }
}

// impl From<atrium_api::com::atproto::repo::strong_ref::MainData> for Post {
//     fn from(value: atrium_api::com::atproto::repo::strong_ref::MainData) -> Self {
//         Self {
//             author: value.author,
//             created_at: value.created_at,
//             text: value.text,
//             id: value.id,
//             cid: value.cid,
//             language: value.language,
//             reply: value.reply.map(|r| ReplyRef {
//                 parent: r.parent,
//                 root: r.root,
//             }),
//             tags: value.tags,
//             labels: value.labels,
//             embed: value.embed.map(|e| e.into()),
//         }
//     }
// }

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PostData {
    pub author: Did,
    pub cid: Cid,
    pub record: PostRecord,
}

impl PostData {
    pub fn new(author: Did, cid: CidLink, record: PostRecord) -> Self {
        Self {
            author,
            cid: cid.0,
            record,
        }
    }

    /// Get media associated with the post
    pub fn get_media(&self) -> Option<Vec<Media>> {
        let embed = self.record.embed.as_ref()?;

        match embed {
            atrium_api::types::Union::Refs(RecordEmbedRefs::AppBskyEmbedImagesMain(m)) => Some(
                m.images
                    .iter()
                    .map(|i| Media::Image(i.data.clone().into()))
                    .collect(),
            ),
            atrium_api::types::Union::Refs(RecordEmbedRefs::AppBskyEmbedVideoMain(m)) => {
                Some(vec![Media::Video(m.data.clone().into())])
            }
            atrium_api::types::Union::Refs(RecordEmbedRefs::AppBskyEmbedRecordWithMediaMain(m)) => {
                match &m.media {
                    atrium_api::types::Union::Refs(MainMediaRefs::AppBskyEmbedImagesMain(m)) => {
                        Some(
                            m.images
                                .iter()
                                .map(|i| Media::Image(i.data.clone().into()))
                                .collect(),
                        )
                    }
                    atrium_api::types::Union::Refs(MainMediaRefs::AppBskyEmbedVideoMain(m)) => {
                        Some(vec![Media::Video(m.data.clone().into())])
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    // pub fn
}

/// Downloads media associated with a post, returning bytes of the media
pub async fn download_media<C>(
    client: &atrium_api::client::Service<C>,
    did: &Did,
    media: &Media,
) -> Result<Vec<u8>>
where
    C: atrium_api::xrpc::XrpcClient + Send + Sync,
{
    let blob_ref = match media {
        Media::Image(data) => &data.blob,
        Media::Video(data) => &data.blob,
    };

    let bytes = client
        .com
        .atproto
        .sync
        .get_blob(
            atrium_api::com::atproto::sync::get_blob::ParametersData {
                cid: atrium_api::types::string::Cid::new(blob_ref.cid.clone().parse().unwrap()),
                did: did.clone(),
            }
            .into(),
        )
        .await
        .map_err(|e| crate::Error::AtriumError(e.to_string()))?;
    Ok(bytes)
}
// original definition:
//```
// export enum FrameType {
//   Message = 1,
//   Error = -1,
// }
// export const messageFrameHeader = z.object({
//   op: z.literal(FrameType.Message), // Frame op
//   t: z.string().optional(), // Message body type discriminator
// })
// export type MessageFrameHeader = z.infer<typeof messageFrameHeader>
// export const errorFrameHeader = z.object({
//   op: z.literal(FrameType.Error),
// })
// export type ErrorFrameHeader = z.infer<typeof errorFrameHeader>
// ```
#[derive(Debug, Clone, PartialEq, Eq)]
enum FrameHeader {
    Message(Option<String>),
    Error,
}

impl TryFrom<Ipld> for FrameHeader {
    type Error = crate::Error;

    fn try_from(value: Ipld) -> Result<Self> {
        if let Ipld::Map(map) = &value {
            if let Some(Ipld::Integer(i)) = map.get("op") {
                match i {
                    1 => {
                        let t = if let Some(Ipld::String(s)) = map.get("t") {
                            Some(s.clone())
                        } else {
                            None
                        };
                        return Ok(FrameHeader::Message(t));
                    }
                    -1 => return Ok(FrameHeader::Error),
                    _ => {}
                }
            }
        }
        Err(crate::Error::InvalidFrameType(value))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Frame {
    Message(Option<String>, MessageFrame),
    Error(ErrorFrame),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessageFrame {
    pub body: Vec<u8>,
}

#[trait_variant::make(HttpService: Send)]
pub trait Subscription {
    async fn next(
        &mut self,
    ) -> Option<std::result::Result<Frame, <Frame as TryFrom<&[u8]>>::Error>>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorFrame {
    // TODO
    // body: Value,
}

impl TryFrom<&[u8]> for Frame {
    type Error = crate::Error;

    fn try_from(value: &[u8]) -> Result<Self> {
        let mut cursor = Cursor::new(value);
        let (left, right) = match serde_ipld_dagcbor::from_reader::<Ipld, _>(&mut cursor) {
            Err(serde_ipld_dagcbor::DecodeError::TrailingData) => {
                value.split_at(cursor.position() as usize)
            }
            _ => {
                // TODO
                return Err(crate::Error::InvalidFrameData(value.to_vec()));
            }
        };
        let header = FrameHeader::try_from(serde_ipld_dagcbor::from_slice::<Ipld>(left)?)?;
        if let FrameHeader::Message(t) = &header {
            Ok(Frame::Message(
                t.clone(),
                MessageFrame {
                    body: right.to_vec(),
                },
            ))
        } else {
            Ok(Frame::Error(ErrorFrame {}))
        }
    }
}

pub struct CidOld(cid_old::Cid);

impl From<cid_old::Cid> for CidOld {
    fn from(value: cid_old::Cid) -> Self {
        Self(value)
    }
}
impl TryFrom<CidOld> for Cid {
    type Error = cid::Error;
    fn try_from(value: CidOld) -> std::result::Result<Self, Self::Error> {
        let version = match value.0.version() {
            cid_old::Version::V0 => cid::Version::V0,
            cid_old::Version::V1 => cid::Version::V1,
        };

        let codec = value.0.codec();
        let hash = value.0.hash();
        let hash = Multihash::from_bytes(&hash.to_bytes())?;

        Self::new(version, codec, hash)
    }
}

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

    fn serialized_data(s: &str) -> Vec<u8> {
        assert!(s.len() % 2 == 0);
        let b2u = |b: u8| match b {
            b'0'..=b'9' => b - b'0',
            b'a'..=b'f' => b - b'a' + 10,
            _ => unreachable!(),
        };
        s.as_bytes()
            .chunks(2)
            .map(|b| (b2u(b[0]) << 4) + b2u(b[1]))
            .collect()
    }

    #[test]
    fn deserialize_message_frame_header() {
        // {"op": 1, "t": "#commit"}
        let data = serialized_data("a2626f700161746723636f6d6d6974");
        let ipld = serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
        let result = FrameHeader::try_from(ipld);
        assert_eq!(
            result.expect("failed to deserialize"),
            FrameHeader::Message(Some(String::from("#commit")))
        );
    }

    #[test]
    fn deserialize_error_frame_header() {
        // {"op": -1}
        let data = serialized_data("a1626f7020");
        let ipld = serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
        let result = FrameHeader::try_from(ipld);
        assert_eq!(result.expect("failed to deserialize"), FrameHeader::Error);
    }

    #[test]
    fn deserialize_invalid_frame_header() {
        {
            // {"op": 2, "t": "#commit"}
            let data = serialized_data("a2626f700261746723636f6d6d6974");
            let ipld =
                serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
            let result = FrameHeader::try_from(ipld);
            assert!(result.is_err());
        }
        {
            // {"op": -2}
            let data = serialized_data("a1626f7021");
            let ipld =
                serde_ipld_dagcbor::from_slice::<Ipld>(&data).expect("failed to deserialize");
            let result = FrameHeader::try_from(ipld);
            assert!(result.is_err());
        }
    }
}