Move resolving of activitypub objects to separate api endpoint (fixes #1584)

This commit is contained in:
Felix Ableitner 2021-07-20 09:00:20 +02:00
parent f6f169b4eb
commit 251e0d3b82
14 changed files with 202 additions and 104 deletions

4
Cargo.lock generated
View file

@ -1736,10 +1736,13 @@ name = "lemmy_apub_lib"
version = "0.11.3" version = "0.11.3"
dependencies = [ dependencies = [
"activitystreams", "activitystreams",
"anyhow",
"async-trait", "async-trait",
"lemmy_apub_lib_derive", "lemmy_apub_lib_derive",
"lemmy_utils", "lemmy_utils",
"lemmy_websocket", "lemmy_websocket",
"log",
"reqwest",
"serde", "serde",
"serde_json", "serde_json",
"url", "url",
@ -1836,6 +1839,7 @@ dependencies = [
"diesel", "diesel",
"lazy_static", "lazy_static",
"lemmy_api_common", "lemmy_api_common",
"lemmy_apub_lib",
"lemmy_db_queries", "lemmy_db_queries",
"lemmy_db_schema", "lemmy_db_schema",
"lemmy_db_views", "lemmy_db_views",

View file

@ -87,6 +87,9 @@ pub async fn match_websocket_operation(
do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await
} }
UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await, UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await,
UserOperation::ResolveObject => {
do_websocket_operation::<ResolveObject>(context, id, op, data).await
}
UserOperation::TransferCommunity => { UserOperation::TransferCommunity => {
do_websocket_operation::<TransferCommunity>(context, id, op, data).await do_websocket_operation::<TransferCommunity>(context, id, op, data).await
} }

View file

@ -50,7 +50,6 @@ use lemmy_utils::{
LemmyError, LemmyError,
}; };
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for GetModlog { impl Perform for GetModlog {
@ -143,11 +142,6 @@ impl Perform for Search {
) -> Result<SearchResponse, LemmyError> { ) -> Result<SearchResponse, LemmyError> {
let data: &Search = self; let data: &Search = self;
match search_by_apub_id(&data.q, context).await {
Ok(r) => return Ok(r),
Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
}
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw); let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
@ -372,6 +366,20 @@ impl Perform for Search {
} }
} }
#[async_trait::async_trait(?Send)]
impl Perform for ResolveObject {
type Response = ResolveObjectResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<ResolveObjectResponse, LemmyError> {
let local_user_view = get_local_user_view_from_jwt_opt(&self.auth, context.pool()).await?;
search_by_apub_id(&self.q, local_user_view, context).await
}
}
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for TransferSite { impl Perform for TransferSite {
type Response = GetSiteResponse; type Response = GetSiteResponse;

View file

@ -46,26 +46,8 @@ use lemmy_utils::{
LemmyError, LemmyError,
}; };
use log::error; use log::error;
use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerResponse {
pub subject: String,
pub aliases: Vec<Url>,
pub links: Vec<WebFingerLink>,
}
pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError> pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
where where
F: FnOnce(&diesel::PgConnection) -> T + Send + 'static, F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,

View file

@ -50,6 +50,20 @@ pub struct SearchResponse {
pub users: Vec<PersonViewSafe>, pub users: Vec<PersonViewSafe>,
} }
#[derive(Deserialize, Debug)]
pub struct ResolveObject {
pub q: String,
pub auth: Option<String>,
}
#[derive(Serialize, Debug)]
pub enum ResolveObjectResponse {
Comment(CommentView),
Post(PostView),
Community(CommunityView),
Person(PersonViewSafe),
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GetModlog { pub struct GetModlog {
pub mod_person_id: Option<PersonId>, pub mod_person_id: Option<PersonId>,

View file

@ -5,7 +5,8 @@ use activitystreams::{
}; };
use anyhow::anyhow; use anyhow::anyhow;
use itertools::Itertools; use itertools::Itertools;
use lemmy_api_common::{blocking, send_local_notifs, WebFingerResponse}; use lemmy_api_common::{blocking, send_local_notifs};
use lemmy_apub_lib::webfinger::WebfingerResponse;
use lemmy_db_queries::{Crud, DbPool}; use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{comment::Comment, community::Community, person::Person, post::Post}, source::{comment::Comment, community::Community, person::Person, post::Post},
@ -128,7 +129,7 @@ async fn fetch_webfinger_url(mention: &MentionData, client: &Client) -> Result<U
let response = retry(|| client.get(&fetch_url).send()).await?; let response = retry(|| client.get(&fetch_url).send()).await?;
let res: WebFingerResponse = response let res: WebfingerResponse = response
.json() .json()
.await .await
.map_err(|e| RecvError(e.to_string()))?; .map_err(|e| RecvError(e.to_string()))?;

View file

@ -1,25 +1,24 @@
use crate::{ use crate::{
fetcher::{ fetcher::{
community::get_or_fetch_and_upsert_community,
fetch::fetch_remote_object, fetch::fetch_remote_object,
get_or_fetch_and_upsert_community,
get_or_fetch_and_upsert_person,
is_deleted, is_deleted,
person::get_or_fetch_and_upsert_person,
}, },
find_object_by_id, find_object_by_id,
objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub}, objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
Object, Object,
}; };
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::{blocking, site::SearchResponse}; use itertools::Itertools;
use lemmy_db_queries::{ use lemmy_api_common::{blocking, site::ResolveObjectResponse};
source::{ use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
use lemmy_db_queries::source::{
comment::Comment_, comment::Comment_,
community::Community_, community::Community_,
person::Person_, person::Person_,
post::Post_, post::Post_,
private_message::PrivateMessage_, private_message::PrivateMessage_,
},
SearchType,
}; };
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
comment::Comment, comment::Comment,
@ -28,11 +27,14 @@ use lemmy_db_schema::source::{
post::Post, post::Post,
private_message::PrivateMessage, private_message::PrivateMessage,
}; };
use lemmy_db_views::{comment_view::CommentView, post_view::PostView}; use lemmy_db_views::{
comment_view::CommentView,
local_user_view::LocalUserView,
post_view::PostView,
};
use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe}; use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
use lemmy_utils::{settings::structs::Settings, LemmyError}; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url; use url::Url;
/// The types of ActivityPub objects that can be fetched directly by searching for their ID. /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
@ -54,50 +56,62 @@ enum SearchAcceptedObjects {
/// http://lemmy_delta:8571/comment/2 /// http://lemmy_delta:8571/comment/2
pub async fn search_by_apub_id( pub async fn search_by_apub_id(
query: &str, query: &str,
local_user_view: Option<LocalUserView>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<SearchResponse, LemmyError> { ) -> Result<ResolveObjectResponse, LemmyError> {
// Parse the shorthand query url let query_url = match Url::parse(query) {
let query_url = if query.contains('@') { Ok(u) => u,
debug!("Search for {}", query); Err(_) => {
let split = query.split('@').collect::<Vec<&str>>(); let (kind, name) = query.split_at(1);
let kind = match kind {
// Person type will look like ['', username, instance] "@" => WebfingerType::Person,
// Community will look like [!community, instance] "!" => WebfingerType::Group,
let (name, instance) = if split.len() == 3 { _ => return Err(anyhow!("invalid query").into()),
(format!("/u/{}", split[1]), split[2]) };
} else if split.len() == 2 { // remote actor, use webfinger to resolve url
if split[0].contains('!') { if name.contains('@') {
let split2 = split[0].split('!').collect::<Vec<&str>>(); let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
(format!("/c/{}", split2[1]), split[1]) webfinger_resolve_actor(name, domain, kind, context.client()).await?
} else { }
return Err(anyhow!("Invalid search query: {}", query).into()); // local actor, read from database and return
else {
let name: String = name.into();
return match kind {
WebfingerType::Group => {
let res = blocking(context.pool(), move |conn| {
let community = Community::read_from_name(conn, &name)?;
CommunityView::read(conn, community.id, local_user_view.map(|l| l.person.id))
})
.await??;
Ok(ResolveObjectResponse::Community(res))
}
WebfingerType::Person => {
let res = blocking(context.pool(), move |conn| {
let person = Person::find_by_name(conn, &name)?;
PersonViewSafe::read(conn, person.id)
})
.await??;
Ok(ResolveObjectResponse::Person(res))
}
};
}
} }
} else {
return Err(anyhow!("Invalid search query: {}", query).into());
}; };
let url = format!( let request_counter = &mut 0;
"{}://{}{}", // this does a fetch (even for local objects), just to determine its type and fetch it again
Settings::get().get_protocol_string(), // below. we need to fix this when rewriting the fetcher.
instance,
name
);
Url::parse(&url)?
} else {
Url::parse(query)?
};
let recursion_counter = &mut 0;
let fetch_response = let fetch_response =
fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, recursion_counter) fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, request_counter)
.await; .await;
if is_deleted(&fetch_response) { if is_deleted(&fetch_response) {
delete_object_locally(&query_url, context).await?; delete_object_locally(&query_url, context).await?;
return Err(anyhow!("Object was deleted").into());
} }
// Necessary because we get a stack overflow using FetchError // Necessary because we get a stack overflow using FetchError
let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?; let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?;
build_response(fet_res, query_url, recursion_counter, context).await build_response(fet_res, query_url, request_counter, context).await
} }
async fn build_response( async fn build_response(
@ -105,58 +119,45 @@ async fn build_response(
query_url: Url, query_url: Url,
recursion_counter: &mut i32, recursion_counter: &mut i32,
context: &LemmyContext, context: &LemmyContext,
) -> Result<SearchResponse, LemmyError> { ) -> Result<ResolveObjectResponse, LemmyError> {
let mut response = SearchResponse { use ResolveObjectResponse as ROR;
type_: SearchType::All.to_string(), Ok(match fetch_response {
comments: vec![],
posts: vec![],
communities: vec![],
users: vec![],
};
match fetch_response {
SearchAcceptedObjects::Person(p) => { SearchAcceptedObjects::Person(p) => {
let person_id = p.id(&query_url)?; let person_uri = p.id(&query_url)?;
let person = get_or_fetch_and_upsert_person(person_id, context, recursion_counter).await?;
response.users = vec![ let person = get_or_fetch_and_upsert_person(person_uri, context, recursion_counter).await?;
ROR::Person(
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
PersonViewSafe::read(conn, person.id) PersonViewSafe::read(conn, person.id)
}) })
.await??, .await??,
]; )
} }
SearchAcceptedObjects::Group(g) => { SearchAcceptedObjects::Group(g) => {
let community_uri = g.id(&query_url)?; let community_uri = g.id(&query_url)?;
let community = let community =
get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?; get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?;
ROR::Community(
response.communities = vec![
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
CommunityView::read(conn, community.id, None) CommunityView::read(conn, community.id, None)
}) })
.await??, .await??,
]; )
} }
SearchAcceptedObjects::Page(p) => { SearchAcceptedObjects::Page(p) => {
let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?; let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?;
ROR::Post(blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??)
response.posts =
vec![blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??];
} }
SearchAcceptedObjects::Comment(c) => { SearchAcceptedObjects::Comment(c) => {
let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?; let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?;
ROR::Comment(
response.comments = vec![
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
CommentView::read(conn, c.id, None) CommentView::read(conn, c.id, None)
}) })
.await??, .await??,
]; )
} }
}; })
Ok(response)
} }
async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> { async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> {
@ -194,5 +195,5 @@ async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Resul
.await??; .await??;
} }
} }
Err(anyhow!("Object was deleted").into()) Ok(())
} }

View file

@ -14,3 +14,6 @@ serde = { version = "1.0.127", features = ["derive"] }
async-trait = "0.1.51" async-trait = "0.1.51"
url = { version = "2.2.2", features = ["serde"] } url = { version = "2.2.2", features = ["serde"] }
serde_json = { version = "1.0.66", features = ["preserve_order"] } serde_json = { version = "1.0.66", features = ["preserve_order"] }
anyhow = "1.0.41"
reqwest = { version = "0.11.4", features = ["json"] }
log = "0.4.14"

View file

@ -6,6 +6,8 @@ use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use url::Url; use url::Url;
pub mod webfinger;
pub trait ActivityFields { pub trait ActivityFields {
fn id_unchecked(&self) -> &Url; fn id_unchecked(&self) -> &Url;
fn actor(&self) -> &Url; fn actor(&self) -> &Url;

View file

@ -0,0 +1,72 @@
use anyhow::anyhow;
use lemmy_utils::{
request::{retry, RecvError},
settings::structs::Settings,
LemmyError,
};
use log::debug;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Serialize, Deserialize, Debug)]
pub struct WebfingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebfingerResponse {
pub subject: String,
pub aliases: Vec<Url>,
pub links: Vec<WebfingerLink>,
}
pub enum WebfingerType {
Person,
Group,
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
pub async fn webfinger_resolve_actor(
name: &str,
domain: &str,
webfinger_type: WebfingerType,
client: &Client,
) -> Result<Url, LemmyError> {
let webfinger_type = match webfinger_type {
WebfingerType::Person => "acct",
WebfingerType::Group => "group",
};
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource={}:{}@{}",
Settings::get().get_protocol_string(),
domain,
webfinger_type,
name,
domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| client.get(&fetch_url).send()).await?;
let res: WebfingerResponse = response
.json()
.await
.map_err(|e| RecvError(e.to_string()))?;
let link = res
.links
.iter()
.find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
.ok_or_else(|| anyhow!("No application/activity+json link found."))?;
link
.href
.to_owned()
.ok_or_else(|| anyhow!("No href found.").into())
}

