use urls for id again, more comments

This commit is contained in:
Felix 2020-03-11 18:26:58 +01:00
parent 18be8b10f5
commit 8867fa1d52
5 changed files with 31 additions and 19 deletions

View file

@ -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);

View file

@ -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<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>;
@ -15,9 +14,8 @@ pub trait GroupHelper {
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>;
@ -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<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) {
@ -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<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) {
@ -61,8 +59,10 @@ impl GroupHelper for Group {
}
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())
}

View file

@ -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<S: Display, T: Display>(point: S, value: T) -> String {
format!(
"{}://{}/federation/{}/{}",
@ -105,6 +108,13 @@ pub fn make_apub_endpoint<S: Display, T: Display>(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::<Vec<&str>>();
Ok((split[4], split[5]))
}
pub fn get_apub_protocol_string() -> &'static str {
"http"
}

View file

@ -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<GetCommunityResponse,
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
@ -47,12 +47,12 @@ pub fn get_remote_community(identifier: String) -> Result<GetCommunityResponse,
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)?,

View file

@ -556,6 +556,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
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)?