diff --git a/src/backend/api/instance.rs b/src/backend/api/instance.rs index 6e04ec1..903300c 100644 --- a/src/backend/api/instance.rs +++ b/src/backend/api/instance.rs @@ -1,6 +1,9 @@ use crate::{ backend::{database::IbisData, error::MyResult, federation::activities::follow::Follow}, - common::{DbInstance, FollowInstance, GetInstance, InstanceView, LocalUserView, ResolveObject}, + common::{ + DbInstance, FollowInstance, FollowInstanceResponse, GetInstance, InstanceView, + LocalUserView, ResolveObject, + }, }; use activitypub_federation::{config::Data, fetch::object_id::ObjectId}; use axum::{extract::Query, Extension, Form, Json}; @@ -23,13 +26,13 @@ pub(in crate::backend::api) async fn follow_instance( Extension(user): Extension, data: Data, Form(query): Form, -) -> MyResult<()> { +) -> MyResult> { let target = DbInstance::read(query.id, &data)?; let pending = !target.local; DbInstance::follow(&user.person, &target, pending, &data)?; let instance = DbInstance::read(query.id, &data)?; Follow::send(user.person, &instance, &data).await?; - Ok(()) + Ok(Json(FollowInstanceResponse { success: true })) } /// Fetch a remote instance actor. This automatically synchronizes the remote articles collection to diff --git a/src/common/mod.rs b/src/common/mod.rs index cb5f983..6605fc2 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -244,6 +244,11 @@ pub struct FollowInstance { pub id: InstanceId, } +#[derive(Deserialize, Serialize, Debug)] +pub struct FollowInstanceResponse { + pub success: bool +} + #[derive(Deserialize, Serialize, Clone, Debug)] pub struct SearchArticleForm { pub query: String, diff --git a/src/frontend/api.rs b/src/frontend/api.rs index 4093748..5840d62 100644 --- a/src/frontend/api.rs +++ b/src/frontend/api.rs @@ -3,10 +3,10 @@ use crate::{ newtypes::{ArticleId, ConflictId}, utils::http_protocol_str, ApiConflict, ApproveArticleForm, ArticleView, CreateArticleForm, DbArticle, DbInstance, - DbPerson, DeleteConflictForm, EditArticleForm, FollowInstance, ForkArticleForm, - GetArticleForm, GetInstance, GetUserForm, InstanceView, ListArticlesForm, LocalUserView, - LoginUserForm, Notification, ProtectArticleForm, RegisterUserForm, ResolveObject, - SearchArticleForm, SiteView, + DbPerson, DeleteConflictForm, EditArticleForm, FollowInstance, FollowInstanceResponse, + ForkArticleForm, GetArticleForm, GetInstance, GetUserForm, InstanceView, ListArticlesForm, + LocalUserView, LoginUserForm, Notification, ProtectArticleForm, RegisterUserForm, + ResolveObject, SearchArticleForm, SiteView, }, frontend::error::MyResult, }; @@ -164,22 +164,12 @@ impl ApiClient { Ok(instance_resolved) } - pub async fn follow_instance(&self, follow_form: FollowInstance) -> MyResult<()> { - todo!(); - /* - // cant use post helper because follow doesnt return json - let res = self - .client - .post(self.request_endpoint("/api/v1/instance/follow")) - .form(&follow_form) - .send() - .await?; - if res.status() == StatusCode::OK { - Ok(()) - } else { - Err(anyhow!("API error: {}", res.text().await?).into()) - } - */ + pub async fn follow_instance( + &self, + follow_form: FollowInstance, + ) -> MyResult { + self.post("/api/v1/instance/follow", Some(follow_form)) + .await } pub async fn site(&self) -> MyResult { @@ -268,8 +258,7 @@ impl ApiClient { use gloo_net::http::*; use leptos::prelude::on_cleanup; use send_wrapper::SendWrapper; - use std::collections::HashMap; - use web_sys::{RequestCredentials, UrlSearchParams}; + use web_sys::RequestCredentials; SendWrapper::new(async move { let abort_controller = SendWrapper::new(web_sys::AbortController::new().ok());