2022-06-02 16:47:12 +05:30
|
|
|
use crate::{
|
2023-03-08 03:31:36 +05:30
|
|
|
activities::{accept::Accept, create_post::CreatePost, follow::Follow},
|
2022-06-02 16:47:12 +05:30
|
|
|
error::Error,
|
2023-02-11 18:02:35 +05:30
|
|
|
instance::DatabaseHandle,
|
2023-02-19 17:56:01 +05:30
|
|
|
objects::post::DbPost,
|
2022-11-26 21:42:08 +05:30
|
|
|
utils::generate_object_id,
|
2022-06-02 16:47:12 +05:30
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-06 06:47:34 +05:30
|
|
|
activity_queue::send_activity,
|
2023-03-16 06:41:48 +05:30
|
|
|
config::Data,
|
2023-03-06 06:47:34 +05:30
|
|
|
fetch::{object_id::ObjectId, webfinger::webfinger_resolve_actor},
|
|
|
|
http_signatures::generate_actor_keypair,
|
2023-02-19 17:56:01 +05:30
|
|
|
kinds::actor::PersonType,
|
2023-03-10 02:39:44 +05:30
|
|
|
protocol::{context::WithContext, public_key::PublicKey, verification::verify_domains_match},
|
2023-03-17 02:11:29 +05:30
|
|
|
traits::{ActivityHandler, Actor, Object},
|
2022-06-02 16:47:12 +05:30
|
|
|
};
|
2023-03-08 03:31:36 +05:30
|
|
|
use chrono::{Local, NaiveDateTime};
|
2022-06-02 16:47:12 +05:30
|
|
|
use serde::{Deserialize, Serialize};
|
2023-02-19 17:56:01 +05:30
|
|
|
use std::fmt::Debug;
|
2022-06-02 16:47:12 +05:30
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2023-02-19 17:56:01 +05:30
|
|
|
pub struct DbUser {
|
|
|
|
pub name: String,
|
|
|
|
pub ap_id: ObjectId<DbUser>,
|
2022-06-02 16:47:12 +05:30
|
|
|
pub inbox: Url,
|
|
|
|
// exists for all users (necessary to verify http signatures)
|
|
|
|
public_key: String,
|
|
|
|
// exists only for local users
|
|
|
|
private_key: Option<String>,
|
2023-03-08 03:31:36 +05:30
|
|
|
last_refreshed_at: NaiveDateTime,
|
2022-06-02 16:47:12 +05:30
|
|
|
pub followers: Vec<Url>,
|
|
|
|
pub local: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List of all activities which this actor can receive.
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(untagged)]
|
2022-11-14 17:34:36 +05:30
|
|
|
#[enum_delegate::implement(ActivityHandler)]
|
2022-06-02 16:47:12 +05:30
|
|
|
pub enum PersonAcceptedActivities {
|
|
|
|
Follow(Follow),
|
|
|
|
Accept(Accept),
|
2023-03-08 03:31:36 +05:30
|
|
|
CreateNote(CreatePost),
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|
|
|
|
|
2023-02-19 17:56:01 +05:30
|
|
|
impl DbUser {
|
|
|
|
pub fn new(hostname: &str, name: String) -> Result<DbUser, Error> {
|
|
|
|
let ap_id = Url::parse(&format!("http://{}/{}", hostname, &name))?.into();
|
|
|
|
let inbox = Url::parse(&format!("http://{}/{}/inbox", hostname, &name))?;
|
|
|
|
let keypair = generate_actor_keypair()?;
|
|
|
|
Ok(DbUser {
|
|
|
|
name,
|
2022-06-02 16:47:12 +05:30
|
|
|
ap_id,
|
|
|
|
inbox,
|
|
|
|
public_key: keypair.public_key,
|
|
|
|
private_key: Some(keypair.private_key),
|
2023-03-08 03:31:36 +05:30
|
|
|
last_refreshed_at: Local::now().naive_local(),
|
2022-06-02 16:47:12 +05:30
|
|
|
followers: vec![],
|
|
|
|
local: true,
|
2023-02-19 17:56:01 +05:30
|
|
|
})
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Person {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
kind: PersonType,
|
2023-02-19 17:56:01 +05:30
|
|
|
preferred_username: String,
|
|
|
|
id: ObjectId<DbUser>,
|
2022-06-02 16:47:12 +05:30
|
|
|
inbox: Url,
|
|
|
|
public_key: PublicKey,
|
|
|
|
}
|
|
|
|
|
2023-02-19 17:56:01 +05:30
|
|
|
impl DbUser {
|
2022-06-02 16:47:12 +05:30
|
|
|
pub fn followers(&self) -> &Vec<Url> {
|
|
|
|
&self.followers
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn followers_url(&self) -> Result<Url, Error> {
|
|
|
|
Ok(Url::parse(&format!("{}/followers", self.ap_id.inner()))?)
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
pub async fn follow(&self, other: &str, data: &Data<DatabaseHandle>) -> Result<(), Error> {
|
2023-03-02 04:49:10 +05:30
|
|
|
let other: DbUser = webfinger_resolve_actor(other, data).await?;
|
|
|
|
let id = generate_object_id(data.domain())?;
|
2022-06-02 16:47:12 +05:30
|
|
|
let follow = Follow::new(self.ap_id.clone(), other.ap_id.clone(), id.clone());
|
2023-02-19 17:56:01 +05:30
|
|
|
self.send(follow, vec![other.shared_inbox_or_inbox()], data)
|
2023-02-11 18:02:35 +05:30
|
|
|
.await?;
|
2022-06-02 16:47:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
pub async fn post(&self, post: DbPost, data: &Data<DatabaseHandle>) -> Result<(), Error> {
|
2023-03-02 04:49:10 +05:30
|
|
|
let id = generate_object_id(data.domain())?;
|
2023-03-17 02:11:29 +05:30
|
|
|
let create = CreatePost::new(post.into_json(data).await?, id.clone());
|
2022-06-06 20:06:44 +05:30
|
|
|
let mut inboxes = vec![];
|
2022-06-02 16:47:12 +05:30
|
|
|
for f in self.followers.clone() {
|
2023-03-02 04:49:10 +05:30
|
|
|
let user: DbUser = ObjectId::from(f).dereference(data).await?;
|
2022-06-06 20:06:44 +05:30
|
|
|
inboxes.push(user.shared_inbox_or_inbox());
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|
2023-02-19 17:56:01 +05:30
|
|
|
self.send(create, inboxes, data).await?;
|
2022-06-02 16:47:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:44 +05:30
|
|
|
pub(crate) async fn send<Activity>(
|
2022-06-02 16:47:12 +05:30
|
|
|
&self,
|
|
|
|
activity: Activity,
|
2022-06-06 20:06:44 +05:30
|
|
|
recipients: Vec<Url>,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: &Data<DatabaseHandle>,
|
2022-06-03 17:30:16 +05:30
|
|
|
) -> Result<(), <Activity as ActivityHandler>::Error>
|
|
|
|
where
|
2023-02-19 17:56:01 +05:30
|
|
|
Activity: ActivityHandler + Serialize + Debug + Send + Sync,
|
2022-06-03 17:30:16 +05:30
|
|
|
<Activity as ActivityHandler>::Error: From<anyhow::Error> + From<serde_json::Error>,
|
|
|
|
{
|
|
|
|
let activity = WithContext::new_default(activity);
|
2023-03-16 06:41:48 +05:30
|
|
|
send_activity(activity, self, recipients, data).await?;
|
2022-06-02 16:47:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:49:56 +05:30
|
|
|
#[async_trait::async_trait]
|
2023-03-17 02:11:29 +05:30
|
|
|
impl Object for DbUser {
|
2023-02-11 18:02:35 +05:30
|
|
|
type DataType = DatabaseHandle;
|
2023-03-17 02:11:29 +05:30
|
|
|
type Kind = Person;
|
2023-02-11 18:35:37 +05:30
|
|
|
type Error = Error;
|
2022-06-02 16:47:12 +05:30
|
|
|
|
2023-03-08 03:31:36 +05:30
|
|
|
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
|
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
}
|
|
|
|
|
2023-03-17 02:11:29 +05:30
|
|
|
async fn read_from_id(
|
2022-06-02 16:47:12 +05:30
|
|
|
object_id: Url,
|
2023-03-16 06:41:48 +05:30
|
|
|
data: &Data<Self::DataType>,
|
2022-06-02 16:47:12 +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> {
|
2022-06-02 16:47:12 +05:30
|
|
|
Ok(Person {
|
2023-02-19 17:56:01 +05:30
|
|
|
preferred_username: self.name.clone(),
|
2022-06-02 16:47:12 +05:30
|
|
|
kind: Default::default(),
|
|
|
|
id: self.ap_id.clone(),
|
|
|
|
inbox: self.inbox.clone(),
|
|
|
|
public_key: self.public_key(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-10 02:39:44 +05:30
|
|
|
async fn verify(
|
2023-03-17 02:11:29 +05:30
|
|
|
json: &Self::Kind,
|
2023-03-10 02:39:44 +05:30
|
|
|
expected_domain: &Url,
|
2023-03-16 06:41:48 +05:30
|
|
|
_data: &Data<Self::DataType>,
|
2023-03-10 02:39:44 +05:30
|
|
|
) -> Result<(), Self::Error> {
|
2023-03-17 02:11:29 +05:30
|
|
|
verify_domains_match(json.id.inner(), expected_domain)?;
|
2023-03-10 02:39:44 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-17 02:11:29 +05:30
|
|
|
async fn from_json(json: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, Self::Error> {
|
2023-03-08 03:31:36 +05:30
|
|
|
let user = 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,
|
2022-06-02 16:47:12 +05:30
|
|
|
private_key: None,
|
2023-03-08 03:31:36 +05:30
|
|
|
last_refreshed_at: Local::now().naive_local(),
|
2022-06-02 16:47:12 +05:30
|
|
|
followers: vec![],
|
|
|
|
local: false,
|
2023-03-08 03:31:36 +05:30
|
|
|
};
|
|
|
|
let mut mutex = data.users.lock().unwrap();
|
|
|
|
mutex.push(user.clone());
|
|
|
|
Ok(user)
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 17:56:01 +05:30
|
|
|
impl Actor for DbUser {
|
2023-03-16 06:41:48 +05:30
|
|
|
fn id(&self) -> Url {
|
|
|
|
self.ap_id.inner().clone()
|
2023-03-10 02:39:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fn public_key_pem(&self) -> &str {
|
2022-06-02 16:47:12 +05:30
|
|
|
&self.public_key
|
|
|
|
}
|
2022-06-03 17:30:16 +05:30
|
|
|
|
2023-03-16 06:41:48 +05:30
|
|
|
fn private_key_pem(&self) -> Option<String> {
|
|
|
|
self.private_key.clone()
|
|
|
|
}
|
|
|
|
|
2022-06-03 17:30:16 +05:30
|
|
|
fn inbox(&self) -> Url {
|
|
|
|
self.inbox.clone()
|
|
|
|
}
|
2022-06-02 16:47:12 +05:30
|
|
|
}
|