Skip to content

Commit

Permalink
more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Jan 8, 2024
1 parent 71aee71 commit 7773a67
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 43 deletions.
10 changes: 0 additions & 10 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,16 +835,6 @@ fn limit_expire_time(expires: DateTime<Utc>) -> LemmyResult<Option<DateTime<Utc>
}
}

pub fn check_community_valid(community: &Community) -> LemmyResult<()> {
if community.deleted || community.removed {
Err(LemmyErrorType::Deleted)?
}
if community.local_only {
return Err(LemmyErrorType::CouldntFindCommunity.into());
}
Ok(())
}

#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
Expand Down
30 changes: 16 additions & 14 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,22 @@ pub async fn create_post(
mark_post_as_read(person_id, post_id, &mut context.pool()).await?;

if let Some(url) = updated_post.url.clone() {
spawn_try_task(async move {
let mut webmention =
Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
webmention.set_checked(true);
match webmention
.send()
.instrument(tracing::info_span!("Sending webmention"))
.await
{
Err(WebmentionError::NoEndpointDiscovered(_)) => Ok(()),
Ok(_) => Ok(()),
Err(e) => Err(e).with_lemmy_type(LemmyErrorType::CouldntSendWebmention),
}
});
if community.local_only {
spawn_try_task(async move {
let mut webmention =
Webmention::new::<Url>(updated_post.ap_id.clone().into(), url.clone().into())?;
webmention.set_checked(true);
match webmention
.send()
.instrument(tracing::info_span!("Sending webmention"))
.await
{
Err(WebmentionError::NoEndpointDiscovered(_)) => Ok(()),
Ok(_) => Ok(()),
Err(e) => Err(e).with_lemmy_type(LemmyErrorType::CouldntSendWebmention),
}
});
}
};

build_post_response(&context, community_id, &local_user_view.person, post_id).await
Expand Down
2 changes: 1 addition & 1 deletion crates/api_crud/src/site/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub async fn get_site(
|pool| CommunityBlockView::for_person(pool, person_id),
|pool| InstanceBlockView::for_person(pool, person_id),
|pool| PersonBlockView::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id),
|pool| CommunityModeratorView::for_person(pool, person_id, true),
|pool| LocalUserLanguage::read(pool, local_user_id)
))
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
Expand Down
6 changes: 5 additions & 1 deletion crates/apub/src/activities/following/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use lemmy_db_schema::{
},
traits::Followable,
};
use lemmy_utils::error::LemmyError;
use lemmy_utils::error::{LemmyError, LemmyErrorType};
use url::Url;

