2021-10-31 22:26:22 +05:30
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2021-11-02 15:47:55 +05:30
|
|
|
use std::path::Path;
|
|
|
|
|
2021-10-31 22:26:22 +05:30
|
|
|
use actix_web::web;
|
2021-11-02 15:30:25 +05:30
|
|
|
use graphql_client::{reqwest::post_graphql, GraphQLQuery};
|
2021-10-31 22:26:22 +05:30
|
|
|
use reqwest::Client;
|
2021-11-02 15:30:25 +05:30
|
|
|
use sled::{Db, Tree};
|
2021-10-31 22:26:22 +05:30
|
|
|
|
2021-11-02 15:47:55 +05:30
|
|
|
use crate::SETTINGS;
|
|
|
|
|
2021-10-31 22:26:22 +05:30
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Data {
|
|
|
|
pub client: Client,
|
2021-11-02 15:30:25 +05:30
|
|
|
cache: Db,
|
|
|
|
pub posts: Tree,
|
2021-10-31 22:26:22 +05:30
|
|
|
}
|
|
|
|
|
2021-11-02 15:30:25 +05:30
|
|
|
#[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;
|
|
|
|
|
2021-10-31 22:26:22 +05:30
|
|
|
pub type AppData = web::Data<Data>;
|
|
|
|
|
|
|
|
impl Data {
|
|
|
|
pub fn new() -> AppData {
|
2021-11-02 16:03:10 +05:30
|
|
|
let path = Path::new(SETTINGS.cache.as_ref().unwrap()).join("posts_cache");
|
2021-11-02 15:47:55 +05:30
|
|
|
let cache = sled::open(path).unwrap();
|
2021-11-02 15:30:25 +05:30
|
|
|
let posts = cache.open_tree("posts").unwrap();
|
2021-10-31 22:26:22 +05:30
|
|
|
AppData::new(Self {
|
|
|
|
client: Client::new(),
|
2021-11-02 15:30:25 +05:30
|
|
|
cache,
|
|
|
|
posts,
|
2021-10-31 22:26:22 +05:30
|
|
|
})
|
|
|
|
}
|
2021-11-02 15:30:25 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 22:26:22 +05:30
|
|
|
}
|