View file

@ -16,6 +16,7 @@ lemmy_db_views = { version = "=0.11.3", path = "../db_views" }
lemmy_db_views_actor = { version = "=0.11.3", path = "../db_views_actor" } lemmy_db_views_actor = { version = "=0.11.3", path = "../db_views_actor" }
lemmy_db_schema = { version = "=0.11.3", path = "../db_schema" } lemmy_db_schema = { version = "=0.11.3", path = "../db_schema" }
lemmy_api_common = { version = "=0.11.3", path = "../api_common" } lemmy_api_common = { version = "=0.11.3", path = "../api_common" }
lemmy_apub_lib = { version = "=0.11.3", path = "../apub_lib" }
diesel = "1.4.7" diesel = "1.4.7"
actix = "0.12.0" actix = "0.12.0"
actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["rustls"] } actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["rustls"] }

View file

@ -1,6 +1,7 @@
use actix_web::{error::ErrorBadRequest, web::Query, *}; use actix_web::{error::ErrorBadRequest, web::Query, *};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::{blocking, WebFingerLink, WebFingerResponse}; use lemmy_api_common::blocking;
use lemmy_apub_lib::webfinger::{WebfingerLink, WebfingerResponse};
use lemmy_db_queries::source::{community::Community_, person::Person_}; use lemmy_db_queries::source::{community::Community_, person::Person_};
use lemmy_db_schema::source::{community::Community, person::Person}; use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_utils::{ use lemmy_utils::{
@ -68,17 +69,17 @@ async fn get_webfinger_response(
return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found")))); return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
}; };
let json = WebFingerResponse { let json = WebfingerResponse {
subject: info.resource.to_owned(), subject: info.resource.to_owned(),
aliases: vec![url.to_owned().into()], aliases: vec![url.to_owned().into()],
links: vec![ links: vec![
WebFingerLink { WebfingerLink {
rel: Some("http://webfinger.net/rel/profile-page".to_string()), rel: Some("http://webfinger.net/rel/profile-page".to_string()),
type_: Some("text/html".to_string()), type_: Some("text/html".to_string()),
href: Some(url.to_owned().into()), href: Some(url.to_owned().into()),
template: None, template: None,
}, },
WebFingerLink { WebfingerLink {
rel: Some("self".to_string()), rel: Some("self".to_string()),
type_: Some("application/activity+json".to_string()), type_: Some("application/activity+json".to_string()),
href: Some(url.into()), href: Some(url.into()),

View file

@ -110,6 +110,7 @@ pub enum UserOperation {
AddAdmin, AddAdmin,
BanPerson, BanPerson,
Search, Search,
ResolveObject,
MarkAllAsRead, MarkAllAsRead,
SaveUserSettings, SaveUserSettings,
TransferCommunity, TransferCommunity,

View file

@ -33,6 +33,11 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route(web::get().to(route_get::<Search>)), .route(web::get().to(route_get::<Search>)),
) )
.service(
web::resource("/resolve_object")
.wrap(rate_limit.message())
.route(web::get().to(route_get::<ResolveObject>)),
)
// Community // Community
.service( .service(
web::resource("/community") web::resource("/community")