impl Follow {
Expand Down Expand Up @@ -103,6 +103,10 @@ impl ActivityHandler for Follow {
PersonFollower::follow(&mut context.pool(), &form).await?;
}
UserOrCommunity::Community(c) => {
// Dont allow following local-only community via federation.
if c.local_only {
return Err(LemmyErrorType::CouldntFindCommunity.into());
}
let form = CommunityFollowerForm {
community_id: c.id,
person_id: actor.id,
Expand Down
8 changes: 6 additions & 2 deletions crates/apub/src/api/read_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ pub async fn read_person(
.list(&mut context.pool())
.await?;

let moderates =
CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?;
let moderates = CommunityModeratorView::for_person(
&mut context.pool(),
person_details_id,
local_user_view.is_some(),
)
.await?;

// Return the jwt
Ok(Json(GetPersonDetailsResponse {
Expand Down
9 changes: 7 additions & 2 deletions crates/apub/src/http/comment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::{
http::{create_apub_response, create_apub_tombstone_response, redirect_remote_object},
http::{
check_community_valid,
create_apub_response,
create_apub_tombstone_response,
redirect_remote_object,
},
objects::comment::ApubComment,
};
use activitypub_federation::{config::Data, traits::Object};
use actix_web::{web::Path, HttpResponse};
use lemmy_api_common::{context::LemmyContext, utils::check_community_valid};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{
newtypes::CommentId,
source::{comment::Comment, community::Community, post::Post},
Expand Down
6 changes: 3 additions & 3 deletions crates/apub/src/http/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
community_moderators::ApubCommunityModerators,
community_outbox::ApubCommunityOutbox,
},
http::{create_apub_response, create_apub_tombstone_response},
http::{check_community_valid, create_apub_response, create_apub_tombstone_response},
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitypub_federation::{
Expand All @@ -16,7 +16,7 @@ use activitypub_federation::{
traits::{Collection, Object},
};
use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
use lemmy_api_common::{context::LemmyContext, utils::check_community_valid};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{source::community::Community, traits::ApubActor};
use lemmy_utils::error::LemmyError;
use serde::Deserialize;
Expand Down Expand Up @@ -132,7 +132,7 @@ pub(crate) mod tests {
},
traits::Crud,
};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
use lemmy_utils::error::LemmyResult;
use serde::de::DeserializeOwned;
use serial_test::serial;

Expand Down
17 changes: 15 additions & 2 deletions crates/apub/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ use activitypub_federation::{
use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
use http::{header::LOCATION, StatusCode};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{newtypes::DbUrl, source::activity::SentActivity};
use lemmy_utils::error::{LemmyError, LemmyResult};
use lemmy_db_schema::{
newtypes::DbUrl,
source::{activity::SentActivity, community::Community},
};
use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult};
use serde::{Deserialize, Serialize};
use std::ops::Deref;
use url::Url;
Expand Down Expand Up @@ -106,3 +109,13 @@ pub(crate) async fn get_activity(
)
}
}

pub fn check_community_valid(community: &Community) -> LemmyResult<()> {
if community.deleted || community.removed {
Err(LemmyErrorType::Deleted)?
}
if community.local_only {
return Err(LemmyErrorType::CouldntFindCommunity.into());
}
Ok(())
}
9 changes: 7 additions & 2 deletions crates/apub/src/http/post.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use crate::{
http::{create_apub_response, create_apub_tombstone_response, redirect_remote_object},
http::{
check_community_valid,
create_apub_response,
create_apub_tombstone_response,
redirect_remote_object,
},
objects::post::ApubPost,
};
use activitypub_federation::{config::Data, traits::Object};
use actix_web::{web, HttpResponse};
use lemmy_api_common::{context::LemmyContext, utils::check_community_valid};
use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{
newtypes::PostId,
source::{community::Community, post::Post},
Expand Down
15 changes: 11 additions & 4 deletions crates/db_views_actor/src/community_moderator_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,24 @@ impl CommunityModeratorView {
.await
}

pub async fn for_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Vec<Self>, Error> {
pub async fn for_person(
pool: &mut DbPool<'_>,
person_id: PersonId,
is_authenticated: bool,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
community_moderator::table
let mut query = community_moderator::table
.inner_join(community::table)
.inner_join(person::table)
.filter(community_moderator::person_id.eq(person_id))
.filter(community::deleted.eq(false))
.filter(community::removed.eq(false))
.select((community::all_columns, person::all_columns))
.load::<CommunityModeratorView>(conn)
.await
.into_boxed();
if !is_authenticated {
query = query.filter(community::local_only.eq(false));
}
query.load::<CommunityModeratorView>(conn).await
}

/// Finds all communities first mods / creators
Expand Down
5 changes: 4 additions & 1 deletion crates/routes/src/feeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use lemmy_db_views_actor::{
};
use lemmy_utils::{
cache_header::cache_1hour,
error::LemmyError,
error::{LemmyError, LemmyErrorType},
utils::markdown::{markdown_to_html, sanitize_html},
};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -268,6 +268,9 @@ async fn get_feed_community(
) -> Result<Channel, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?;
let community = Community::read_from_name(&mut context.pool(), community_name, false).await?;
if community.local_only {
return Err(LemmyErrorType::CouldntFindCommunity.into());
}

check_private_instance(&None, &site_view.local_site)?;

Expand Down

0 comments on commit 7773a67

Please sign in to comment.