summaryrefslogtreecommitdiffstats
path: root/server/src/apub
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/apub')
-rw-r--r--server/src/apub/activities.rs12
-rw-r--r--server/src/apub/comment.rs18
-rw-r--r--server/src/apub/community.rs84
-rw-r--r--server/src/apub/community_inbox.rs12
-rw-r--r--server/src/apub/extensions/group_extensions.rs6
-rw-r--r--server/src/apub/extensions/signatures.rs18
-rw-r--r--server/src/apub/fetcher.rs39
-rw-r--r--server/src/apub/mod.rs60
-rw-r--r--server/src/apub/post.rs16
-rw-r--r--server/src/apub/private_message.rs14
-rw-r--r--server/src/apub/shared_inbox.rs29
-rw-r--r--server/src/apub/user.rs24
-rw-r--r--server/src/apub/user_inbox.rs20
13 files changed, 157 insertions, 195 deletions
diff --git a/server/src/apub/activities.rs b/server/src/apub/activities.rs
index e5dc7045..204a380d 100644
--- a/server/src/apub/activities.rs
+++ b/server/src/apub/activities.rs
@@ -1,12 +1,18 @@
use crate::{
- apub::{extensions::signatures::sign, is_apub_id_valid, ActorType},
- db::{activity::insert_activity, community::Community, user::User_},
+ apub::{
+ community::do_announce,
+ extensions::signatures::sign,
+ insert_activity,
+ is_apub_id_valid,
+ ActorType,
+ },
request::retry_custom,
DbPool,
LemmyError,
};
use activitystreams::{context, object::properties::ObjectProperties, public, Activity, Base};
use actix_web::client::Client;
+use lemmy_db::{community::Community, user::User_};
use log::debug;
use serde::Serialize;
use std::fmt::Debug;
@@ -43,7 +49,7 @@ where
// if this is a local community, we need to do an announce from the community instead
if community.local {
- Community::do_announce(activity, &community, creator, client, pool).await?;
+ do_announce(activity, &community, creator, client, pool).await?;
} else {
send_activity(client, &activity, creator, to).await?;
}
diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs
index dbc15909..af3581cb 100644
--- a/server/src/apub/comment.rs
+++ b/server/src/apub/comment.rs
@@ -17,19 +17,9 @@ use crate::{
ToApub,
},
blocking,
- convert_datetime,
- db::{
- comment::{Comment, CommentForm},
- community::Community,
- post::Post,
- user::User_,
- Crud,
- },
routes::DbPoolParam,
- scrape_text_for_mentions,
DbPool,
LemmyError,
- MentionData,
};
use activitystreams::{
activity::{Create, Delete, Dislike, Like, Remove, Undo, Update},
@@ -40,6 +30,14 @@ use activitystreams::{
use activitystreams_new::object::Tombstone;
use actix_web::{body::Body, client::Client, web::Path, HttpResponse};
use itertools::Itertools;
+use lemmy_db::{
+ comment::{Comment, CommentForm},
+ community::Community,
+ post::Post,
+ user::User_,
+ Crud,
+};
+use lemmy_utils::{convert_datetime, scrape_text_for_mentions, MentionData};
use log::debug;
use serde::Deserialize;
diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs
index bfc896af..8b623e71 100644
--- a/server/src/apub/community.rs
+++ b/server/src/apub/community.rs
@@ -7,20 +7,13 @@ use crate::{
extensions::group_extensions::GroupExtension,
fetcher::get_or_fetch_and_upsert_remote_user,
get_shared_inbox,
+ insert_activity,
ActorType,
FromApub,
GroupExt,
ToApub,
},
blocking,
- convert_datetime,
- db::{
- activity::insert_activity,
- community::{Community, CommunityForm},
- community_view::{CommunityFollowerView, CommunityModeratorView},
- user::User_,
- },
- naive_now,
routes::DbPoolParam,
DbPool,
LemmyError,
@@ -44,6 +37,13 @@ use activitystreams_new::{
};
use actix_web::{body::Body, client::Client, web, HttpResponse};
use itertools::Itertools;
+use lemmy_db::{
+ community::{Community, CommunityForm},
+ community_view::{CommunityFollowerView, CommunityModeratorView},
+ naive_now,
+ user::User_,
+};
+use lemmy_utils::convert_datetime;
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, str::FromStr};
@@ -462,39 +462,37 @@ pub async fn get_apub_community_followers(
Ok(create_apub_response(&collection))
}
-impl Community {
- pub async fn do_announce<A>(
- activity: A,
- community: &Community,
- sender: &dyn ActorType,
- client: &Client,
- pool: &DbPool,
- ) -> Result<HttpResponse, LemmyError>
- where
- A: Activity + Base + Serialize + Debug,
- {
- let mut announce = Announce::default();
- populate_object_props(
- &mut announce.object_props,
- vec![community.get_followers_url()],
- &format!("{}/announce/{}", community.actor_id, uuid::Uuid::new_v4()),
- )?;
- announce
- .announce_props
- .set_actor_xsd_any_uri(community.actor_id.to_owned())?
- .set_object_base_box(BaseBox::from_concrete(activity)?)?;
-
- insert_activity(community.creator_id, announce.clone(), true, pool).await?;
-
- // dont send to the instance where the activity originally came from, because that would result
- // in a database error (same data inserted twice)
- let mut to = community.get_follower_inboxes(pool).await?;
-
- // this seems to be the "easiest" stable alternative for remove_item()
- to.retain(|x| *x != sender.get_shared_inbox_url());
-
- send_activity(client, &announce, community, to).await?;
-
- Ok(HttpResponse::Ok().finish())
- }
+pub async fn do_announce<A>(
+ activity: A,
+ community: &Community,
+ sender: &dyn ActorType,
+ client: &Client,
+ pool: &DbPool,
+) -> Result<HttpResponse, LemmyError>
+where
+ A: Activity + Base + Serialize + Debug,
+{
+ let mut announce = Announce::default();
+ populate_object_props(
+ &mut announce.object_props,
+ vec![community.get_followers_url()],
+ &format!("{}/announce/{}", community.actor_id, uuid::Uuid::new_v4()),
+ )?;
+ announce
+ .announce_props
+ .set_actor_xsd_any_uri(community.actor_id.to_owned())?
+ .set_object_base_box(BaseBox::from_concrete(activity)?)?;
+
+ insert_activity(community.creator_id, announce.clone(), true, pool).await?;
+
+ // dont send to the instance where the activity originally came from, because that would result
+ // in a database error (same data inserted twice)
+ let mut to = community.get_follower_inboxes(pool).await?;
+
+ // this seems to be the "easiest" stable alternative for remove_item()
+ to.retain(|x| *x != sender.get_shared_inbox_url());
+
+ send_activity(client, &announce, community, to).await?;
+
+ Ok(HttpResponse::Ok().finish())
}
diff --git a/server/src/apub/community_inbox.rs b/server/src/apub/community_inbox.rs
index 996e0c25..8ea64434 100644
--- a/server/src/apub/community_inbox.rs
+++ b/server/src/apub/community_inbox.rs
@@ -2,21 +2,21 @@ use crate::{
apub::{
extensions::signatures::verify,
fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user},
+ insert_activity,
ActorType,
},
blocking,
- db::{
- activity::insert_activity,
- community::{Community, CommunityFollower, CommunityFollowerForm},
- user::User_,
- Followable,
- },
routes::{ChatServerParam, DbPoolParam},
LemmyError,
};
use activitystreams::activity::Undo;
use activitystreams_new::activity::Follow;
use actix_web::{client::Client, web, HttpRequest, HttpResponse};
+use lemmy_db::{
+ community::{Community, CommunityFollower, CommunityFollowerForm},
+ user::User_,
+ Followable,
+};
use log::debug;
use serde::Deserialize;
use std::fmt::Debug;
diff --git a/server/src/apub/extensions/group_extensions.rs b/server/src/apub/extensions/group_extensions.rs
index 1c24eef5..2120f6f1 100644
--- a/server/src/apub/extensions/group_extensions.rs
+++ b/server/src/apub/extensions/group_extensions.rs
@@ -1,9 +1,7 @@
-use crate::{
- db::{category::Category, Crud},
- LemmyError,
-};
+use crate::LemmyError;
use activitystreams::{ext::Extension, Actor};
use diesel::PgConnection;
+use lemmy_db::{category::Category, Crud};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
diff --git a/server/src/apub/extensions/signatures.rs b/server/src/apub/extensions/signatures.rs
index af46bc5e..1c930a95 100644
--- a/server/src/apub/extensions/signatures.rs
+++ b/server/src/apub/extensions/signatures.rs
@@ -9,7 +9,6 @@ use log::debug;
use openssl::{
hash::MessageDigest,
pkey::PKey,
- rsa::Rsa,
sign::{Signer, Verifier},
};
use serde::{Deserialize, Serialize};
@@ -19,23 +18,6 @@ lazy_static! {
static ref HTTP_SIG_CONFIG: Config = Config::new();
}
-pub struct Keypair {
- pub private_key: String,
- pub public_key: String,
-}
-
-/// Generate the asymmetric keypair for ActivityPub HTTP signatures.
-pub fn generate_actor_keypair() -> Result<Keypair, LemmyError> {
- let rsa = Rsa::generate(2048)?;
- let pkey = PKey::from_rsa(rsa)?;
- let public_key = pkey.public_key_to_pem()?;
- let private_key = pkey.private_key_to_pem_pkcs8()?;
- Ok(Keypair {
- private_key: String::from_utf8(private_key)?,
- public_key: String::from_utf8(public_key)?,
- })
-}
-
/// Signs request headers with the given keypair.
pub async fn sign(
request: ClientRequest,
diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs
index d8a1e764..0604129d 100644
--- a/server/src/apub/fetcher.rs
+++ b/server/src/apub/fetcher.rs
@@ -1,29 +1,7 @@
use crate::{
api::site::SearchResponse,
- apub::{
- get_apub_protocol_string,
- is_apub_id_valid,
- FromApub,
- GroupExt,
- PageExt,
- PersonExt,
- APUB_JSON_CONTENT_TYPE,
- },
+ apub::{is_apub_id_valid, FromApub, GroupExt, PageExt, PersonExt, APUB_JSON_CONTENT_TYPE},
blocking,
- db::{
- comment::{Comment, CommentForm},
- comment_view::CommentView,
- community::{Community, CommunityForm, CommunityModerator, CommunityModeratorForm},
- community_view::CommunityView,
- post::{Post, PostForm},
- post_view::PostView,
- user::{UserForm, User_},
- user_view::UserView,
- Crud,
- Joinable,
- SearchType,
- },
- naive_now,
request::{retry, RecvError},
routes::nodeinfo::{NodeInfo, NodeInfoWellKnown},
DbPool,
@@ -34,6 +12,21 @@ use activitystreams_new::{base::BaseExt, prelude::*, primitives::XsdAnyUri};
use actix_web::client::Client;
use chrono::NaiveDateTime;
use diesel::{result::Error::NotFound, PgConnection};
+use lemmy_db::{
+ comment::{Comment, CommentForm},
+ comment_view::CommentView,
+ community::{Community, CommunityForm, CommunityModerator, CommunityModeratorForm},
+ community_view::CommunityView,
+ naive_now,
+ post::{Post, PostForm},
+ post_view::PostView,
+ user::{UserForm, User_},
+ user_view::UserView,
+ Crud,
+ Joinable,
+ SearchType,
+};
+use lemmy_utils::get_apub_protocol_string;
use log::debug;
use serde::Deserialize;
use std::{fmt::Debug, time::Duration};
diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs
index 561dc49a..eeac5fec 100644
--- a/server/src/apub/mod.rs
+++ b/server/src/apub/mod.rs
@@ -16,14 +16,11 @@ use crate::{
page_extension::PageExtension,
signatures::{PublicKey, PublicKeyExtension},
},
- convert_datetime,
- db::user::User_,
+ blocking,
request::{retry, RecvError},
routes::webfinger::WebFingerResponse,
DbPool,
LemmyError,
- MentionData,
- Settings,
};
use activitystreams::object::Page;
use activitystreams_ext::{Ext1, Ext2};
@@ -35,6 +32,9 @@ use activitystreams_new::{
};
use actix_web::{body::Body, client::Client, HttpResponse};
use chrono::NaiveDateTime;
+use failure::_core::fmt::Debug;
+use lemmy_db::{activity::do_insert_activity, user::User_};
+use lemmy_utils::{convert_datetime, get_apub_protocol_string, settings::Settings, MentionData};
use log::debug;
use serde::Serialize;
use url::Url;
@@ -45,14 +45,6 @@ type PageExt = Ext1<Page, PageExtension>;
pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
-pub enum EndpointType {
- Community,
- User,
- Post,
- Comment,
- PrivateMessage,
-}
-
/// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
/// headers.
fn create_apub_response<T>(data: &T) -> HttpResponse<Body>
@@ -73,34 +65,6 @@ where
.json(data)
}
-/// Generates the ActivityPub ID for a given object type and ID.
-pub fn make_apub_endpoint(endpoint_type: EndpointType, name: &str) -> Url {
- let point = match endpoint_type {
- EndpointType::Community => "c",
- EndpointType::User => "u",
- EndpointType::Post => "post",
- EndpointType::Comment => "comment",
- EndpointType::PrivateMessage => "private_message",
- };
-
- Url::parse(&format!(
- "{}://{}/{}/{}",
- get_apub_protocol_string(),
- Settings::get().hostname,
- point,
- name
- ))
- .unwrap()
-}
-
-pub fn get_apub_protocol_string() -> &'static str {
- if Settings::get().federation.tls_enabled {
- "https"
- } else {
- "http"
- }
-}
-
// Checks if the ID has a valid format, correct scheme, and is in the allowed instance list.
fn is_apub_id_valid(apub_id: &Url) -> bool {
debug!("Checking {}", apub_id);
@@ -374,3 +338,19 @@ pub async fn fetch_webfinger_url(
.to_owned()
.ok_or_else(|| format_err!("No href found.").into())
}
+
+pub async fn insert_activity<T>(
+ user_id: i32,
+ data: T,
+ local: bool,
+ pool: &DbPool,
+) -> Result<(), LemmyError>
+where
+ T: Serialize + Debug + Send + 'static,
+{
+ blocking(pool, move |conn| {
+ do_insert_activity(conn, user_id, &data, local)
+ })
+ .await??;
+ Ok(())
+}
diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs
index 255629e8..ba0372c4 100644
--- a/server/src/apub/post.rs
+++ b/server/src/apub/post.rs
@@ -6,7 +6,6 @@ use crate::{
create_tombstone,
extensions::page_extension::PageExtension,
fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user},
- get_apub_protocol_string,
ActorType,
ApubLikeableType,
ApubObjectType,
@@ -15,17 +14,9 @@ use crate::{
ToApub,
},
blocking,
- convert_datetime,
- db::{
- community::Community,
- post::{Post, PostForm},
- user::User_,
- Crud,
- },
routes::DbPoolParam,
DbPool,
LemmyError,
- Settings,
};
use activitystreams::{
activity::{Create, Delete, Dislike, Like, Remove, Undo, Update},
@@ -36,6 +27,13 @@ use activitystreams::{
use activitystreams_ext::Ext1;
use activitystreams_new::object::Tombstone;
use actix_web::{body::Body, client::Client, web, HttpResponse};
+use lemmy_db::{
+ community::Community,
+ post::{Post, PostForm},
+ user::User_,
+ Crud,
+};
+use lemmy_utils::{convert_datetime, get_apub_protocol_string, settings::Settings};
use serde::Deserialize;
#[derive(Deserialize)]
diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs
index 48bbd1f0..567a6178 100644
--- a/server/src/apub/private_message.rs
+++ b/server/src/apub/private_message.rs
@@ -3,18 +3,12 @@ use crate::{
activities::send_activity,
create_tombstone,
fetcher::get_or_fetch_and_upsert_remote_user,
+ insert_activity,
ApubObjectType,
FromApub,
ToApub,
},
blocking,
- convert_datetime,
- db::{
- activity::insert_activity,
- private_message::{PrivateMessage, PrivateMessageForm},
- user::User_,
- Crud,
- },
DbPool,
LemmyError,
};
@@ -25,6 +19,12 @@ use activitystreams::{
};
use activitystreams_new::object::Tombstone;
use actix_web::client::Client;
+use lemmy_db::{
+ private_message::{PrivateMessage, PrivateMessageForm},
+ user::User_,
+ Crud,
+};
+use lemmy_utils::convert_datetime;
#[async_trait::async_trait(?Send)]
impl ToApub for PrivateMessage {
diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs
index fa9794b2..75ce3415 100644
--- a/server/src/apub/shared_inbox.rs
+++ b/server/src/apub/shared_inbox.rs
@@ -5,6 +5,7 @@ use crate::{
post::PostResponse,
},
apub::{
+ community::do_announce,
extensions::signatures::verify,
fetcher::{
get_or_fetch_and_insert_remote_comment,
@@ -12,25 +13,13 @@ use crate::{
get_or_fetch_and_upsert_remote_community,
get_or_fetch_and_upsert_remote_user,
},
+ insert_activity,
FromApub,
GroupExt,
PageExt,
},
blocking,
- db::{
- activity::insert_activity,
- comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
- comment_view::CommentView,
- community::{Community, CommunityForm},
- community_view::CommunityView,
- post::{Post, PostForm, PostLike, PostLikeForm},
- post_view::PostView,
- Crud,
- Likeable,
- },
- naive_now,
routes::{ChatServerParam, DbPoolParam},
- scrape_text_for_mentions,
websocket::{
server::{SendComment, SendCommunityRoomMessage, SendPost},
UserOperation,
@@ -46,6 +35,18 @@ use activitystreams::{
BaseBox,
};
use actix_web::{client::Client, web, HttpRequest, HttpResponse};
+use lemmy_db::{
+ comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
+ comment_view::CommentView,
+ community::{Community, CommunityForm},
+ community_view::CommunityView,
+ naive_now,
+ post::{Post, PostForm, PostLike, PostLikeForm},
+ post_view::PostView,
+ Crud,
+ Likeable,
+};
+use lemmy_utils::scrape_text_for_mentions;
use log::debug;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
@@ -234,7 +235,7 @@ where
if community.local {
let sending_user = get_or_fetch_and_upsert_remote_user(sender, client, pool).await?;
- Community::do_announce(activity, &community, &sending_user, client, pool).await
+ do_announce(activity, &community, &sending_user, client, pool).await
} else {
Ok(HttpResponse::NotFound().finish())
}
diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs
index a3194355..323407c7 100644
--- a/server/src/apub/user.rs
+++ b/server/src/apub/user.rs
@@ -1,12 +1,15 @@
use crate::{
- apub::{activities::send_activity, create_apub_response, ActorType, FromApub, PersonExt, ToApub},
- blocking,
- convert_datetime,
- db::{
- activity::insert_activity,
- user::{UserForm, User_},
+ api::claims::Claims,
+ apub::{
+ activities::send_activity,
+ create_apub_response,
+ insert_activity,
+ ActorType,
+ FromApub,
+ PersonExt,
+ ToApub,
},
- naive_now,
+ blocking,
routes::DbPoolParam,
DbPool,
LemmyError,
@@ -22,6 +25,11 @@ use activitystreams_new::{
};
use actix_web::{body::Body, client::Client, web, HttpResponse};
use failure::_core::str::FromStr;
+use lemmy_db::{
+ naive_now,
+ user::{UserForm, User_},
+};
+use lemmy_utils::convert_datetime;
use serde::Deserialize;
#[derive(Deserialize)]
@@ -240,7 +248,7 @@ pub async fn get_apub_user_http(
) -> Result<HttpResponse<Body>, LemmyError> {
let user_name = info.into_inner().user_name;
let user = blocking(&db, move |conn| {
- User_::find_by_email_or_username(conn, &user_name)
+ Claims::find_by_email_or_username(conn, &user_name)
})
.await??;
let u = user.to_apub(&db).await?;
diff --git a/server/src/apub/user_inbox.rs b/server/src/apub/user_inbox.rs
index 80280ade..226235e6 100644
--- a/server/src/apub/user_inbox.rs
+++ b/server/src/apub/user_inbox.rs
@@ -3,19 +3,10 @@ use crate::{
apub::{
extensions::signatures::verify,
fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user},
+ insert_activity,
FromApub,
},
blocking,
- db::{
- activity::insert_activity,
- community::{CommunityFollower, CommunityFollowerForm},
- private_message::{PrivateMessage, PrivateMessageForm},
- private_message_view::PrivateMessageView,
- user::User_,
- Crud,
- Followable,
- },
- naive_now,
routes::{ChatServerParam, DbPoolParam},
websocket::{server::SendUserRoomMessage, UserOperation},
DbPool,
@@ -26,6 +17,15 @@ use activitystreams::{
object::Note,
};
use actix_web::{client::Client, web, HttpRequest, HttpResponse};
+use lemmy_db::{
+ community::{CommunityFollower, CommunityFollowerForm},
+ naive_now,
+ private_message::{PrivateMessage, PrivateMessageForm},
+ private_message_view::PrivateMessageView,
+ user::User_,
+ Crud,
+ Followable,
+};
use log::debug;
use serde::Deserialize;
use std::fmt::Debug;