1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-12-01 11:01:09 +00:00

Dont hardcode http protocol

This commit is contained in:
Felix Ableitner 2024-02-15 11:17:38 +01:00
parent e325de6352
commit 5ab62aecd5
7 changed files with 64 additions and 36 deletions

View file

@ -6,6 +6,7 @@ use crate::backend::error::MyResult;
use crate::backend::federation::activities::create_article::CreateArticle; use crate::backend::federation::activities::create_article::CreateArticle;
use crate::backend::federation::activities::submit_article_update; use crate::backend::federation::activities::submit_article_update;
use crate::backend::utils::generate_article_version; use crate::backend::utils::generate_article_version;
use crate::common::utils::http_protocol_str;
use crate::common::validation::can_edit_article; use crate::common::validation::can_edit_article;
use crate::common::LocalUserView; use crate::common::LocalUserView;
use crate::common::{ApiConflict, ResolveObject}; use crate::common::{ApiConflict, ResolveObject};
@ -37,7 +38,8 @@ pub(in crate::backend::api) async fn create_article(
let local_instance = DbInstance::read_local_instance(&data.db_connection)?; let local_instance = DbInstance::read_local_instance(&data.db_connection)?;
let ap_id = ObjectId::parse(&format!( let ap_id = ObjectId::parse(&format!(
"http://{}:{}/article/{}", "{}://{}:{}/article/{}",
http_protocol_str(),
local_instance.ap_id.inner().host_str().unwrap(), local_instance.ap_id.inner().host_str().unwrap(),
local_instance.ap_id.inner().port().unwrap(), local_instance.ap_id.inner().port().unwrap(),
create_article.title create_article.title
@ -175,7 +177,8 @@ pub(in crate::backend::api) async fn fork_article(
let local_instance = DbInstance::read_local_instance(&data.db_connection)?; let local_instance = DbInstance::read_local_instance(&data.db_connection)?;
let ap_id = ObjectId::parse(&format!( let ap_id = ObjectId::parse(&format!(
"http://{}:{}/article/{}", "{}://{}:{}/article/{}",
http_protocol_str(),
local_instance.ap_id.inner().domain().unwrap(), local_instance.ap_id.inner().domain().unwrap(),
local_instance.ap_id.inner().port().unwrap(), local_instance.ap_id.inner().port().unwrap(),
&fork_form.new_title &fork_form.new_title

View file

@ -2,6 +2,7 @@ use crate::backend::database::schema::{instance, instance_follow};
use crate::backend::database::schema::{local_user, person}; use crate::backend::database::schema::{local_user, person};
use crate::backend::database::IbisData; use crate::backend::database::IbisData;
use crate::backend::error::MyResult; use crate::backend::error::MyResult;
use crate::common::utils::http_protocol_str;
use crate::common::{DbInstance, DbLocalUser, DbPerson, LocalUserView}; use crate::common::{DbInstance, DbLocalUser, DbPerson, LocalUserView};
use activitypub_federation::config::Data; use activitypub_federation::config::Data;
use activitypub_federation::fetch::object_id::ObjectId; use activitypub_federation::fetch::object_id::ObjectId;
@ -59,8 +60,11 @@ impl DbPerson {
) -> MyResult<LocalUserView> { ) -> MyResult<LocalUserView> {
let mut conn = data.db_connection.lock().unwrap(); let mut conn = data.db_connection.lock().unwrap();
let domain = &data.config.federation.domain; let domain = &data.config.federation.domain;
let ap_id = ObjectId::parse(&format!("http://{domain}/user/{username}"))?; let ap_id = ObjectId::parse(&format!(
let inbox_url = format!("http://{domain}/inbox"); "{}://{domain}/user/{username}",
http_protocol_str()
))?;
let inbox_url = format!("{}://{domain}/inbox", http_protocol_str());
let keypair = generate_actor_keypair()?; let keypair = generate_actor_keypair()?;
let person_form = DbPersonForm { let person_form = DbPersonForm {
username, username,
@ -114,7 +118,7 @@ impl DbPerson {
.select(person::all_columns) .select(person::all_columns)
.into_boxed(); .into_boxed();
query = if let Some(domain) = domain { query = if let Some(domain) = domain {
let domain_pattern = format!("http://{domain}/%"); let domain_pattern = format!("{}://{domain}/%", http_protocol_str());
query query
.filter(person::ap_id.ilike(domain_pattern)) .filter(person::ap_id.ilike(domain_pattern))
.filter(person::local.eq(false)) .filter(person::local.eq(false))

View file

@ -8,6 +8,7 @@ use crate::backend::federation::activities::submit_article_update;
use crate::backend::federation::routes::federation_routes; use crate::backend::federation::routes::federation_routes;
use crate::backend::federation::VerifyUrlData; use crate::backend::federation::VerifyUrlData;
use crate::backend::utils::generate_activity_id; use crate::backend::utils::generate_activity_id;
use crate::common::utils::http_protocol_str;
use crate::common::{DbArticle, DbInstance, DbPerson, EditVersion, MAIN_PAGE_NAME}; use crate::common::{DbArticle, DbInstance, DbPerson, EditVersion, MAIN_PAGE_NAME};
use crate::frontend::app::App; use crate::frontend::app::App;
use activitypub_federation::config::{Data, FederationConfig, FederationMiddleware}; use activitypub_federation::config::{Data, FederationConfig, FederationMiddleware};
@ -112,9 +113,10 @@ and to list interesting articles.";
async fn setup(data: &Data<IbisData>) -> Result<(), Error> { async fn setup(data: &Data<IbisData>) -> Result<(), Error> {
let domain = &data.config.federation.domain; let domain = &data.config.federation.domain;
let ap_id = ObjectId::parse(&format!("http://{domain}"))?; let ap_id = ObjectId::parse(&format!("{}://{domain}", http_protocol_str()))?;
let articles_url = CollectionId::parse(&format!("http://{domain}/all_articles"))?; let articles_url =
let inbox_url = format!("http://{domain}/inbox"); CollectionId::parse(&format!("{}://{domain}/all_articles", http_protocol_str()))?;
let inbox_url = format!("{}://{domain}/inbox", http_protocol_str());
let keypair = generate_actor_keypair()?; let keypair = generate_actor_keypair()?;
let form = DbInstanceForm { let form = DbInstanceForm {
domain: domain.to_string(), domain: domain.to_string(),
@ -140,7 +142,10 @@ async fn setup(data: &Data<IbisData>) -> Result<(), Error> {
let form = DbArticleForm { let form = DbArticleForm {
title: MAIN_PAGE_NAME.to_string(), title: MAIN_PAGE_NAME.to_string(),
text: String::new(), text: String::new(),
ap_id: ObjectId::parse(&format!("http://{domain}/article/{MAIN_PAGE_NAME}"))?, ap_id: ObjectId::parse(&format!(
"{}://{domain}/article/{MAIN_PAGE_NAME}",
http_protocol_str()
))?,
instance_id: instance.id, instance_id: instance.id,
local: true, local: true,
}; };

View file

@ -1,6 +1,6 @@
use crate::backend::error::MyResult; use crate::backend::error::MyResult;
use crate::common::EditVersion;
use crate::common::EditView; use crate::common::EditView;
use crate::common::{utils, EditVersion};
use activitypub_federation::fetch::object_id::ObjectId; use activitypub_federation::fetch::object_id::ObjectId;
use activitypub_federation::traits::Object; use activitypub_federation::traits::Object;
use anyhow::anyhow; use anyhow::anyhow;
@ -22,7 +22,12 @@ where
.take(7) .take(7)
.map(char::from) .map(char::from)
.collect(); .collect();
Url::parse(&format!("http://{}/objects/{}", domain, id)) Url::parse(&format!(
"{}://{}/objects/{}",
utils::http_protocol_str(),
domain,
id
))
} }
/// Starting from empty string, apply edits until the specified version is reached. If no version is /// Starting from empty string, apply edits until the specified version is reached. If no version is

View file

@ -20,3 +20,11 @@ pub fn extract_domain(url: &String) -> String {
} }
format!("{}{port}", url.host_str().unwrap()) format!("{}{port}", url.host_str().unwrap())
} }
pub fn http_protocol_str() -> &'static str {
if cfg!(debug_assertions) {
"http"
} else {
"https"
}
}

View file

@ -1,3 +1,4 @@
use crate::common::utils::http_protocol_str;
use crate::common::{ApiConflict, ListArticlesData}; use crate::common::{ApiConflict, ListArticlesData};
use crate::common::{ArticleView, LoginUserData, RegisterUserData}; use crate::common::{ArticleView, LoginUserData, RegisterUserData};
use crate::common::{CreateArticleData, EditArticleData, ForkArticleData, LocalUserView}; use crate::common::{CreateArticleData, EditArticleData, ForkArticleData, LocalUserView};
@ -26,9 +27,7 @@ impl ApiClient {
T: for<'de> Deserialize<'de>, T: for<'de> Deserialize<'de>,
R: Serialize, R: Serialize,
{ {
let mut req = self let mut req = self.client.get(self.request_endpoint(endpoint));
.client
.get(format!("http://{}/api/v1/{}", &self.hostname, endpoint));
if let Some(query) = query { if let Some(query) = query {
req = req.query(&query); req = req.query(&query);
} }
@ -36,17 +35,17 @@ impl ApiClient {
} }
pub async fn get_article(&self, data: GetArticleData) -> MyResult<ArticleView> { pub async fn get_article(&self, data: GetArticleData) -> MyResult<ArticleView> {
self.get_query("article", Some(data)).await self.get_query("/api/v1/article", Some(data)).await
} }
pub async fn list_articles(&self, data: ListArticlesData) -> MyResult<Vec<DbArticle>> { pub async fn list_articles(&self, data: ListArticlesData) -> MyResult<Vec<DbArticle>> {
self.get_query("article/list", Some(data)).await self.get_query("/api/v1/article/list", Some(data)).await
} }
pub async fn register(&self, register_form: RegisterUserData) -> MyResult<LocalUserView> { pub async fn register(&self, register_form: RegisterUserData) -> MyResult<LocalUserView> {
let req = self let req = self
.client .client
.post(format!("http://{}/api/v1/account/register", self.hostname)) .post(self.request_endpoint("/api/v1/account/register"))
.form(&register_form); .form(&register_form);
handle_json_res::<LocalUserView>(req).await handle_json_res::<LocalUserView>(req).await
} }
@ -54,7 +53,7 @@ impl ApiClient {
pub async fn login(&self, login_form: LoginUserData) -> MyResult<LocalUserView> { pub async fn login(&self, login_form: LoginUserData) -> MyResult<LocalUserView> {
let req = self let req = self
.client .client
.post(format!("http://{}/api/v1/account/login", self.hostname)) .post(self.request_endpoint("/api/v1/account/login"))
.form(&login_form); .form(&login_form);
handle_json_res::<LocalUserView>(req).await handle_json_res::<LocalUserView>(req).await
} }
@ -62,7 +61,7 @@ impl ApiClient {
pub async fn create_article(&self, data: &CreateArticleData) -> MyResult<ArticleView> { pub async fn create_article(&self, data: &CreateArticleData) -> MyResult<ArticleView> {
let req = self let req = self
.client .client
.post(format!("http://{}/api/v1/article", &self.hostname)) .post(self.request_endpoint("/api/v1/article"))
.form(data); .form(data);
handle_json_res(req).await handle_json_res(req).await
} }
@ -73,7 +72,7 @@ impl ApiClient {
) -> MyResult<Option<ApiConflict>> { ) -> MyResult<Option<ApiConflict>> {
let req = self let req = self
.client .client
.patch(format!("http://{}/api/v1/article", self.hostname)) .patch(self.request_endpoint("/api/v1/article"))
.form(edit_form); .form(edit_form);
handle_json_res(req).await handle_json_res(req).await
} }
@ -91,11 +90,11 @@ impl ApiClient {
} }
pub async fn search(&self, search_form: &SearchArticleData) -> MyResult<Vec<DbArticle>> { pub async fn search(&self, search_form: &SearchArticleData) -> MyResult<Vec<DbArticle>> {
self.get_query("search", Some(search_form)).await self.get_query("/api/v1/search", Some(search_form)).await
} }
pub async fn get_local_instance(&self) -> MyResult<InstanceView> { pub async fn get_local_instance(&self) -> MyResult<InstanceView> {
self.get_query("instance", None::<i32>).await self.get_query("/api/v1/instance", None::<i32>).await
} }
pub async fn follow_instance_with_resolve( pub async fn follow_instance_with_resolve(
@ -104,10 +103,10 @@ impl ApiClient {
) -> MyResult<DbInstance> { ) -> MyResult<DbInstance> {
// fetch beta instance on alpha // fetch beta instance on alpha
let resolve_form = ResolveObject { let resolve_form = ResolveObject {
id: Url::parse(&format!("http://{}", follow_instance))?, id: Url::parse(&format!("{}://{}", http_protocol_str(), follow_instance))?,
}; };
let instance_resolved: DbInstance = self let instance_resolved: DbInstance = self
.get_query("instance/resolve", Some(resolve_form)) .get_query("/api/v1/instance/resolve", Some(resolve_form))
.await?; .await?;
// send follow // send follow
@ -122,7 +121,7 @@ impl ApiClient {
// cant use post helper because follow doesnt return json // cant use post helper because follow doesnt return json
let res = self let res = self
.client .client
.post(format!("http://{}/api/v1/instance/follow", self.hostname)) .post(self.request_endpoint("/api/v1/instance/follow"))
.form(&follow_form) .form(&follow_form)
.send() .send()
.await?; .await?;
@ -134,16 +133,15 @@ impl ApiClient {
} }
pub async fn my_profile(&self) -> MyResult<LocalUserView> { pub async fn my_profile(&self) -> MyResult<LocalUserView> {
let req = self.client.get(format!( let req = self
"http://{}/api/v1/account/my_profile", .client
self.hostname .get(self.request_endpoint("/api/v1/account/my_profile"));
));
handle_json_res(req).await handle_json_res(req).await
} }
pub async fn logout(&self) -> MyResult<()> { pub async fn logout(&self) -> MyResult<()> {
self.client self.client
.get(format!("http://{}/api/v1/account/logout", self.hostname)) .get(self.request_endpoint("/api/v1/account/logout"))
.send() .send()
.await?; .await?;
Ok(()) Ok(())
@ -152,7 +150,7 @@ impl ApiClient {
pub async fn fork_article(&self, form: &ForkArticleData) -> MyResult<ArticleView> { pub async fn fork_article(&self, form: &ForkArticleData) -> MyResult<ArticleView> {
let req = self let req = self
.client .client
.post(format!("http://{}/api/v1/article/fork", self.hostname)) .post(self.request_endpoint("/api/v1/article/fork"))
.form(form); .form(form);
Ok(handle_json_res(req).await.unwrap()) Ok(handle_json_res(req).await.unwrap())
} }
@ -160,23 +158,27 @@ impl ApiClient {
pub async fn get_conflicts(&self) -> MyResult<Vec<ApiConflict>> { pub async fn get_conflicts(&self) -> MyResult<Vec<ApiConflict>> {
let req = self let req = self
.client .client
.get(format!("http://{}/api/v1/edit_conflicts", &self.hostname)); .get(self.request_endpoint("/api/v1/edit_conflicts"));
Ok(handle_json_res(req).await.unwrap()) Ok(handle_json_res(req).await.unwrap())
} }
pub async fn resolve_article(&self, id: Url) -> MyResult<ArticleView> { pub async fn resolve_article(&self, id: Url) -> MyResult<ArticleView> {
let resolve_object = ResolveObject { id }; let resolve_object = ResolveObject { id };
self.get_query("article/resolve", Some(resolve_object)) self.get_query("/api/v1/article/resolve", Some(resolve_object))
.await .await
} }
pub async fn resolve_instance(&self, id: Url) -> MyResult<DbInstance> { pub async fn resolve_instance(&self, id: Url) -> MyResult<DbInstance> {
let resolve_object = ResolveObject { id }; let resolve_object = ResolveObject { id };
self.get_query("instance/resolve", Some(resolve_object)) self.get_query("/api/v1/instance/resolve", Some(resolve_object))
.await .await
} }
pub async fn get_user(&self, data: GetUserData) -> MyResult<DbPerson> { pub async fn get_user(&self, data: GetUserData) -> MyResult<DbPerson> {
self.get_query("user", Some(data)).await self.get_query("/api/v1/user", Some(data)).await
}
fn request_endpoint(&self, path: &str) -> String {
format!("{}://{}{path}", http_protocol_str(), &self.hostname)
} }
} }

View file

@ -1,3 +1,4 @@
use crate::common::utils::http_protocol_str;
use crate::common::{DbInstance, FollowInstance}; use crate::common::{DbInstance, FollowInstance};
use crate::frontend::app::GlobalState; use crate::frontend::app::GlobalState;
use leptos::*; use leptos::*;
@ -10,7 +11,7 @@ pub fn InstanceDetails() -> impl IntoView {
let params = use_params_map(); let params = use_params_map();
let hostname = move || params.get().get("hostname").cloned().unwrap(); let hostname = move || params.get().get("hostname").cloned().unwrap();
let instance_profile = create_resource(hostname, move |hostname| async move { let instance_profile = create_resource(hostname, move |hostname| async move {
let url = Url::parse(&format!("http://{hostname}")).unwrap(); let url = Url::parse(&format!("{}://{hostname}", http_protocol_str())).unwrap();
GlobalState::api_client() GlobalState::api_client()
.resolve_instance(url) .resolve_instance(url)
.await .await