Implement Blog::into_activity

This commit is contained in:
Bat 2018-06-21 22:07:04 +01:00
parent a0f680a9b6
commit dccab062e3
2 changed files with 32 additions and 13 deletions

View file

@ -16,8 +16,9 @@ use openssl::{
}; };
use webfinger::*; use webfinger::*;
use BASE_URL;
use activity_pub::{ use activity_pub::{
ApSignature, ActivityStream, Id, IntoId, ApSignature, ActivityStream, Id, IntoId, PublicKey,
inbox::WithInbox, inbox::WithInbox,
sign sign
}; };
@ -60,6 +61,8 @@ const BLOG_PREFIX: &'static str = "~";
impl Blog { impl Blog {
insert!(blogs, NewBlog); insert!(blogs, NewBlog);
get!(blogs); get!(blogs);
find_by!(blogs, find_by_ap_url, ap_url as String);
find_by!(blogs, find_by_name, actor_id as String, instance_id as i32);
pub fn get_instance(&self, conn: &PgConnection) -> Instance { pub fn get_instance(&self, conn: &PgConnection) -> Instance {
Instance::get(conn, self.instance_id).expect("Couldn't find instance") Instance::get(conn, self.instance_id).expect("Couldn't find instance")
@ -73,8 +76,6 @@ impl Blog {
.expect("Couldn't load blogs ") .expect("Couldn't load blogs ")
} }
find_by!(blogs, find_by_name, actor_id as String, instance_id as i32);
pub fn find_local(conn: &PgConnection, name: String) -> Option<Blog> { pub fn find_local(conn: &PgConnection, name: String) -> Option<Blog> {
Blog::find_by_name(conn, name, Instance::local_id(conn)) Blog::find_by_name(conn, name, Instance::local_id(conn))
} }
@ -144,8 +145,23 @@ impl Blog {
}) })
} }
pub fn into_activity(&self, _conn: &PgConnection) -> Group { pub fn into_activity(&self, _conn: &PgConnection) -> CustomGroup {
Group::default() // TODO let mut blog = Group::default();
blog.ap_actor_props.set_preferred_username_string(self.actor_id.clone()).expect("Blog::into_activity: preferredUsername error");
blog.object_props.set_name_string(self.title.clone()).expect("Blog::into_activity: name error");
blog.ap_actor_props.set_outbox_string(self.outbox_url.clone()).expect("Blog::into_activity: outbox error");
blog.ap_actor_props.set_inbox_string(self.inbox_url.clone()).expect("Blog::into_activity: inbox error");
blog.object_props.set_summary_string(self.summary.clone()).expect("Blog::into_activity: summary error");
blog.object_props.set_id_string(self.ap_url.clone()).expect("Blog::into_activity: id error");
let mut public_key = PublicKey::default();
public_key.set_id_string(format!("{}#main-key", self.ap_url)).expect("Blog::into_activity: publicKey.id error");
public_key.set_owner_string(self.ap_url.clone()).expect("Blog::into_activity: publicKey.owner error");
public_key.set_public_key_pem_string(self.public_key.clone()).expect("Blog::into_activity: publicKey.publicKeyPem error");
let mut ap_signature = ApSignature::default();
ap_signature.set_public_key_publickey(public_key).expect("Blog::into_activity: publicKey error");
CustomGroup::new(blog, ap_signature)
} }
pub fn update_boxes(&self, conn: &PgConnection) { pub fn update_boxes(&self, conn: &PgConnection) {
@ -208,13 +224,16 @@ impl Blog {
} }
} }
// FIXME: see User::from_url for correct behavior
pub fn from_url(conn: &PgConnection, url: String) -> Option<Blog> { pub fn from_url(conn: &PgConnection, url: String) -> Option<Blog> {
blogs::table.filter(blogs::ap_url.eq(url)) Blog::find_by_ap_url(conn, url.clone()).or_else(|| {
.limit(1) // The requested user was not in the DB
.load::<Blog>(conn) // We try to fetch it if it is remote
.expect("Error loading blog from url") if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() {
.into_iter().nth(0) Some(Blog::fetch_from_url(conn, url).unwrap())
} else {
None
}
})
} }
} }

View file

@ -1,4 +1,4 @@
use activitypub::{actor::Group, collection::OrderedCollection}; use activitypub::collection::OrderedCollection;
use rocket::{ use rocket::{
request::Form, request::Form,
response::{Redirect, Flash} response::{Redirect, Flash}
@ -32,7 +32,7 @@ fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
} }
#[get("/~/<name>", format = "application/activity+json", rank = 1)] #[get("/~/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityStream<Group> { fn activity_details(name: String, conn: DbConn) -> ActivityStream<CustomGroup> {
let blog = Blog::find_local(&*conn, name).unwrap(); let blog = Blog::find_local(&*conn, name).unwrap();
ActivityStream::new(blog.into_activity(&*conn)) ActivityStream::new(blog.into_activity(&*conn))
} }