~boringcactus/gemifedi

ref: 811641efa71eb4afbc8ac183baa7cd3760809f20 gemifedi/src/mastodon_util.rs -rw-r--r-- 1.3 KiB
811641ef — Melody Horn improve instructions more 1 year, 2 months ago
                                                                                
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
use std::{
    fs::{self, create_dir_all},
    path::PathBuf,
};

use elefren::{
    prelude::*,
    registration::Registered,
};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct AppInstall {
    parts: (String, String, String, String, Scopes, bool),
}

pub fn get_app_install(domain: &str) -> Registered<elefren::http_send::HttpSender> {
    let mut data_path = PathBuf::from("data");
    data_path.push("domains");
    create_dir_all(&data_path).unwrap();
    data_path.push(format!("{}.toml", domain));

    if data_path.exists() {
        let file_data = fs::read(data_path).unwrap();
        let install: AppInstall = toml::from_slice(file_data.as_slice()).unwrap();
        let (base, client_id, client_secret, redirect, scopes, force_login) = install.parts;
        Registered::from_parts(&base, &client_id, &client_secret, &redirect, scopes, force_login)
    } else {
        let registered = Registration::new(format!("https://{}", domain))
            .client_name("gemifedi")
            .website("https://git.sr.ht/~boringcactus/gemifedi")
            .build()
            .unwrap();
        let parts = registered.clone().into_parts();
        let install = AppInstall { parts };
        let file_data = toml::to_string(&install).unwrap();
        fs::write(data_path, file_data).unwrap();
        registered
    }
}