Skip to content

Commit

Permalink
Merge pull request 'Split lemmy_apub crate into two parts apub and ap…
Browse files Browse the repository at this point in the history
…ub_receive' (#190) from split-apub-crate into main

Reviewed-on: https://yerbamate.ml/LemmyNet/lemmy/pulls/190
  • Loading branch information
dessalines committed Mar 31, 2021
2 parents 36a79e1 + 722cdb7 commit 1a3a215
Show file tree
Hide file tree
Showing 35 changed files with 246 additions and 163 deletions.
46 changes: 45 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"crates/api_crud",
"crates/api_common",
"crates/apub",
"crates/apub_receive",
"crates/utils",
"crates/db_queries",
"crates/db_schema",
Expand All @@ -29,6 +30,7 @@ members = [
lemmy_api = { path = "./crates/api" }
lemmy_api_crud = { path = "./crates/api_crud" }
lemmy_apub = { path = "./crates/apub" }
lemmy_apub_receive = { path = "./crates/apub_receive" }
lemmy_utils = { path = "./crates/utils" }
lemmy_db_schema = { path = "./crates/db_schema" }
lemmy_db_queries = { path = "./crates/db_queries" }
Expand Down
1 change: 0 additions & 1 deletion crates/api_crud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ itertools = "0.10.0"
uuid = { version = "0.8.2", features = ["serde", "v4"] }
sha2 = "0.9.3"
async-trait = "0.1.42"
captcha = "0.0.8"
anyhow = "1.0.38"
thiserror = "1.0.23"
background-jobs = "0.8.0"
Expand Down
1 change: 0 additions & 1 deletion crates/apub/src/activities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub(crate) mod receive;
pub mod send;
2 changes: 1 addition & 1 deletion crates/apub/src/extensions/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use lemmy_utils::LemmyError;
use serde_json::json;
use url::Url;

pub(crate) fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
pub fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
let context_ext = AnyBase::from_arbitrary_json(json!(
{
"sc": "http://schema.org#",
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) mod context;
pub mod context;
pub(crate) mod group_extension;
pub(crate) mod page_extension;
pub(crate) mod person_extension;
pub(crate) mod signatures;
pub mod signatures;
7 changes: 2 additions & 5 deletions crates/apub/src/extensions/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ lazy_static! {

/// Creates an HTTP post request to `inbox_url`, with the given `client` and `headers`, and
/// `activity` as request body. The request is signed with `private_key` and then sent.
pub async fn sign_and_send(
pub(crate) async fn sign_and_send(
client: &Client,
headers: BTreeMap<String, String>,
inbox_url: &Url,
Expand Down Expand Up @@ -65,10 +65,7 @@ pub async fn sign_and_send(
}

/// Verifies the HTTP signature on an incoming inbox request.
pub(crate) fn verify_signature(
request: &HttpRequest,
actor: &dyn ActorType,
) -> Result<(), LemmyError> {
pub fn verify_signature(request: &HttpRequest, actor: &dyn ActorType) -> Result<(), LemmyError> {
let public_key = actor.public_key().context(location_info!())?;
let verified = CONFIG2
.begin_verify(
Expand Down
6 changes: 3 additions & 3 deletions crates/apub/src/fetcher/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
person::get_or_fetch_and_upsert_person,
should_refetch_actor,
},
inbox::person_inbox::receive_announce,
objects::FromApub,
GroupExt,
};
Expand All @@ -31,7 +30,7 @@ use url::Url;
///
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
/// Otherwise it is fetched from the remote instance, stored and returned.
pub(crate) async fn get_or_fetch_and_upsert_community(
pub async fn get_or_fetch_and_upsert_community(
apub_id: &Url,
context: &LemmyContext,
recursion_counter: &mut i32,
Expand Down Expand Up @@ -158,7 +157,8 @@ async fn fetch_community_outbox(
}

for activity in outbox_activities {
receive_announce(context, activity, community, recursion_counter).await?;
todo!("{:?} {:?} {:?}", activity, community, recursion_counter);
//receive_announce(context, activity, community, recursion_counter).await?;
}

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions crates/apub/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod community;
pub mod community;
mod fetch;
pub(crate) mod objects;
pub(crate) mod person;
pub mod objects;
pub mod person;
pub mod search;

use crate::{
Expand Down Expand Up @@ -42,7 +42,7 @@ where
///
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
/// Otherwise it is fetched from the remote instance, stored and returned.
pub(crate) async fn get_or_fetch_and_upsert_actor(
pub async fn get_or_fetch_and_upsert_actor(
apub_id: &Url,
context: &LemmyContext,
recursion_counter: &mut i32,
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/fetcher/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use url::Url;
/// pulled from its apub ID, inserted and returned.
///
/// The parent community is also pulled if necessary. Comments are not pulled.
pub(crate) async fn get_or_fetch_and_insert_post(
pub async fn get_or_fetch_and_insert_post(
post_ap_id: &Url,
context: &LemmyContext,
recursion_counter: &mut i32,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub(crate) async fn get_or_fetch_and_insert_post(
/// pulled from its apub ID, inserted and returned.
///
/// The parent community, post and comment are also pulled if necessary.
pub(crate) async fn get_or_fetch_and_insert_comment(
pub async fn get_or_fetch_and_insert_comment(
comment_ap_id: &Url,
context: &LemmyContext,
recursion_counter: &mut i32,
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/fetcher/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use url::Url;
///
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
/// Otherwise it is fetched from the remote instance, stored and returned.
pub(crate) async fn get_or_fetch_and_upsert_person(
pub async fn get_or_fetch_and_upsert_person(
apub_id: &Url,
context: &LemmyContext,
recursion_counter: &mut i32,
Expand Down
67 changes: 56 additions & 11 deletions crates/apub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ pub mod activities;
pub mod activity_queue;
pub mod extensions;
pub mod fetcher;
pub mod http;
pub mod inbox;
pub mod objects;
pub mod routes;

use crate::extensions::{
group_extension::GroupExtension,
Expand All @@ -20,7 +17,7 @@ use activitystreams::{
activity::Follow,
actor,
base::AnyBase,
object::{ApObject, Note, Page},
object::{ApObject, AsObject, Note, ObjectExt, Page},
};
use activitystreams_ext::{Ext1, Ext2};
use anyhow::{anyhow, Context};
Expand All @@ -36,8 +33,10 @@ use lemmy_db_schema::{
post::Post,
private_message::PrivateMessage,
},
CommunityId,
DbUrl,
};
use lemmy_db_views_actor::community_person_ban_view::CommunityPersonBanView;
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::Serialize;
Expand All @@ -49,8 +48,8 @@ type GroupExt = Ext2<actor::ApActor<ApObject<actor::Group>>, GroupExtension, Pub
/// Activitystreams type for person
type PersonExt = Ext2<actor::ApActor<ApObject<actor::Person>>, PersonExtension, PublicKeyExtension>;
/// Activitystreams type for post
type PageExt = Ext1<ApObject<Page>, PageExtension>;
type NoteExt = ApObject<Note>;
pub type PageExt = Ext1<ApObject<Page>, PageExtension>;
pub type NoteExt = ApObject<Note>;

pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";

Expand All @@ -63,7 +62,7 @@ pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
/// - URL not being in the blocklist (if it is active)
///
/// Note that only one of allowlist and blacklist can be enabled, not both.
fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
pub fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
let settings = Settings::get();
let domain = apub_id.domain().context(location_info!())?.to_string();
let local_instance = settings.get_hostname_without_port()?;
Expand Down Expand Up @@ -294,13 +293,13 @@ pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError>
Ok(Url::parse(&url)?.into())
}

pub(crate) fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
pub fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
}

/// Store a sent or received activity in the database, for logging purposes. These records are not
/// persistent.
pub(crate) async fn insert_activity<T>(
pub async fn insert_activity<T>(
ap_id: &Url,
activity: T,
local: bool,
Expand All @@ -318,15 +317,15 @@ where
Ok(())
}

pub(crate) enum PostOrComment {
pub enum PostOrComment {
Comment(Box<Comment>),
Post(Box<Post>),
}

/// Tries to find a post or comment in the local database, without any network requests.
/// This is used to handle deletions and removals, because in case we dont have the object, we can
/// simply ignore the activity.
pub(crate) async fn find_post_or_comment_by_id(
pub async fn find_post_or_comment_by_id(
context: &LemmyContext,
apub_id: Url,
) -> Result<PostOrComment, LemmyError> {
Expand Down Expand Up @@ -400,3 +399,49 @@ pub(crate) async fn find_object_by_id(

Err(NotFound.into())
}

pub async fn check_community_or_site_ban(
person: &Person,
community_id: CommunityId,
pool: &DbPool,
) -> Result<(), LemmyError> {
if person.banned {
return Err(anyhow!("Person is banned from site").into());
}
let person_id = person.id;
let is_banned =
move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
if blocking(pool, is_banned).await? {
return Err(anyhow!("Person is banned from community").into());
}

Ok(())
}

pub fn get_activity_to_and_cc<T, Kind>(activity: &T) -> Vec<Url>
where
T: AsObject<Kind>,
{
let mut to_and_cc = vec![];
if let Some(to) = activity.to() {
let to = to.to_owned().unwrap_to_vec();
let mut to = to
.iter()
.map(|t| t.as_xsd_any_uri())
.flatten()
.map(|t| t.to_owned())
.collect();
to_and_cc.append(&mut to);
}
if let Some(cc) = activity.cc() {
let cc = cc.to_owned().unwrap_to_vec();
let mut cc = cc
.iter()
.map(|c| c.as_xsd_any_uri())
.flatten()
.map(|c| c.to_owned())
.collect();
to_and_cc.append(&mut cc);
}
to_and_cc
}
7 changes: 4 additions & 3 deletions crates/apub/src/objects/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
check_community_or_site_ban,
check_is_apub_id_valid,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
inbox::{community_inbox::check_community_or_site_ban, get_activity_to_and_cc},
get_activity_to_and_cc,
PageExt,
};
use activitystreams::{
Expand Down Expand Up @@ -33,14 +34,14 @@ pub(crate) mod private_message;

/// Trait for converting an object or actor into the respective ActivityPub type.
#[async_trait::async_trait(?Send)]
pub(crate) trait ToApub {
pub trait ToApub {
type ApubType;
async fn to_apub(&self, pool: &DbPool) -> Result<Self::ApubType, LemmyError>;
fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
}

#[async_trait::async_trait(?Send)]
pub(crate) trait FromApub {
pub trait FromApub {
type ApubType;
/// Converts an object from ActivityPub type to Lemmy internal type.
///
Expand Down
Loading

0 comments on commit 1a3a215

Please sign in to comment.