Remove useless functions from activity_pub::actor::Actor

This commit is contained in:
Bat 2018-06-21 18:23:01 +01:00
parent 7812b9bc8f
commit 606a3d12c7
7 changed files with 27 additions and 110 deletions

View File

@ -1,44 +1,15 @@
use diesel::PgConnection; use diesel::PgConnection;
use serde_json;
use activity_pub::ap_url; use activity_pub::ap_url;
use models::instance::Instance; use models::instance::Instance;
pub enum ActorType {
Person,
Blog
}
impl ToString for ActorType {
fn to_string(&self) -> String {
String::from(match self {
ActorType::Person => "Person",
ActorType::Blog => "Blog"
})
}
}
pub trait Actor: Sized { pub trait Actor: Sized {
fn get_box_prefix() -> &'static str; fn get_box_prefix() -> &'static str;
fn get_actor_id(&self) -> String; fn get_actor_id(&self) -> String;
fn get_display_name(&self) -> String;
fn get_summary(&self) -> String;
fn get_instance(&self, conn: &PgConnection) -> Instance; fn get_instance(&self, conn: &PgConnection) -> Instance;
fn get_actor_type() -> ActorType;
fn get_inbox_url(&self) -> String;
fn get_shared_inbox_url(&self) -> Option<String>;
fn custom_props(&self, _conn: &PgConnection) -> serde_json::Map<String, serde_json::Value> {
serde_json::Map::new()
}
fn compute_outbox(&self, conn: &PgConnection) -> String { fn compute_outbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "outbox") self.compute_box(conn, "outbox")
} }
@ -59,6 +30,4 @@ pub trait Actor: Sized {
user = self.get_actor_id() user = self.get_actor_id()
)) ))
} }
fn from_url(conn: &PgConnection, url: String) -> Option<Self>;
} }

View File

