Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code to apub library #1795

Merged
merged 4 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ members = [
lemmy_api = { version = "=0.13.0", path = "./crates/api" }
lemmy_api_crud = { version = "=0.13.0", path = "./crates/api_crud" }
lemmy_apub = { version = "=0.13.0", path = "./crates/apub" }
lemmy_apub_lib = { version = "=0.13.0", path = "./crates/apub_lib" }
lemmy_utils = { version = "=0.13.0", path = "./crates/utils" }
lemmy_db_schema = { version = "=0.13.0", path = "./crates/db_schema" }
lemmy_db_queries = { version = "=0.13.0", path = "./crates/db_queries" }
Expand Down
2 changes: 1 addition & 1 deletion api_tests/prepare-drone-federation-test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

export LEMMY_TEST_SEND_SYNC=1
export APUB_TESTING_SEND_SYNC=1
export RUST_BACKTRACE=1
export RUST_LOG="warn,lemmy_server=debug,lemmy_api=debug,lemmy_api_common=debug,lemmy_api_crud=debug,lemmy_apub=debug,lemmy_db_queries=debug,lemmy_db_schema=debug,lemmy_db_views=debug,lemmy_db_views_actor=debug,lemmy_db_views_moderator=debug,lemmy_routes=debug,lemmy_utils=debug,lemmy_websocket=debug"

Expand Down
1 change: 1 addition & 0 deletions crates/api_crud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "AGPL-3.0"

[dependencies]
lemmy_apub = { version = "=0.13.0", path = "../apub" }
lemmy_apub_lib = { version = "=0.13.0", path = "../apub_lib" }
lemmy_utils = { version = "=0.13.0", path = "../utils" }
lemmy_db_queries = { version = "=0.13.0", path = "../db_queries" }
lemmy_db_schema = { version = "=0.13.0", path = "../db_schema" }
Expand Down
10 changes: 4 additions & 6 deletions crates/api_crud/src/community/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use lemmy_api_common::{
is_admin,
};
use lemmy_apub::{
fetcher::object_id::ObjectId,
generate_apub_endpoint,
generate_followers_url,
generate_inbox_url,
generate_shared_inbox_url,
EndpointType,
};
use lemmy_db_queries::{diesel_option_overwrite_to_url, ApubObject, Crud, Followable, Joinable};
use lemmy_db_queries::{diesel_option_overwrite_to_url, Crud, Followable, Joinable};
use lemmy_db_schema::source::{
community::{
Community,
Expand Down Expand Up @@ -67,11 +68,8 @@ impl PerformCrud for CreateCommunity {
&data.name,
&context.settings().get_protocol_and_hostname(),
)?;
let actor_id_cloned = community_actor_id.to_owned();
let community_dupe = blocking(context.pool(), move |conn| {
Community::read_from_apub_id(conn, &actor_id_cloned)
})
.await?;
let community_actor_id_wrapped = ObjectId::<Community>::new(community_actor_id.clone());
let community_dupe = community_actor_id_wrapped.dereference_local(context).await;
if community_dupe.is_ok() {
return Err(ApiError::err("community_already_exists").into());
}
Expand Down
21 changes: 7 additions & 14 deletions crates/api_crud/src/community/read.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
use lemmy_db_queries::{
from_opt_str_to_opt_enum,
ApubObject,
DeleteableOrRemoveable,
ListingType,
SortType,
};
use lemmy_apub::{build_actor_id_from_shortname, fetcher::object_id::ObjectId, EndpointType};
use lemmy_db_queries::{from_opt_str_to_opt_enum, DeleteableOrRemoveable, ListingType, SortType};
use lemmy_db_schema::source::community::*;
use lemmy_db_views_actor::{
community_moderator_view::CommunityModeratorView,
Expand Down Expand Up @@ -38,12 +32,11 @@ impl PerformCrud for GetCommunity {
let community_actor_id =
build_actor_id_from_shortname(EndpointType::Community, &name, &context.settings())?;

blocking(context.pool(), move |conn| {
Community::read_from_apub_id(conn, &community_actor_id)
})
.await?
.map_err(|_| ApiError::err("couldnt_find_community"))?
.id
ObjectId::<Community>::new(community_actor_id)
.dereference(context, &mut 0)
.await
.map_err(|_| ApiError::err("couldnt_find_community"))?
.id
}
};

Expand Down
11 changes: 5 additions & 6 deletions crates/api_crud/src/user/read.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
use lemmy_db_queries::{from_opt_str_to_opt_enum, ApubObject, SortType};
use lemmy_apub::{build_actor_id_from_shortname, fetcher::object_id::ObjectId, EndpointType};
use lemmy_db_queries::{from_opt_str_to_opt_enum, SortType};
use lemmy_db_schema::source::person::*;
use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
use lemmy_db_views_actor::{
Expand Down Expand Up @@ -45,10 +45,9 @@ impl PerformCrud for GetPersonDetails {
let actor_id =
build_actor_id_from_shortname(EndpointType::Person, &name, &context.settings())?;

let person = blocking(context.pool(), move |conn| {
Person::read_from_apub_id(conn, &actor_id)
})
.await?;
let person = ObjectId::<Person>::new(actor_id)
.dereference(context, &mut 0)
.await;
person
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
.id
Expand Down
5 changes: 0 additions & 5 deletions crates/apub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ strum = "0.21.0"
strum_macros = "0.21.1"
url = { version = "2.2.2", features = ["serde"] }
percent-encoding = "2.1.0"
openssl = "0.10.36"
http = "0.2.5"
http-signature-normalization-actix = { version = "0.5.0-beta.10", default-features = false, features = ["server", "sha-2"] }
http-signature-normalization-reqwest = { version = "0.2.0", default-features = false, features = ["sha-2"] }
base64 = "0.13.0"
tokio = "1.12.0"
futures = "0.3.17"
itertools = "0.10.1"
Expand All @@ -49,7 +46,5 @@ sha2 = "0.9.8"
async-trait = "0.1.51"
anyhow = "1.0.44"
thiserror = "1.0.29"
background-jobs = "0.10.0"
reqwest = { version = "0.11.4", features = ["json"] }
lazy_static = "1.4.0"

21 changes: 13 additions & 8 deletions crates/apub/src/activities/comment/create_or_update.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use crate::{
activities::{
comment::{collect_non_local_mentions, get_notif_recipients},
community::announce::AnnouncableActivities,
community::{announce::AnnouncableActivities, send_to_community},
extract_community,
generate_activity_id,
verify_activity,
verify_person_in_community,
CreateOrUpdateType,
},
activity_queue::send_to_community_new,
extensions::context::lemmy_context,
context::lemmy_context,
fetcher::object_id::ObjectId,
objects::{comment::Note, FromApub, ToApub},
ActorType,
};
use activitystreams::{base::AnyBase, link::Mention, primitives::OneOrMany, unparsed::Unparsed};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, verify_domains_match, ActivityFields, ActivityHandler};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
verify::verify_domains_match,
};
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
use lemmy_utils::LemmyError;
Expand Down Expand Up @@ -76,15 +79,17 @@ impl CreateOrUpdateComment {
};

let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
send_to_community_new(activity, &id, actor, &community, maa.inboxes, context).await
send_to_community(activity, &id, actor, &community, maa.inboxes, context).await
}
}

#[async_trait::async_trait(?Send)]
impl ActivityHandler for CreateOrUpdateComment {
type DataType = LemmyContext;

async fn verify(
&self,
context: &LemmyContext,
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let community = extract_community(&self.cc, context, request_counter).await?;
Expand All @@ -101,7 +106,7 @@ impl ActivityHandler for CreateOrUpdateComment {

async fn receive(
self,
context: &LemmyContext,
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let comment =
Expand Down
8 changes: 4 additions & 4 deletions crates/apub/src/activities/comment/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{fetcher::object_id::ObjectId, ActorType};
use crate::fetcher::object_id::ObjectId;
use activitystreams::{
base::BaseExt,
link::{LinkExt, Mention},
};
use anyhow::anyhow;
use itertools::Itertools;
use lemmy_api_common::{blocking, send_local_notifs};
use lemmy_apub_lib::webfinger::WebfingerResponse;
use lemmy_apub_lib::{traits::ActorType, webfinger::WebfingerResponse};
use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::{
source::{comment::Comment, community::Community, person::Person, post::Post},
Expand Down Expand Up @@ -68,7 +68,7 @@ pub async fn collect_non_local_mentions(
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
let mut addressed_ccs = vec![community.actor_id(), parent_creator.actor_id()];
// Note: dont include community inbox here, as we send to it separately with `send_to_community()`
let mut inboxes = vec![parent_creator.get_shared_inbox_or_inbox_url()];
let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];

// Add the mention tag
let mut tags = Vec::new();
Expand All @@ -88,7 +88,7 @@ pub async fn collect_non_local_mentions(
addressed_ccs.push(actor_id.to_owned().to_string().parse()?);

let mention_person = actor_id.dereference(context, &mut 0).await?;
inboxes.push(mention_person.get_shared_inbox_or_inbox_url());
inboxes.push(mention_person.shared_inbox_or_inbox_url());

let mut mention_tag = Mention::new();
mention_tag
Expand Down
24 changes: 14 additions & 10 deletions crates/apub/src/activities/community/add_mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use crate::{
activities::{
community::announce::AnnouncableActivities,
community::{announce::AnnouncableActivities, send_to_community},
generate_activity_id,
verify_activity,
verify_add_remove_moderator_target,
verify_mod_action,
verify_person_in_community,
},
activity_queue::send_to_community_new,
extensions::context::lemmy_context,
context::lemmy_context,
fetcher::object_id::ObjectId,
generate_moderators_url,
ActorType,
};
use activitystreams::{
activity::kind::AddType,
Expand All @@ -20,7 +18,11 @@ use activitystreams::{
unparsed::Unparsed,
};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_queries::{source::community::CommunityModerator_, Joinable};
use lemmy_db_schema::source::{
community::{Community, CommunityModerator, CommunityModeratorForm},
Expand Down Expand Up @@ -72,28 +74,30 @@ impl AddMod {
};

let activity = AnnouncableActivities::AddMod(add);
let inboxes = vec![added_mod.get_shared_inbox_or_inbox_url()];
send_to_community_new(activity, &id, actor, community, inboxes, context).await
let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
send_to_community(activity, &id, actor, community, inboxes, context).await
}
}

#[async_trait::async_trait(?Send)]
impl ActivityHandler for AddMod {
type DataType = LemmyContext;

async fn verify(
&self,
context: &LemmyContext,
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
verify_activity(self, &context.settings())?;
verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
verify_mod_action(&self.actor, self.cc[0].clone(), context, request_counter).await?;
verify_add_remove_moderator_target(&self.target, &self.cc[0])?;
Ok(())
}

async fn receive(
self,
context: &LemmyContext,
context: &Data<LemmyContext>,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let community = self.cc[0].dereference(context, request_counter).await?;
Expand Down
Loading