1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-12-23 23:11:24 +00:00

remove some unused code

This commit is contained in:
Felix Ableitner 2023-11-16 14:52:23 +01:00
parent 59c6c8d46a
commit 5da26f78e4
6 changed files with 2 additions and 215 deletions

View file

@ -1,14 +1,12 @@
use crate::federation::objects::article::DbArticle;
use crate::federation::objects::instance::DbInstance;
use crate::federation::objects::{article::DbArticle, person::DbUser};
use std::sync::{Arc, Mutex};
pub type DatabaseHandle = Arc<Database>;
/// Our "database" which contains all known posts and users (local and federated)
pub struct Database {
pub instances: Mutex<Vec<DbInstance>>,
pub users: Mutex<Vec<DbUser>>,
pub articles: Mutex<Vec<DbArticle>>,
}

View file

@ -1,3 +1,2 @@
pub mod accept;
pub mod follow;
pub mod update;

View file

@ -1,60 +0,0 @@
use crate::federation::objects::article::DbArticle;
use crate::{database::DatabaseHandle, federation::objects::person::DbUser};
use activitypub_federation::kinds::activity::UpdateType;
use activitypub_federation::{config::Data, fetch::object_id::ObjectId, traits::ActivityHandler};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::error::MyResult;
use crate::utils::generate_object_id;
/// represents a diff between two strings
#[derive(Deserialize, Serialize, Debug)]
pub struct Diff {}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Update {
actor: ObjectId<DbUser>,
object: ObjectId<DbArticle>,
/// https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
result: Diff,
#[serde(rename = "type")]
kind: UpdateType,
id: Url,
}
impl Update {
pub fn new(actor: ObjectId<DbUser>, object: ObjectId<DbArticle>) -> MyResult<Update> {
let id = generate_object_id(actor.inner())?;
Ok(Update {
actor,
object,
result: Diff {},
kind: Default::default(),
id,
})
}
}
#[async_trait::async_trait]
impl ActivityHandler for Update {
type DataType = DatabaseHandle;
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>) -> Result<(), Self::Error> {
Ok(())
}
async fn receive(self, _data: &Data<Self::DataType>) -> Result<(), Self::Error> {
Ok(())
}
}

View file

@ -1,10 +1,8 @@
use crate::database::{Database, DatabaseHandle};
use crate::error::Error;
use crate::federation::objects::instance::DbInstance;
use activitypub_federation::config::{FederationConfig, UrlVerifier};
use activitypub_federation::config::FederationConfig;
use activitypub_federation::http_signatures::generate_actor_keypair;
use async_trait::async_trait;
use chrono::Local;
use std::sync::{Arc, Mutex};
use url::Url;
@ -29,7 +27,6 @@ pub async fn federation_config(hostname: &str) -> Result<FederationConfig<Databa
};
let database = Arc::new(Database {
instances: Mutex::new(vec![local_instance]),
users: Mutex::new(vec![]),
articles: Mutex::new(vec![]),
});
let config = FederationConfig::builder()
@ -40,18 +37,3 @@ pub async fn federation_config(hostname: &str) -> Result<FederationConfig<Databa
.await?;
Ok(config)
}
/// Use this to store your federation blocklist, or a database connection needed to retrieve it.
#[derive(Clone)]
struct MyUrlVerifier();
#[async_trait]
impl UrlVerifier for MyUrlVerifier {
async fn verify(&self, url: &Url) -> Result<(), &'static str> {
if url.domain() == Some("malicious.com") {
Err("malicious domain")
} else {
Ok(())
}
}
}

View file

@ -1,3 +1,2 @@
pub mod article;
pub mod instance;
pub mod person;

View file

@ -1,131 +0,0 @@
use crate::database::DatabaseHandle;
use crate::error::Error;
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::{Actor, Object},
};
use chrono::{Local, NaiveDateTime};
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,
public_key: String,
private_key: Option<String>,
last_refreshed_at: NaiveDateTime,
pub followers: Vec<Url>,
pub local: bool,
}
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,
ap_id,
inbox,
public_key: keypair.public_key,
private_key: Some(keypair.private_key),
last_refreshed_at: Local::now().naive_local(),
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]
impl Object for DbUser {
type DataType = DatabaseHandle;
type Kind = Person;
type Error = Error;
fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
Some(self.last_refreshed_at)
}
async fn read_from_id(
object_id: Url,
data: &Data<Self::DataType>,
) -> 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)
}
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, Self::Error> {
Ok(Person {
preferred_username: self.name.clone(),
kind: Default::default(),
id: self.ap_id.clone(),
inbox: self.inbox.clone(),
public_key: self.public_key(),
})
}
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> {
let user = DbUser {
name: json.preferred_username,
ap_id: json.id,
inbox: json.inbox,
public_key: json.public_key.public_key_pem,
private_key: None,
last_refreshed_at: Local::now().naive_local(),
followers: vec![],
local: false,
};
let mut mutex = data.users.lock().unwrap();
mutex.push(user.clone());
Ok(user)
}
}
impl Actor for DbUser {
fn id(&self) -> Url {
self.ap_id.inner().clone()
}
fn public_key_pem(&self) -> &str {
&self.public_key
}
fn private_key_pem(&self) -> Option<String> {
self.private_key.clone()
}
fn inbox(&self) -> Url {
self.inbox.clone()
}
}