move library code to separate crate (most of it)

This commit is contained in:
Felix Ableitner 2021-06-24 03:50:41 +02:00
parent b605057e85
commit dfce4b67ca
10 changed files with 110 additions and 78 deletions

14
Cargo.lock generated
View file

@ -1880,6 +1880,19 @@ dependencies = [
"uuid",
]
[[package]]
name = "lemmy_apub_lib"
version = "0.1.0"
dependencies = [
"activitystreams",
"activitystreams-ext",
"async-trait",
"lemmy_utils",
"lemmy_websocket",
"serde",
"url",
]
[[package]]
name = "lemmy_apub_receive"
version = "0.1.0"
@ -1904,6 +1917,7 @@ dependencies = [
"itertools",
"lemmy_api_common",
"lemmy_apub",
"lemmy_apub_lib",
"lemmy_db_queries",
"lemmy_db_schema",
"lemmy_db_views",

View file

@ -14,6 +14,7 @@ members = [
"crates/api",
"crates/api_crud",
"crates/api_common",
"crates/apub_lib",
"crates/apub",
"crates/apub_receive",
"crates/utils",

View file

@ -0,0 +1,13 @@
[package]
name = "lemmy_apub_lib"
version = "0.1.0"
edition = "2018"
[dependencies]
lemmy_utils = { path = "../utils" }
lemmy_websocket = { path = "../websocket" }
activitystreams = "0.7.0-alpha.11"
activitystreams-ext = "0.1.0-alpha.2"
serde = { version = "1.0.123", features = ["derive"] }
async-trait = "0.1.42"
url = { version = "2.2.1", features = ["serde"] }

View file

@ -0,0 +1,66 @@
use activitystreams::{
error::DomainError,
};
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use std::marker::PhantomData;
use url::Url;
// for now, limit it to activity routing only, no http sigs, parsing or any of that
// need to route in this order:
// 1. recipient actor
// 2. activity type
// 3. inner object (recursively until object is empty or an url)
// TODO: turn this into a trait in which app has to implement the following functions:
// .checkIdValid() - for unique, instance block etc
// .checkHttpSig::<RequestType>()
// .fetchObject() - for custom http client
// .checkActivity() - for common validity checks
pub struct InboxConfig {
//actors: Vec<ActorConfig>,
}
impl InboxConfig {
pub fn shared_inbox_handler() {
todo!()
}
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum PublicUrl {
#[serde(rename = "https://www.w3.org/ns/activitystreams#Public")]
Public,
}
pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> {
if a.domain() != b.domain() {
return Err(DomainError.into());
}
Ok(())
}
// todo: later add a similar trait SendActivity
// todo: maybe add a separate method verify()
#[async_trait::async_trait(?Send)]
pub trait ReceiveActivity {
// todo: later handle request_counter completely inside library
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError>;
}
// todo: instead of phantomdata, might use option<kind> to cache the fetched object (or just fetch on construction)
pub struct ObjectId<'a, Kind>(Url, &'a PhantomData<Kind>);
impl<Kind> ObjectId<'_, Kind> {
pub fn url(self) -> Url {
self.0
}
pub fn dereference(self) -> Result<Kind, LemmyError> {
// todo: fetch object from http or database
todo!()
}
}

View file

@ -5,6 +5,7 @@ edition = "2018"
[dependencies]
lemmy_utils = { path = "../utils" }
lemmy_apub_lib = { path = "../apub_lib" }
lemmy_apub = { path = "../apub" }
lemmy_db_queries = { path = "../db_queries" }
lemmy_db_schema = { path = "../db_schema" }

View file

@ -1,4 +1,3 @@
use crate::inbox::new_inbox_routing::{ReceiveActivity, Activity, PublicUrl};
use url::Url;
use lemmy_apub::NoteExt;
use activitystreams::
@ -15,6 +14,8 @@ use lemmy_utils::utils::scrape_text_for_mentions;
use lemmy_db_schema::source::post::Post;
use lemmy_db_queries::Crud;
use lemmy_apub::fetcher::person::get_or_fetch_and_upsert_person;
use lemmy_apub_lib::{PublicUrl, ReceiveActivity};
use crate::inbox::new_inbox_routing::Activity;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]

