webcomment/webui/src/api.rs
2023-05-02 18:03:21 +02:00

58 lines
1.3 KiB
Rust

use crate::types::*;
use webcomment_common::{api::*, types::*};
use gloo::{console, net::http};
use parking_lot::RwLock;
use std::collections::HashMap;
pub struct ApiInner {
pub admin_psw: Option<String>,
pub comments: HashMap<CommentId, StoredComment>,
pub url: String,
}
pub struct Api {
pub inner: RwLock<ApiInner>,
}
impl Api {
pub async fn get_comments_by_topic(&self, topic: String) {
match http::Request::post(&format!("{}/api/comments_by_topic", &self.inner.read().url))
.body(
serde_json::to_string(&queries::CommentsByTopic {
mutation_token: None,
topic,
})
.unwrap(),
)
.send()
.await
{
Ok(resp) => {
let Ok(Ok(resps::Response::CommentsByTopic(resp))) = resp.json::<resps::Result>().await else {
// TODO error
return;
};
let mut inner = self.inner.write();
for comment in resp.approved_comments {
let Ok(comment_id) = CommentId::from_base64(&comment.id) else {
continue
};
inner.comments.insert(
comment_id,
StoredComment {
author: comment.author,
email: comment.email,
last_edit_time: comment.last_edit_time,
post_time: comment.post_time,
text: comment.text,
},
);
}
}
Err(e) => console::log!("get_comments_by_topic: {e:?}"),
}
}
}