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);
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<i32, Error>;
+ fn set_id(group: &mut Group, id: &str);
+ fn get_id(group: &Group) -> Result<String, Error>;
fn set_title(group: &mut Group, title: &str);
fn get_title(group: &Group) -> Result<String, Error>;
fn set_description(group: &mut Group, description: &Option<String>);
fn get_description(group: &Group) -> Result<Option<String>, 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<i32, Error>;
+ fn set_creator_id(group: &mut Group, creator_id: String);
+ fn get_creator_id(group: &Group) -> Result<String, Error>;
fn set_published(group: &mut Group, published: NaiveDateTime);
fn get_published(group: &Group) -> Result<NaiveDateTime, Error>;
// 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<i32, Error> {
- Ok(get_string_value(group.clone().object_props.id).parse::<i32>()?)
+ fn get_id(group: &Group) -> Result<String, Error> {
+ Ok(get_string_value(group.clone().object_props.id))
}
fn set_title(group: &mut Group, title: &str) {
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<i32, Error> {
- Ok(get_string_value(group.clone().object_props.attributed_to).parse::<i32>()?)
+ fn get_creator_id(group: &Group) -> Result<String, Error> {
+ Ok(get_string_value(group.clone().object_props.attributed_to))
}
fn set_published(group: &mut Group, published: NaiveDateTime) {
}
fn get_published(group: &Group) -> Result<NaiveDateTime, Error> {
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())
}
pub mod puller;
pub mod user;
use crate::Settings;
+use failure::Error;
use std::fmt::Display;
}
}
+// 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<S: Display, T: Display>(point: S, value: T) -> String {
format!(
"{}://{}/federation/{}/{}",
)
}
+/// 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::<Vec<&str>>();
+ Ok((split[4], split[5]))
+}
+
pub fn get_apub_protocol_string() -> &'static str {
"http"
}
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
let instance = x[1];
let community_uri = format!("http://{}/federation/c/{}", instance, name);
let community: Group = reqwest::get(&community_uri)?.json()?;
- dbg!(&community);
// TODO: looks like a bunch of data is missing from the activitypub response
// TODO: i dont think simple numeric ids are going to work, we probably need something like uuids
admins: vec![],
community: CommunityView {
// TODO: we need to merge id and name into a single thing (stuff like @user@instance.com)
- id: Group::get_id(&community)?,
+ id: parse_apub_endpoint(&Group::get_id(&community)?)?.1.parse::<i32>()?,
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::<i32>()?,
removed: false,
published: Group::get_published(&community)?,
updated: Group::get_updated(&community)?,
if let Some(community_name) = get_community.name.to_owned() {
if community_name.contains('@') {
// TODO: need to support sort, filter etc for remote communities
+ // TODO: need to to this for http api as well
get_remote_community(community_name)?
} else {
Oper::new(get_community).perform(&conn)?