Move apub to separate workspace

This commit is contained in:
Felix Ableitner 2020-09-23 16:39:30 +02:00
parent 49e481e3ac
commit 8b6860881c
38 changed files with 597 additions and 160 deletions

47
Cargo.lock generated
View file

@ -1787,6 +1787,48 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lemmy_apub"
version = "0.1.0"
dependencies = [
"activitystreams",
"activitystreams-ext",
"actix",
"actix-rt",
"actix-web",
"anyhow",
"async-trait",
"awc",
"background-jobs",
"base64 0.12.3",
"bcrypt",
"chrono",
"diesel",
"futures",
"http",
"http-signature-normalization-actix",
"itertools",
"lazy_static",
"lemmy_db",
"lemmy_structs",
"lemmy_utils",
"lemmy_websocket",
"log",
"openssl",
"percent-encoding",
"rand 0.7.3",
"reqwest",
"serde 1.0.116",
"serde_json",
"sha2",
"strum",
"strum_macros",
"thiserror",
"tokio",
"url",
"uuid 0.8.1",
]
[[package]] [[package]]
name = "lemmy_db" name = "lemmy_db"
version = "0.1.0" version = "0.1.0"
@ -1822,8 +1864,6 @@ dependencies = [
name = "lemmy_server" name = "lemmy_server"
version = "0.0.1" version = "0.0.1"
dependencies = [ dependencies = [
"activitystreams",
"activitystreams-ext",
"actix", "actix",
"actix-files", "actix-files",
"actix-rt", "actix-rt",
@ -1847,6 +1887,7 @@ dependencies = [
"itertools", "itertools",
"jsonwebtoken", "jsonwebtoken",
"lazy_static", "lazy_static",
"lemmy_apub",
"lemmy_db", "lemmy_db",
"lemmy_rate_limit", "lemmy_rate_limit",
"lemmy_structs", "lemmy_structs",
@ -1873,7 +1914,6 @@ dependencies = [
name = "lemmy_structs" name = "lemmy_structs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix",
"actix-web", "actix-web",
"chrono", "chrono",
"diesel", "diesel",
@ -1901,6 +1941,7 @@ dependencies = [
"openssl", "openssl",
"rand 0.7.3", "rand 0.7.3",
"regex", "regex",
"reqwest",
"serde 1.0.116", "serde 1.0.116",
"serde_json", "serde_json",
"thiserror", "thiserror",

View file

@ -8,6 +8,7 @@ lto = true
[workspace] [workspace]
members = [ members = [
"lemmy_apub",
"lemmy_utils", "lemmy_utils",
"lemmy_db", "lemmy_db",
"lemmy_structs", "lemmy_structs",
@ -21,10 +22,9 @@ lemmy_db = { path = "./lemmy_db" }
lemmy_structs = { path = "./lemmy_structs" } lemmy_structs = { path = "./lemmy_structs" }
lemmy_rate_limit = { path = "./lemmy_rate_limit" } lemmy_rate_limit = { path = "./lemmy_rate_limit" }
lemmy_websocket = { path = "./lemmy_websocket" } lemmy_websocket = { path = "./lemmy_websocket" }
lemmy_apub = { path = "./lemmy_apub" }
diesel = "1.4" diesel = "1.4"
diesel_migrations = "1.4" diesel_migrations = "1.4"
activitystreams = "0.7.0-alpha.4"
activitystreams-ext = "0.1.0-alpha.2"
bcrypt = "0.8" bcrypt = "0.8"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
serde_json = { version = "1.0", features = ["preserve_order"]} serde_json = { version = "1.0", features = ["preserve_order"]}

43
lemmy_apub/Cargo.toml Normal file
View file

@ -0,0 +1,43 @@
[package]
name = "lemmy_apub"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
[dependencies]
lemmy_utils = { path = "../lemmy_utils" }
lemmy_db = { path = "../lemmy_db" }
lemmy_structs = { path = "../lemmy_structs" }
lemmy_websocket = { path = "../lemmy_websocket" }
diesel = "1.4"
activitystreams = "0.7.0-alpha.4"
activitystreams-ext = "0.1.0-alpha.2"
bcrypt = "0.8"
chrono = { version = "0.4", features = ["serde"] }
serde_json = { version = "1.0", features = ["preserve_order"]}
serde = { version = "1.0", features = ["derive"] }
actix = "0.10"
actix-web = { version = "3.0", default-features = false }
actix-rt = { version = "1.1", default-features = false }
awc = { version = "2.0", default-features = false }
log = "0.4"
rand = "0.7"
strum = "0.19"
strum_macros = "0.19"
lazy_static = "1.3"
url = { version = "2.1", features = ["serde"] }
percent-encoding = "2.1"
openssl = "0.10"
http = "0.2"
http-signature-normalization-actix = { version = "0.4", default-features = false, features = ["sha-2"] }
base64 = "0.12"
tokio = "0.2"
futures = "0.3"
itertools = "0.9"
uuid = { version = "0.8", features = ["serde", "v4"] }
sha2 = "0.9"
async-trait = "0.1"
anyhow = "1.0"
thiserror = "1.0"
background-jobs = " 0.8"
reqwest = { version = "0.10", features = ["json"] }

View file

@ -1,4 +1,4 @@
use crate::apub::{activity_queue::send_activity, community::do_announce, insert_activity}; use crate::{activity_queue::send_activity, community::do_announce, insert_activity};
use activitystreams::{ use activitystreams::{
base::{Extends, ExtendsExt}, base::{Extends, ExtendsExt},
object::AsObject, object::AsObject,
@ -35,7 +35,7 @@ where
Ok(()) Ok(())
} }
pub(in crate::apub) fn generate_activity_id<T>(kind: T) -> Result<Url, ParseError> pub(in crate) fn generate_activity_id<T>(kind: T) -> Result<Url, ParseError>
where where
T: ToString, T: ToString,
{ {

View file

@ -1,4 +1,4 @@
use crate::apub::{check_is_apub_id_valid, extensions::signatures::sign, ActorType}; use crate::{check_is_apub_id_valid, extensions::signatures::sign, ActorType};
use activitystreams::{ use activitystreams::{
base::{Extends, ExtendsExt}, base::{Extends, ExtendsExt},
object::AsObject, object::AsObject,

View file

@ -1,23 +1,20 @@
use crate::{ use crate::{
apub::{ activities::{generate_activity_id, send_activity_to_community},
activities::{generate_activity_id, send_activity_to_community}, check_actor_domain,
check_actor_domain, create_apub_response,
create_apub_response, create_apub_tombstone_response,
create_apub_tombstone_response, create_tombstone,
create_tombstone, fetch_webfinger_url,
fetch_webfinger_url, fetcher::{
fetcher::{ get_or_fetch_and_insert_comment,
get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post,
get_or_fetch_and_insert_post, get_or_fetch_and_upsert_user,
get_or_fetch_and_upsert_user,
},
ActorType,
ApubLikeableType,
ApubObjectType,
FromApub,
ToApub,
}, },
DbPool, ActorType,
ApubLikeableType,
ApubObjectType,
FromApub,
ToApub,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -45,6 +42,7 @@ use lemmy_db::{
post::Post, post::Post,
user::User_, user::User_,
Crud, Crud,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -1,20 +1,17 @@
use crate::{ use crate::{
apub::{ activities::generate_activity_id,
activities::generate_activity_id, activity_queue::send_activity,
activity_queue::send_activity, check_actor_domain,
check_actor_domain, create_apub_response,
create_apub_response, create_apub_tombstone_response,
create_apub_tombstone_response, create_tombstone,
create_tombstone, extensions::group_extensions::GroupExtension,
extensions::group_extensions::GroupExtension, fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_user},
fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_user}, insert_activity,
insert_activity, ActorType,
ActorType, FromApub,
FromApub, GroupExt,
GroupExt, ToApub,
ToApub,
},
DbPool,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -43,6 +40,7 @@ use lemmy_db::{
naive_now, naive_now,
post::Post, post::Post,
user::User_, user::User_,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -1,4 +1,4 @@
use crate::apub::ActorType; use crate::ActorType;
use activitystreams::unparsed::UnparsedMutExt; use activitystreams::unparsed::UnparsedMutExt;
use activitystreams_ext::UnparsedExtension; use activitystreams_ext::UnparsedExtension;
use actix_web::{client::ClientRequest, HttpRequest}; use actix_web::{client::ClientRequest, HttpRequest};

View file

@ -1,14 +1,11 @@
use crate::{ use crate::{
apub::{ check_is_apub_id_valid,
check_is_apub_id_valid, ActorType,
ActorType, FromApub,
FromApub, GroupExt,
GroupExt, PageExt,
PageExt, PersonExt,
PersonExt, APUB_JSON_CONTENT_TYPE,
APUB_JSON_CONTENT_TYPE,
},
request::{retry, RecvError},
}; };
use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*}; use activitystreams::{base::BaseExt, collection::OrderedCollection, object::Note, prelude::*};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
@ -29,7 +26,12 @@ use lemmy_db::{
SearchType, SearchType,
}; };
use lemmy_structs::{blocking, site::SearchResponse}; use lemmy_structs::{blocking, site::SearchResponse};
use lemmy_utils::{apub::get_apub_protocol_string, location_info, LemmyError}; use lemmy_utils::{
apub::get_apub_protocol_string,
location_info,
request::{retry, RecvError},
LemmyError,
};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug; use log::debug;
use reqwest::Client; use reqwest::Client;

View file

@ -1,4 +1,4 @@
use crate::apub::inbox::{ use crate::inbox::{
activities::{ activities::{
create::receive_create, create::receive_create,
delete::receive_delete, delete::receive_delete,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,
get_user_from_activity, get_user_from_activity,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post}, fetcher::{get_or_fetch_and_insert_comment, get_or_fetch_and_insert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
check_is_apub_id_valid, check_is_apub_id_valid,
extensions::signatures::verify, extensions::signatures::verify,
fetcher::get_or_fetch_and_upsert_user, fetcher::get_or_fetch_and_upsert_user,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
check_is_apub_id_valid, check_is_apub_id_valid,
community::do_announce, community::do_announce,
extensions::signatures::verify, extensions::signatures::verify,
@ -95,7 +95,7 @@ pub async fn shared_inbox(
res res
} }
pub(in crate::apub::inbox) fn receive_unhandled_activity<A>( pub(in crate::inbox) fn receive_unhandled_activity<A>(
activity: A, activity: A,
) -> Result<HttpResponse, LemmyError> ) -> Result<HttpResponse, LemmyError>
where where
@ -105,7 +105,7 @@ where
Ok(HttpResponse::NotImplemented().finish()) Ok(HttpResponse::NotImplemented().finish())
} }
pub(in crate::apub::inbox) async fn get_user_from_activity<T, A>( pub(in crate::inbox) async fn get_user_from_activity<T, A>(
activity: &T, activity: &T,
context: &LemmyContext, context: &LemmyContext,
) -> Result<User_, LemmyError> ) -> Result<User_, LemmyError>
@ -117,7 +117,7 @@ where
get_or_fetch_and_upsert_user(&user_uri, context).await get_or_fetch_and_upsert_user(&user_uri, context).await
} }
pub(in crate::apub::inbox) fn get_community_id_from_activity<T, A>( pub(in crate::inbox) fn get_community_id_from_activity<T, A>(
activity: &T, activity: &T,
) -> Result<Url, LemmyError> ) -> Result<Url, LemmyError>
where where
@ -134,7 +134,7 @@ where
) )
} }
pub(in crate::apub::inbox) async fn announce_if_community_is_local<T, Kind>( pub(in crate::inbox) async fn announce_if_community_is_local<T, Kind>(
activity: T, activity: T,
user: &User_, user: &User_,
context: &LemmyContext, context: &LemmyContext,

View file

@ -1,4 +1,4 @@
use crate::apub::{ use crate::{
check_is_apub_id_valid, check_is_apub_id_valid,
extensions::signatures::verify, extensions::signatures::verify,
fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community}, fetcher::{get_or_fetch_and_upsert_actor, get_or_fetch_and_upsert_community},

363
lemmy_apub/src/lib.rs Normal file
View file

@ -0,0 +1,363 @@
#[macro_use]
extern crate lazy_static;
pub mod activities;
pub mod activity_queue;
pub mod comment;
pub mod community;
pub mod extensions;
pub mod fetcher;
pub mod inbox;
pub mod post;
pub mod private_message;
pub mod user;
use crate::extensions::{
group_extensions::GroupExtension,
page_extension::PageExtension,
signatures::{PublicKey, PublicKeyExtension},
};
use activitystreams::{
activity::Follow,
actor::{ApActor, Group, Person},
base::AsBase,
markers::Base,
object::{Page, Tombstone},
prelude::*,
};
use activitystreams_ext::{Ext1, Ext2};
use actix_web::{body::Body, HttpResponse};
use anyhow::{anyhow, Context};
use chrono::NaiveDateTime;
use lemmy_db::{activity::do_insert_activity, user::User_, DbPool};
use lemmy_structs::{blocking, WebFingerResponse};
use lemmy_utils::{
apub::get_apub_protocol_string,
location_info,
request::{retry, RecvError},
settings::Settings,
utils::{convert_datetime, MentionData},
LemmyError,
};
use lemmy_websocket::LemmyContext;
use log::debug;
use reqwest::Client;
use serde::Serialize;
use url::{ParseError, Url};
type GroupExt = Ext2<ApActor<Group>, GroupExtension, PublicKeyExtension>;
type PersonExt = Ext1<ApActor<Person>, PublicKeyExtension>;
type PageExt = Ext1<Page, PageExtension>;
pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
/// 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>
where
T: Serialize,
{
HttpResponse::Ok()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
}
fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
where
T: Serialize,
{
HttpResponse::Gone()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
}
// Checks if the ID has a valid format, correct scheme, and is in the allowed instance list.
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
.hostname
.split(':')
.collect::<Vec<&str>>()
.first()
.context(location_info!())?
.to_string();
if !settings.federation.enabled {
return if domain == local_instance {
Ok(())
} else {
Err(
anyhow!(
"Trying to connect with {}, but federation is disabled",
domain
)
.into(),
)
};
}
if apub_id.scheme() != get_apub_protocol_string() {
return Err(anyhow!("invalid apub id scheme: {:?}", apub_id.scheme()).into());
}
let mut allowed_instances = Settings::get().get_allowed_instances();
let blocked_instances = Settings::get().get_blocked_instances();
if !allowed_instances.is_empty() {
// need to allow this explicitly because apub activities might contain objects from our local
// instance. split is needed to remove the port in our federation test setup.
allowed_instances.push(local_instance);
if allowed_instances.contains(&domain) {
Ok(())
} else {
Err(anyhow!("{} not in federation allowlist", domain).into())
}
} else if !blocked_instances.is_empty() {
if blocked_instances.contains(&domain) {
Err(anyhow!("{} is in federation blocklist", domain).into())
} else {
Ok(())
}
} else {
panic!("Invalid config, both allowed_instances and blocked_instances are specified");
}
}
#[async_trait::async_trait(?Send)]
pub trait ToApub {
type Response;
async fn to_apub(&self, pool: &DbPool) -> Result<Self::Response, LemmyError>;
fn to_tombstone(&self) -> Result<Tombstone, LemmyError>;
}
/// Updated is actually the deletion time
fn create_tombstone<T>(
deleted: bool,
object_id: &str,
updated: Option<NaiveDateTime>,
former_type: T,
) -> Result<Tombstone, LemmyError>
where
T: ToString,
{
if deleted {
if let Some(updated) = updated {
let mut tombstone = Tombstone::new();
tombstone.set_id(object_id.parse()?);
tombstone.set_former_type(former_type.to_string());
tombstone.set_deleted(convert_datetime(updated));
Ok(tombstone)
} else {
Err(anyhow!("Cant convert to tombstone because updated time was None.").into())
}
} else {
Err(anyhow!("Cant convert object to tombstone if it wasnt deleted").into())
}
}
#[async_trait::async_trait(?Send)]
pub trait FromApub {
type ApubType;
/// Converts an object from ActivityPub type to Lemmy internal type.
///
/// * `apub` The object to read from
/// * `context` LemmyContext which holds DB pool, HTTP client etc
/// * `expected_domain` If present, ensure that the apub object comes from the same domain as
/// this URL
///
async fn from_apub(
apub: &Self::ApubType,
context: &LemmyContext,
expected_domain: Option<Url>,
) -> Result<Self, LemmyError>
where
Self: Sized;
}
#[async_trait::async_trait(?Send)]
pub trait ApubObjectType {
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
}
pub(in crate) fn check_actor_domain<T, Kind>(
apub: &T,
expected_domain: Option<Url>,
) -> Result<String, LemmyError>
where
T: Base + AsBase<Kind>,
{
let actor_id = if let Some(url) = expected_domain {
let domain = url.domain().context(location_info!())?;
apub.id(domain)?.context(location_info!())?
} else {
let actor_id = apub.id_unchecked().context(location_info!())?;
check_is_apub_id_valid(&actor_id)?;
actor_id
};
Ok(actor_id.to_string())
}
#[async_trait::async_trait(?Send)]
pub trait ApubLikeableType {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_like(&self, creator: &User_, context: &LemmyContext)
-> Result<(), LemmyError>;
}
#[async_trait::async_trait(?Send)]
pub trait ActorType {
fn actor_id_str(&self) -> String;
// TODO: every actor should have a public key, so this shouldnt be an option (needs to be fixed in db)
fn public_key(&self) -> Option<String>;
fn private_key(&self) -> Option<String>;
/// numeric id in the database, used for insert_activity
fn user_id(&self) -> i32;
// These two have default impls, since currently a community can't follow anything,
// and a user can't be followed (yet)
#[allow(unused_variables)]
async fn send_follow(
&self,
follow_actor_id: &Url,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_unfollow(
&self,
follow_actor_id: &Url,
context: &LemmyContext,
) -> Result<(), LemmyError>;
#[allow(unused_variables)]
async fn send_accept_follow(
&self,
follow: Follow,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
/// For a given community, returns the inboxes of all followers.
async fn get_follower_inboxes(&self, pool: &DbPool) -> Result<Vec<Url>, LemmyError>;
fn actor_id(&self) -> Result<Url, ParseError> {
Url::parse(&self.actor_id_str())
}
// TODO move these to the db rows
fn get_inbox_url(&self) -> Result<Url, ParseError> {
Url::parse(&format!("{}/inbox", &self.actor_id_str()))
}
fn get_shared_inbox_url(&self) -> Result<Url, LemmyError> {
let actor_id = self.actor_id()?;
let url = format!(
"{}://{}{}/inbox",
&actor_id.scheme(),
&actor_id.host_str().context(location_info!())?,
if let Some(port) = actor_id.port() {
format!(":{}", port)
} else {
"".to_string()
},
);
Ok(Url::parse(&url)?)
}
fn get_outbox_url(&self) -> Result<Url, ParseError> {
Url::parse(&format!("{}/outbox", &self.actor_id_str()))
}
fn get_followers_url(&self) -> Result<Url, ParseError> {
Url::parse(&format!("{}/followers", &self.actor_id_str()))
}
fn get_following_url(&self) -> String {
format!("{}/following", &self.actor_id_str())
}
fn get_liked_url(&self) -> String {
format!("{}/liked", &self.actor_id_str())
}
fn get_public_key_ext(&self) -> Result<PublicKeyExtension, LemmyError> {
Ok(
PublicKey {
id: format!("{}#main-key", self.actor_id_str()),
owner: self.actor_id_str(),
public_key_pem: self.public_key().context(location_info!())?,
}
.to_ext(),
)
}
}
pub async fn fetch_webfinger_url(
mention: &MentionData,
client: &Client,
) -> Result<Url, LemmyError> {
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
get_apub_protocol_string(),
mention.domain,
mention.name,
mention.domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| client.get(&fetch_url).send()).await?;
let res: WebFingerResponse = response
.json()
.await
.map_err(|e| RecvError(e.to_string()))?;
let link = res
.links
.iter()
.find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
.ok_or_else(|| anyhow!("No application/activity+json link found."))?;
link
.href
.to_owned()
.map(|u| Url::parse(&u))
.transpose()?
.ok_or_else(|| anyhow!("No href found.").into())
}
pub async fn insert_activity<T>(
user_id: i32,
data: T,
local: bool,
pool: &DbPool,
) -> Result<(), LemmyError>
where
T: Serialize + std::fmt::Debug + Send + 'static,
{
blocking(pool, move |conn| {
do_insert_activity(conn, user_id, &data, local)
})
.await??;
Ok(())
}

View file

@ -1,20 +1,17 @@
use crate::{ use crate::{
apub::{ activities::{generate_activity_id, send_activity_to_community},
activities::{generate_activity_id, send_activity_to_community}, check_actor_domain,
check_actor_domain, create_apub_response,
create_apub_response, create_apub_tombstone_response,
create_apub_tombstone_response, create_tombstone,
create_tombstone, extensions::page_extension::PageExtension,
extensions::page_extension::PageExtension, fetcher::{get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user},
fetcher::{get_or_fetch_and_upsert_community, get_or_fetch_and_upsert_user}, ActorType,
ActorType, ApubLikeableType,
ApubLikeableType, ApubObjectType,
ApubObjectType, FromApub,
FromApub, PageExt,
PageExt, ToApub,
ToApub,
},
DbPool,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -39,6 +36,7 @@ use lemmy_db::{
post::{Post, PostForm}, post::{Post, PostForm},
user::User_, user::User_,
Crud, Crud,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -1,18 +1,15 @@
use crate::{ use crate::{
apub::{ activities::generate_activity_id,
activities::generate_activity_id, activity_queue::send_activity,
activity_queue::send_activity, check_actor_domain,
check_actor_domain, check_is_apub_id_valid,
check_is_apub_id_valid, create_tombstone,
create_tombstone, fetcher::get_or_fetch_and_upsert_user,
fetcher::get_or_fetch_and_upsert_user, insert_activity,
insert_activity, ActorType,
ActorType, ApubObjectType,
ApubObjectType, FromApub,
FromApub, ToApub,
ToApub,
},
DbPool,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -30,6 +27,7 @@ use lemmy_db::{
private_message::{PrivateMessage, PrivateMessageForm}, private_message::{PrivateMessage, PrivateMessageForm},
user::User_, user::User_,
Crud, Crud,
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError}; use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};

View file

@ -1,17 +1,14 @@
use crate::{ use crate::{
apub::{ activities::generate_activity_id,
activities::generate_activity_id, activity_queue::send_activity,
activity_queue::send_activity, check_actor_domain,
check_actor_domain, create_apub_response,
create_apub_response, fetcher::get_or_fetch_and_upsert_actor,
fetcher::get_or_fetch_and_upsert_actor, insert_activity,
insert_activity, ActorType,
ActorType, FromApub,
FromApub, PersonExt,
PersonExt, ToApub,
ToApub,
},
DbPool,
}; };
use activitystreams::{ use activitystreams::{
activity::{ activity::{
@ -29,6 +26,7 @@ use anyhow::Context;
use lemmy_db::{ use lemmy_db::{
naive_now, naive_now,
user::{UserForm, User_}, user::{UserForm, User_},
DbPool,
}; };
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{ use lemmy_utils::{

View file

@ -14,7 +14,6 @@ lemmy_utils = { path = "../lemmy_utils" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
log = "0.4" log = "0.4"
diesel = "1.4" diesel = "1.4"
actix = "0.10"
actix-web = { version = "3.0" } actix-web = { version = "3.0" }
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
serde_json = { version = "1.0", features = ["preserve_order"]} serde_json = { version = "1.0", features = ["preserve_order"]}

View file

@ -16,6 +16,24 @@ use lemmy_db::{
}; };
use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError}; use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
use log::error; use log::error;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerResponse {
pub subject: String,
pub aliases: Vec<String>,
pub links: Vec<WebFingerLink>,
}
pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError> pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
where where

View file

@ -27,3 +27,4 @@ openssl = "0.10"
url = { version = "2.1", features = ["serde"] } url = { version = "2.1", features = ["serde"] }
actix-web = {version = "3.0", default-features = false } actix-web = {version = "3.0", default-features = false }
anyhow = "1.0" anyhow = "1.0"
reqwest = { version = "0.10", features = ["json"] }

View file

@ -3,6 +3,7 @@ extern crate lazy_static;
pub mod apub; pub mod apub;
pub mod email; pub mod email;
pub mod request;
pub mod settings; pub mod settings;
#[cfg(test)] #[cfg(test)]
mod test; mod test;

View file

@ -1,5 +1,5 @@
use crate::LemmyError;
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_utils::LemmyError;
use std::future::Future; use std::future::Future;
use thiserror::Error; use thiserror::Error;

View file

@ -1,15 +1,13 @@
use crate::{ use crate::api::{
api::{ check_community_ban,
check_community_ban, get_post,
get_post, get_user_from_jwt,
get_user_from_jwt, get_user_from_jwt_opt,
get_user_from_jwt_opt, is_mod_or_admin,
is_mod_or_admin, Perform,
Perform,
},
apub::{ApubLikeableType, ApubObjectType},
}; };
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_apub::{ApubLikeableType, ApubObjectType};
use lemmy_db::{ use lemmy_db::{
comment::*, comment::*,
comment_view::*, comment_view::*,

View file

@ -1,9 +1,7 @@
use crate::{ use crate::api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, is_mod_or_admin, Perform};
api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, is_mod_or_admin, Perform},
apub::ActorType,
};
use actix_web::web::Data; use actix_web::web::Data;
use anyhow::Context; use anyhow::Context;
use lemmy_apub::ActorType;
use lemmy_db::{ use lemmy_db::{
comment::Comment, comment::Comment,
comment_view::CommentQueryBuilder, comment_view::CommentQueryBuilder,

View file

@ -1,9 +1,9 @@
use crate::{ use crate::{
api::{check_community_ban, get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, Perform}, api::{check_community_ban, get_user_from_jwt, get_user_from_jwt_opt, is_mod_or_admin, Perform},
apub::{ApubLikeableType, ApubObjectType},
fetch_iframely_and_pictrs_data, fetch_iframely_and_pictrs_data,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_apub::{ApubLikeableType, ApubObjectType};
use lemmy_db::{ use lemmy_db::{
comment_view::*, comment_view::*,
community_view::*, community_view::*,

View file

@ -1,10 +1,10 @@
use crate::{ use crate::{
api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, api::{get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform},
apub::fetcher::search_by_apub_id,
version, version,
}; };
use actix_web::web::Data; use actix_web::web::Data;
use anyhow::Context; use anyhow::Context;
use lemmy_apub::fetcher::search_by_apub_id;
use lemmy_db::{ use lemmy_db::{
category::*, category::*,
comment_view::*, comment_view::*,

View file

@ -1,6 +1,5 @@
use crate::{ use crate::{
api::{claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform}, api::{claims::Claims, get_user_from_jwt, get_user_from_jwt_opt, is_admin, Perform},
apub::ApubObjectType,
captcha_espeak_wav_base64, captcha_espeak_wav_base64,
}; };
use actix_web::web::Data; use actix_web::web::Data;
@ -8,6 +7,7 @@ use anyhow::Context;
use bcrypt::verify; use bcrypt::verify;
use captcha::{gen, Difficulty}; use captcha::{gen, Difficulty};
use chrono::Duration; use chrono::Duration;
use lemmy_apub::ApubObjectType;
use lemmy_db::{ use lemmy_db::{
comment::*, comment::*,
comment_view::*, comment_view::*,

View file

@ -1,18 +1,18 @@
#![recursion_limit = "512"] #![recursion_limit = "512"]
#[macro_use]
extern crate lazy_static;
pub mod api; pub mod api;
pub mod apub;
pub mod code_migrations; pub mod code_migrations;
pub mod request;
pub mod routes; pub mod routes;
pub mod version; pub mod version;
use crate::request::{retry, RecvError};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_db::DbPool; use lemmy_db::DbPool;
use lemmy_utils::{apub::get_apub_protocol_string, settings::Settings, LemmyError}; use lemmy_utils::{
apub::get_apub_protocol_string,
request::{retry, RecvError},
settings::Settings,
LemmyError,
};
use log::error; use log::error;
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use reqwest::Client; use reqwest::Client;

View file

@ -16,11 +16,11 @@ use diesel::{
PgConnection, PgConnection,
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
use lemmy_apub::activity_queue::create_activity_queue;
use lemmy_db::get_database_url_from_env; use lemmy_db::get_database_url_from_env;
use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit}; use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit};
use lemmy_server::{ use lemmy_server::{
api::match_websocket_operation, api::match_websocket_operation,
apub::activity_queue::create_activity_queue,
code_migrations::run_advanced_migrations, code_migrations::run_advanced_migrations,
routes::*, routes::*,
}; };

View file

@ -1,4 +1,6 @@
use crate::apub::{ use actix_web::*;
use http_signature_normalization_actix::digest::middleware::VerifyDigest;
use lemmy_apub::{
comment::get_apub_comment, comment::get_apub_comment,
community::*, community::*,
inbox::{community_inbox::community_inbox, shared_inbox::shared_inbox, user_inbox::user_inbox}, inbox::{community_inbox::community_inbox, shared_inbox::shared_inbox, user_inbox::user_inbox},
@ -6,8 +8,6 @@ use crate::apub::{
user::*, user::*,
APUB_JSON_CONTENT_TYPE, APUB_JSON_CONTENT_TYPE,
}; };
use actix_web::*;
use http_signature_normalization_actix::digest::middleware::VerifyDigest;
use lemmy_utils::settings::Settings; use lemmy_utils::settings::Settings;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};

View file

@ -1,7 +1,7 @@
use actix_web::{error::ErrorBadRequest, web::Query, *}; use actix_web::{error::ErrorBadRequest, web::Query, *};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_db::{community::Community, user::User_}; use lemmy_db::{community::Community, user::User_};
use lemmy_structs::blocking; use lemmy_structs::{blocking, WebFingerLink, WebFingerResponse};
use lemmy_utils::{ use lemmy_utils::{
settings::Settings, settings::Settings,
LemmyError, LemmyError,
@ -9,30 +9,13 @@ use lemmy_utils::{
WEBFINGER_USER_REGEX, WEBFINGER_USER_REGEX,
}; };
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize}; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Params { pub struct Params {
resource: String, resource: String,
} }
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerResponse {
pub subject: String,
pub aliases: Vec<String>,
pub links: Vec<WebFingerLink>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
pub fn config(cfg: &mut web::ServiceConfig) { pub fn config(cfg: &mut web::ServiceConfig) {
if Settings::get().federation.enabled { if Settings::get().federation.enabled {
cfg.route( cfg.route(