Move apub related code from websocket into api package

This commit is contained in:
Felix Ableitner 2020-03-14 13:15:23 +01:00
parent b01f4f75d6
commit 5896a9d251
4 changed files with 30 additions and 34 deletions

View file

@ -1,4 +1,6 @@
use super::*; use super::*;
use crate::apub::puller::{get_all_communities, get_remote_community};
use crate::settings::Settings;
use diesel::PgConnection; use diesel::PgConnection;
use std::str::FromStr; use std::str::FromStr;
@ -117,6 +119,13 @@ 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 get_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) => {
@ -333,6 +342,12 @@ 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;
if Settings::get().federation_enabled {
return Ok(ListCommunitiesResponse {
communities: get_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),

View file

@ -1,4 +1,5 @@
use super::*; use super::*;
use crate::settings::Settings;
use diesel::PgConnection; use diesel::PgConnection;
use std::str::FromStr; use std::str::FromStr;
@ -220,6 +221,12 @@ 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 {
dbg!(&data);
// 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),

View file

@ -26,14 +26,14 @@ fn fetch_communities_from_instance(domain: &str) -> Result<Vec<CommunityView>, E
} }
// TODO: this should be cached or stored in the database // TODO: this should be cached or stored in the database
fn get_remote_community_uri(identifier: String) -> String { fn get_remote_community_uri(identifier: &str) -> String {
let x: Vec<&str> = identifier.split('@').collect(); let x: Vec<&str> = identifier.split('@').collect();
let name = x[0].replace("!", ""); let name = x[0].replace("!", "");
let instance = x[1]; let instance = x[1];
format!("http://{}/federation/c/{}", instance, name) format!("http://{}/federation/c/{}", instance, name)
} }
pub fn get_remote_community_posts(identifier: String) -> Result<GetPostsResponse, Error> { pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
let community: Group = reqwest::get(&get_remote_community_uri(identifier))?.json()?; let community: Group = reqwest::get(&get_remote_community_uri(identifier))?.json()?;
let outbox_uri = &community.ap_actor_props.get_outbox().to_string(); let outbox_uri = &community.ap_actor_props.get_outbox().to_string();
let outbox: OrderedCollection = reqwest::get(outbox_uri)?.json()?; let outbox: OrderedCollection = reqwest::get(outbox_uri)?.json()?;
@ -42,8 +42,8 @@ pub fn get_remote_community_posts(identifier: String) -> Result<GetPostsResponse
unimplemented!() unimplemented!()
} }
pub fn get_remote_community(identifier: String) -> Result<GetCommunityResponse, failure::Error> { pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> {
let community: Group = reqwest::get(&get_remote_community_uri(identifier.clone()))?.json()?; let community: Group = reqwest::get(&get_remote_community_uri(identifier))?.json()?;
let followers_uri = &community let followers_uri = &community
.ap_actor_props .ap_actor_props
.get_followers() .get_followers()
@ -62,7 +62,7 @@ pub fn get_remote_community(identifier: String) -> Result<GetCommunityResponse,
community: CommunityView { community: CommunityView {
// TODO: we need to merge id and name into a single thing (stuff like @user@instance.com) // TODO: we need to merge id and name into a single thing (stuff like @user@instance.com)
id: 1337, //community.object_props.get_id() id: 1337, //community.object_props.get_id()
name: identifier, name: identifier.to_string(),
title: community title: community
.object_props .object_props
.get_name_xsd_string() .get_name_xsd_string()

View file

@ -20,7 +20,6 @@ use crate::api::post::*;
use crate::api::site::*; use crate::api::site::*;
use crate::api::user::*; use crate::api::user::*;
use crate::api::*; use crate::api::*;
use crate::apub::puller::*;
use crate::websocket::UserOperation; use crate::websocket::UserOperation;
use crate::Settings; use crate::Settings;
@ -502,9 +501,6 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
let user_operation: UserOperation = UserOperation::from_str(&op)?; let user_operation: UserOperation = UserOperation::from_str(&op)?;
// TODO: none of the chat messages are going to work if stuff is submitted via http api,
// need to move that handling elsewhere
// A DDOS check // A DDOS check
chat.check_rate_limit_message(msg.id, false)?; chat.check_rate_limit_message(msg.id, false)?;
@ -552,21 +548,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
UserOperation::GetCommunity => { UserOperation::GetCommunity => {
let get_community: GetCommunity = serde_json::from_str(data)?; let get_community: GetCommunity = serde_json::from_str(data)?;
let mut res = if Settings::get().federation_enabled { let mut res = Oper::new(get_community).perform(&conn)?;
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)?
}
} else {
Oper::new(get_community).perform(&conn)?
}
} else {
Oper::new(get_community).perform(&conn)?
};
let community_id = res.community.id; let community_id = res.community.id;
@ -581,14 +563,8 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
to_json_string(&user_operation, &res) to_json_string(&user_operation, &res)
} }
UserOperation::ListCommunities => { UserOperation::ListCommunities => {
if Settings::get().federation_enabled {
let res = get_all_communities()?;
let val = ListCommunitiesResponse { communities: res };
to_json_string(&user_operation, &val)
} else {
do_user_operation::<ListCommunities, ListCommunitiesResponse>(user_operation, data, &conn) do_user_operation::<ListCommunities, ListCommunitiesResponse>(user_operation, data, &conn)
} }
}
UserOperation::CreateCommunity => { UserOperation::CreateCommunity => {
chat.check_rate_limit_register(msg.id, true)?; chat.check_rate_limit_register(msg.id, true)?;
let create_community: CreateCommunity = serde_json::from_str(data)?; let create_community: CreateCommunity = serde_json::from_str(data)?;
@ -648,9 +624,7 @@ fn parse_json_message(chat: &mut ChatServer, msg: StandardMessage) -> Result<Str
} }
UserOperation::GetPosts => { UserOperation::GetPosts => {
let get_posts: GetPosts = serde_json::from_str(data)?; let get_posts: GetPosts = serde_json::from_str(data)?;
dbg!(&get_posts);
// TODO: intercept here (but the type is wrong)
//get_remote_community_posts(get_posts.community_id.unwrap())
if get_posts.community_id.is_none() { if get_posts.community_id.is_none() {
// 0 is the "all" community // 0 is the "all" community
chat.join_community_room(0, msg.id); chat.join_community_room(0, msg.id);