View file

@ -1,6 +1,3 @@
use crate::{
inbox::new_inbox_routing::{verify_domains_match, Activity, ReceiveActivity},
};
use activitystreams::{
activity::kind::{AcceptType, FollowType},
};
@ -14,6 +11,8 @@ use lemmy_db_schema::source::community::CommunityFollower;
use lemmy_utils::{LemmyError};
use lemmy_websocket::LemmyContext;
use url::Url;
use lemmy_apub_lib::{ReceiveActivity, verify_domains_match};
use crate::inbox::new_inbox_routing::Activity;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]

View file

@ -1,4 +1,3 @@
use crate::inbox::new_inbox_routing::{Activity, ReceiveActivity};
use activitystreams::activity::kind::CreateType;
use lemmy_api_common::{blocking, person::PrivateMessageResponse};
use lemmy_apub::{objects::FromApub, NoteExt};
@ -7,6 +6,8 @@ use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::Priva
use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperationCrud};
use url::Url;
use lemmy_apub_lib::{ReceiveActivity};
use crate::inbox::new_inbox_routing::Activity;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]

View file

@ -1,79 +1,16 @@
use crate::activities_new::follow::Accept;
use activitystreams::{
base::AnyBase,
error::DomainError,
primitives::OneOrMany,
unparsed::Unparsed,
};
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use std::marker::PhantomData;
use url::Url;
use crate::activities_new::private_message::CreatePrivateMessage;
use crate::activities_new::comment::CreateComment;
use lemmy_apub_lib::ReceiveActivity;
use activitystreams::primitives::OneOrMany;
use activitystreams::base::AnyBase;
use url::Url;
use activitystreams::unparsed::Unparsed;
// for now, limit it to activity routing only, no http sigs, parsing or any of that
// need to route in this order:
// 1. recipient actor
// 2. activity type
// 3. inner object (recursively until object is empty or an url)
// library part
// todo: move this to separate crate
// TODO: turn this into a trait in which app has to implement the following functions:
// .checkIdValid() - for unique, instance block etc
// .checkHttpSig::<RequestType>()
// .fetchObject() - for custom http client
// .checkActivity() - for common validity checks
pub struct InboxConfig {
//actors: Vec<ActorConfig>,
}
impl InboxConfig {
pub fn shared_inbox_handler() {
todo!()
}
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum PublicUrl {
#[serde(rename = "https://www.w3.org/ns/activitystreams#Public")]
Public,
}
pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), LemmyError> {
if a.domain() != b.domain() {
return Err(DomainError.into());
}
Ok(())
}
// todo: later add a similar trait SendActivity
// todo: maybe add a separate method verify()
#[async_trait::async_trait(?Send)]
pub trait ReceiveActivity {
// todo: later handle request_counter completely inside library
async fn receive(
&self,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError>;
}
// todo: instead of phantomdata, might use option<kind> to cache the fetched object (or just fetch on construction)
pub struct ObjectId<'a, Kind>(Url, &'a PhantomData<Kind>);
impl<Kind> ObjectId<'_, Kind> {
pub fn url(self) -> Url {
self.0
}
pub fn dereference(self) -> Result<Kind, LemmyError> {
// todo: fetch object from http or database
todo!()
}
}
// TODO: would be nice if we could move this to lemmy_apub_lib crate. doing that gives error:
// "only traits defined in the current crate can be implemented for arbitrary types"
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Activity<Kind> {
@ -97,8 +34,6 @@ impl<Kind> Activity<Kind> {
}
}
// application part
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum PersonAcceptedActivitiesNew {
Accept(Accept),

View file

@ -19,7 +19,6 @@ use crate::{
is_activity_already_known,
is_addressed_to_community_followers,
is_addressed_to_local_person,
new_inbox_routing::{Activity, PersonAcceptedActivitiesNew, ReceiveActivity},
receive_for_community::{
receive_add_for_community,
receive_block_user_for_community,
@ -61,6 +60,8 @@ use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use strum_macros::EnumString;
use url::Url;
use crate::inbox::new_inbox_routing::{PersonAcceptedActivitiesNew, Activity};
use lemmy_apub_lib::ReceiveActivity;
/// Allowed activities for person inbox.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]