summaryrefslogtreecommitdiffstats
path: root/server/src/apub/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/apub/mod.rs')
-rw-r--r--server/src/apub/mod.rs78
1 files changed, 30 insertions, 48 deletions
diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs
index 90df8734..eeac5fec 100644
--- a/server/src/apub/mod.rs
+++ b/server/src/apub/mod.rs
@@ -16,41 +16,35 @@ 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::{
- actor::{properties::ApActorProperties, Group, Person},
- object::Page,
+use activitystreams::object::Page;
+use activitystreams_ext::{Ext1, Ext2};
+use activitystreams_new::{
+ activity::Follow,
+ actor::{ApActor, Group, Person},
+ object::Tombstone,
+ prelude::*,
};
-use activitystreams_ext::{Ext1, Ext2, Ext3};
-use activitystreams_new::{activity::Follow, object::Tombstone, prelude::*};
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;
-type GroupExt = Ext3<Group, GroupExtension, ApActorProperties, PublicKeyExtension>;
-type PersonExt = Ext2<Person, ApActorProperties, PublicKeyExtension>;
+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";
-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>
@@ -71,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);
@@ -163,7 +129,7 @@ fn create_tombstone(
pub trait FromApub {
type ApubType;
async fn from_apub(
- apub: &Self::ApubType,
+ apub: &mut Self::ApubType,
client: &Client,
pool: &DbPool,
) -> Result<Self, LemmyError>
@@ -372,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(())
+}