activitypub-federation-rust/examples/live_federation/objects/person.rs

141 lines
3.8 KiB
Rust
Raw Normal View History

2023-03-08 03:31:36 +05:30
use crate::{activities::create_post::CreatePost, database::DatabaseHandle, error::Error};
use activitypub_federation::{
config::Data,
2023-03-08 03:31:36 +05:30
fetch::object_id::ObjectId,
http_signatures::generate_actor_keypair,
kinds::actor::PersonType,
protocol::{public_key::PublicKey, verification::verify_domains_match},
2023-03-17 02:11:29 +05:30
traits::{ActivityHandler, Actor, Object},
2023-03-08 03:31:36 +05:30
};
use chrono::{DateTime, Utc};
2023-03-08 03:31:36 +05:30
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use url::Url;
#[derive(Debug, Clone)]
pub struct DbUser {
pub name: String,
pub ap_id: ObjectId<DbUser>,
pub inbox: Url,
// exists for all users (necessary to verify http signatures)
pub public_key: String,
// exists only for local users
pub private_key: Option<String>,
last_refreshed_at: DateTime<Utc>,
2023-03-08 03:31:36 +05:30
pub followers: Vec<Url>,
pub local: bool,
}
/// List of all activities which this actor can receive.
#[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)]
#[enum_delegate::implement(ActivityHandler)]
pub enum PersonAcceptedActivities {
CreateNote(CreatePost),
}
impl DbUser {
pub fn new(hostname: &str, name: &str) -> Result<DbUser, Error> {
let ap_id = Url::parse(&format!("https://{}/{}", hostname, &name))?.into();
let inbox = Url::parse(&format!("https://{}/{}/inbox", hostname, &name))?;
let keypair = generate_actor_keypair()?;
Ok(DbUser {
name: name.to_string(),
ap_id,
inbox,
public_key: keypair.public_key,
private_key: Some(keypair.private_key),
last_refreshed_at: Utc::now(),
2023-03-08 03:31:36 +05:30
followers: vec![],
local: true,
})
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Person {
#[serde(rename = "type")]
kind: PersonType,
preferred_username: String,
id: ObjectId<DbUser>,
inbox: Url,
public_key: PublicKey,
}
#[async_trait::async_trait]
2023-03-17 02:11:29 +05:30
impl Object for DbUser {
2023-03-08 03:31:36 +05:30
type DataType = DatabaseHandle;
2023-03-17 02:11:29 +05:30
type Kind = Person;
2023-03-08 03:31:36 +05:30
type Error = Error;
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
2023-03-08 03:31:36 +05:30
Some(self.last_refreshed_at)
}
2023-03-17 02:11:29 +05:30
async fn read_from_id(
2023-03-08 03:31:36 +05:30
object_id: Url,
data: &Data<Self::DataType>,
2023-03-08 03:31:36 +05:30
) -> Result<Option<Self>, Self::Error> {
let users = data.users.lock().unwrap();
let res = users
.clone()
.into_iter()
.find(|u| u.ap_id.inner() == &object_id);
Ok(res)
}
2023-03-17 02:11:29 +05:30
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
2023-03-08 03:31:36 +05:30
Ok(Person {
preferred_username: self.name.clone(),
kind: Default::default(),
id: self.ap_id.clone(),
inbox: self.inbox.clone(),
public_key: self.public_key(),
2023-03-08 03:31:36 +05:30
})
}
async fn verify(
2023-03-17 02:11:29 +05:30
json: &Self::Kind,
expected_domain: &Url,
_data: &Data<Self::DataType>,
) -> Result<(), Self::Error> {
2023-03-17 02:11:29 +05:30
verify_domains_match(json.id.inner(), expected_domain)?;
Ok(())
}
2023-03-17 02:11:29 +05:30
async fn from_json(
json: Self::Kind,
_data: &Data<Self::DataType>,
2023-03-08 03:31:36 +05:30
) -> Result<Self, Self::Error> {
Ok(DbUser {
2023-03-17 02:11:29 +05:30
name: json.preferred_username,
ap_id: json.id,
inbox: json.inbox,
public_key: json.public_key.public_key_pem,
2023-03-08 03:31:36 +05:30
private_key: None,
last_refreshed_at: Utc::now(),
2023-03-08 03:31:36 +05:30
followers: vec![],
local: false,
})
}
}
impl Actor for DbUser {
fn id(&self) -> Url {
self.ap_id.inner().clone()
}
fn public_key_pem(&self) -> &str {
2023-03-08 03:31:36 +05:30
&self.public_key
}
fn private_key_pem(&self) -> Option<String> {
self.private_key.clone()
2023-03-08 03:31:36 +05:30
}
fn inbox(&self) -> Url {
self.inbox.clone()
}
2023-03-08 03:31:36 +05:30
}