webcomment/webui/src/api.rs
2023-05-13 09:35:45 +02:00

99 lines
2.4 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>,
/// Comments that are not yet attributed CommentId by the server
pub local_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:?}"),
}
}
pub async fn new_comment(&self, new_comment: StoredComment, topic: String) {
let local_id = CommentId::new();
self.inner.write().local_comments.insert(local_id.clone(), new_comment.clone());
match http::Request::post(&format!("{}/api/query_new_comment", &self.inner.read().url))
.body(
serde_json::to_string(&queries::NewComment {
author: new_comment.author,
topic,
email: new_comment.email.unwrap_or_default(),
text: new_comment.text,
})
.unwrap(),
)
.send()
.await
{
Ok(resp) => {
let Ok(Ok(resps::Response::NewComment(resp))) = resp.json::<resps::Result>().await else {
// TODO error
return;
};
let mut inner = self.inner.write();
let Ok(comment_id) = CommentId::from_base64(&resp.id) else {
// TODO error
return;
};
let Some(comment) = inner.local_comments.remove(&local_id) else {
// TODO error
return;
};
inner.comments.insert(
comment_id,
comment,
);
}
Err(e) => console::log!("get_comments_by_topic: {e:?}"),
}
}
}