    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

    /*
     * Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the
     * License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU Affero General Public License for more details.
     *
     * You should have received a copy of the GNU Affero General Public License
     * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     */
    use std::path::Path;

    use actix_web::web;
    use graphql_client::{reqwest::post_graphql, GraphQLQuery};
    use reqwest::header::USER_AGENT;
    use reqwest::Client;
    use serde::{Deserialize, Serialize};
    use sled::{Db, Tree};

    use crate::proxy::StringUtils;
    use crate::render_html;
    use crate::SETTINGS;

    const POST_CACHE_VERSION: usize = 3;
    const GIST_CACHE_VERSION: usize = 1;

    #[derive(Clone)]
    pub struct Data {
        pub client: Client,
        cache: Db,
        pub posts: Tree,
        pub gists: Tree,
    }

    #[derive(GraphQLQuery)]
    #[graphql(
        schema_path = "schemas/schema.graphql",
        query_path = "schemas/query.graphql",
        response_derives = "Debug, Serialize, Deserialize, Clone"
    )]
    pub struct GetPost;

    pub type PostResp = get_post::GetPostPost;

    pub type AppData = web::Data<Data>;

    impl PostResp {
        pub fn get_subtitle(&self) -> &str {
            self.preview_content.as_ref().unwrap().subtitle.as_str()
        }
    }

    #[derive(Deserialize, Serialize)]
    pub struct GistContent {
        pub files: Vec<GistFile>,
        pub html_url: String,
    }

    #[derive(Deserialize, Clone, Serialize)]
    pub struct GistFile {
        pub file_name: String,
        pub content: String,
        pub language: String,
        pub raw_url: String,
    }

    impl GistFile {
        pub fn get_html_content(&self) -> String {
            let mut content = self.content.as_str();
            if self.content.starts_with('"') {
                content = self.content.slice(1..);
            }

            if content.ends_with('"') {
                content = content.slice(..content.len() - 1);
            }
            content.replace("\\t", "  ")
        }
    }

    #[derive(GraphQLQuery)]
    #[graphql(
        schema_path = "schemas/schema.graphql",
        query_path = "schemas/query.graphql",
        response_derives = "Debug, Serialize, Deserialize, Clone"
    )]
    pub struct GetPostLight;

    #[derive(Debug, Clone)]
    pub struct PostUrl {
        pub slug: String,
        pub username: String,
    }

    impl Data {
        pub fn new() -> AppData {
            let path = Path::new(SETTINGS.cache.as_ref().unwrap()).join("posts_cache");
            let cache = sled::open(path).unwrap();
            let posts = cache.open_tree("posts").unwrap();
            let gists = cache.open_tree("gists").unwrap();
            let res = Self {
                client: Client::new(),
                cache,
                posts,
                gists,
            };
            res.migrate();

            AppData::new(res)
        }

        fn migrate(&self) {
            const POST_KEY: &str = "POST_CACHE_VERSION";
            const GIST_KEY: &str = "GIST_CACHE_VERSION";
            let trees = [
                (&self.posts, POST_KEY, POST_CACHE_VERSION),
                (&self.gists, GIST_KEY, GIST_CACHE_VERSION),
            ];

            for (tree, key, current_version) in trees {
                if let Ok(Some(v)) = tree.get(key) {
                    let version = bincode::deserialize::<usize>(&v[..]).unwrap();
                    let clean = version != current_version;

                    if clean {
                        log::info!(
                            "Upgrading {} from version {} to version {}",
                            key,
                            version,
                            current_version
                        );
                        tree.clear().unwrap();
                        tree.flush().unwrap();
                        tree.insert(key, bincode::serialize(&current_version).unwrap())
                            .unwrap();
                    }
                }
            }
        }

        pub async fn get_post(&self, id: &str) -> PostResp {
            match self.posts.get(id) {
                Ok(Some(v)) => bincode::deserialize(&v[..]).unwrap(),
                _ => {
                    let vars = get_post::Variables { id: id.to_owned() };
                    const URL: &str = "https://medium.com/_/graphql";

                    let res = post_graphql::<GetPost, _>(&self.client, URL, vars)
                        .await
                        .unwrap();
                    let res = res.data.expect("missing response data").post.unwrap();
                    self.posts
                        .insert(id, bincode::serialize(&res).unwrap())
                        .unwrap();
                    res
                }
            }
        }

        pub async fn get_post_light(&self, id: &str) -> PostUrl {
            match self.posts.get(id) {
                Ok(Some(v)) => {
                    let cached: PostResp = bincode::deserialize(&v[..]).unwrap();
                    PostUrl {
                        slug: cached.unique_slug,
                        username: cached.creator.username,
                    }
                }
                _ => {
                    let vars = get_post_light::Variables { id: id.to_owned() };
                    const URL: &str = "https://medium.com/_/graphql";

                    let res = post_graphql::<GetPostLight, _>(&self.client, URL, vars)
                        .await
                        .unwrap();
                    let res = res.data.expect("missing response data").post.unwrap();
                    PostUrl {
                        slug: res.unique_slug,
                        username: res.creator.username,
                    }
                }
            }
        }

        pub fn get_gist_id(url: &str) -> &str {
            url.split('/').last().unwrap()
        }

        pub async fn get_gist(&self, gist_url: String) -> (String, GistContent) {
            let id = Self::get_gist_id(&gist_url).to_owned();
            let file_name = if gist_url.contains('?') {
                let parsed = url::Url::parse(&gist_url).unwrap();
                if let Some((_, file_name)) = parsed.query_pairs().find(|(k, _)| k == "file") {
                    Some(file_name.into_owned())
                } else {
                    None
                }
            } else {
                None
            };

            let mut gist = match self.gists.get(&id) {
                Ok(Some(v)) => bincode::deserialize(&v[..]).unwrap(),
                _ => {
                    const URL: &str = "https://api.github.com/gists/";

                    let url = format!("{}{}", URL, id);

                    let resp = self
                        .client
                        .get(&url)
                        .header(USER_AGENT, "libmedium")
                        .send()
                        .await
                        .unwrap()
                        .json::<serde_json::Value>()
                        .await
                        .unwrap();
                    let files = resp.get("files").unwrap();
                    let v = files.as_object().unwrap();

                    fn to_gist_file(name: &str, file_obj: &serde_json::Value) -> GistFile {
                        GistFile {
                            file_name: name.to_string(),
                            content: file_obj
                                .get("content")
                                .unwrap()
                                .as_str()
                                .unwrap()
                                .to_owned(),
                            language: file_obj
                                .get("language")
                                .unwrap()
                                .as_str()
                                .unwrap()
                                .to_owned(),
                            raw_url: file_obj
                                .get("raw_url")
                                .unwrap()
                                .as_str()
                                .unwrap()
                                .to_owned(),
                        }
                    }

                    let mut files = Vec::with_capacity(v.len());
                    v.iter().for_each(|(name, file_obj)| {
                        let file = to_gist_file(name, file_obj);
                        files.push(file);
                    });

                    let gist = GistContent {
                        files,
                        html_url: resp.get("html_url").unwrap().as_str().unwrap().to_owned(),
                    };

                    self.gists
                        .insert(&id, bincode::serialize(&gist).unwrap())
                        .unwrap();
                    gist
                }
            };

            let gist = if let Some(file_name) = file_name {
                let mut files: Vec<GistFile> = Vec::with_capacity(1);
                let mut file = gist
                    .files
                    .iter()
                    .find(|f| f.file_name == file_name)
                    .unwrap()
                    .to_owned();
                let highlight = render_html::SourcegraphQuery {
                    filepath: &file.file_name,
                    code: &file.content,
                };
                file.content = highlight.syntax_highlight();
                files.push(file);
                GistContent {
                    files,
                    html_url: gist_url,
                }
            } else {
                gist.files.iter_mut().for_each(|f| {
                    let highlight = render_html::SourcegraphQuery {
                        filepath: &f.file_name,
                        code: &f.content,
                    };
                    f.content = highlight.syntax_highlight();
                });
                gist
            };

            (id, gist)
        }
    }