2024-04-18 04:03:52 +02:00
|
|
|
use crate::{
|
|
|
|
|
activities::create_post::CreatePost,
|
|
|
|
|
database::{State, StateHandle},
|
|
|
|
|
entities::{self, user},
|
|
|
|
|
error::Error,
|
|
|
|
|
};
|
2024-04-09 19:48:18 +02:00
|
|
|
use activitypub_federation::{
|
|
|
|
|
config::Data,
|
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
|
http_signatures::generate_actor_keypair,
|
|
|
|
|
kinds::actor::PersonType,
|
|
|
|
|
protocol::{public_key::PublicKey, verification::verify_domains_match},
|
|
|
|
|
traits::{ActivityHandler, Actor, Object},
|
|
|
|
|
};
|
2024-04-18 03:41:52 +02:00
|
|
|
use chrono::{prelude, DateTime, Utc};
|
|
|
|
|
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
|
2024-04-09 19:48:18 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-04-18 19:01:14 +02:00
|
|
|
use tracing::info;
|
2024-04-09 19:48:18 +02:00
|
|
|
use std::fmt::Debug;
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
2024-04-16 19:53:24 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-04-09 19:48:18 +02:00
|
|
|
pub struct DbUser {
|
|
|
|
|
pub name: String,
|
2024-04-18 03:41:52 +02:00
|
|
|
pub ap_id: ObjectId<user::Model>,
|
2024-04-09 19:48:18 +02:00
|
|
|
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>,
|
|
|
|
|
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(),
|
|
|
|
|
followers: vec![],
|
|
|
|
|
local: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct Person {
|
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
|
kind: PersonType,
|
|
|
|
|
preferred_username: String,
|
2024-04-18 19:01:14 +02:00
|
|
|
name: String,
|
|
|
|
|
summary: Option<String>,
|
|
|
|
|
url: Url,
|
2024-04-18 03:41:52 +02:00
|
|
|
id: ObjectId<user::Model>,
|
2024-04-09 19:48:18 +02:00
|
|
|
inbox: Url,
|
|
|
|
|
public_key: PublicKey,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2024-04-18 03:41:52 +02:00
|
|
|
impl Object for user::Model {
|
|
|
|
|
type DataType = StateHandle;
|
2024-04-09 19:48:18 +02:00
|
|
|
type Kind = Person;
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
|
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn read_from_id(
|
|
|
|
|
object_id: Url,
|
|
|
|
|
data: &Data<Self::DataType>,
|
|
|
|
|
) -> Result<Option<Self>, Self::Error> {
|
2024-04-18 03:41:52 +02:00
|
|
|
let res = entities::prelude::User::find()
|
|
|
|
|
.filter(entities::user::Column::Id.eq(object_id.as_str()))
|
|
|
|
|
.one(data.database_connection.as_ref())
|
|
|
|
|
.await?;
|
2024-04-09 19:48:18 +02:00
|
|
|
Ok(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
|
|
|
|
|
Ok(Person {
|
2024-04-18 03:41:52 +02:00
|
|
|
preferred_username: self.username.clone(),
|
2024-04-09 19:48:18 +02:00
|
|
|
kind: Default::default(),
|
2024-04-18 03:41:52 +02:00
|
|
|
id: Url::parse(&self.id).unwrap().into(),
|
|
|
|
|
inbox: Url::parse(&self.inbox).unwrap(),
|
2024-04-09 19:48:18 +02:00
|
|
|
public_key: self.public_key(),
|
2024-04-18 19:01:14 +02:00
|
|
|
name: self.name.clone(),
|
|
|
|
|
summary: self.summary.clone(),
|
|
|
|
|
url: Url::parse(&self.url).unwrap(),
|
2024-04-09 19:48:18 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn verify(
|
|
|
|
|
json: &Self::Kind,
|
|
|
|
|
expected_domain: &Url,
|
|
|
|
|
_data: &Data<Self::DataType>,
|
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
|
verify_domains_match(json.id.inner(), expected_domain)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn from_json(
|
|
|
|
|
json: Self::Kind,
|
|
|
|
|
_data: &Data<Self::DataType>,
|
|
|
|
|
) -> Result<Self, Self::Error> {
|
2024-04-18 03:41:52 +02:00
|
|
|
let model = user::ActiveModel {
|
|
|
|
|
id: Set(json.id.to_string()),
|
|
|
|
|
username: Set(json.preferred_username),
|
2024-04-18 19:01:14 +02:00
|
|
|
name: Set(json.name),
|
2024-04-18 03:41:52 +02:00
|
|
|
inbox: Set(json.inbox.to_string()),
|
|
|
|
|
public_key: Set(json.public_key.public_key_pem),
|
|
|
|
|
local: Set(false),
|
2024-04-18 19:01:14 +02:00
|
|
|
summary: Set(json.summary),
|
|
|
|
|
url: Set(json.url.to_string()),
|
2024-04-18 19:09:10 +02:00
|
|
|
follower_count: Set(0),
|
|
|
|
|
following_count: Set(0),
|
2024-04-18 19:14:06 +02:00
|
|
|
created_at: Set(Utc::now()),
|
|
|
|
|
last_refreshed_at: Set(Utc::now()),
|
2024-04-18 03:41:52 +02:00
|
|
|
..Default::default()
|
|
|
|
|
};
|
2024-04-18 19:01:14 +02:00
|
|
|
let model = model.insert(_data.database_connection.as_ref()).await;
|
|
|
|
|
if let Err(err) = model {
|
|
|
|
|
eprintln!("Error inserting user: {:?}", err);
|
|
|
|
|
Err(err.into())
|
|
|
|
|
} else {
|
|
|
|
|
info!("User inserted: {:?}", model.as_ref().unwrap());
|
|
|
|
|
Ok(model.unwrap())
|
|
|
|
|
}
|
2024-04-09 19:48:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 03:41:52 +02:00
|
|
|
impl Actor for user::Model {
|
2024-04-09 19:48:18 +02:00
|
|
|
fn id(&self) -> Url {
|
2024-04-18 03:41:52 +02:00
|
|
|
Url::parse(&self.id).unwrap()
|
2024-04-09 19:48:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn public_key_pem(&self) -> &str {
|
|
|
|
|
&self.public_key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn private_key_pem(&self) -> Option<String> {
|
|
|
|
|
self.private_key.clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn inbox(&self) -> Url {
|
2024-04-18 03:41:52 +02:00
|
|
|
Url::parse(&self.inbox).unwrap()
|
2024-04-09 19:48:18 +02:00
|
|
|
}
|
|
|
|
|
}
|