mirror of https://github.com/LemmyNet/lemmy.git
Store remote communities/posts in db, federate posts!
parent
1b0da74b57
commit
b7103a7e14
|
@ -1,9 +1,8 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::apub::puller::{fetch_all_communities, fetch_remote_community};
|
use crate::apub::{format_community_name, gen_keypair_str, make_apub_endpoint, EndpointType};
|
||||||
use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType};
|
|
||||||
use crate::settings::Settings;
|
|
||||||
use diesel::PgConnection;
|
use diesel::PgConnection;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct GetCommunity {
|
pub struct GetCommunity {
|
||||||
|
@ -41,7 +40,6 @@ pub struct ListCommunities {
|
||||||
pub page: Option<i64>,
|
pub page: Option<i64>,
|
||||||
pub limit: Option<i64>,
|
pub limit: Option<i64>,
|
||||||
pub auth: Option<String>,
|
pub auth: Option<String>,
|
||||||
pub local_only: Option<bool>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
@ -121,13 +119,6 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
|
||||||
fn perform(&self, conn: &PgConnection) -> Result<GetCommunityResponse, Error> {
|
fn perform(&self, conn: &PgConnection) -> Result<GetCommunityResponse, Error> {
|
||||||
let data: &GetCommunity = &self.data;
|
let data: &GetCommunity = &self.data;
|
||||||
|
|
||||||
if data.name.is_some()
|
|
||||||
&& Settings::get().federation.enabled
|
|
||||||
&& data.name.as_ref().unwrap().contains('@')
|
|
||||||
{
|
|
||||||
return fetch_remote_community(data.name.as_ref().unwrap());
|
|
||||||
}
|
|
||||||
|
|
||||||
let user_id: Option<i32> = match &data.auth {
|
let user_id: Option<i32> = match &data.auth {
|
||||||
Some(auth) => match Claims::decode(&auth) {
|
Some(auth) => match Claims::decode(&auth) {
|
||||||
Ok(claims) => {
|
Ok(claims) => {
|
||||||
|
@ -139,25 +130,25 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let community_id = match data.id {
|
let community = match data.id {
|
||||||
Some(id) => id,
|
Some(id) => Community::read(&conn, id)?,
|
||||||
None => {
|
None => {
|
||||||
match Community::read_from_name(
|
match Community::read_from_name(
|
||||||
&conn,
|
&conn,
|
||||||
data.name.to_owned().unwrap_or_else(|| "main".to_string()),
|
data.name.to_owned().unwrap_or_else(|| "main".to_string()),
|
||||||
) {
|
) {
|
||||||
Ok(community) => community.id,
|
Ok(community) => community,
|
||||||
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
|
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let community_view = match CommunityView::read(&conn, community_id, user_id) {
|
let mut community_view = match CommunityView::read(&conn, community.id, user_id) {
|
||||||
Ok(community) => community,
|
Ok(community) => community,
|
||||||
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
|
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let moderators = match CommunityModeratorView::for_community(&conn, community_id) {
|
let moderators = match CommunityModeratorView::for_community(&conn, community.id) {
|
||||||
Ok(moderators) => moderators,
|
Ok(moderators) => moderators,
|
||||||
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
|
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
|
||||||
};
|
};
|
||||||
|
@ -168,6 +159,12 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
|
||||||
let creator_user = admins.remove(creator_index);
|
let creator_user = admins.remove(creator_index);
|
||||||
admins.insert(0, creator_user);
|
admins.insert(0, creator_user);
|
||||||
|
|
||||||
|
if !community.local {
|
||||||
|
let domain = Url::parse(&community.actor_id)?;
|
||||||
|
community_view.name =
|
||||||
|
format_community_name(&community_view.name.to_string(), domain.host_str().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
// Return the jwt
|
// Return the jwt
|
||||||
Ok(GetCommunityResponse {
|
Ok(GetCommunityResponse {
|
||||||
community: community_view,
|
community: community_view,
|
||||||
|
@ -226,6 +223,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> {
|
||||||
private_key: Some(community_private_key),
|
private_key: Some(community_private_key),
|
||||||
public_key: Some(community_public_key),
|
public_key: Some(community_public_key),
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = match Community::create(&conn, &community_form) {
|
let inserted_community = match Community::create(&conn, &community_form) {
|
||||||
|
@ -323,6 +321,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> {
|
||||||
private_key: read_community.private_key,
|
private_key: read_community.private_key,
|
||||||
public_key: read_community.public_key,
|
public_key: read_community.public_key,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
|
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
|
||||||
|
@ -358,13 +357,6 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
|
||||||
fn perform(&self, conn: &PgConnection) -> Result<ListCommunitiesResponse, Error> {
|
fn perform(&self, conn: &PgConnection) -> Result<ListCommunitiesResponse, Error> {
|
||||||
let data: &ListCommunities = &self.data;
|
let data: &ListCommunities = &self.data;
|
||||||
|
|
||||||
let local_only = data.local_only.unwrap_or(false);
|
|
||||||
if Settings::get().federation.enabled && !local_only {
|
|
||||||
return Ok(ListCommunitiesResponse {
|
|
||||||
communities: fetch_all_communities()?,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let user_claims: Option<Claims> = match &data.auth {
|
let user_claims: Option<Claims> = match &data.auth {
|
||||||
Some(auth) => match Claims::decode(&auth) {
|
Some(auth) => match Claims::decode(&auth) {
|
||||||
Ok(claims) => Some(claims.claims),
|
Ok(claims) => Some(claims.claims),
|
||||||
|
@ -591,6 +583,7 @@ impl Perform<GetCommunityResponse> for Oper<TransferCommunity> {
|
||||||
private_key: read_community.private_key,
|
private_key: read_community.private_key,
|
||||||
public_key: read_community.public_key,
|
public_key: read_community.public_key,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _updated_community = match Community::update(&conn, data.community_id, &community_form) {
|
let _updated_community = match Community::update(&conn, data.community_id, &community_form) {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::settings::Settings;
|
|
||||||
use diesel::PgConnection;
|
use diesel::PgConnection;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
@ -133,6 +132,7 @@ impl Perform<PostResponse> for Oper<CreatePost> {
|
||||||
thumbnail_url: pictshare_thumbnail,
|
thumbnail_url: pictshare_thumbnail,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = match Post::create(&conn, &post_form) {
|
let inserted_post = match Post::create(&conn, &post_form) {
|
||||||
|
@ -228,11 +228,6 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
|
||||||
fn perform(&self, conn: &PgConnection) -> Result<GetPostsResponse, Error> {
|
fn perform(&self, conn: &PgConnection) -> Result<GetPostsResponse, Error> {
|
||||||
let data: &GetPosts = &self.data;
|
let data: &GetPosts = &self.data;
|
||||||
|
|
||||||
if Settings::get().federation.enabled {
|
|
||||||
// TODO: intercept here (but the type is wrong)
|
|
||||||
//get_remote_community_posts(get_posts.community_id.unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
let user_claims: Option<Claims> = match &data.auth {
|
let user_claims: Option<Claims> = match &data.auth {
|
||||||
Some(auth) => match Claims::decode(&auth) {
|
Some(auth) => match Claims::decode(&auth) {
|
||||||
Ok(claims) => Some(claims.claims),
|
Ok(claims) => Some(claims.claims),
|
||||||
|
@ -398,6 +393,7 @@ impl Perform<PostResponse> for Oper<EditPost> {
|
||||||
thumbnail_url: pictshare_thumbnail,
|
thumbnail_url: pictshare_thumbnail,
|
||||||
ap_id: read_post.ap_id,
|
ap_id: read_post.ap_id,
|
||||||
local: read_post.local,
|
local: read_post.local,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
|
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
|
||||||
|
|
|
@ -317,6 +317,7 @@ impl Perform<LoginResponse> for Oper<Register> {
|
||||||
private_key: Some(community_private_key),
|
private_key: Some(community_private_key),
|
||||||
public_key: Some(community_public_key),
|
public_key: Some(community_public_key),
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
Community::create(&conn, &community_form).unwrap()
|
Community::create(&conn, &community_form).unwrap()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::apub::puller::fetch_remote_object;
|
use crate::apub::puller::fetch_remote_object;
|
||||||
use crate::apub::*;
|
use crate::apub::*;
|
||||||
use crate::convert_datetime;
|
use crate::convert_datetime;
|
||||||
use crate::db::community::Community;
|
use crate::db::community::{Community, CommunityForm};
|
||||||
use crate::db::community_view::{CommunityFollowerView, CommunityView};
|
use crate::db::community_view::CommunityFollowerView;
|
||||||
use crate::db::establish_unpooled_connection;
|
use crate::db::establish_unpooled_connection;
|
||||||
use crate::db::post::Post;
|
use crate::db::post::Post;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
@ -85,47 +85,34 @@ impl Community {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommunityView {
|
impl CommunityForm {
|
||||||
pub fn from_group(group: &GroupExt, domain: &str) -> Result<CommunityView, Error> {
|
pub fn from_group(group: &GroupExt) -> Result<Self, Error> {
|
||||||
let followers_uri = &group.extension.get_followers().unwrap().to_string();
|
let followers_uri = &group.extension.get_followers().unwrap().to_string();
|
||||||
let outbox_uri = &group.extension.get_outbox().to_string();
|
let outbox_uri = &group.extension.get_outbox().to_string();
|
||||||
let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
|
let _outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
|
||||||
let followers = fetch_remote_object::<UnorderedCollection>(followers_uri)?;
|
let _followers = fetch_remote_object::<UnorderedCollection>(followers_uri)?;
|
||||||
let oprops = &group.base.object_props;
|
let oprops = &group.base.object_props;
|
||||||
let aprops = &group.extension;
|
let aprops = &group.extension;
|
||||||
Ok(CommunityView {
|
Ok(CommunityForm {
|
||||||
// TODO: we need to merge id and name into a single thing (stuff like @user@instance.com)
|
name: oprops.get_name_xsd_string().unwrap().to_string(),
|
||||||
id: 1337, //community.object_props.get_id()
|
|
||||||
name: format_community_name(&oprops.get_name_xsd_string().unwrap().to_string(), domain),
|
|
||||||
title: aprops.get_preferred_username().unwrap().to_string(),
|
title: aprops.get_preferred_username().unwrap().to_string(),
|
||||||
description: oprops.get_summary_xsd_string().map(|s| s.to_string()),
|
description: oprops.get_summary_xsd_string().map(|s| s.to_string()),
|
||||||
category_id: -1,
|
category_id: 1,
|
||||||
creator_id: -1, //community.object_props.get_attributed_to_xsd_any_uri()
|
creator_id: 2, //community.object_props.get_attributed_to_xsd_any_uri()
|
||||||
removed: false,
|
removed: None,
|
||||||
published: oprops
|
published: oprops
|
||||||
.get_published()
|
.get_published()
|
||||||
.unwrap()
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
||||||
.as_ref()
|
|
||||||
.naive_local()
|
|
||||||
.to_owned(),
|
|
||||||
updated: oprops
|
updated: oprops
|
||||||
.get_updated()
|
.get_updated()
|
||||||
.map(|u| u.as_ref().to_owned().naive_local()),
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
||||||
deleted: false,
|
deleted: None,
|
||||||
nsfw: false,
|
nsfw: false,
|
||||||
creator_name: "".to_string(),
|
actor_id: oprops.get_id().unwrap().to_string(),
|
||||||
creator_avatar: None,
|
local: false,
|
||||||
category_name: "".to_string(),
|
private_key: None,
|
||||||
number_of_subscribers: *followers
|
public_key: None,
|
||||||
.collection_props
|
last_refreshed_at: None,
|
||||||
.get_total_items()
|
|
||||||
.unwrap()
|
|
||||||
.as_ref() as i64,
|
|
||||||
number_of_posts: *outbox.collection_props.get_total_items().unwrap().as_ref() as i64,
|
|
||||||
number_of_comments: -1,
|
|
||||||
hot_rank: -1,
|
|
||||||
user_id: None,
|
|
||||||
subscribed: None,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,14 +99,3 @@ pub fn get_following_instances() -> Vec<&'static str> {
|
||||||
.split(',')
|
.split(',')
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a tuple of (username, domain) from an identifier like "main@dev.lemmy.ml"
|
|
||||||
fn split_identifier(identifier: &str) -> (String, String) {
|
|
||||||
let x: Vec<&str> = identifier.split('@').collect();
|
|
||||||
(x[0].replace("!", ""), x[1].to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_remote_community_uri(identifier: &str) -> String {
|
|
||||||
let (name, domain) = split_identifier(identifier);
|
|
||||||
format!("http://{}/federation/c/{}", domain, name)
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType};
|
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType};
|
||||||
use crate::db::post::Post;
|
use crate::convert_datetime;
|
||||||
use crate::db::post_view::PostView;
|
use crate::db::post::{Post, PostForm};
|
||||||
use crate::{convert_datetime, naive_now};
|
|
||||||
use activitystreams::{object::properties::ObjectProperties, object::Page};
|
use activitystreams::{object::properties::ObjectProperties, object::Page};
|
||||||
use actix_web::body::Body;
|
use actix_web::body::Body;
|
||||||
use actix_web::web::Path;
|
use actix_web::web::Path;
|
||||||
|
@ -61,53 +60,32 @@ impl Post {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PostView {
|
impl PostForm {
|
||||||
pub fn from_page(page: &Page) -> Result<PostView, Error> {
|
pub fn from_page(page: &Page) -> Result<PostForm, Error> {
|
||||||
let oprops = &page.object_props;
|
let oprops = &page.object_props;
|
||||||
Ok(PostView {
|
Ok(PostForm {
|
||||||
id: -1,
|
|
||||||
name: oprops.get_name_xsd_string().unwrap().to_string(),
|
name: oprops.get_name_xsd_string().unwrap().to_string(),
|
||||||
url: oprops.get_url_xsd_any_uri().map(|u| u.to_string()),
|
url: oprops.get_url_xsd_any_uri().map(|u| u.to_string()),
|
||||||
body: oprops.get_content_xsd_string().map(|c| c.to_string()),
|
body: oprops.get_content_xsd_string().map(|c| c.to_string()),
|
||||||
creator_id: -1,
|
creator_id: 2,
|
||||||
community_id: -1,
|
community_id: -1,
|
||||||
removed: false,
|
removed: None,
|
||||||
locked: false,
|
locked: None,
|
||||||
published: oprops
|
published: oprops
|
||||||
.get_published()
|
.get_published()
|
||||||
.unwrap()
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
||||||
.as_ref()
|
|
||||||
.naive_local()
|
|
||||||
.to_owned(),
|
|
||||||
updated: oprops
|
updated: oprops
|
||||||
.get_updated()
|
.get_updated()
|
||||||
.map(|u| u.as_ref().to_owned().naive_local()),
|
.map(|u| u.as_ref().to_owned().naive_local()),
|
||||||
deleted: false,
|
deleted: None,
|
||||||
nsfw: false,
|
nsfw: false,
|
||||||
stickied: false,
|
stickied: None,
|
||||||
embed_title: None,
|
embed_title: None,
|
||||||
embed_description: None,
|
embed_description: None,
|
||||||
embed_html: None,
|
embed_html: None,
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
banned: false,
|
ap_id: oprops.get_id().unwrap().to_string(),
|
||||||
banned_from_community: false,
|
local: false,
|
||||||
creator_name: "".to_string(),
|
|
||||||
creator_avatar: None,
|
|
||||||
community_name: "".to_string(),
|
|
||||||
community_removed: false,
|
|
||||||
community_deleted: false,
|
|
||||||
community_nsfw: false,
|
|
||||||
number_of_comments: -1,
|
|
||||||
score: -1,
|
|
||||||
upvotes: -1,
|
|
||||||
downvotes: -1,
|
|
||||||
hot_rank: -1,
|
|
||||||
newest_activity_time: naive_now(),
|
|
||||||
user_id: None,
|
|
||||||
my_vote: None,
|
|
||||||
subscribed: None,
|
|
||||||
read: None,
|
|
||||||
saved: None,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
use crate::api::community::GetCommunityResponse;
|
|
||||||
use crate::api::post::GetPostsResponse;
|
|
||||||
use crate::apub::*;
|
use crate::apub::*;
|
||||||
use crate::db::community_view::CommunityView;
|
use crate::db::community::{Community, CommunityForm};
|
||||||
use crate::db::post_view::PostView;
|
use crate::db::post::{Post, PostForm};
|
||||||
|
use crate::db::Crud;
|
||||||
use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
|
use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use activitystreams::collection::{OrderedCollection, UnorderedCollection};
|
use activitystreams::collection::{OrderedCollection, UnorderedCollection};
|
||||||
use activitystreams::object::Page;
|
use activitystreams::object::Page;
|
||||||
use activitystreams::BaseBox;
|
use activitystreams::BaseBox;
|
||||||
|
use diesel::PgConnection;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use isahc::prelude::*;
|
use isahc::prelude::*;
|
||||||
use log::warn;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
@ -24,7 +23,7 @@ fn fetch_node_info(domain: &str) -> Result<NodeInfo, Error> {
|
||||||
Ok(fetch_remote_object::<NodeInfo>(&well_known.links.href)?)
|
Ok(fetch_remote_object::<NodeInfo>(&well_known.links.href)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_communities_from_instance(domain: &str) -> Result<Vec<CommunityView>, Error> {
|
fn fetch_communities_from_instance(domain: &str) -> Result<Vec<CommunityForm>, Error> {
|
||||||
let node_info = fetch_node_info(domain)?;
|
let node_info = fetch_node_info(domain)?;
|
||||||
|
|
||||||
if let Some(community_list_url) = node_info.metadata.community_list_url {
|
if let Some(community_list_url) = node_info.metadata.community_list_url {
|
||||||
|
@ -33,10 +32,10 @@ fn fetch_communities_from_instance(domain: &str) -> Result<Vec<CommunityView>, E
|
||||||
.collection_props
|
.collection_props
|
||||||
.get_many_items_base_boxes()
|
.get_many_items_base_boxes()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let communities: Result<Vec<CommunityView>, Error> = object_boxes
|
let communities: Result<Vec<CommunityForm>, Error> = object_boxes
|
||||||
.map(|c| -> Result<CommunityView, Error> {
|
.map(|c| {
|
||||||
let group = c.to_owned().to_concrete::<GroupExt>()?;
|
let group = c.to_owned().to_concrete::<GroupExt>()?;
|
||||||
CommunityView::from_group(&group, domain)
|
CommunityForm::from_group(&group)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Ok(communities?)
|
Ok(communities?)
|
||||||
|
@ -69,44 +68,51 @@ where
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
|
fn fetch_remote_community_posts(instance: &str, community: &str) -> Result<Vec<PostForm>, Error> {
|
||||||
let community = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?;
|
let endpoint = format!("http://{}/federation/c/{}", instance, community);
|
||||||
|
let community = fetch_remote_object::<GroupExt>(&endpoint)?;
|
||||||
let outbox_uri = &community.extension.get_outbox().to_string();
|
let outbox_uri = &community.extension.get_outbox().to_string();
|
||||||
let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
|
let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
|
||||||
let items = outbox.collection_props.get_many_items_base_boxes();
|
let items = outbox.collection_props.get_many_items_base_boxes();
|
||||||
|
|
||||||
let posts: Result<Vec<PostView>, Error> = items
|
let posts = items
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.map(|obox: &BaseBox| {
|
.map(|obox: &BaseBox| {
|
||||||
let page = obox.clone().to_concrete::<Page>().unwrap();
|
let page = obox.clone().to_concrete::<Page>().unwrap();
|
||||||
PostView::from_page(&page)
|
PostForm::from_page(&page)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect::<Result<Vec<PostForm>, Error>>()?;
|
||||||
Ok(GetPostsResponse { posts: posts? })
|
Ok(posts)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> {
|
// TODO: in the future, this should only be done when an instance is followed for the first time
|
||||||
let group = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?;
|
// after that, we should rely in the inbox, and fetch on demand when needed
|
||||||
// TODO: this is only for testing until we can call that function from GetPosts
|
pub fn fetch_all(conn: &PgConnection) -> Result<(), Error> {
|
||||||
// (once string ids are supported)
|
|
||||||
//dbg!(get_remote_community_posts(identifier)?);
|
|
||||||
|
|
||||||
let (_, domain) = split_identifier(identifier);
|
|
||||||
Ok(GetCommunityResponse {
|
|
||||||
moderators: vec![],
|
|
||||||
admins: vec![],
|
|
||||||
community: CommunityView::from_group(&group, &domain)?,
|
|
||||||
online: 0,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fetch_all_communities() -> Result<Vec<CommunityView>, Error> {
|
|
||||||
let mut communities_list: Vec<CommunityView> = vec![];
|
|
||||||
for instance in &get_following_instances() {
|
for instance in &get_following_instances() {
|
||||||
match fetch_communities_from_instance(instance) {
|
let communities = fetch_communities_from_instance(instance)?;
|
||||||
Ok(mut c) => communities_list.append(c.as_mut()),
|
|
||||||
Err(e) => warn!("Failed to fetch instance list from remote instance: {}", e),
|
for community in &communities {
|
||||||
};
|
let existing = Community::read_from_actor_id(conn, &community.actor_id);
|
||||||
|
let community_id = match existing {
|
||||||
|
// TODO: should make sure that this is actually a `NotFound` error
|
||||||
|
Err(_) => Community::create(conn, community)?.id,
|
||||||
|
Ok(c) => Community::update(conn, c.id, community)?.id,
|
||||||
|
};
|
||||||
|
let mut posts = fetch_remote_community_posts(instance, &community.name)?;
|
||||||
|
for post_ in &mut posts {
|
||||||
|
post_.community_id = community_id;
|
||||||
|
let existing = Post::read_from_apub_id(conn, &post_.ap_id);
|
||||||
|
match existing {
|
||||||
|
// TODO: should make sure that this is actually a `NotFound` error
|
||||||
|
Err(_) => {
|
||||||
|
Post::create(conn, post_)?;
|
||||||
|
}
|
||||||
|
Ok(p) => {
|
||||||
|
Post::update(conn, p.id, post_)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(communities_list)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,6 +93,7 @@ fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), Error> {
|
||||||
private_key: Some(community_private_key),
|
private_key: Some(community_private_key),
|
||||||
public_key: Some(community_public_key),
|
public_key: Some(community_public_key),
|
||||||
last_refreshed_at: Some(naive_now()),
|
last_refreshed_at: Some(naive_now()),
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
Community::update(&conn, ccommunity.id, &form)?;
|
Community::update(&conn, ccommunity.id, &form)?;
|
||||||
|
|
|
@ -247,6 +247,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
@ -269,6 +270,7 @@ mod tests {
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
||||||
|
|
|
@ -474,6 +474,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
@ -496,6 +497,7 @@ mod tests {
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub struct Community {
|
||||||
pub last_refreshed_at: chrono::NaiveDateTime,
|
pub last_refreshed_at: chrono::NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
|
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize, Debug)]
|
||||||
#[table_name = "community"]
|
#[table_name = "community"]
|
||||||
pub struct CommunityForm {
|
pub struct CommunityForm {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -31,6 +31,7 @@ pub struct CommunityForm {
|
||||||
pub category_id: i32,
|
pub category_id: i32,
|
||||||
pub creator_id: i32,
|
pub creator_id: i32,
|
||||||
pub removed: Option<bool>,
|
pub removed: Option<bool>,
|
||||||
|
pub published: Option<chrono::NaiveDateTime>,
|
||||||
pub updated: Option<chrono::NaiveDateTime>,
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
pub deleted: Option<bool>,
|
pub deleted: Option<bool>,
|
||||||
pub nsfw: bool,
|
pub nsfw: bool,
|
||||||
|
@ -79,6 +80,13 @@ impl Community {
|
||||||
.first::<Self>(conn)
|
.first::<Self>(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_from_actor_id(conn: &PgConnection, community_id: &str) -> Result<Self, Error> {
|
||||||
|
use crate::schema::community::dsl::*;
|
||||||
|
community
|
||||||
|
.filter(actor_id.eq(community_id))
|
||||||
|
.first::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn list(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
pub fn list(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
||||||
use crate::schema::community::dsl::*;
|
use crate::schema::community::dsl::*;
|
||||||
community.load::<Community>(conn)
|
community.load::<Community>(conn)
|
||||||
|
@ -271,6 +279,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
|
|
@ -505,6 +505,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
@ -527,6 +528,7 @@ mod tests {
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub struct Post {
|
||||||
pub local: bool,
|
pub local: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable, AsChangeset, Clone)]
|
#[derive(Insertable, AsChangeset, Clone, Debug)]
|
||||||
#[table_name = "post"]
|
#[table_name = "post"]
|
||||||
pub struct PostForm {
|
pub struct PostForm {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
@ -37,6 +37,7 @@ pub struct PostForm {
|
||||||
pub community_id: i32,
|
pub community_id: i32,
|
||||||
pub removed: Option<bool>,
|
pub removed: Option<bool>,
|
||||||
pub locked: Option<bool>,
|
pub locked: Option<bool>,
|
||||||
|
pub published: Option<chrono::NaiveDateTime>,
|
||||||
pub updated: Option<chrono::NaiveDateTime>,
|
pub updated: Option<chrono::NaiveDateTime>,
|
||||||
pub deleted: Option<bool>,
|
pub deleted: Option<bool>,
|
||||||
pub nsfw: bool,
|
pub nsfw: bool,
|
||||||
|
@ -65,6 +66,11 @@ impl Post {
|
||||||
.load::<Self>(conn)
|
.load::<Self>(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Self, Error> {
|
||||||
|
use crate::schema::post::dsl::*;
|
||||||
|
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update_ap_id(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
|
pub fn update_ap_id(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
|
||||||
use crate::schema::post::dsl::*;
|
use crate::schema::post::dsl::*;
|
||||||
|
|
||||||
|
@ -105,11 +111,13 @@ impl Crud<PostForm> for Post {
|
||||||
|
|
||||||
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
|
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
|
||||||
use crate::schema::post::dsl::*;
|
use crate::schema::post::dsl::*;
|
||||||
|
dbg!(&new_post);
|
||||||
insert_into(post).values(new_post).get_result::<Self>(conn)
|
insert_into(post).values(new_post).get_result::<Self>(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
|
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
|
||||||
use crate::schema::post::dsl::*;
|
use crate::schema::post::dsl::*;
|
||||||
|
dbg!(&new_post);
|
||||||
diesel::update(post.find(post_id))
|
diesel::update(post.find(post_id))
|
||||||
.set(new_post)
|
.set(new_post)
|
||||||
.get_result::<Self>(conn)
|
.get_result::<Self>(conn)
|
||||||
|
@ -280,6 +288,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
@ -302,6 +311,7 @@ mod tests {
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
||||||
|
|
|
@ -399,6 +399,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
@ -421,6 +422,7 @@ mod tests {
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
||||||
|
|
|
@ -131,6 +131,7 @@ mod tests {
|
||||||
private_key: None,
|
private_key: None,
|
||||||
public_key: None,
|
public_key: None,
|
||||||
last_refreshed_at: None,
|
last_refreshed_at: None,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
let inserted_community = Community::create(&conn, &new_community).unwrap();
|
||||||
|
@ -153,6 +154,7 @@ mod tests {
|
||||||
thumbnail_url: None,
|
thumbnail_url: None,
|
||||||
ap_id: "changeme".into(),
|
ap_id: "changeme".into(),
|
||||||
local: true,
|
local: true,
|
||||||
|
published: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
let inserted_post = Post::create(&conn, &new_post).unwrap();
|
||||||
|
|
|
@ -6,16 +6,21 @@ use actix::prelude::*;
|
||||||
use actix_web::*;
|
use actix_web::*;
|
||||||
use diesel::r2d2::{ConnectionManager, Pool};
|
use diesel::r2d2::{ConnectionManager, Pool};
|
||||||
use diesel::PgConnection;
|
use diesel::PgConnection;
|
||||||
|
use failure::Error;
|
||||||
|
use lemmy_server::apub::puller::fetch_all;
|
||||||
use lemmy_server::db::code_migrations::run_advanced_migrations;
|
use lemmy_server::db::code_migrations::run_advanced_migrations;
|
||||||
use lemmy_server::routes::{api, federation, feeds, index, nodeinfo, webfinger, websocket};
|
use lemmy_server::routes::{api, federation, feeds, index, nodeinfo, webfinger, websocket};
|
||||||
use lemmy_server::settings::Settings;
|
use lemmy_server::settings::Settings;
|
||||||
use lemmy_server::websocket::server::*;
|
use lemmy_server::websocket::server::*;
|
||||||
use std::io;
|
use log::warn;
|
||||||
|
use std::thread;
|
||||||
|
use std::thread::sleep;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
embed_migrations!();
|
embed_migrations!();
|
||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() -> io::Result<()> {
|
async fn main() -> Result<(), Error> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let settings = Settings::get();
|
let settings = Settings::get();
|
||||||
|
|
||||||
|
@ -34,36 +39,50 @@ async fn main() -> io::Result<()> {
|
||||||
// Set up websocket server
|
// Set up websocket server
|
||||||
let server = ChatServer::startup(pool.clone()).start();
|
let server = ChatServer::startup(pool.clone()).start();
|
||||||
|
|
||||||
|
// TODO: its probably failing because the other instance is not up yet
|
||||||
|
// need to make a new thread and wait a bit before fetching
|
||||||
|
thread::spawn(move || {
|
||||||
|
// some work here
|
||||||
|
sleep(Duration::from_secs(5));
|
||||||
|
println!("Fetching apub data");
|
||||||
|
match fetch_all(&conn) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => warn!("Error during apub fetch: {}", e),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Starting http server at {}:{}",
|
"Starting http server at {}:{}",
|
||||||
settings.bind, settings.port
|
settings.bind, settings.port
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create Http server with websocket support
|
// Create Http server with websocket support
|
||||||
HttpServer::new(move || {
|
Ok(
|
||||||
App::new()
|
HttpServer::new(move || {
|
||||||
.wrap(middleware::Logger::default())
|
App::new()
|
||||||
.data(pool.clone())
|
.wrap(middleware::Logger::default())
|
||||||
.data(server.clone())
|
.data(pool.clone())
|
||||||
// The routes
|
.data(server.clone())
|
||||||
.configure(api::config)
|
// The routes
|
||||||
.configure(federation::config)
|
.configure(api::config)
|
||||||
.configure(feeds::config)
|
.configure(federation::config)
|
||||||
.configure(index::config)
|
.configure(feeds::config)
|
||||||
.configure(nodeinfo::config)
|
.configure(index::config)
|
||||||
.configure(webfinger::config)
|
.configure(nodeinfo::config)
|
||||||
.configure(websocket::config)
|
.configure(webfinger::config)
|
||||||
// static files
|
.configure(websocket::config)
|
||||||
.service(actix_files::Files::new(
|
// static files
|
||||||
"/static",
|
.service(actix_files::Files::new(
|
||||||
settings.front_end_dir.to_owned(),
|
"/static",
|
||||||
))
|
settings.front_end_dir.to_owned(),
|
||||||
.service(actix_files::Files::new(
|
))
|
||||||
"/docs",
|
.service(actix_files::Files::new(
|
||||||
settings.front_end_dir.to_owned() + "/documentation",
|
"/docs",
|
||||||
))
|
settings.front_end_dir.to_owned() + "/documentation",
|
||||||
})
|
))
|
||||||
.bind((settings.bind, settings.port))?
|
})
|
||||||
.run()
|
.bind((settings.bind, settings.port))?
|
||||||
.await
|
.run()
|
||||||
|
.await?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue