2019-12-19 21:59:13 +00:00
|
|
|
pub mod community;
|
|
|
|
pub mod post;
|
2019-12-27 17:25:07 +00:00
|
|
|
pub mod puller;
|
2019-12-19 21:59:13 +00:00
|
|
|
pub mod user;
|
|
|
|
use crate::Settings;
|
2020-03-11 17:26:58 +00:00
|
|
|
use failure::Error;
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
use std::fmt::Display;
|
2020-03-12 00:01:25 +00:00
|
|
|
use url::Url;
|
2019-12-19 21:59:13 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::db::community::Community;
|
|
|
|
use crate::db::post::Post;
|
|
|
|
use crate::db::user::User_;
|
|
|
|
use crate::db::{ListingType, SortType};
|
|
|
|
use crate::{naive_now, Settings};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_person() {
|
|
|
|
let user = User_ {
|
|
|
|
id: 52,
|
|
|
|
name: "thom".into(),
|
|
|
|
fedi_name: "rrf".into(),
|
|
|
|
preferred_username: None,
|
|
|
|
password_encrypted: "here".into(),
|
|
|
|
email: None,
|
2020-01-22 21:35:29 +00:00
|
|
|
matrix_user_id: None,
|
2019-12-29 20:39:48 +00:00
|
|
|
avatar: None,
|
2019-12-19 21:59:13 +00:00
|
|
|
published: naive_now(),
|
|
|
|
admin: false,
|
|
|
|
banned: false,
|
|
|
|
updated: None,
|
|
|
|
show_nsfw: false,
|
|
|
|
theme: "darkly".into(),
|
|
|
|
default_sort_type: SortType::Hot as i16,
|
|
|
|
default_listing_type: ListingType::Subscribed as i16,
|
|
|
|
lang: "browser".into(),
|
2020-01-02 21:55:54 +00:00
|
|
|
show_avatars: true,
|
|
|
|
send_notifications_to_email: false,
|
2019-12-19 21:59:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let person = user.as_person();
|
|
|
|
assert_eq!(
|
|
|
|
format!("https://{}/federation/u/thom", Settings::get().hostname),
|
2020-03-12 00:01:25 +00:00
|
|
|
person.unwrap().object_props.get_id().unwrap().to_string()
|
2019-12-19 21:59:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_community() {
|
|
|
|
let community = Community {
|
|
|
|
id: 42,
|
|
|
|
name: "Test".into(),
|
|
|
|
title: "Test Title".into(),
|
|
|
|
description: Some("Test community".into()),
|
|
|
|
category_id: 32,
|
|
|
|
creator_id: 52,
|
|
|
|
removed: false,
|
|
|
|
published: naive_now(),
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
deleted: false,
|
|
|
|
nsfw: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let group = community.as_group();
|
|
|
|
assert_eq!(
|
|
|
|
format!("https://{}/federation/c/Test", Settings::get().hostname),
|
2020-03-12 00:01:25 +00:00
|
|
|
group.unwrap().object_props.get_id().unwrap().to_string()
|
2019-12-19 21:59:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_post() {
|
|
|
|
let post = Post {
|
|
|
|
id: 62,
|
|
|
|
name: "A test post".into(),
|
|
|
|
url: None,
|
|
|
|
body: None,
|
|
|
|
creator_id: 52,
|
|
|
|
community_id: 42,
|
|
|
|
published: naive_now(),
|
|
|
|
removed: false,
|
|
|
|
locked: false,
|
|
|
|
stickied: false,
|
|
|
|
nsfw: false,
|
|
|
|
deleted: false,
|
|
|
|
updated: None,
|
2020-03-07 23:31:13 +00:00
|
|
|
embed_title: None,
|
|
|
|
embed_description: None,
|
|
|
|
embed_html: None,
|
|
|
|
thumbnail_url: None,
|
2019-12-19 21:59:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let page = post.as_page();
|
|
|
|
assert_eq!(
|
|
|
|
format!("https://{}/federation/post/62", Settings::get().hostname),
|
2020-03-12 00:01:25 +00:00
|
|
|
page.unwrap().object_props.get_id().unwrap().to_string()
|
2019-12-19 21:59:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 17:26:58 +00:00
|
|
|
// TODO: this should take an enum community/user/post for `point`
|
|
|
|
// TODO: also not sure what exactly `value` should be (numeric id, name string, ...)
|
2020-03-12 00:01:25 +00:00
|
|
|
pub fn make_apub_endpoint<S: Display, T: Display>(point: S, value: T) -> Url {
|
|
|
|
Url::parse(&format!(
|
2020-02-29 17:38:47 +00:00
|
|
|
"{}://{}/federation/{}/{}",
|
|
|
|
get_apub_protocol_string(),
|
2019-12-19 21:59:13 +00:00
|
|
|
Settings::get().hostname,
|
|
|
|
point,
|
|
|
|
value
|
2020-03-12 00:01:25 +00:00
|
|
|
))
|
|
|
|
.unwrap()
|
2019-12-19 21:59:13 +00:00
|
|
|
}
|
2020-02-29 17:38:47 +00:00
|
|
|
|
2020-03-11 17:26:58 +00:00
|
|
|
/// 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> {
|
2020-03-12 00:01:25 +00:00
|
|
|
let split = id.split('/').collect::<Vec<&str>>();
|
2020-03-11 17:26:58 +00:00
|
|
|
Ok((split[4], split[5]))
|
|
|
|
}
|
|
|
|
|
2020-02-29 17:38:47 +00:00
|
|
|
pub fn get_apub_protocol_string() -> &'static str {
|
|
|
|
"http"
|
|
|
|
}
|