2022-06-02 16:47:12 +05:30
|
|
|
use crate::{
|
|
|
|
activities::accept::Accept,
|
|
|
|
generate_object_id,
|
2023-02-11 18:02:35 +05:30
|
|
|
instance::DatabaseHandle,
|
2023-02-19 17:56:01 +05:30
|
|
|
objects::person::DbUser,
|
2022-06-02 16:47:12 +05:30
|
|
|
};
|
2022-06-06 20:06:44 +05:30
|
|
|
use activitypub_federation::{
|
2023-03-16 06:41:48 +05:30
|
|
|
config::Data,
|
2023-03-06 06:47:34 +05:30
|
|
|
fetch::object_id::ObjectId,
|
2023-02-19 17:56:01 +05:30
|
|
|
kinds::activity::FollowType,
|
2022-06-06 20:06:44 +05:30
|
|
|
traits::{ActivityHandler, Actor},
|
|
|
|
};
|
2022-06-02 16:47:12 +05:30
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Clone, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Follow {
|
2023-02-19 17:56:01 +05:30
|
|
|
pub(crate) actor: ObjectId<DbUser>,
|
|
|
|
pub(crate) object: ObjectId<DbUser>,
|
2022-06-02 16:47:12 +05:30
|
|
|
#[serde(rename = "type")]
|
|
|
|
kind: FollowType,
|
|
|
|
id: Url,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Follow {
|
2023-02-19 17:56:01 +05:30
|
|
|
pub fn new(actor: ObjectId<DbUser>, object: ObjectId<DbUser>, id: Url) -> Follow {
|
2022-06-02 16:47:12 +05:30
|
|
|
Follow {
|
|
|
|
actor,
|
|
|
|
object,
|
|
|
|
kind: Default::default(),
|
|
|
|
id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:49:56 +05:30
|
|
|
#[async_trait::async_trait]
|
2022-06-02 16:47:12 +05:30
|
|
|
impl ActivityHandler for Follow {
|
2023-02-11 18:02:35 +05:30
|
|
|
type DataType = DatabaseHandle;
|
2022-06-02 16:47:12 +05:30
|
|
|
type Error = crate::error::Error;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
async fn verify(&self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2023-03-10 02:39:44 +05:30
|
|
|
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)]
|
2023-03-16 06:41:48 +05:30
|
|
|
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2022-06-02 16:47:12 +05:30
|
|
|
// add to followers
|
2022-11-29 02:49:56 +05:30
|
|
|
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
|
2023-02-11 18:02:35 +05:30
|
|
|
let follower = self.actor.dereference(data).await?;
|
2023-03-02 04:49:10 +05:30
|
|
|
let id = generate_object_id(data.domain())?;
|
2022-06-02 16:47:12 +05:30
|
|
|
let accept = Accept::new(local_user.ap_id.clone(), self, id.clone());
|
|
|
|
local_user
|
2023-02-11 18:02:35 +05:30
|
|
|
.send(accept, vec![follower.shared_inbox_or_inbox()], data)
|
2022-06-02 16:47:12 +05:30
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|