use crate::apub::fetcher::{fetch_remote_object, fetch_remote_user};
+use crate::apub::signatures::PublicKey;
use crate::apub::*;
use crate::db::community::{Community, CommunityForm};
use crate::db::community_view::CommunityFollowerView;
impl Community {
fn as_group(&self, conn: &PgConnection) -> Result<GroupExt, Error> {
- let base_url = make_apub_endpoint(EndpointType::Community, &self.name);
-
let mut group = Group::default();
let oprops: &mut ObjectProperties = group.as_mut();
let creator = User_::read(conn, self.creator_id)?;
oprops
.set_context_xsd_any_uri(context())?
- .set_id(base_url.to_owned())?
+ .set_id(self.actor_id.to_owned())?
.set_name_xsd_string(self.name.to_owned())?
.set_published(convert_datetime(self.published))?
.set_attributed_to_xsd_any_uri(make_apub_endpoint(EndpointType::User, &creator.name))?;
actor_props
.set_preferred_username(self.title.to_owned())?
- .set_inbox(format!("{}/inbox", &base_url))?
- .set_outbox(format!("{}/outbox", &base_url))?
- .set_followers(format!("{}/followers", &base_url))?;
+ .set_inbox(format!("{}/inbox", &self.actor_id))?
+ .set_outbox(format!("{}/outbox", &self.actor_id))?
+ .set_followers(format!("{}/followers", &self.actor_id))?;
+
+ let public_key = PublicKey {
+ id: format!("{}#main-key", self.actor_id),
+ owner: self.actor_id.to_owned(),
+ public_key_pem: self.public_key.to_owned().unwrap(),
+ };
- Ok(group.extend(actor_props))
+ Ok(group.extend(actor_props).extend(public_key.to_ext()))
}
}
impl CommunityForm {
pub fn from_group(group: &GroupExt, conn: &PgConnection) -> Result<Self, Error> {
- let followers_uri = Url::parse(&group.extension.get_followers().unwrap().to_string())?;
- let outbox_uri = Url::parse(&group.extension.get_outbox().to_string())?;
+ let oprops = &group.base.base.object_props;
+ let aprops = &group.base.extension;
+ let public_key: &PublicKey = &group.extension.public_key;
+
+ let followers_uri = Url::parse(&aprops.get_followers().unwrap().to_string())?;
+ let outbox_uri = Url::parse(&aprops.get_outbox().to_string())?;
let _outbox = fetch_remote_object::<OrderedCollection>(&outbox_uri)?;
let _followers = fetch_remote_object::<UnorderedCollection>(&followers_uri)?;
- let oprops = &group.base.object_props;
- let aprops = &group.extension;
let apub_id = Url::parse(&oprops.get_attributed_to_xsd_any_uri().unwrap().to_string())?;
let creator = fetch_remote_user(&apub_id, conn)?;
+
Ok(CommunityForm {
name: oprops.get_name_xsd_string().unwrap().to_string(),
title: aprops.get_preferred_username().unwrap().to_string(),
actor_id: oprops.get_id().unwrap().to_string(),
local: false,
private_key: None,
- public_key: None,
+ public_key: Some(public_key.to_owned().public_key_pem),
last_refreshed_at: Some(naive_now()),
})
}
pub mod fetcher;
pub mod inbox;
pub mod post;
+pub mod signatures;
pub mod user;
+use crate::apub::signatures::PublicKeyExtension;
use crate::Settings;
use activitystreams::actor::{properties::ApActorProperties, Group, Person};
use activitystreams::ext::Ext;
use openssl::{pkey::PKey, rsa::Rsa};
use url::Url;
-type GroupExt = Ext<Group, ApActorProperties>;
-type PersonExt = Ext<Person, ApActorProperties>;
+type GroupExt = Ext<Ext<Group, ApActorProperties>, PublicKeyExtension>;
+type PersonExt = Ext<Ext<Person, ApActorProperties>, PublicKeyExtension>;
static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";
--- /dev/null
+// For this example, we'll use the Extensible trait, the Extension trait, the Actor trait, and
+// the Person type
+use activitystreams::{actor::Actor, ext::Extension};
+
+// The following is taken from here:
+// https://docs.rs/activitystreams/0.5.0-alpha.17/activitystreams/ext/index.html
+
+#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PublicKey {
+ pub id: String,
+ pub owner: String,
+ pub public_key_pem: String,
+}
+
+#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PublicKeyExtension {
+ pub public_key: PublicKey,
+}
+
+impl PublicKey {
+ pub fn to_ext(&self) -> PublicKeyExtension {
+ PublicKeyExtension {
+ public_key: self.to_owned(),
+ }
+ }
+}
+
+impl<T> Extension<T> for PublicKeyExtension where T: Actor {}
-use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType, PersonExt};
+use crate::apub::signatures::PublicKey;
+use crate::apub::{create_apub_response, PersonExt};
use crate::db::user::{UserForm, User_};
use crate::{convert_datetime, naive_now};
use activitystreams::{
info: Path<UserQuery>,
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
) -> Result<HttpResponse<Body>, Error> {
- dbg!(&info.user_name);
let user = User_::find_by_email_or_username(&&db.get()?, &info.user_name)?;
- let base_url = make_apub_endpoint(EndpointType::User, &user.name);
let mut person = Person::default();
let oprops: &mut ObjectProperties = person.as_mut();
oprops
.set_context_xsd_any_uri(context())?
- .set_id(base_url.to_string())?
+ .set_id(user.actor_id.to_string())?
.set_name_xsd_string(user.name.to_owned())?
.set_published(convert_datetime(user.published))?;
let mut actor_props = ApActorProperties::default();
actor_props
- .set_inbox(format!("{}/inbox", &base_url))?
- .set_outbox(format!("{}/outbox", &base_url))?
- .set_following(format!("{}/following", &base_url))?
- .set_liked(format!("{}/liked", &base_url))?;
+ .set_inbox(format!("{}/inbox", &user.actor_id))?
+ .set_outbox(format!("{}/outbox", &user.actor_id))?
+ .set_following(format!("{}/following", &user.actor_id))?
+ .set_liked(format!("{}/liked", &user.actor_id))?;
- Ok(create_apub_response(&person.extend(actor_props)))
+ let public_key = PublicKey {
+ id: format!("{}#main-key", user.actor_id),
+ owner: user.actor_id.to_owned(),
+ public_key_pem: user.public_key.unwrap(),
+ };
+
+ Ok(create_apub_response(
+ &person.extend(actor_props).extend(public_key.to_ext()),
+ ))
}
impl UserForm {
pub fn from_person(person: &PersonExt) -> Result<Self, Error> {
- let oprops = &person.base.object_props;
- let aprops = &person.extension;
+ let oprops = &person.base.base.object_props;
+ let aprops = &person.base.extension;
+ let public_key: &PublicKey = &person.extension.public_key;
+
Ok(UserForm {
name: oprops.get_name_xsd_string().unwrap().to_string(),
preferred_username: aprops.get_preferred_username().map(|u| u.to_string()),
bio: oprops.get_summary_xsd_string().map(|s| s.to_string()),
local: false,
private_key: None,
- public_key: None,
+ public_key: Some(public_key.to_owned().public_key_pem),
last_refreshed_at: Some(naive_now()),
})
}