From 18d46d22bf46a3cb72ba81570f5a1e4d2ba279b3 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 13 Nov 2024 11:07:28 +0100 Subject: [PATCH] Simplify code so that unapproved articles can be viewed directly (but not included in listings) --- src/backend/api/article.rs | 27 +++++++------------ src/backend/api/mod.rs | 9 ------- src/backend/api/user.rs | 3 +-- src/backend/database/article.rs | 13 +++------ src/backend/database/conflict.rs | 8 ++---- src/backend/federation/objects/edit.rs | 2 +- .../federation/objects/edits_collection.rs | 2 +- src/backend/federation/routes.rs | 4 +-- 8 files changed, 20 insertions(+), 48 deletions(-) diff --git a/src/backend/api/article.rs b/src/backend/api/article.rs index cf62f86..e6b1fd9 100644 --- a/src/backend/api/article.rs +++ b/src/backend/api/article.rs @@ -1,4 +1,4 @@ -use super::{check_is_admin, is_admin_opt}; +use super::check_is_admin; use crate::{ backend::{ database::{ @@ -42,7 +42,7 @@ use diffy::create_patch; /// Create a new article with empty text, and federate it to followers. #[debug_handler] pub(in crate::backend::api) async fn create_article( - Extension(mut user): Extension, + user: Extension, data: Data, Form(create_article): Form, ) -> MyResult> { @@ -80,12 +80,10 @@ pub(in crate::backend::api) async fn create_article( resolve_conflict_id: None, }; - // workaround so the edit goes through - user.local_user.admin = true; - let _ = edit_article(Extension(user), data.reset_request_count(), Form(edit_data)).await?; + let _ = edit_article(user, data.reset_request_count(), Form(edit_data)).await?; // allow reading unapproved article here - let article_view = DbArticle::read_view(article.id, true, &data)?; + let article_view = DbArticle::read_view(article.id, &data)?; CreateArticle::send_to_followers(article_view.article.clone(), &data).await?; Ok(Json(article_view)) @@ -106,12 +104,11 @@ pub(in crate::backend::api) async fn edit_article( data: Data, Form(mut edit_form): Form, ) -> MyResult>> { - let is_admin = check_is_admin(&user).is_ok(); // resolve conflict if any if let Some(resolve_conflict_id) = edit_form.resolve_conflict_id { DbConflict::delete(resolve_conflict_id, &data)?; } - let original_article = DbArticle::read_view(edit_form.article_id, is_admin, &data)?; + let original_article = DbArticle::read_view(edit_form.article_id, &data)?; if edit_form.new_text == original_article.article.text { return Err(anyhow!("Edit contains no changes").into()); } @@ -153,7 +150,7 @@ pub(in crate::backend::api) async fn edit_article( previous_version_id: previous_version.hash, }; let conflict = DbConflict::create(&form, &data)?; - Ok(Json(conflict.to_api_conflict(is_admin, &data).await?)) + Ok(Json(conflict.to_api_conflict(&data).await?)) } } @@ -161,22 +158,19 @@ pub(in crate::backend::api) async fn edit_article( #[debug_handler] pub(in crate::backend::api) async fn get_article( Query(query): Query, - user: Option>, data: Data, ) -> MyResult> { - let is_admin = is_admin_opt(&user); match (query.title, query.id) { (Some(title), None) => Ok(Json(DbArticle::read_view_title( &title, query.domain, - is_admin, &data, )?)), (None, Some(id)) => { if query.domain.is_some() { return Err(anyhow!("Cant combine id and instance_domain").into()); } - let article = DbArticle::read_view(id, is_admin, &data)?; + let article = DbArticle::read_view(id, &data)?; Ok(Json(article)) } _ => Err(anyhow!("Must pass exactly one of title, id").into()), @@ -199,13 +193,12 @@ pub(in crate::backend::api) async fn list_articles( /// how an article should be edited. #[debug_handler] pub(in crate::backend::api) async fn fork_article( - Extension(user): Extension, + Extension(_user): Extension, data: Data, Form(fork_form): Form, ) -> MyResult> { - let is_admin = check_is_admin(&user).is_ok(); // TODO: lots of code duplicated from create_article(), can move it into helper - let original_article = DbArticle::read_view(fork_form.article_id, is_admin, &data)?; + let original_article = DbArticle::read_view(fork_form.article_id, &data)?; let local_instance = DbInstance::read_local_instance(&data)?; let ap_id = ObjectId::parse(&format!( @@ -249,7 +242,7 @@ pub(in crate::backend::api) async fn fork_article( CreateArticle::send_to_followers(article.clone(), &data).await?; - Ok(Json(DbArticle::read_view(article.id, is_admin, &data)?)) + Ok(Json(DbArticle::read_view(article.id, &data)?)) } /// Fetch a remote article, including edits collection. Allows viewing and editing. Note that new diff --git a/src/backend/api/mod.rs b/src/backend/api/mod.rs index 529a947..c0e216c 100644 --- a/src/backend/api/mod.rs +++ b/src/backend/api/mod.rs @@ -36,7 +36,6 @@ use axum::{ middleware::{self, Next}, response::Response, routing::{get, post}, - Extension, Router, }; use axum_extra::extract::CookieJar; @@ -94,11 +93,3 @@ fn check_is_admin(user: &LocalUserView) -> MyResult<()> { } Ok(()) } - -fn is_admin_opt(user: &Option>) -> bool { - if let Some(user) = user { - user.local_user.admin - } else { - false - } -} diff --git a/src/backend/api/user.rs b/src/backend/api/user.rs index 73c8054..452242a 100644 --- a/src/backend/api/user.rs +++ b/src/backend/api/user.rs @@ -161,11 +161,10 @@ pub(crate) async fn list_notifications( Extension(user): Extension, data: Data, ) -> MyResult>> { - let is_admin = check_is_admin(&user).is_ok(); let conflicts = DbConflict::list(&user.person, &data)?; let conflicts: Vec<_> = try_join_all(conflicts.into_iter().map(|c| { let data = data.reset_request_count(); - async move { c.to_api_conflict(is_admin, &data).await } + async move { c.to_api_conflict(&data).await } })) .await?; let mut notifications: Vec<_> = conflicts diff --git a/src/backend/database/article.rs b/src/backend/database/article.rs index c8a5743..a7fd939 100644 --- a/src/backend/database/article.rs +++ b/src/backend/database/article.rs @@ -87,12 +87,9 @@ impl DbArticle { .get_result::(conn.deref_mut())?) } - pub fn read_view(id: ArticleId, is_admin: bool, data: &IbisData) -> MyResult { + pub fn read_view(id: ArticleId, data: &IbisData) -> MyResult { let mut conn = data.db_pool.get()?; - let mut query = article::table.find(id).into_boxed(); - if !is_admin { - query = query.filter(article::dsl::approved.eq(true)); - } + let query = article::table.find(id).into_boxed(); let article: DbArticle = query.get_result(conn.deref_mut())?; let latest_version = article.latest_edit_version(data)?; let edits = DbEdit::read_for_article(&article, data)?; @@ -106,7 +103,6 @@ impl DbArticle { pub fn read_view_title( title: &str, domain: Option, - admin: bool, data: &IbisData, ) -> MyResult { let mut conn = data.db_pool.get()?; @@ -115,14 +111,11 @@ impl DbArticle { .inner_join(instance::table) .filter(article::dsl::title.eq(title)) .into_boxed(); - let mut query = if let Some(domain) = domain { + let query = if let Some(domain) = domain { query.filter(instance::dsl::domain.eq(domain)) } else { query.filter(article::dsl::local.eq(true)) }; - if !admin { - query = query.filter(article::dsl::approved.eq(true)); - } query .select(article::all_columns) .get_result(conn.deref_mut())? diff --git a/src/backend/database/conflict.rs b/src/backend/database/conflict.rs index b34916c..43d85a1 100644 --- a/src/backend/database/conflict.rs +++ b/src/backend/database/conflict.rs @@ -78,12 +78,8 @@ impl DbConflict { Ok(delete(conflict::table.find(id)).get_result(conn.deref_mut())?) } - pub async fn to_api_conflict( - &self, - is_admin: bool, - data: &Data, - ) -> MyResult> { - let article = DbArticle::read_view(self.article_id, is_admin, data)?; + pub async fn to_api_conflict(&self, data: &Data) -> MyResult> { + let article = DbArticle::read_view(self.article_id, data)?; // Make sure to get latest version from origin so that all conflicts can be resolved let original_article = article.article.ap_id.dereference_forced(data).await?; diff --git a/src/backend/federation/objects/edit.rs b/src/backend/federation/objects/edit.rs index 2559cce..a8405f2 100644 --- a/src/backend/federation/objects/edit.rs +++ b/src/backend/federation/objects/edit.rs @@ -52,7 +52,7 @@ impl Object for DbEdit { } async fn into_json(self, data: &Data) -> Result { - let article = DbArticle::read_view(self.article_id, false, data)?; + let article = DbArticle::read_view(self.article_id, data)?; let creator = DbPerson::read(self.creator_id, data)?; Ok(ApubEdit { kind: PatchType::Patch, diff --git a/src/backend/federation/objects/edits_collection.rs b/src/backend/federation/objects/edits_collection.rs index 3b96bba..3059958 100644 --- a/src/backend/federation/objects/edits_collection.rs +++ b/src/backend/federation/objects/edits_collection.rs @@ -35,7 +35,7 @@ impl Collection for DbEditCollection { owner: &Self::Owner, data: &Data, ) -> Result { - let article = DbArticle::read_view(owner.id, false, data)?; + let article = DbArticle::read_view(owner.id, data)?; let edits = future::try_join_all( article diff --git a/src/backend/federation/routes.rs b/src/backend/federation/routes.rs index 4647c04..2fca4cc 100644 --- a/src/backend/federation/routes.rs +++ b/src/backend/federation/routes.rs @@ -94,7 +94,7 @@ async fn http_get_article( Path(title): Path, data: Data, ) -> MyResult>> { - let article = DbArticle::read_view_title(&title, None, false, &data)?; + let article = DbArticle::read_view_title(&title, None, &data)?; let json = article.article.into_json(&data).await?; Ok(FederationJson(WithContext::new_default(json))) } @@ -104,7 +104,7 @@ async fn http_get_article_edits( Path(title): Path, data: Data, ) -> MyResult>> { - let article = DbArticle::read_view_title(&title, None, false, &data)?; + let article = DbArticle::read_view_title(&title, None, &data)?; let json = DbEditCollection::read_local(&article.article, &data).await?; Ok(FederationJson(WithContext::new_default(json))) }