diff options
Diffstat (limited to 'src/entity')
-rw-r--r-- | src/entity/user.rs | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/entity/user.rs b/src/entity/user.rs index 47a761a..1abf50f 100644 --- a/src/entity/user.rs +++ b/src/entity/user.rs @@ -3,12 +3,14 @@ pub mod followers; use std::fmt::Display; use activitypub_federation::{ + activity_queue::queue_activity, + activity_sending::SendActivityTask, config::Data, fetch::object_id::ObjectId, http_signatures::generate_actor_keypair, kinds::actor::{ApplicationType, GroupType, OrganizationType, PersonType, ServiceType}, - protocol::public_key::PublicKey, - traits::{Actor, Object}, + protocol::{context::WithContext, public_key::PublicKey}, + traits::{Activity, Actor, Object}, }; use async_trait::async_trait; use serde::{Deserialize, Serialize}; @@ -21,7 +23,7 @@ use uuid::Uuid; use crate::{error::AppError, state::AppHandle}; #[derive(PartialEq, Clone, Debug)] -pub(crate) struct User { +pub struct User { pub id: String, pub username: String, pub ap_id: ObjectId<User>, @@ -166,6 +168,30 @@ impl User { ).fetch_one(&services.postgres).await?; Self::try_from(user) } + + pub(crate) async fn send<A>( + &self, + activity: A, + recipients: Vec<Url>, + use_queue: bool, + data: &Data<AppHandle>, + ) -> Result<(), AppError> + where + A: Activity + Serialize + std::fmt::Debug + Send + Sync, + <A as Activity>::Error: From<anyhow::Error> + From<serde_json::Error>, + { + let activity = WithContext::new_default(activity); + // Send through queue in some cases and bypass it in others to test both code paths + if use_queue { + queue_activity(&activity, self, recipients, data).await?; + } else { + let sends = SendActivityTask::prepare(&activity, self, recipients, data).await?; + for send in sends { + send.sign_and_send(data).await?; + } + } + Ok(()) + } } #[derive(Clone, Debug, Deserialize, Serialize)] |