@ -18,7 +18,7 @@ use webfinger::*;
use activity_pub::{ use activity_pub::{
ActivityStream, Id, IntoId, ActivityStream, Id, IntoId,
actor::{Actor as APActor, ActorType}, actor::{Actor as APActor},
inbox::WithInbox, inbox::WithInbox,
sign sign
}; };
@ -199,6 +199,15 @@ impl Blog {
] ]
} }
} }
// FIXME: see User::from_url for correct behavior
pub fn from_url(conn: &PgConnection, url: String) -> Option<Blog> {
blogs::table.filter(blogs::ap_url.eq(url))
.limit(1)
.load::<Blog>(conn)
.expect("Error loading blog from url")
.into_iter().nth(0)
}
} }
impl IntoId for Blog { impl IntoId for Blog {
@ -229,37 +238,9 @@ impl APActor for Blog {
self.actor_id.to_string() self.actor_id.to_string()
} }
fn get_display_name(&self) -> String {
self.title.clone()
}
fn get_summary(&self) -> String {
self.summary.clone()
}
fn get_instance(&self, conn: &PgConnection) -> Instance { fn get_instance(&self, conn: &PgConnection) -> Instance {
Instance::get(conn, self.instance_id).unwrap() Instance::get(conn, self.instance_id).unwrap()
} }
fn get_actor_type () -> ActorType {
ActorType::Blog
}
fn get_inbox_url(&self) -> String {
self.inbox_url.clone()
}
fn get_shared_inbox_url(&self) -> Option<String> {
None
}
fn from_url(conn: &PgConnection, url: String) -> Option<Blog> {
blogs::table.filter(blogs::ap_url.eq(url))
.limit(1)
.load::<Blog>(conn)
.expect("Error loading blog from url")
.into_iter().nth(0)
}
} }
impl sign::Signer for Blog { impl sign::Signer for Blog {

View File

@ -9,7 +9,6 @@ use serde_json;
use activity_pub::{ use activity_pub::{
ap_url, Id, IntoId, PUBLIC_VISIBILTY, ap_url, Id, IntoId, PUBLIC_VISIBILTY,
actor::Actor,
inbox::{FromActivity, Notify} inbox::{FromActivity, Notify}
}; };
use models::{ use models::{

View File

@ -1,7 +1,7 @@
use activitypub::{Actor, activity::{Accept, Follow as FollowAct}}; use activitypub::{Actor, activity::{Accept, Follow as FollowAct}};
use diesel::{self, PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl}; use diesel::{self, PgConnection, ExpressionMethods, QueryDsl, RunQueryDsl};
use activity_pub::{broadcast, Id, IntoId, actor::Actor as ApActor, inbox::{FromActivity, Notify, WithInbox}, sign::Signer}; use activity_pub::{broadcast, Id, IntoId, inbox::{FromActivity, Notify, WithInbox}, sign::Signer};
use models::{ use models::{
blogs::Blog, blogs::Blog,
notifications::*, notifications::*,

View File

@ -5,7 +5,6 @@ use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use activity_pub::{ use activity_pub::{
Id, Id,
IntoId, IntoId,
actor::Actor,
inbox::{FromActivity, Deletable, Notify} inbox::{FromActivity, Deletable, Notify}
}; };
use models::{ use models::{

View File

@ -2,7 +2,7 @@ use activitypub::activity::{Announce, Undo};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods};
use activity_pub::{Id, IntoId, actor::Actor, inbox::{FromActivity, Notify, Deletable}}; use activity_pub::{Id, IntoId, inbox::{FromActivity, Notify, Deletable}};
use models::{notifications::*, posts::Post, users::User}; use models::{notifications::*, posts::Post, users::User};
use schema::reshares; use schema::reshares;

View File

@ -28,7 +28,7 @@ use webfinger::*;
use BASE_URL; use BASE_URL;
use activity_pub::{ use activity_pub::{
ap_url, ActivityStream, Id, IntoId, ap_url, ActivityStream, Id, IntoId,
actor::{ActorType, Actor as APActor}, actor::{Actor as APActor},
inbox::{Inbox, WithInbox}, inbox::{Inbox, WithInbox},
sign::{Signer, gen_keypair} sign::{Signer, gen_keypair}
}; };
@ -308,8 +308,8 @@ impl User {
pub fn into_activity(&self, _conn: &PgConnection) -> Person { pub fn into_activity(&self, _conn: &PgConnection) -> Person {
let mut actor = Person::default(); let mut actor = Person::default();
actor.object_props.set_id_string(self.ap_url.clone()).expect("User::into_activity: id error"); actor.object_props.set_id_string(self.ap_url.clone()).expect("User::into_activity: id error");
actor.object_props.set_name_string(self.get_display_name()).expect("User::into_activity: name error"); actor.object_props.set_name_string(self.display_name.clone()).expect("User::into_activity: name error");
actor.object_props.set_summary_string(self.get_summary()).expect("User::into_activity: summary error"); actor.object_props.set_summary_string(self.summary.get().clone()).expect("User::into_activity: summary error");
actor.object_props.set_url_string(self.ap_url.clone()).expect("User::into_activity: url error"); actor.object_props.set_url_string(self.ap_url.clone()).expect("User::into_activity: url error");
actor.ap_actor_props.set_inbox_string(self.inbox_url.clone()).expect("User::into_activity: inbox error"); actor.ap_actor_props.set_inbox_string(self.inbox_url.clone()).expect("User::into_activity: inbox error");
actor.ap_actor_props.set_outbox_string(self.outbox_url.clone()).expect("User::into_activity: outbox error"); actor.ap_actor_props.set_outbox_string(self.outbox_url.clone()).expect("User::into_activity: outbox error");
@ -350,6 +350,18 @@ impl User {
] ]
} }
} }
pub fn from_url(conn: &PgConnection, url: String) -> Option<User> {
User::find_by_ap_url(conn, url.clone()).or_else(|| {
// The requested user was not in the DB
// We try to fetch it if it is remote
if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() {
Some(User::fetch_from_url(conn, url).unwrap())
} else {
None
}
})
}
} }
impl<'a, 'r> FromRequest<'a, 'r> for User { impl<'a, 'r> FromRequest<'a, 'r> for User {
@ -374,52 +386,9 @@ impl APActor for User {
self.username.to_string() self.username.to_string()
} }
fn get_display_name(&self) -> String {
self.display_name.clone()
}
fn get_summary(&self) -> String {
self.summary.get().clone()
}
fn get_instance(&self, conn: &PgConnection) -> Instance { fn get_instance(&self, conn: &PgConnection) -> Instance {
Instance::get(conn, self.instance_id).unwrap() Instance::get(conn, self.instance_id).unwrap()
} }
fn get_actor_type() -> ActorType {
ActorType::Person
}
fn get_inbox_url(&self) -> String {
self.inbox_url.clone()
}
fn get_shared_inbox_url(&self) -> Option<String> {
self.shared_inbox_url.clone()
}
fn custom_props(&self, conn: &PgConnection) -> serde_json::Map<String, serde_json::Value> {
let mut res = serde_json::Map::new();
res.insert("publicKey".to_string(), json!({
"id": self.get_key_id(),
"owner": self.ap_url,
"publicKeyPem": self.public_key
}));
res.insert("followers".to_string(), serde_json::Value::String(self.compute_box(conn, "followers")));
res
}
fn from_url(conn: &PgConnection, url: String) -> Option<User> {
User::find_by_ap_url(conn, url.clone()).or_else(|| {
// The requested user was not in the DB
// We try to fetch it if it is remote
if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() {
Some(User::fetch_from_url(conn, url).unwrap())
} else {
None
}
})
}
} }
impl IntoId for User { impl IntoId for User {