activitypub-federation-rust/examples/federation-axum/activities/follow.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

2022-06-02 16:47:12 +05:30
use crate::{
activities::accept::Accept,
generate_object_id,
instance::InstanceHandle,
objects::person::MyUser,
};
2022-06-06 20:06:44 +05:30
use activitypub_federation::{
core::object_id::ObjectId,
data::Data,
traits::{ActivityHandler, Actor},
};
2022-06-02 16:47:12 +05:30
use activitystreams_kinds::activity::FollowType;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Follow {
pub(crate) actor: ObjectId<MyUser>,
pub(crate) object: ObjectId<MyUser>,
#[serde(rename = "type")]
kind: FollowType,
id: Url,
}
impl Follow {
pub fn new(actor: ObjectId<MyUser>, object: ObjectId<MyUser>, id: Url) -> Follow {
Follow {
actor,
object,
kind: Default::default(),
id,
}
}
}
#[async_trait::async_trait]
2022-06-02 16:47:12 +05:30
impl ActivityHandler for Follow {
type DataType = InstanceHandle;
type Error = crate::error::Error;
fn id(&self) -> &Url {
&self.id
}
fn actor(&self) -> &Url {
self.actor.inner()
}
async fn verify(
&self,
_data: &Data<Self::DataType>,
_request_counter: &mut i32,
) -> Result<(), Self::Error> {
Ok(())
}
2022-06-02 19:40:18 +05:30
// Ignore clippy false positive: https://github.com/rust-lang/rust-clippy/issues/6446
#[allow(clippy::await_holding_lock)]
2022-06-02 16:47:12 +05:30
async fn receive(
self,
data: &Data<Self::DataType>,
request_counter: &mut i32,
) -> Result<(), Self::Error> {
// add to followers
let local_user = {
let mut users = data.users.lock().unwrap();
let local_user = users.first_mut().unwrap();
local_user.followers.push(self.actor.inner().clone());
local_user.clone()
};
2022-06-02 16:47:12 +05:30
// send back an accept
let follower = self
.actor
.dereference(data, data.local_instance(), request_counter)
2022-06-02 16:47:12 +05:30
.await?;
let id = generate_object_id(data.local_instance().hostname())?;
let accept = Accept::new(local_user.ap_id.clone(), self, id.clone());
local_user
2022-06-06 20:06:44 +05:30
.send(
accept,
vec![follower.shared_inbox_or_inbox()],
data.local_instance(),
)
2022-06-02 16:47:12 +05:30
.await?;
Ok(())
}
}