use super::*;
-use crate::apub::puller::{fetch_all_communities, fetch_remote_community};
-use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType};
-use crate::settings::Settings;
+use crate::apub::{format_community_name, gen_keypair_str, make_apub_endpoint, EndpointType};
use diesel::PgConnection;
use std::str::FromStr;
+use url::Url;
#[derive(Serialize, Deserialize)]
pub struct GetCommunity {
pub page: Option<i64>,
pub limit: Option<i64>,
pub auth: Option<String>,
- pub local_only: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug)]
fn perform(&self, conn: &PgConnection) -> Result<GetCommunityResponse, Error> {
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 {
Some(auth) => match Claims::decode(&auth) {
Ok(claims) => {
None => None,
};
- let community_id = match data.id {
- Some(id) => id,
+ let community = match data.id {
+ Some(id) => Community::read(&conn, id)?,
None => {
match Community::read_from_name(
&conn,
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()),
}
}
};
- 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,
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,
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
let creator_user = admins.remove(creator_index);
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
Ok(GetCommunityResponse {
community: community_view,
private_key: Some(community_private_key),
public_key: Some(community_public_key),
last_refreshed_at: None,
+ published: None,
};
let inserted_community = match Community::create(&conn, &community_form) {
private_key: read_community.private_key,
public_key: read_community.public_key,
last_refreshed_at: None,
+ published: None,
};
let _updated_community = match Community::update(&conn, data.edit_id, &community_form) {
fn perform(&self, conn: &PgConnection) -> Result<ListCommunitiesResponse, Error> {
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 {
Some(auth) => match Claims::decode(&auth) {
Ok(claims) => Some(claims.claims),
private_key: read_community.private_key,
public_key: read_community.public_key,
last_refreshed_at: None,
+ published: None,
};
let _updated_community = match Community::update(&conn, data.community_id, &community_form) {
use super::*;
-use crate::settings::Settings;
use diesel::PgConnection;
use std::str::FromStr;
thumbnail_url: pictshare_thumbnail,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = match Post::create(&conn, &post_form) {
fn perform(&self, conn: &PgConnection) -> Result<GetPostsResponse, Error> {
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 {
Some(auth) => match Claims::decode(&auth) {
Ok(claims) => Some(claims.claims),
thumbnail_url: pictshare_thumbnail,
ap_id: read_post.ap_id,
local: read_post.local,
+ published: None,
};
let _updated_post = match Post::update(&conn, data.edit_id, &post_form) {
private_key: Some(community_private_key),
public_key: Some(community_public_key),
last_refreshed_at: None,
+ published: None,
};
Community::create(&conn, &community_form).unwrap()
}
use crate::apub::puller::fetch_remote_object;
use crate::apub::*;
use crate::convert_datetime;
-use crate::db::community::Community;
-use crate::db::community_view::{CommunityFollowerView, CommunityView};
+use crate::db::community::{Community, CommunityForm};
+use crate::db::community_view::CommunityFollowerView;
use crate::db::establish_unpooled_connection;
use crate::db::post::Post;
use crate::settings::Settings;
}
}
-impl CommunityView {
- pub fn from_group(group: &GroupExt, domain: &str) -> Result<CommunityView, Error> {
+impl CommunityForm {
+ pub fn from_group(group: &GroupExt) -> Result<Self, Error> {
let followers_uri = &group.extension.get_followers().unwrap().to_string();
let outbox_uri = &group.extension.get_outbox().to_string();
- let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
- let followers = fetch_remote_object::<UnorderedCollection>(followers_uri)?;
+ let _outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
+ let _followers = fetch_remote_object::<UnorderedCollection>(followers_uri)?;
let oprops = &group.base.object_props;
let aprops = &group.extension;
- Ok(CommunityView {
- // TODO: we need to merge id and name into a single thing (stuff like @user@instance.com)
- id: 1337, //community.object_props.get_id()
- name: format_community_name(&oprops.get_name_xsd_string().unwrap().to_string(), domain),
+ Ok(CommunityForm {
+ name: oprops.get_name_xsd_string().unwrap().to_string(),
title: aprops.get_preferred_username().unwrap().to_string(),
description: oprops.get_summary_xsd_string().map(|s| s.to_string()),
- category_id: -1,
- creator_id: -1, //community.object_props.get_attributed_to_xsd_any_uri()
- removed: false,
+ category_id: 1,
+ creator_id: 2, //community.object_props.get_attributed_to_xsd_any_uri()
+ removed: None,
published: oprops
.get_published()
- .unwrap()
- .as_ref()
- .naive_local()
- .to_owned(),
+ .map(|u| u.as_ref().to_owned().naive_local()),
updated: oprops
.get_updated()
.map(|u| u.as_ref().to_owned().naive_local()),
- deleted: false,
+ deleted: None,
nsfw: false,
- creator_name: "".to_string(),
- creator_avatar: None,
- category_name: "".to_string(),
- number_of_subscribers: *followers
- .collection_props
- .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,
+ actor_id: oprops.get_id().unwrap().to_string(),
+ local: false,
+ private_key: None,
+ public_key: None,
+ last_refreshed_at: None,
})
}
}
.split(',')
.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)
-}
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType};
-use crate::db::post::Post;
-use crate::db::post_view::PostView;
-use crate::{convert_datetime, naive_now};
+use crate::convert_datetime;
+use crate::db::post::{Post, PostForm};
use activitystreams::{object::properties::ObjectProperties, object::Page};
use actix_web::body::Body;
use actix_web::web::Path;
}
}
-impl PostView {
- pub fn from_page(page: &Page) -> Result<PostView, Error> {
+impl PostForm {
+ pub fn from_page(page: &Page) -> Result<PostForm, Error> {
let oprops = &page.object_props;
- Ok(PostView {
- id: -1,
+ Ok(PostForm {
name: oprops.get_name_xsd_string().unwrap().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()),
- creator_id: -1,
+ creator_id: 2,
community_id: -1,
- removed: false,
- locked: false,
+ removed: None,
+ locked: None,
published: oprops
.get_published()
- .unwrap()
- .as_ref()
- .naive_local()
- .to_owned(),
+ .map(|u| u.as_ref().to_owned().naive_local()),
updated: oprops
.get_updated()
.map(|u| u.as_ref().to_owned().naive_local()),
- deleted: false,
+ deleted: None,
nsfw: false,
- stickied: false,
+ stickied: None,
embed_title: None,
embed_description: None,
embed_html: None,
thumbnail_url: None,
- banned: false,
- banned_from_community: 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,
+ ap_id: oprops.get_id().unwrap().to_string(),
+ local: false,
})
}
}
-use crate::api::community::GetCommunityResponse;
-use crate::api::post::GetPostsResponse;
use crate::apub::*;
-use crate::db::community_view::CommunityView;
-use crate::db::post_view::PostView;
+use crate::db::community::{Community, CommunityForm};
+use crate::db::post::{Post, PostForm};
+use crate::db::Crud;
use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
use crate::settings::Settings;
use activitystreams::collection::{OrderedCollection, UnorderedCollection};
use activitystreams::object::Page;
use activitystreams::BaseBox;
+use diesel::PgConnection;
use failure::Error;
use isahc::prelude::*;
-use log::warn;
use serde::Deserialize;
use std::time::Duration;
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)?;
if let Some(community_list_url) = node_info.metadata.community_list_url {
.collection_props
.get_many_items_base_boxes()
.unwrap();
- let communities: Result<Vec<CommunityView>, Error> = object_boxes
- .map(|c| -> Result<CommunityView, Error> {
+ let communities: Result<Vec<CommunityForm>, Error> = object_boxes
+ .map(|c| {
let group = c.to_owned().to_concrete::<GroupExt>()?;
- CommunityView::from_group(&group, domain)
+ CommunityForm::from_group(&group)
})
.collect();
Ok(communities?)
Ok(res)
}
-pub fn fetch_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
- let community = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?;
+fn fetch_remote_community_posts(instance: &str, community: &str) -> Result<Vec<PostForm>, Error> {
+ 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 = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
let items = outbox.collection_props.get_many_items_base_boxes();
- let posts: Result<Vec<PostView>, Error> = items
+ let posts = items
.unwrap()
.map(|obox: &BaseBox| {
let page = obox.clone().to_concrete::<Page>().unwrap();
- PostView::from_page(&page)
+ PostForm::from_page(&page)
})
- .collect();
- Ok(GetPostsResponse { posts: posts? })
+ .collect::<Result<Vec<PostForm>, Error>>()?;
+ Ok(posts)
}
-pub fn fetch_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> {
- let group = fetch_remote_object::<GroupExt>(&get_remote_community_uri(identifier))?;
- // TODO: this is only for testing until we can call that function from GetPosts
- // (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![];
+// TODO: in the future, this should only be done when an instance is followed for the first time
+// after that, we should rely in the inbox, and fetch on demand when needed
+pub fn fetch_all(conn: &PgConnection) -> Result<(), Error> {
for instance in &get_following_instances() {
- match 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),
- };
+ let communities = fetch_communities_from_instance(instance)?;
+
+ 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(())
}
private_key: Some(community_private_key),
public_key: Some(community_public_key),
last_refreshed_at: Some(naive_now()),
+ published: None,
};
Community::update(&conn, ccommunity.id, &form)?;
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
thumbnail_url: None,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
thumbnail_url: None,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
pub last_refreshed_at: chrono::NaiveDateTime,
}
-#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
+#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize, Debug)]
#[table_name = "community"]
pub struct CommunityForm {
pub name: String,
pub category_id: i32,
pub creator_id: i32,
pub removed: Option<bool>,
+ pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
.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> {
use crate::schema::community::dsl::*;
community.load::<Community>(conn)
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
thumbnail_url: None,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
pub local: bool,
}
-#[derive(Insertable, AsChangeset, Clone)]
+#[derive(Insertable, AsChangeset, Clone, Debug)]
#[table_name = "post"]
pub struct PostForm {
pub name: String,
pub community_id: i32,
pub removed: Option<bool>,
pub locked: Option<bool>,
+ pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub nsfw: bool,
.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> {
use crate::schema::post::dsl::*;
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
+ dbg!(&new_post);
insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
+ dbg!(&new_post);
diesel::update(post.find(post_id))
.set(new_post)
.get_result::<Self>(conn)
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
thumbnail_url: None,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
thumbnail_url: None,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
private_key: None,
public_key: None,
last_refreshed_at: None,
+ published: None,
};
let inserted_community = Community::create(&conn, &new_community).unwrap();
thumbnail_url: None,
ap_id: "changeme".into(),
local: true,
+ published: None,
};
let inserted_post = Post::create(&conn, &new_post).unwrap();
use actix_web::*;
use diesel::r2d2::{ConnectionManager, Pool};
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::routes::{api, federation, feeds, index, nodeinfo, webfinger, websocket};
use lemmy_server::settings::Settings;
use lemmy_server::websocket::server::*;
-use std::io;
+use log::warn;
+use std::thread;
+use std::thread::sleep;
+use std::time::Duration;
embed_migrations!();
#[actix_rt::main]
-async fn main() -> io::Result<()> {
+async fn main() -> Result<(), Error> {
env_logger::init();
let settings = Settings::get();
// Set up websocket server
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!(
"Starting http server at {}:{}",
settings.bind, settings.port
);
// Create Http server with websocket support
- HttpServer::new(move || {
- App::new()
- .wrap(middleware::Logger::default())
- .data(pool.clone())
- .data(server.clone())
- // The routes
- .configure(api::config)
- .configure(federation::config)
- .configure(feeds::config)
- .configure(index::config)
- .configure(nodeinfo::config)
- .configure(webfinger::config)
- .configure(websocket::config)
- // static files
- .service(actix_files::Files::new(
- "/static",
- settings.front_end_dir.to_owned(),
- ))
- .service(actix_files::Files::new(
- "/docs",
- settings.front_end_dir.to_owned() + "/documentation",
- ))
- })
- .bind((settings.bind, settings.port))?
- .run()
- .await
+ Ok(
+ HttpServer::new(move || {
+ App::new()
+ .wrap(middleware::Logger::default())
+ .data(pool.clone())
+ .data(server.clone())
+ // The routes
+ .configure(api::config)
+ .configure(federation::config)
+ .configure(feeds::config)
+ .configure(index::config)
+ .configure(nodeinfo::config)
+ .configure(webfinger::config)
+ .configure(websocket::config)
+ // static files
+ .service(actix_files::Files::new(
+ "/static",
+ settings.front_end_dir.to_owned(),
+ ))
+ .service(actix_files::Files::new(
+ "/docs",
+ settings.front_end_dir.to_owned() + "/documentation",
+ ))
+ })
+ .bind((settings.bind, settings.port))?
+ .run()
+ .await?,
+ )
}