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 f3ab32bada
commit 61107b6546
4 changed files with 30 additions and 34 deletions

View File

@ -1,4 +1,6 @@
use super::*;
use crate::apub::puller::{get_all_communities, get_remote_community};
use crate::settings::Settings;
use diesel::PgConnection;
use std::str::FromStr;
@ -117,6 +119,13 @@ impl Perform<GetCommunityResponse> for Oper<GetCommunity> {
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 get_remote_community(data.name.as_ref().unwrap());
}
let user_id: Option<i32> = match &data.auth {
Some(auth) => match Claims::decode(&auth) {
Ok(claims) => {
@ -333,6 +342,12 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> {
fn perform(&self, conn: &PgConnection) -> Result<ListCommunitiesResponse, Error> {
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 {
Some(auth) => match Claims::decode(&auth) {
Ok(claims) => Some(claims.claims),

View File

@ -1,4 +1,5 @@
use super::*;
use crate::settings::Settings;
use diesel::PgConnection;
use std::str::FromStr;
@ -220,6 +221,12 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> {
fn perform(&self, conn: &PgConnection) -> Result<GetPostsResponse, Error> {
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 {
Some(auth) => match Claims::decode(&auth) {
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
fn get_remote_community_uri(identifier: String) -> String {
fn get_remote_community_uri(identifier: &str) -> String {
let x: Vec<&str> = identifier.split('@').collect();
let name = x[0].replace("!", "");
let instance = x[1];
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 outbox_uri = &community.ap_actor_props.get_outbox().to_string();
let outbox: OrderedCollection = reqwest::get(outbox_uri)?.json()?;
@ -42,8 +42,8 @@ pub fn get_remote_community_posts(identifier: String) -> Result<GetPostsResponse
unimplemented!()
}
pub fn get_remote_community(identifier: String) -> Result<GetCommunityResponse, failure::Error> {
let community: Group = reqwest::get(&get_remote_community_uri(identifier.clone()))?.json()?;
pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> {
let community: Group = reqwest::get(&get_remote_community_uri(identifier))?.json()?;
let followers_uri = &community
.ap_actor_props
.get_followers()
@ -62,7 +62,7 @@ pub fn get_remote_community(identifier: String) -> Result<GetCommunityResponse,
community: 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: identifier,
name: identifier.to_string(),
title: community
.object_props
.get_name_xsd_string()

View File

@ -20,7 +20,6 @@ use crate::api::post::*;
use crate::api::site::*;
use crate::api::user::*;
use crate::api::*;
use crate::apub::puller::*;
use crate::websocket::UserOperation;
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)?;
// 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
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 => {
let get_community: GetCommunity = serde_json::from_str(data)?;
let mut res = if Settings::get().federation_enabled {
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 mut res = Oper::new(get_community).perform(&conn)?;
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)
}
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)
}
}
UserOperation::CreateCommunity => {
chat.check_rate_limit_register(msg.id, true)?;
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 => {
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() {
// 0 is the "all" community
chat.join_community_room(0, msg.id);