From 8867fa1d52967c3755eabf677654c3bb04b0fd67 Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 11 Mar 2020 18:26:58 +0100 Subject: [PATCH] use urls for id again, more comments --- server/src/apub/community.rs | 7 ++++--- server/src/apub/group_wrapper.rs | 26 +++++++++++++------------- server/src/apub/mod.rs | 10 ++++++++++ server/src/apub/puller.rs | 6 +++--- server/src/websocket/server.rs | 1 + 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 2a16eeae..877534b3 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -12,16 +12,17 @@ use serde_json::{Value}; impl Community { pub fn as_group(&self) -> Group { - let base_url = make_apub_endpoint("c", &self.name); + let base_url = make_apub_endpoint("c", &self.id); let mut group = Group::default(); group.object_props.set_context_object(context()).ok(); - Group::set_id(&mut group, self.id); + Group::set_id(&mut group, &base_url); + Group::set_title(&mut group, &self.title); Group::set_published(&mut group, self.published); Group::set_updated(&mut group, self.updated); - Group::set_creator_id(&mut group, self.creator_id); + Group::set_creator_id(&mut group, make_apub_endpoint("u", &self.creator_id)); Group::set_description(&mut group, &self.description); diff --git a/server/src/apub/group_wrapper.rs b/server/src/apub/group_wrapper.rs index dd140df2..98eb2cf3 100644 --- a/server/src/apub/group_wrapper.rs +++ b/server/src/apub/group_wrapper.rs @@ -5,9 +5,8 @@ use failure::Error; use serde_json::Value; pub trait GroupHelper { - // TODO: id really needs to be a url - fn set_id(group: &mut Group, id: i32); - fn get_id(group: &Group) -> Result; + fn set_id(group: &mut Group, id: &str); + fn get_id(group: &Group) -> Result; fn set_title(group: &mut Group, title: &str); fn get_title(group: &Group) -> Result; @@ -15,9 +14,8 @@ pub trait GroupHelper { fn set_description(group: &mut Group, description: &Option); fn get_description(group: &Group) -> Result, Error>; - // TODO: also needs to be changed to url - fn set_creator_id(group: &mut Group, creator_id: i32); - fn get_creator_id(group: &Group) -> Result; + fn set_creator_id(group: &mut Group, creator_id: String); + fn get_creator_id(group: &Group) -> Result; fn set_published(group: &mut Group, published: NaiveDateTime); fn get_published(group: &Group) -> Result; @@ -28,11 +26,11 @@ pub trait GroupHelper { // TODO: something is crashing and not reporting the error impl GroupHelper for Group { - fn set_id(group: &mut Group, id: i32) { + fn set_id(group: &mut Group, id: &str) { group.object_props.id = Some(Value::String(id.to_string())); } - fn get_id(group: &Group) -> Result { - Ok(get_string_value(group.clone().object_props.id).parse::()?) + fn get_id(group: &Group) -> Result { + Ok(get_string_value(group.clone().object_props.id)) } fn set_title(group: &mut Group, title: &str) { @@ -49,11 +47,11 @@ impl GroupHelper for Group { Ok(get_string_value_opt(group.to_owned().object_props.summary)) } - fn set_creator_id(group: &mut Group, creator_id: i32) { + fn set_creator_id(group: &mut Group, creator_id: String) { group.object_props.attributed_to = Some(Value::String(creator_id.to_string())); } - fn get_creator_id(group: &Group) -> Result { - Ok(get_string_value(group.clone().object_props.attributed_to).parse::()?) + fn get_creator_id(group: &Group) -> Result { + Ok(get_string_value(group.clone().object_props.attributed_to)) } fn set_published(group: &mut Group, published: NaiveDateTime) { @@ -61,8 +59,10 @@ impl GroupHelper for Group { } fn get_published(group: &Group) -> Result { let str = get_string_value(group.to_owned().object_props.published); - // TODO: no idea which date format + // TODO: date parsing is failing, no idea if this is even the right format + dbg!(&str); let date = DateTime::parse_from_rfc2822(&str)?; + dbg!(&date); Ok(date.naive_local()) } diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index 31dc3ced..00b3b27c 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -4,6 +4,7 @@ pub mod post; pub mod puller; pub mod user; use crate::Settings; +use failure::Error; use std::fmt::Display; @@ -95,6 +96,8 @@ mod tests { } } +// TODO: this should take an enum community/user/post for `point` +// TODO: also not sure what exactly `value` should be (numeric id, name string, ...) pub fn make_apub_endpoint(point: S, value: T) -> String { format!( "{}://{}/federation/{}/{}", @@ -105,6 +108,13 @@ pub fn make_apub_endpoint(point: S, value: T) -> String ) } +/// Parses an ID generated by `make_apub_endpoint()`. Will break when federating with anything +/// that is not Lemmy. This is just a crutch until we change the database to store URLs as ID. +pub fn parse_apub_endpoint(id: &str) -> Result<(&str, &str), Error> { + let split = id.split("/").collect::>(); + Ok((split[4], split[5])) +} + pub fn get_apub_protocol_string() -> &'static str { "http" } diff --git a/server/src/apub/puller.rs b/server/src/apub/puller.rs index ce9469fc..52647695 100644 --- a/server/src/apub/puller.rs +++ b/server/src/apub/puller.rs @@ -7,6 +7,7 @@ use crate::apub::group_wrapper::GroupHelper; use crate::db::community_view::CommunityView; use crate::settings::Settings; use activitypub::actor::Group; +use crate::apub::parse_apub_endpoint; // TODO: right now all of the data is requested on demand, for production we will need to store // things in the local database to not ruin the performance @@ -38,7 +39,6 @@ pub fn get_remote_community(identifier: String) -> Result Result()?, name, title: Group::get_title(&community)?, description: Group::get_description(&community)?, category_id: -1, - creator_id: Group::get_creator_id(&community)?, + creator_id: parse_apub_endpoint(&Group::get_creator_id(&community)?)?.1.parse::()?, removed: false, published: Group::get_published(&community)?, updated: Group::get_updated(&community)?, diff --git a/server/src/websocket/server.rs b/server/src/websocket/server.rs index 66b10cd9..727eb7d8 100644 --- a/server/src/websocket/server.rs +++ b/server/src/websocket/server.rs @@ -556,6 +556,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result