aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f2dfc52c518ae7b7964033e63d824d63a949f53f (plain)
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
mod bots;
mod report;

use std::{env, fs, num::NonZeroU64};

use atrium_api::app::bsky::embed::defs::AspectRatioData;
use atrium_api::app::bsky::embed::gallery::{ImageData, MainData as GalleryData, MainItemsItem};
use atrium_api::app::bsky::feed::post::{RecordData, RecordEmbedRefs, ReplyRefData};
use atrium_api::com::atproto::repo::strong_ref::{Main as StrongRef, MainData as StrongRefData};
use atrium_api::types::{Union, string::Datetime};
use bsky_sdk::BskyAgent;
use clap::{Parser, ValueEnum};
use report::{Post, Report};
use serde::Deserialize;

pub(crate) const MAX_POST_GRAPHEMES: usize = 300;
pub(crate) const MAX_POST_IMAGES: usize = 10;
pub(crate) const MAX_IMAGE_SIZE: u64 = 2_000_000;

#[derive(Debug, Parser)]
struct Args {
    bot: Bot,
    report_kind: ReportKind,

    #[arg(long)]
    dry: bool,
}

#[derive(Clone, Debug, ValueEnum)]
enum Bot {
    #[value(name = "fdroid")]
    FDroid,
}

#[derive(Clone, Debug, ValueEnum)]
enum ReportKind {
    Breaking,
}

#[tokio::main]
async fn main() -> Result<(), String> {
    tracing_subscriber::fmt::init();

    let args = Args::parse();
    let credentials = (!args.dry).then(load_credentials).transpose()?;

    let (posts, update_state) = match (args.bot, args.report_kind) {
        (Bot::FDroid, ReportKind::Breaking) => bots::fdroid::Breaking::report().await?,
    };

    if args.dry {
        let post_count = posts.len();
        if posts.is_empty() {
            println!("No posts to publish.");
        }
        for (index, post) in posts.into_iter().enumerate() {
            if index > 0 {
                println!();
            }
            println!("Post {}/{}:\n{}", index + 1, post_count, post.text);
            for image in post.images {
                println!("Image: {}", image.alt);
            }
        }
        return Ok(());
    }

    if !posts.is_empty() {
        publish(posts, credentials.unwrap()).await?;
    }

    // Do not advance the state unless every post was published successfully.
    update_state()
}

#[derive(Deserialize)]
struct Credentials {
    identifier: String,
    password: String,
}

fn load_credentials() -> Result<Credentials, String> {
    let path = env::var("BSKY_CREDENTIALS_FILE").map_err(|_| {
        "BSKY_CREDENTIALS_FILE is not set; use --dry to run without publishing".to_owned()
    })?;
    let data = fs::read(&path).map_err(|error| format!("failed to read {path}: {error}"))?;
    serde_json::from_slice(&data).map_err(|error| format!("failed to parse {path}: {error}"))
}

async fn publish(posts: Vec<Post>, credentials: Credentials) -> Result<(), String> {
    let agent = BskyAgent::builder()
        .build()
        .await
        .map_err(|error| format!("failed to create Bluesky client: {error}"))?;
    agent
        .login(credentials.identifier, credentials.password)
        .await
        .map_err(|error| format!("failed to log in to Bluesky: {error}"))?;

    let post_count = posts.len();
    let image_count: usize = posts.iter().map(|post| post.images.len()).sum();
    let mut uploaded_image_count = 0;
    let mut records = Vec::with_capacity(post_count);

    for (index, post) in posts.into_iter().enumerate() {
        let embed = if post.images.is_empty() {
            None
        } else {
            let mut images = Vec::with_capacity(post.images.len());
            for (image_index, image) in post.images.into_iter().enumerate() {
                let dimensions = imagesize::blob_size(&image.data).map_err(|error| {
                    format!(
                        "failed to determine dimensions of image {} for post {}: {error}",
                        image_index + 1,
                        index + 1
                    )
                })?;
                let aspect_ratio = AspectRatioData {
                    width: NonZeroU64::new(dimensions.width as u64).ok_or_else(|| {
                        format!(
                            "image {} for post {} has zero width",
                            image_index + 1,
                            index + 1
                        )
                    })?,
                    height: NonZeroU64::new(dimensions.height as u64).ok_or_else(|| {
                        format!(
                            "image {} for post {} has zero height",
                            image_index + 1,
                            index + 1
                        )
                    })?,
                }
                .into();
                uploaded_image_count += 1;
                tracing::info!(
                    image = uploaded_image_count,
                    images = image_count,
                    "uploading image"
                );
                let blob = agent
                    .api
                    .com
                    .atproto
                    .repo
                    .upload_blob(image.data)
                    .await
                    .map_err(|error| {
                        format!(
                            "failed to upload image {} for post {}: {error}",
                            image_index + 1,
                            index + 1
                        )
                    })?;
                images.push(Union::Refs(MainItemsItem::Image(Box::new(
                    ImageData {
                        alt: image.alt,
                        aspect_ratio,
                        image: blob.data.blob,
                    }
                    .into(),
                ))));
            }
            Some(Union::Refs(RecordEmbedRefs::AppBskyEmbedGalleryMain(
                Box::new(GalleryData { items: images }.into()),
            )))
        };

        records.push(RecordData {
            created_at: Datetime::now(),
            text: post.text,
            facets: Some(post.facets),
            embed,
            langs: Some(post.langs),
            tags: Some(post.tags),
            entities: None,
            labels: None,
            reply: None,
        });
    }

    let mut root: Option<StrongRef> = None;
    let mut parent: Option<StrongRef> = None;
    for (index, mut record) in records.into_iter().enumerate() {
        if let Some(parent) = parent.as_ref() {
            record.reply = Some(
                ReplyRefData {
                    parent: parent.clone(),
                    root: root.as_ref().unwrap().clone(),
                }
                .into(),
            );
        }

        tracing::info!(post = index + 1, posts = post_count, "creating post");
        let output = agent
            .create_record(record)
            .await
            .map_err(|error| format!("failed to publish post {}: {error}", index + 1))?;
        tracing::info!(
            post = index + 1,
            posts = post_count,
            uri = %output.uri,
            cid = ?output.cid,
            "created post"
        );
        let reference: StrongRef = StrongRefData {
            uri: output.uri.clone(),
            cid: output.cid.clone(),
        }
        .into();
        root.get_or_insert_with(|| reference.clone());
        parent = Some(reference);
    }

    Ok(())
}

pub(crate) fn http_client() -> reqwest::Client {
    reqwest::Client::builder()
        .user_agent(env!("CARGO_PKG_NAME"))
        .build()
        .unwrap()
}