Merge pull request #1897 from LemmyNet/mastodon-compat

Mastodon compat
This commit is contained in:
Dessalines 2021-11-17 09:12:01 -05:00 committed by GitHub
commit 5f6419ff76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
67 changed files with 660 additions and 595 deletions

2
Cargo.lock generated
View file

@ -1962,7 +1962,7 @@ dependencies = [
"diesel", "diesel",
"lazy_static", "lazy_static",
"lemmy_api_common", "lemmy_api_common",
"lemmy_apub_lib", "lemmy_apub",
"lemmy_db_schema", "lemmy_db_schema",
"lemmy_db_views", "lemmy_db_views",
"lemmy_db_views_actor", "lemmy_db_views_actor",

View file

@ -11,10 +11,13 @@ use lemmy_api_common::{
site::*, site::*,
}; };
use lemmy_apub::{ use lemmy_apub::{
fetcher::search::{search_by_apub_id, SearchableObjects}, fetcher::{
get_actor_id_from_name, search::{search_by_apub_id, SearchableObjects},
webfinger::webfinger_resolve,
},
objects::community::ApubCommunity,
EndpointType,
}; };
use lemmy_apub_lib::webfinger::WebfingerType;
use lemmy_db_schema::{ use lemmy_db_schema::{
from_opt_str_to_opt_enum, from_opt_str_to_opt_enum,
newtypes::PersonId, newtypes::PersonId,
@ -175,7 +178,7 @@ impl Perform for Search {
let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All); let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
let community_id = data.community_id; let community_id = data.community_id;
let community_actor_id = if let Some(name) = &data.community_name { let community_actor_id = if let Some(name) = &data.community_name {
get_actor_id_from_name(WebfingerType::Group, name, context) webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
.await .await
.ok() .ok()
} else { } else {

View file

@ -152,6 +152,7 @@ impl PerformCrud for CreateComment {
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
CreateOrUpdateType::Create, CreateOrUpdateType::Create,
context, context,
&mut 0,
) )
.await?; .await?;
let object = PostOrComment::Comment(Box::new(apub_comment)); let object = PostOrComment::Comment(Box::new(apub_comment));

View file

@ -7,7 +7,7 @@ use lemmy_api_common::{
get_local_user_view_from_jwt, get_local_user_view_from_jwt,
is_mod_or_admin, is_mod_or_admin,
}; };
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove}; use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove, DeletableObjects};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::Comment, comment::Comment,
@ -69,20 +69,6 @@ impl PerformCrud for DeleteComment {
.await? .await?
.map_err(|e| ApiError::err("couldnt_update_comment", e))?; .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_delete(
&local_user_view.person.clone().into(),
&community.clone().into(),
updated_comment.ap_id.clone().into(),
deleted,
context,
)
.await?;
let post_id = updated_comment.post_id; let post_id = updated_comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let recipient_ids = send_local_notifs( let recipient_ids = send_local_notifs(
@ -95,6 +81,20 @@ impl PerformCrud for DeleteComment {
) )
.await?; .await?;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_delete(
&local_user_view.person.clone().into(),
&community.clone().into(),
DeletableObjects::Comment(Box::new(updated_comment.into())),
deleted,
context,
)
.await?;
send_comment_ws_message( send_comment_ws_message(
data.comment_id, data.comment_id,
UserOperationCrud::DeleteComment, UserOperationCrud::DeleteComment,
@ -162,21 +162,6 @@ impl PerformCrud for RemoveComment {
}) })
.await??; .await??;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_remove(
&local_user_view.person.clone().into(),
&community.into(),
updated_comment.ap_id.clone().into(),
data.reason.clone().unwrap_or_else(|| "".to_string()),
removed,
context,
)
.await?;
let post_id = updated_comment.post_id; let post_id = updated_comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let recipient_ids = send_local_notifs( let recipient_ids = send_local_notifs(
@ -189,6 +174,21 @@ impl PerformCrud for RemoveComment {
) )
.await?; .await?;
// Send the apub message
let community = blocking(context.pool(), move |conn| {
Community::read(conn, orig_comment.post.community_id)
})
.await??;
send_apub_remove(
&local_user_view.person.clone().into(),
&community.into(),
DeletableObjects::Comment(Box::new(updated_comment.into())),
data.reason.clone().unwrap_or_else(|| "".to_string()),
removed,
context,
)
.await?;
send_comment_ws_message( send_comment_ws_message(
data.comment_id, data.comment_id,
UserOperationCrud::RemoveComment, UserOperationCrud::RemoveComment,

View file

@ -1,8 +1,11 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt}; use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
use lemmy_apub::get_actor_id_from_name; use lemmy_apub::{
use lemmy_apub_lib::webfinger::WebfingerType; fetcher::webfinger::webfinger_resolve,
objects::community::ApubCommunity,
EndpointType,
};
use lemmy_db_schema::{ use lemmy_db_schema::{
from_opt_str_to_opt_enum, from_opt_str_to_opt_enum,
traits::DeleteableOrRemoveable, traits::DeleteableOrRemoveable,
@ -36,7 +39,7 @@ impl PerformCrud for GetComments {
let community_id = data.community_id; let community_id = data.community_id;
let community_actor_id = if let Some(name) = &data.community_name { let community_actor_id = if let Some(name) = &data.community_name {
get_actor_id_from_name(WebfingerType::Group, name, context) webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
.await .await
.ok() .ok()
} else { } else {

View file

@ -91,6 +91,7 @@ impl PerformCrud for EditComment {
&local_user_view.person.into(), &local_user_view.person.into(),
CreateOrUpdateType::Update, CreateOrUpdateType::Update,
context, context,
&mut 0,
) )
.await?; .await?;

View file

@ -1,7 +1,7 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin}; use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt, is_admin};
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove}; use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove, DeletableObjects};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
community::Community, community::Community,
@ -51,7 +51,7 @@ impl PerformCrud for DeleteCommunity {
send_apub_delete( send_apub_delete(
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
&updated_community.clone().into(), &updated_community.clone().into(),
updated_community.actor_id.clone().into(), DeletableObjects::Community(Box::new(updated_community.into())),
deleted, deleted,
context, context,
) )
@ -111,7 +111,7 @@ impl PerformCrud for RemoveCommunity {
send_apub_remove( send_apub_remove(
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
&updated_community.clone().into(), &updated_community.clone().into(),
updated_community.actor_id.clone().into(), DeletableObjects::Community(Box::new(updated_community.into())),
data.reason.clone().unwrap_or_else(|| "".to_string()), data.reason.clone().unwrap_or_else(|| "".to_string()),
removed, removed,
context, context,

View file

@ -1,8 +1,12 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt}; use lemmy_api_common::{blocking, community::*, get_local_user_view_from_jwt_opt};
use lemmy_apub::{get_actor_id_from_name, objects::community::ApubCommunity}; use lemmy_apub::{
use lemmy_apub_lib::{object_id::ObjectId, webfinger::WebfingerType}; fetcher::webfinger::webfinger_resolve,
objects::community::ApubCommunity,
EndpointType,
};
use lemmy_apub_lib::object_id::ObjectId;
use lemmy_db_schema::{ use lemmy_db_schema::{
from_opt_str_to_opt_enum, from_opt_str_to_opt_enum,
traits::DeleteableOrRemoveable, traits::DeleteableOrRemoveable,
@ -35,7 +39,8 @@ impl PerformCrud for GetCommunity {
None => { None => {
let name = data.name.to_owned().unwrap_or_else(|| "main".to_string()); let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
let community_actor_id = let community_actor_id =
get_actor_id_from_name(WebfingerType::Group, &name, context).await?; webfinger_resolve::<ApubCommunity>(&name, EndpointType::Community, context, &mut 0)
.await?;
ObjectId::<ApubCommunity>::new(community_actor_id) ObjectId::<ApubCommunity>::new(community_actor_id)
.dereference(context, &mut 0) .dereference(context, &mut 0)

View file

@ -8,7 +8,7 @@ use lemmy_api_common::{
is_mod_or_admin, is_mod_or_admin,
post::*, post::*,
}; };
use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove}; use lemmy_apub::activities::deletion::{send_apub_delete, send_apub_remove, DeletableObjects};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
community::Community, community::Community,
@ -70,7 +70,7 @@ impl PerformCrud for DeletePost {
send_apub_delete( send_apub_delete(
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
&community.into(), &community.into(),
updated_post.ap_id.into(), DeletableObjects::Post(Box::new(updated_post.into())),
deleted, deleted,
context, context,
) )
@ -146,7 +146,7 @@ impl PerformCrud for RemovePost {
send_apub_remove( send_apub_remove(
&local_user_view.person.clone().into(), &local_user_view.person.clone().into(),
&community.into(), &community.into(),
updated_post.ap_id.into(), DeletableObjects::Post(Box::new(updated_post.into())),
data.reason.clone().unwrap_or_else(|| "".to_string()), data.reason.clone().unwrap_or_else(|| "".to_string()),
removed, removed,
context, context,

View file

@ -1,8 +1,11 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*}; use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, mark_post_as_read, post::*};
use lemmy_apub::get_actor_id_from_name; use lemmy_apub::{
use lemmy_apub_lib::webfinger::WebfingerType; fetcher::webfinger::webfinger_resolve,
objects::community::ApubCommunity,
EndpointType,
};
use lemmy_db_schema::{ use lemmy_db_schema::{
from_opt_str_to_opt_enum, from_opt_str_to_opt_enum,
traits::DeleteableOrRemoveable, traits::DeleteableOrRemoveable,
@ -138,7 +141,7 @@ impl PerformCrud for GetPosts {
let limit = data.limit; let limit = data.limit;
let community_id = data.community_id; let community_id = data.community_id;
let community_actor_id = if let Some(name) = &data.community_name { let community_actor_id = if let Some(name) = &data.community_name {
get_actor_id_from_name(WebfingerType::Group, name, context) webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
.await .await
.ok() .ok()
} else { } else {

View file

@ -1,8 +1,12 @@
use crate::PerformCrud; use crate::PerformCrud;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*}; use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
use lemmy_apub::{get_actor_id_from_name, objects::person::ApubPerson}; use lemmy_apub::{
use lemmy_apub_lib::{object_id::ObjectId, webfinger::WebfingerType}; fetcher::webfinger::webfinger_resolve,
objects::person::ApubPerson,
EndpointType,
};
use lemmy_apub_lib::object_id::ObjectId;
use lemmy_db_schema::{from_opt_str_to_opt_enum, SortType}; use lemmy_db_schema::{from_opt_str_to_opt_enum, SortType};
use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder}; use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
use lemmy_db_views_actor::{ use lemmy_db_views_actor::{
@ -42,7 +46,8 @@ impl PerformCrud for GetPersonDetails {
.username .username
.to_owned() .to_owned()
.unwrap_or_else(|| "admin".to_string()); .unwrap_or_else(|| "admin".to_string());
let actor_id = get_actor_id_from_name(WebfingerType::Person, &name, context).await?; let actor_id =
webfinger_resolve::<ApubPerson>(&name, EndpointType::Person, context, &mut 0).await?;
let person = ObjectId::<ApubPerson>::new(actor_id) let person = ObjectId::<ApubPerson>::new(actor_id)
.dereference(context, &mut 0) .dereference(context, &mut 0)

View file

@ -23,7 +23,13 @@
"http://enterprise.lemmy.ml/c/main", "http://enterprise.lemmy.ml/c/main",
"http://ds9.lemmy.ml/u/lemmy_alpha" "http://ds9.lemmy.ml/u/lemmy_alpha"
], ],
"tag": [], "tag": [
{
"href": "http://ds9.lemmy.ml/u/lemmy_alpha",
"type": "Mention",
"name": "@lemmy_alpha@ds9.lemmy.ml"
}
],
"type": "Create", "type": "Create",
"id": "http://ds9.lemmy.ml/activities/create/1e77d67c-44ac-45ed-bf2a-460e21f60236" "id": "http://ds9.lemmy.ml/activities/create/1e77d67c-44ac-45ed-bf2a-460e21f60236"
} }

View file

@ -3,7 +3,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"object": "http://ds9.lemmy.ml/post/1", "object": {
"id": "http://ds9.lemmy.ml/post/1",
"type": "Tombstone"
},
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View file

@ -3,7 +3,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"object": "http://ds9.lemmy.ml/comment/1", "object": {
"id": "http://ds9.lemmy.ml/comment/1",
"type": "Tombstone"
},
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View file

@ -8,7 +8,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"object": "http://ds9.lemmy.ml/post/1", "object": {
"id": "http://ds9.lemmy.ml/post/1",
"type": "Tombstone"
},
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View file

@ -8,7 +8,10 @@
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
"object": "http://ds9.lemmy.ml/comment/1", "object": {
"id": "http://ds9.lemmy.ml/comment/1",
"type": "Tombstone"
},
"cc": [ "cc": [
"http://enterprise.lemmy.ml/c/main" "http://enterprise.lemmy.ml/c/main"
], ],

View file

@ -1,13 +1,7 @@
{ {
"actor": "http://enterprise.lemmy.ml/c/main", "actor": "http://enterprise.lemmy.ml/c/main",
"to": [
"http://ds9.lemmy.ml/u/lemmy_alpha"
],
"object": { "object": {
"actor": "http://ds9.lemmy.ml/u/lemmy_alpha", "actor": "http://ds9.lemmy.ml/u/lemmy_alpha",
"to": [
"http://enterprise.lemmy.ml/c/main"
],
"object": "http://enterprise.lemmy.ml/c/main", "object": "http://enterprise.lemmy.ml/c/main",
"type": "Follow", "type": "Follow",
"id": "http://ds9.lemmy.ml/activities/follow/6abcd50b-b8ca-4952-86b0-a6dd8cc12866" "id": "http://ds9.lemmy.ml/activities/follow/6abcd50b-b8ca-4952-86b0-a6dd8cc12866"

View file

@ -1,8 +1,5 @@
{ {
"actor": "http://ds9.lemmy.ml/u/lemmy_alpha", "actor": "http://ds9.lemmy.ml/u/lemmy_alpha",
"to": [
"http://enterprise.lemmy.ml/c/main"
],
"object": "http://enterprise.lemmy.ml/c/main", "object": "http://enterprise.lemmy.ml/c/main",
"type": "Follow", "type": "Follow",
"id": "http://ds9.lemmy.ml/activities/follow/6abcd50b-b8ca-4952-86b0-a6dd8cc12866" "id": "http://ds9.lemmy.ml/activities/follow/6abcd50b-b8ca-4952-86b0-a6dd8cc12866"

View file

@ -1,13 +1,7 @@
{ {
"actor": "http://ds9.lemmy.ml/u/lemmy_alpha", "actor": "http://ds9.lemmy.ml/u/lemmy_alpha",
"to": [
"http://enterprise.lemmy.ml/c/main"
],
"object": { "object": {
"actor": "http://ds9.lemmy.ml/u/lemmy_alpha", "actor": "http://ds9.lemmy.ml/u/lemmy_alpha",
"to": [
"http://enterprise.lemmy.ml/c/main"
],
"object": "http://enterprise.lemmy.ml/c/main", "object": "http://enterprise.lemmy.ml/c/main",
"type": "Follow", "type": "Follow",
"id": "http://ds9.lemmy.ml/activities/follow/dc2f1bc5-f3a0-4daa-a46b-428cbfbd023c" "id": "http://ds9.lemmy.ml/activities/follow/dc2f1bc5-f3a0-4daa-a46b-428cbfbd023c"

View file

@ -3,6 +3,10 @@
"type": "Note", "type": "Note",
"attributedTo": "https://enterprise.lemmy.ml/u/picard", "attributedTo": "https://enterprise.lemmy.ml/u/picard",
"to": ["https://www.w3.org/ns/activitystreams#Public"], "to": ["https://www.w3.org/ns/activitystreams#Public"],
"cc": [
"https://enterprise.lemmy.ml/c/tenforward",
"https://enterprise.lemmy.ml/u/picard"
],
"inReplyTo": "https://enterprise.lemmy.ml/post/55143", "inReplyTo": "https://enterprise.lemmy.ml/post/55143",
"content": "<p>first comment!</p>\n", "content": "<p>first comment!</p>\n",
"mediaType": "text/html", "mediaType": "text/html",
@ -10,6 +14,13 @@
"content": "first comment!", "content": "first comment!",
"mediaType": "text/markdown" "mediaType": "text/markdown"
}, },
"tag": [
{
"href": "https://enterprise.lemmy.ml/u/picard",
"type": "Mention",
"name": "@picard@enterprise.lemmy.ml"
}
],
"published": "2021-03-01T13:42:43.966208+00:00", "published": "2021-03-01T13:42:43.966208+00:00",
"updated": "2021-03-01T13:43:03.955787+00:00" "updated": "2021-03-01T13:43:03.955787+00:00"
} }

View file

@ -16,10 +16,12 @@
"content": "<p>So does this federate now?</p>", "content": "<p>So does this federate now?</p>",
"inReplyTo": "https://ds9.lemmy.ml/post/1723", "inReplyTo": "https://ds9.lemmy.ml/post/1723",
"published": "2021-11-09T11:42:35Z", "published": "2021-11-09T11:42:35Z",
"tag": { "tag": [
{
"type": "Mention", "type": "Mention",
"href": "https://ds9.lemmy.ml/u/nutomic" "href": "https://ds9.lemmy.ml/u/nutomic"
}, }
],
"url": "https://friends.grishka.me/posts/66561", "url": "https://friends.grishka.me/posts/66561",
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"

View file

@ -5,10 +5,12 @@
"content": "<p>So does this federate now?</p>", "content": "<p>So does this federate now?</p>",
"inReplyTo": "https://ds9.lemmy.ml/post/1723", "inReplyTo": "https://ds9.lemmy.ml/post/1723",
"published": "2021-11-09T11:42:35Z", "published": "2021-11-09T11:42:35Z",
"tag": { "tag": [
{
"type": "Mention", "type": "Mention",
"href": "https://ds9.lemmy.ml/u/nutomic" "href": "https://ds9.lemmy.ml/u/nutomic"
}, }
],
"url": "https://friends.grishka.me/posts/66561", "url": "https://friends.grishka.me/posts/66561",
"to": [ "to": [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
activities::{ activities::{
check_community_deleted_or_removed, check_community_deleted_or_removed,
comment::{collect_non_local_mentions, get_notif_recipients}, comment::get_notif_recipients,
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -12,7 +12,7 @@ use crate::{
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson}, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType}, protocol::activities::{create_or_update::comment::CreateOrUpdateComment, CreateOrUpdateType},
}; };
use activitystreams::public; use activitystreams::{link::LinkExt, public};
use lemmy_api_common::{blocking, check_post_deleted_or_removed}; use lemmy_api_common::{blocking, check_post_deleted_or_removed};
use lemmy_apub_lib::{ use lemmy_apub_lib::{
data::Data, data::Data,
@ -33,6 +33,7 @@ impl CreateOrUpdateComment {
actor: &ApubPerson, actor: &ApubPerson,
kind: CreateOrUpdateType, kind: CreateOrUpdateType,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// TODO: might be helpful to add a comment method to retrieve community directly // TODO: might be helpful to add a comment method to retrieve community directly
let post_id = comment.post_id; let post_id = comment.post_id;
@ -48,21 +49,34 @@ impl CreateOrUpdateComment {
kind.clone(), kind.clone(),
&context.settings().get_protocol_and_hostname(), &context.settings().get_protocol_and_hostname(),
)?; )?;
let maa = collect_non_local_mentions(&comment, &community, context).await?; let note = comment.into_apub(context).await?;
let create_or_update = CreateOrUpdateComment { let create_or_update = CreateOrUpdateComment {
actor: ObjectId::new(actor.actor_id()), actor: ObjectId::new(actor.actor_id()),
to: vec![public()], to: vec![public()],
object: comment.into_apub(context).await?, cc: note.cc.clone(),
cc: maa.ccs, tag: note.tag.clone(),
tag: maa.tags, object: note,
kind, kind,
id: id.clone(), id: id.clone(),
unparsed: Default::default(), unparsed: Default::default(),
}; };
let tagged_users: Vec<ObjectId<ApubPerson>> = create_or_update
.tag
.iter()
.map(|t| t.href())
.flatten()
.map(|t| ObjectId::new(t.clone()))
.collect();
let mut inboxes = vec![];
for t in tagged_users {
let person = t.dereference(context, request_counter).await?;
inboxes.push(person.shared_inbox_or_inbox_url());
}
let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update); let activity = AnnouncableActivities::CreateOrUpdateComment(create_or_update);
send_to_community(activity, &id, actor, &community, maa.inboxes, context).await send_activity_in_community(activity, &id, actor, &community, inboxes, context).await
} }
} }

View file

@ -1,26 +1,13 @@
use crate::objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson}; use crate::objects::person::ApubPerson;
use activitystreams::{
base::BaseExt,
link::{LinkExt, Mention},
};
use anyhow::anyhow;
use itertools::Itertools;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType, webfinger::WebfingerResponse}; use lemmy_apub_lib::object_id::ObjectId;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::LocalUserId, newtypes::LocalUserId,
source::{comment::Comment, person::Person, post::Post}, source::{comment::Comment, post::Post},
traits::Crud, traits::Crud,
DbPool,
};
use lemmy_utils::{
request::{retry, RecvError},
utils::{scrape_text_for_mentions, MentionData},
LemmyError,
}; };
use lemmy_utils::{utils::scrape_text_for_mentions, LemmyError};
use lemmy_websocket::{send::send_local_notifs, LemmyContext}; use lemmy_websocket::{send::send_local_notifs, LemmyContext};
use log::debug;
use url::Url;
pub mod create_or_update; pub mod create_or_update;
@ -42,114 +29,3 @@ async fn get_notif_recipients(
let mentions = scrape_text_for_mentions(&comment.content); let mentions = scrape_text_for_mentions(&comment.content);
send_local_notifs(mentions, comment, &*actor, &post, true, context).await send_local_notifs(mentions, comment, &*actor, &post, true, context).await
} }
pub struct MentionsAndAddresses {
pub ccs: Vec<Url>,
pub inboxes: Vec<Url>,
pub tags: Vec<Mention>,
}
/// This takes a comment, and builds a list of to_addresses, inboxes,
/// and mention tags, so they know where to be sent to.
/// Addresses are the persons / addresses that go in the cc field.
pub async fn collect_non_local_mentions(
comment: &ApubComment,
community: &ApubCommunity,
context: &LemmyContext,
) -> Result<MentionsAndAddresses, LemmyError> {
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
let mut addressed_ccs: Vec<Url> = vec![community.actor_id(), parent_creator.actor_id()];
// Note: dont include community inbox here, as we send to it separately with `send_to_community()`
let mut inboxes = vec![parent_creator.shared_inbox_or_inbox_url()];
// Add the mention tag
let mut tags = Vec::new();
// Get the person IDs for any mentions
let mentions = scrape_text_for_mentions(&comment.content)
.into_iter()
// Filter only the non-local ones
.filter(|m| !m.is_local(&context.settings().hostname))
.collect::<Vec<MentionData>>();
for mention in &mentions {
// TODO should it be fetching it every time?
if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_string().parse()?);
let mention_person = actor_id.dereference(context, &mut 0).await?;
inboxes.push(mention_person.shared_inbox_or_inbox_url());
let mut mention_tag = Mention::new();
mention_tag
.set_href(actor_id.into())
.set_name(mention.full_name());
tags.push(mention_tag);
}
}
let inboxes = inboxes.into_iter().unique().collect();
Ok(MentionsAndAddresses {
ccs: addressed_ccs,
inboxes,
tags,
})
}
/// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
/// top-level comment, the creator of the post, otherwise the creator of the parent comment.
async fn get_comment_parent_creator(
pool: &DbPool,
comment: &Comment,
) -> Result<ApubPerson, LemmyError> {
let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
let parent_comment =
blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
parent_comment.creator_id
} else {
let parent_post_id = comment.post_id;
let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
parent_post.creator_id
};
Ok(
blocking(pool, move |conn| Person::read(conn, parent_creator_id))
.await??
.into(),
)
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
async fn fetch_webfinger_url(
mention: &MentionData,
context: &LemmyContext,
) -> Result<Url, LemmyError> {
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
context.settings().get_protocol_string(),
mention.domain,
mention.name,
mention.domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| context.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

@ -1,6 +1,10 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, get_community_from_moderators_url, send_to_community}, community::{
announce::GetCommunity,
get_community_from_moderators_url,
send_activity_in_community,
},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_add_remove_moderator_target, verify_add_remove_moderator_target,
@ -51,7 +55,7 @@ impl AddMod {
let activity = AnnouncableActivities::AddMod(add); let activity = AnnouncableActivities::AddMod(add);
let inboxes = vec![added_mod.shared_inbox_or_inbox_url()]; let inboxes = vec![added_mod.shared_inbox_or_inbox_url()];
send_to_community(activity, &id, actor, community, inboxes, context).await send_activity_in_community(activity, &id, actor, community, inboxes, context).await
} }
} }

View file

@ -14,7 +14,6 @@ use lemmy_apub_lib::{
}; };
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use url::Url;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
pub(crate) trait GetCommunity { pub(crate) trait GetCommunity {
@ -48,13 +47,10 @@ impl AnnounceActivity {
pub async fn send( pub async fn send(
object: AnnouncableActivities, object: AnnouncableActivities,
community: &ApubCommunity, community: &ApubCommunity,
additional_inboxes: Vec<Url>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let announce = AnnounceActivity::new(object.clone(), community, context)?; let announce = AnnounceActivity::new(object.clone(), community, context)?;
let inboxes = community let inboxes = community.get_follower_inboxes(context).await?;
.get_follower_inboxes(additional_inboxes.clone(), context)
.await?;
send_lemmy_activity( send_lemmy_activity(
context, context,
&announce, &announce,
@ -65,9 +61,9 @@ impl AnnounceActivity {
) )
.await?; .await?;
// Pleroma (and likely Mastodon) can't handle activities like Announce/Create/Page, so for // Pleroma (and likely Mastodon) can't handle activities like Announce/Create/Page. So for
// compatibility, we also send Announce/Page and Announce/Note (for new and updated // compatibility to allow them to follow Lemmy communities, we also send Announce/Page and
// posts/comments). // Announce/Note (for new and updated posts/comments).
use AnnouncableActivities::*; use AnnouncableActivities::*;
let object = match object { let object = match object {
CreateOrUpdatePost(c) => Page(c.object), CreateOrUpdatePost(c) => Page(c.object),

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -63,7 +63,7 @@ impl BlockUserFromCommunity {
let activity = AnnouncableActivities::BlockUserFromCommunity(block); let activity = AnnouncableActivities::BlockUserFromCommunity(block);
let inboxes = vec![target.shared_inbox_or_inbox_url()]; let inboxes = vec![target.shared_inbox_or_inbox_url()];
send_to_community(activity, &block_id, actor, community, inboxes, context).await send_activity_in_community(activity, &block_id, actor, community, inboxes, context).await
} }
} }

View file

@ -1,7 +1,6 @@
use crate::{ use crate::{
activities::send_lemmy_activity, activities::send_lemmy_activity,
activity_lists::AnnouncableActivities, activity_lists::AnnouncableActivities,
insert_activity,
objects::community::ApubCommunity, objects::community::ApubCommunity,
protocol::activities::community::announce::AnnounceActivity, protocol::activities::community::announce::AnnounceActivity,
}; };
@ -18,23 +17,19 @@ pub mod report;
pub mod undo_block_user; pub mod undo_block_user;
pub mod update; pub mod update;
pub(crate) async fn send_to_community<T: ActorType>( pub(crate) async fn send_activity_in_community<T: ActorType>(
activity: AnnouncableActivities, activity: AnnouncableActivities,
activity_id: &Url, activity_id: &Url,
actor: &T, actor: &T,
community: &ApubCommunity, community: &ApubCommunity,
additional_inboxes: Vec<Url>, mut inboxes: Vec<Url>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// if this is a local community, we need to do an announce from the community instead
let object_value = serde_json::to_value(&activity)?;
if community.local {
insert_activity(activity_id, object_value, true, false, context.pool()).await?;
AnnounceActivity::send(activity, community, additional_inboxes, context).await?;
} else {
let mut inboxes = additional_inboxes;
inboxes.push(community.shared_inbox_or_inbox_url()); inboxes.push(community.shared_inbox_or_inbox_url());
send_lemmy_activity(context, &activity, activity_id, actor, inboxes, false).await?; send_lemmy_activity(context, &activity, activity_id, actor, inboxes, false).await?;
if community.local {
AnnounceActivity::send(activity, community, context).await?;
} }
Ok(()) Ok(())

View file

@ -1,6 +1,10 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, get_community_from_moderators_url, send_to_community}, community::{
announce::GetCommunity,
get_community_from_moderators_url,
send_activity_in_community,
},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_add_remove_moderator_target, verify_add_remove_moderator_target,
@ -51,7 +55,7 @@ impl RemoveMod {
let activity = AnnouncableActivities::RemoveMod(remove); let activity = AnnouncableActivities::RemoveMod(remove);
let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()]; let inboxes = vec![removed_mod.shared_inbox_or_inbox_url()];
send_to_community(activity, &id, actor, community, inboxes, context).await send_activity_in_community(activity, &id, actor, community, inboxes, context).await
} }
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -53,7 +53,7 @@ impl UndoBlockUserFromCommunity {
let activity = AnnouncableActivities::UndoBlockUserFromCommunity(undo); let activity = AnnouncableActivities::UndoBlockUserFromCommunity(undo);
let inboxes = vec![target.shared_inbox_or_inbox_url()]; let inboxes = vec![target.shared_inbox_or_inbox_url()];
send_to_community(activity, &id, actor, community, inboxes, context).await send_activity_in_community(activity, &id, actor, community, inboxes, context).await
} }
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -46,7 +46,7 @@ impl UpdateCommunity {
}; };
let activity = AnnouncableActivities::UpdateCommunity(update); let activity = AnnouncableActivities::UpdateCommunity(update);
send_to_community(activity, &id, actor, &community, vec![], context).await send_activity_in_community(activity, &id, actor, &community, vec![], context).await
} }
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
deletion::{receive_delete_action, verify_delete_activity, DeletableObjects}, deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
@ -50,11 +50,11 @@ impl ActivityHandler for Delete {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_is_public(&self.to, &self.cc)?; verify_is_public(&self.to, &[])?;
verify_activity(&self.id, self.actor.inner(), &context.settings())?; verify_activity(&self.id, self.actor.inner(), &context.settings())?;
let community = self.get_community(context, request_counter).await?; let community = self.get_community(context, request_counter).await?;
verify_delete_activity( verify_delete_activity(
&self.object, &self.object.id,
&self.actor, &self.actor,
&community, &community,
self.summary.is_some(), self.summary.is_some(),
@ -78,9 +78,16 @@ impl ActivityHandler for Delete {
} else { } else {
Some(reason) Some(reason)
}; };
receive_remove_action(&self.actor, &self.object, reason, context, request_counter).await receive_remove_action(
&self.actor,
&self.object.id,
reason,
context,
request_counter,
)
.await
} else { } else {
receive_delete_action(&self.object, &self.actor, true, context, request_counter).await receive_delete_action(&self.object.id, &self.actor, true, context, request_counter).await
} }
} }
} }
@ -88,16 +95,14 @@ impl ActivityHandler for Delete {
impl Delete { impl Delete {
pub(in crate::activities::deletion) fn new( pub(in crate::activities::deletion) fn new(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, object: DeletableObjects,
object_id: Url,
summary: Option<String>, summary: Option<String>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<Delete, LemmyError> { ) -> Result<Delete, LemmyError> {
Ok(Delete { Ok(Delete {
actor: ObjectId::new(actor.actor_id()), actor: ObjectId::new(actor.actor_id()),
to: vec![public()], to: vec![public()],
object: object_id, object: object.to_tombstone()?,
cc: vec![community.actor_id()],
kind: DeleteType::Delete, kind: DeleteType::Delete,
summary, summary,
id: generate_activity_id( id: generate_activity_id(
@ -110,15 +115,15 @@ impl Delete {
pub(in crate::activities::deletion) async fn send( pub(in crate::activities::deletion) async fn send(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
summary: Option<String>, summary: Option<String>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let delete = Delete::new(actor, community, object_id, summary, context)?; let delete = Delete::new(actor, object, summary, context)?;
let delete_id = delete.id.clone(); let delete_id = delete.id.clone();
let activity = AnnouncableActivities::Delete(delete); let activity = AnnouncableActivities::Delete(delete);
send_to_community(activity, &delete_id, actor, community, vec![], context).await send_activity_in_community(activity, &delete_id, actor, community, vec![], context).await
} }
} }
@ -201,7 +206,7 @@ impl GetCommunity for Delete {
context: &LemmyContext, context: &LemmyContext,
_request_counter: &mut i32, _request_counter: &mut i32,
) -> Result<ApubCommunity, LemmyError> { ) -> Result<ApubCommunity, LemmyError> {
let community_id = match DeletableObjects::read_from_db(&self.object, context).await? { let community_id = match DeletableObjects::read_from_db(&self.object.id, context).await? {
DeletableObjects::Community(c) => c.id, DeletableObjects::Community(c) => c.id,
DeletableObjects::Comment(c) => { DeletableObjects::Comment(c) => {
let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, c.post_id)).await??;

View file

@ -1,14 +1,13 @@
use crate::{ use crate::{
activities::{verify_mod_action, verify_person_in_community}, activities::{verify_mod_action, verify_person_in_community},
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost}, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::activities::deletion::{delete::Delete, undo_delete::UndoDelete}, protocol::{
activities::deletion::{delete::Delete, undo_delete::UndoDelete},
objects::tombstone::Tombstone,
},
}; };
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::{ use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject, verify::verify_domains_match};
object_id::ObjectId,
traits::{ActorType, ApubObject},
verify::verify_domains_match,
};
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post}; use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{ use lemmy_websocket::{
@ -24,14 +23,14 @@ pub mod undo_delete;
pub async fn send_apub_delete( pub async fn send_apub_delete(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
deleted: bool, deleted: bool,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if deleted { if deleted {
Delete::send(actor, community, object_id, None, context).await Delete::send(actor, community, object, None, context).await
} else { } else {
UndoDelete::send(actor, community, object_id, None, context).await UndoDelete::send(actor, community, object, None, context).await
} }
} }
@ -40,15 +39,15 @@ pub async fn send_apub_delete(
pub async fn send_apub_remove( pub async fn send_apub_remove(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
reason: String, reason: String,
removed: bool, removed: bool,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if removed { if removed {
Delete::send(actor, community, object_id, Some(reason), context).await Delete::send(actor, community, object, Some(reason), context).await
} else { } else {
UndoDelete::send(actor, community, object_id, Some(reason), context).await UndoDelete::send(actor, community, object, Some(reason), context).await
} }
} }
@ -74,6 +73,14 @@ impl DeletableObjects {
} }
Err(diesel::NotFound.into()) Err(diesel::NotFound.into())
} }
pub(crate) fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
match self {
DeletableObjects::Community(c) => c.to_tombstone(),
DeletableObjects::Comment(c) => c.to_tombstone(),
DeletableObjects::Post(p) => p.to_tombstone(),
}
}
} }
pub(in crate::activities) async fn verify_delete_activity( pub(in crate::activities) async fn verify_delete_activity(
@ -153,7 +160,7 @@ async fn receive_delete_action(
DeletableObjects::Community(community) => { DeletableObjects::Community(community) => {
if community.local { if community.local {
let mod_ = actor.dereference(context, request_counter).await?; let mod_ = actor.dereference(context, request_counter).await?;
let object = community.actor_id(); let object = DeletableObjects::Community(community.clone());
send_apub_delete(&mod_, &community.clone(), object, true, context).await?; send_apub_delete(&mod_, &community.clone(), object, true, context).await?;
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
deletion::{receive_delete_action, verify_delete_activity, DeletableObjects}, deletion::{receive_delete_action, verify_delete_activity, DeletableObjects},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
@ -40,7 +40,7 @@ impl ActivityHandler for UndoDelete {
self.object.verify(context, request_counter).await?; self.object.verify(context, request_counter).await?;
let community = self.get_community(context, request_counter).await?; let community = self.get_community(context, request_counter).await?;
verify_delete_activity( verify_delete_activity(
&self.object.object, &self.object.object.id,
&self.actor, &self.actor,
&community, &community,
self.object.summary.is_some(), self.object.summary.is_some(),
@ -57,10 +57,10 @@ impl ActivityHandler for UndoDelete {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if self.object.summary.is_some() { if self.object.summary.is_some() {
UndoDelete::receive_undo_remove_action(&self.object.object, context).await UndoDelete::receive_undo_remove_action(&self.object.object.id, context).await
} else { } else {
receive_delete_action( receive_delete_action(
&self.object.object, &self.object.object.id,
&self.actor, &self.actor,
false, false,
context, context,
@ -75,11 +75,11 @@ impl UndoDelete {
pub(in crate::activities::deletion) async fn send( pub(in crate::activities::deletion) async fn send(
actor: &ApubPerson, actor: &ApubPerson,
community: &ApubCommunity, community: &ApubCommunity,
object_id: Url, object: DeletableObjects,
summary: Option<String>, summary: Option<String>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let object = Delete::new(actor, community, object_id, summary, context)?; let object = Delete::new(actor, object, summary, context)?;
let id = generate_activity_id( let id = generate_activity_id(
UndoType::Undo, UndoType::Undo,
@ -96,7 +96,7 @@ impl UndoDelete {
}; };
let activity = AnnouncableActivities::UndoDelete(undo); let activity = AnnouncableActivities::UndoDelete(undo);
send_to_community(activity, &id, actor, community, vec![], context).await send_activity_in_community(activity, &id, actor, community, vec![], context).await
} }
pub(in crate::activities) async fn receive_undo_remove_action( pub(in crate::activities) async fn receive_undo_remove_action(

View file

@ -28,7 +28,6 @@ impl AcceptFollowCommunity {
.await?; .await?;
let accept = AcceptFollowCommunity { let accept = AcceptFollowCommunity {
actor: ObjectId::new(community.actor_id()), actor: ObjectId::new(community.actor_id()),
to: [ObjectId::new(person.actor_id())],
object: follow, object: follow,
kind: AcceptType::Accept, kind: AcceptType::Accept,
id: generate_activity_id( id: generate_activity_id(
@ -52,8 +51,7 @@ impl ActivityHandler for AcceptFollowCommunity {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_activity(&self.id, self.actor.inner(), &context.settings())?; verify_activity(&self.id, self.actor.inner(), &context.settings())?;
verify_urls_match(self.to[0].inner(), self.object.actor.inner())?; verify_urls_match(self.actor.inner(), self.object.object.inner())?;
verify_urls_match(self.actor.inner(), self.object.to[0].inner())?;
self.object.verify(context, request_counter).await?; self.object.verify(context, request_counter).await?;
Ok(()) Ok(())
} }
@ -63,11 +61,15 @@ impl ActivityHandler for AcceptFollowCommunity {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?; let person = self.actor.dereference(context, request_counter).await?;
let to = self.to[0].dereference(context, request_counter).await?; let community = self
.object
.actor
.dereference(context, request_counter)
.await?;
// This will throw an error if no follow was requested // This will throw an error if no follow was requested
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
CommunityFollower::follow_accepted(conn, actor.id, to.id) CommunityFollower::follow_accepted(conn, person.id, community.id)
}) })
.await??; .await??;

View file

@ -15,7 +15,6 @@ use lemmy_apub_lib::{
data::Data, data::Data,
object_id::ObjectId, object_id::ObjectId,
traits::{ActivityHandler, ActorType}, traits::{ActivityHandler, ActorType},
verify::verify_urls_match,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::community::{CommunityFollower, CommunityFollowerForm}, source::community::{CommunityFollower, CommunityFollowerForm},
@ -32,7 +31,6 @@ impl FollowCommunity {
) -> Result<FollowCommunity, LemmyError> { ) -> Result<FollowCommunity, LemmyError> {
Ok(FollowCommunity { Ok(FollowCommunity {
actor: ObjectId::new(actor.actor_id()), actor: ObjectId::new(actor.actor_id()),
to: [ObjectId::new(community.actor_id())],
object: ObjectId::new(community.actor_id()), object: ObjectId::new(community.actor_id()),
kind: FollowType::Follow, kind: FollowType::Follow,
id: generate_activity_id( id: generate_activity_id(
@ -72,9 +70,8 @@ impl ActivityHandler for FollowCommunity {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_activity(&self.id, self.actor.inner(), &context.settings())?; verify_activity(&self.id, self.actor.inner(), &context.settings())?;
verify_urls_match(self.to[0].inner(), self.object.inner())?;
verify_person(&self.actor, context, request_counter).await?; verify_person(&self.actor, context, request_counter).await?;
let community = self.to[0].dereference(context, request_counter).await?; let community = self.object.dereference(context, request_counter).await?;
verify_person_in_community(&self.actor, &community, context, request_counter).await?; verify_person_in_community(&self.actor, &community, context, request_counter).await?;
Ok(()) Ok(())
} }
@ -84,11 +81,11 @@ impl ActivityHandler for FollowCommunity {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?; let person = self.actor.dereference(context, request_counter).await?;
let community = self.to[0].dereference(context, request_counter).await?; let community = self.object.dereference(context, request_counter).await?;
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
community_id: community.id, community_id: community.id,
person_id: actor.id, person_id: person.id,
pending: false, pending: false,
}; };

View file

@ -27,7 +27,6 @@ impl UndoFollowCommunity {
let object = FollowCommunity::new(actor, community, context)?; let object = FollowCommunity::new(actor, community, context)?;
let undo = UndoFollowCommunity { let undo = UndoFollowCommunity {
actor: ObjectId::new(actor.actor_id()), actor: ObjectId::new(actor.actor_id()),
to: [ObjectId::new(community.actor_id())],
object, object,
kind: UndoType::Undo, kind: UndoType::Undo,
id: generate_activity_id( id: generate_activity_id(
@ -50,7 +49,6 @@ impl ActivityHandler for UndoFollowCommunity {
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
verify_activity(&self.id, self.actor.inner(), &context.settings())?; verify_activity(&self.id, self.actor.inner(), &context.settings())?;
verify_urls_match(self.to[0].inner(), self.object.object.inner())?;
verify_urls_match(self.actor.inner(), self.object.actor.inner())?; verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
verify_person(&self.actor, context, request_counter).await?; verify_person(&self.actor, context, request_counter).await?;
self.object.verify(context, request_counter).await?; self.object.verify(context, request_counter).await?;
@ -62,12 +60,16 @@ impl ActivityHandler for UndoFollowCommunity {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = self.actor.dereference(context, request_counter).await?; let person = self.actor.dereference(context, request_counter).await?;
let community = self.to[0].dereference(context, request_counter).await?; let community = self
.object
.object
.dereference(context, request_counter)
.await?;
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
community_id: community.id, community_id: community.id,
person_id: actor.id, person_id: person.id,
pending: false, pending: false,
}; };

View file

@ -117,7 +117,7 @@ fn verify_add_remove_moderator_target(
} }
pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> { pub(crate) fn verify_is_public(to: &[Url], cc: &[Url]) -> Result<(), LemmyError> {
if !to.contains(&public()) && !cc.contains(&public()) { if ![to, cc].iter().any(|set| set.contains(&public())) {
return Err(anyhow!("Object is not public").into()); return Err(anyhow!("Object is not public").into());
} }
Ok(()) Ok(())
@ -175,9 +175,10 @@ async fn send_lemmy_activity<T: Serialize>(
insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?; insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
send_activity( send_activity(
serialised_activity, activity_id,
actor, actor,
inboxes, inboxes,
serialised_activity,
context.client(), context.client(),
context.activity_queue(), context.activity_queue(),
) )

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
activities::{ activities::{
check_community_deleted_or_removed, check_community_deleted_or_removed,
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -63,7 +63,7 @@ impl CreateOrUpdatePost {
let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?; let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
let id = create_or_update.id.clone(); let id = create_or_update.id.clone();
let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update); let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
send_to_community(activity, &id, actor, &community, vec![], context).await send_activity_in_community(activity, &id, actor, &community, vec![], context).await
} }
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -56,7 +56,7 @@ impl UndoVote {
unparsed: Default::default(), unparsed: Default::default(),
}; };
let activity = AnnouncableActivities::UndoVote(undo_vote); let activity = AnnouncableActivities::UndoVote(undo_vote);
send_to_community(activity, &id, actor, &community, vec![], context).await send_activity_in_community(activity, &id, actor, &community, vec![], context).await
} }
} }

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
activities::{ activities::{
community::{announce::GetCommunity, send_to_community}, community::{announce::GetCommunity, send_activity_in_community},
generate_activity_id, generate_activity_id,
verify_activity, verify_activity,
verify_is_public, verify_is_public,
@ -62,7 +62,7 @@ impl Vote {
let vote_id = vote.id.clone(); let vote_id = vote.id.clone();
let activity = AnnouncableActivities::Vote(vote); let activity = AnnouncableActivities::Vote(vote);
send_to_community(activity, &vote_id, actor, &community, vec![], context).await send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
} }
} }

View file

@ -1,3 +1,4 @@
pub mod post_or_comment; pub mod post_or_comment;
pub mod search; pub mod search;
pub mod user_or_community; pub mod user_or_community;
pub mod webfinger;

View file

@ -1,20 +1,12 @@
use crate::{ use crate::{
fetcher::webfinger::webfinger_resolve,
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost}, objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::objects::{group::Group, note::Note, page::Page, person::Person}, protocol::objects::{group::Group, note::Note, page::Page, person::Person},
EndpointType,
}; };
use anyhow::anyhow; use anyhow::anyhow;
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use itertools::Itertools; use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
object_id::ObjectId,
traits::ApubObject,
webfinger::{webfinger_resolve_actor, WebfingerType},
};
use lemmy_db_schema::{
source::{community::Community, person::Person as DbPerson},
DbPool,
};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
@ -31,58 +23,48 @@ pub async fn search_by_apub_id(
query: &str, query: &str,
context: &LemmyContext, context: &LemmyContext,
) -> Result<SearchableObjects, LemmyError> { ) -> Result<SearchableObjects, LemmyError> {
let query_url = match Url::parse(query) {
Ok(u) => u,
Err(_) => {
let (kind, name) = query.split_at(1);
let kind = match kind {
"@" => WebfingerType::Person,
"!" => WebfingerType::Group,
_ => return Err(anyhow!("invalid query").into()),
};
// remote actor, use webfinger to resolve url
if name.contains('@') {
let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
webfinger_resolve_actor(
name,
domain,
kind,
context.client(),
context.settings().get_protocol_string(),
)
.await?
}
// local actor, read from database and return
else {
return find_local_actor_by_name(name, kind, context.pool()).await;
}
}
};
let request_counter = &mut 0; let request_counter = &mut 0;
ObjectId::new(query_url) match Url::parse(query) {
Ok(url) => {
ObjectId::new(url)
.dereference(context, request_counter) .dereference(context, request_counter)
.await .await
} }
Err(_) => {
async fn find_local_actor_by_name( let (kind, identifier) = query.split_at(1);
name: &str, match kind {
kind: WebfingerType, "@" => {
pool: &DbPool, let id = webfinger_resolve::<ApubPerson>(
) -> Result<SearchableObjects, LemmyError> { identifier,
let name: String = name.into(); EndpointType::Person,
Ok(match kind { context,
WebfingerType::Group => SearchableObjects::Community( request_counter,
blocking(pool, move |conn| Community::read_from_name(conn, &name)) )
.await?? .await?;
.into(), Ok(SearchableObjects::Person(
), ObjectId::new(id)
WebfingerType::Person => SearchableObjects::Person( .dereference(context, request_counter)
blocking(pool, move |conn| DbPerson::find_by_name(conn, &name)) .await?,
.await?? ))
.into(), }
), "!" => {
}) let id = webfinger_resolve::<ApubCommunity>(
identifier,
EndpointType::Community,
context,
request_counter,
)
.await?;
Ok(SearchableObjects::Community(
ObjectId::new(id)
.dereference(context, request_counter)
.await?,
))
}
_ => Err(anyhow!("invalid query").into()),
}
}
}
} }
/// 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.

View file

@ -96,7 +96,10 @@ impl ApubObject for UserOrCommunity {
impl ActorType for UserOrCommunity { impl ActorType for UserOrCommunity {
fn actor_id(&self) -> Url { fn actor_id(&self) -> Url {
todo!() match self {
UserOrCommunity::User(p) => p.actor_id(),
UserOrCommunity::Community(p) => p.actor_id(),
}
} }
fn public_key(&self) -> Option<String> { fn public_key(&self) -> Option<String> {

View file

@ -0,0 +1,107 @@
use crate::{generate_local_apub_endpoint, EndpointType};
use anyhow::anyhow;
use itertools::Itertools;
use lemmy_apub_lib::{
object_id::ObjectId,
traits::{ActorType, ApubObject},
};
use lemmy_db_schema::newtypes::DbUrl;
use lemmy_utils::{
request::{retry, RecvError},
LemmyError,
};
use lemmy_websocket::LemmyContext;
use log::debug;
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>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebfingerResponse {
pub subject: String,
pub links: Vec<WebfingerLink>,
}
/// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and
/// outputs the actor id. Used in the API for communities and users.
///
/// TODO: later provide a method in ApubObject to generate the endpoint, so that we dont have to
/// pass in EndpointType
pub async fn webfinger_resolve<Kind>(
identifier: &str,
endpoint_type: EndpointType,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<DbUrl, LemmyError>
where
Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
{
// remote actor
if identifier.contains('@') {
webfinger_resolve_actor::<Kind>(identifier, context, request_counter).await
}
// local actor
else {
let domain = context.settings().get_protocol_and_hostname();
Ok(generate_local_apub_endpoint(
endpoint_type,
identifier,
&domain,
)?)
}
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
async fn webfinger_resolve_actor<Kind>(
identifier: &str,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<DbUrl, LemmyError>
where
Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
{
let protocol = context.settings().get_protocol_string();
let (_, domain) = identifier
.splitn(2, '@')
.collect_tuple()
.expect("invalid query");
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}",
protocol, domain, identifier
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| context.client().get(&fetch_url).send()).await?;
let res: WebfingerResponse = response
.json()
.await
.map_err(|e| RecvError(e.to_string()))?;
let links: Vec<Url> = res
.links
.iter()
.filter(|l| l.type_.eq(&Some("application/activity+json".to_string())))
.map(|l| l.href.clone())
.flatten()
.collect();
for l in links {
let object = ObjectId::<Kind>::new(l)
.dereference(context, request_counter)
.await;
if object.is_ok() {
return object.map(|o| o.actor_id().into());
}
}
Err(anyhow!("Failed to resolve actor for {}", identifier).into())
}

View file

@ -85,7 +85,7 @@ pub(in crate::http) async fn receive_group_inbox(
let community = announcable.get_community(context, &mut 0).await?; let community = announcable.get_community(context, &mut 0).await?;
verify_person_in_community(&actor_id, &community, context, &mut 0).await?; verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
if community.local { if community.local {
AnnounceActivity::send(*announcable, &community, vec![], context).await?; AnnounceActivity::send(*announcable, &community, context).await?;
} }
} }

View file

@ -1,9 +1,18 @@
use crate::fetcher::post_or_comment::PostOrComment;
use anyhow::{anyhow, Context};
use lemmy_api_common::blocking;
use lemmy_db_schema::{newtypes::DbUrl, source::activity::Activity, DbPool};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use std::net::IpAddr;
use url::{ParseError, Url};
pub mod activities; pub mod activities;
pub(crate) mod activity_lists; pub(crate) mod activity_lists;
pub(crate) mod collections; pub(crate) mod collections;
mod context; mod context;
pub mod fetcher; pub mod fetcher;
pub mod http; pub mod http;
pub(crate) mod mentions;
pub mod migrations; pub mod migrations;
pub mod objects; pub mod objects;
pub mod protocol; pub mod protocol;
@ -11,16 +20,6 @@ pub mod protocol;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
use crate::fetcher::post_or_comment::PostOrComment;
use anyhow::{anyhow, Context};
use lemmy_api_common::blocking;
use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
use lemmy_db_schema::{newtypes::DbUrl, source::activity::Activity, DbPool};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use std::net::IpAddr;
use url::{ParseError, Url};
/// Checks if the ID is allowed for sending or receiving. /// Checks if the ID is allowed for sending or receiving.
/// ///
/// In particular, it checks for: /// In particular, it checks for:
@ -145,35 +144,6 @@ fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
Ok(Url::parse(&format!("{}/moderators", community_id))?.into()) Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
} }
/// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and outputs the actor id.
/// Used in the API for communities and users.
pub async fn get_actor_id_from_name(
webfinger_type: WebfingerType,
short_name: &str,
context: &LemmyContext,
) -> Result<DbUrl, LemmyError> {
let split = short_name.split('@').collect::<Vec<&str>>();
let name = split[0];
// If there's no @, its local
if split.len() == 1 {
let domain = context.settings().get_protocol_and_hostname();
let endpoint_type = match webfinger_type {
WebfingerType::Person => EndpointType::Person,
WebfingerType::Group => EndpointType::Community,
};
Ok(generate_local_apub_endpoint(endpoint_type, name, &domain)?)
} else {
let protocol = context.settings().get_protocol_string();
Ok(
webfinger_resolve_actor(name, split[1], webfinger_type, context.client(), protocol)
.await?
.into(),
)
}
}
/// Store a sent or received activity in the database, for logging purposes. These records are not /// Store a sent or received activity in the database, for logging purposes. These records are not
/// persistent. /// persistent.
async fn insert_activity( async fn insert_activity(

134
crates/apub/src/mentions.rs Normal file
View file

@ -0,0 +1,134 @@
use crate::{
fetcher::webfinger::WebfingerResponse,
objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
base::BaseExt,
link::{LinkExt, Mention},
};
use anyhow::anyhow;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{object_id::ObjectId, traits::ActorType};
use lemmy_db_schema::{
source::{comment::Comment, person::Person, post::Post},
traits::Crud,
DbPool,
};
use lemmy_utils::{
request::{retry, RecvError},
utils::{scrape_text_for_mentions, MentionData},
LemmyError,
};
use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url;
pub struct MentionsAndAddresses {
pub ccs: Vec<Url>,
pub tags: Vec<Mention>,
}
/// This takes a comment, and builds a list of to_addresses, inboxes,
/// and mention tags, so they know where to be sent to.
/// Addresses are the persons / addresses that go in the cc field.
pub async fn collect_non_local_mentions(
comment: &ApubComment,
community_id: ObjectId<ApubCommunity>,
context: &LemmyContext,
) -> Result<MentionsAndAddresses, LemmyError> {
let parent_creator = get_comment_parent_creator(context.pool(), comment).await?;
let mut addressed_ccs: Vec<Url> = vec![community_id.into(), parent_creator.actor_id()];
// Add the mention tag
let mut parent_creator_tag = Mention::new();
parent_creator_tag
.set_href(parent_creator.actor_id.clone().into())
.set_name(format!(
"@{}@{}",
&parent_creator.name,
&parent_creator.actor_id().domain().expect("has domain")
));
let mut tags = vec![parent_creator_tag];
// Get the person IDs for any mentions
let mentions = scrape_text_for_mentions(&comment.content)
.into_iter()
// Filter only the non-local ones
.filter(|m| !m.is_local(&context.settings().hostname))
.collect::<Vec<MentionData>>();
for mention in &mentions {
// TODO should it be fetching it every time?
if let Ok(actor_id) = fetch_webfinger_url(mention, context).await {
let actor_id: ObjectId<ApubPerson> = ObjectId::new(actor_id);
debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_string().parse()?);
let mut mention_tag = Mention::new();
mention_tag
.set_href(actor_id.into())
.set_name(mention.full_name());
tags.push(mention_tag);
}
}
Ok(MentionsAndAddresses {
ccs: addressed_ccs,
tags,
})
}
/// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
/// top-level comment, the creator of the post, otherwise the creator of the parent comment.
async fn get_comment_parent_creator(
pool: &DbPool,
comment: &Comment,
) -> Result<ApubPerson, LemmyError> {
let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
let parent_comment =
blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
parent_comment.creator_id
} else {
let parent_post_id = comment.post_id;
let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
parent_post.creator_id
};
Ok(
blocking(pool, move |conn| Person::read(conn, parent_creator_id))
.await??
.into(),
)
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
async fn fetch_webfinger_url(
mention: &MentionData,
context: &LemmyContext,
) -> Result<Url, LemmyError> {
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource=acct:{}@{}",
context.settings().get_protocol_string(),
mention.domain,
mention.name,
mention.domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| context.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

@ -1,6 +1,7 @@
use crate::{ use crate::{
activities::{verify_is_public, verify_person_in_community}, activities::{verify_is_public, verify_person_in_community},
check_is_apub_id_valid, check_is_apub_id_valid,
mentions::collect_non_local_mentions,
protocol::{ protocol::{
objects::{ objects::{
note::{Note, SourceCompat}, note::{Note, SourceCompat},
@ -93,6 +94,11 @@ impl ApubObject for ApubComment {
let post_id = self.post_id; let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??; let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
})
.await??;
let in_reply_to = if let Some(comment_id) = self.parent_id { let in_reply_to = if let Some(comment_id) = self.parent_id {
let parent_comment = let parent_comment =
@ -101,13 +107,14 @@ impl ApubObject for ApubComment {
} else { } else {
ObjectId::<PostOrComment>::new(post.ap_id) ObjectId::<PostOrComment>::new(post.ap_id)
}; };
let maa = collect_non_local_mentions(&self, ObjectId::new(community.actor_id), context).await?;
let note = Note { let note = Note {
r#type: NoteType::Note, r#type: NoteType::Note,
id: ObjectId::new(self.ap_id.clone()), id: ObjectId::new(self.ap_id.clone()),
attributed_to: ObjectId::new(creator.actor_id), attributed_to: ObjectId::new(creator.actor_id),
to: vec![public()], to: vec![public()],
cc: vec![], cc: maa.ccs,
content: markdown_to_html(&self.content), content: markdown_to_html(&self.content),
media_type: Some(MediaTypeHtml::Html), media_type: Some(MediaTypeHtml::Html),
source: SourceCompat::Lemmy(Source { source: SourceCompat::Lemmy(Source {
@ -117,6 +124,7 @@ impl ApubObject for ApubComment {
in_reply_to, in_reply_to,
published: Some(convert_datetime(self.published)), published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime), updated: self.updated.map(convert_datetime),
tag: maa.tags,
unparsed: Default::default(), unparsed: Default::default(),
}; };
@ -124,10 +132,7 @@ impl ApubObject for ApubComment {
} }
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> { fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new( Ok(Tombstone::new(self.ap_id.clone().into()))
NoteType::Note,
self.updated.unwrap_or(self.published),
))
} }
async fn verify( async fn verify(

View file

@ -118,10 +118,7 @@ impl ApubObject for ApubCommunity {
} }
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> { fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new( Ok(Tombstone::new(self.actor_id()))
GroupType::Group,
self.updated.unwrap_or(self.published),
))
} }
async fn verify( async fn verify(
@ -192,7 +189,6 @@ impl ApubCommunity {
/// For a given community, returns the inboxes of all followers. /// For a given community, returns the inboxes of all followers.
pub(crate) async fn get_follower_inboxes( pub(crate) async fn get_follower_inboxes(
&self, &self,
additional_inboxes: Vec<Url>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<Vec<Url>, LemmyError> { ) -> Result<Vec<Url>, LemmyError> {
let id = self.id; let id = self.id;
@ -201,7 +197,7 @@ impl ApubCommunity {
CommunityFollowerView::for_community(conn, id) CommunityFollowerView::for_community(conn, id)
}) })
.await??; .await??;
let follower_inboxes: Vec<Url> = follows let inboxes: Vec<Url> = follows
.into_iter() .into_iter()
.filter(|f| !f.follower.local) .filter(|f| !f.follower.local)
.map(|f| { .map(|f| {
@ -210,12 +206,8 @@ impl ApubCommunity {
.unwrap_or(f.follower.inbox_url) .unwrap_or(f.follower.inbox_url)
.into() .into()
}) })
.collect();
let inboxes = vec![follower_inboxes, additional_inboxes]
.into_iter()
.flatten()
.unique() .unique()
.filter(|inbox| inbox.host_str() != Some(&context.settings().hostname)) .filter(|inbox: &Url| inbox.host_str() != Some(&context.settings().hostname))
// Don't send to blocked instances // Don't send to blocked instances
.filter(|inbox| check_is_apub_id_valid(inbox, false, &context.settings()).is_ok()) .filter(|inbox| check_is_apub_id_valid(inbox, false, &context.settings()).is_ok())
.collect(); .collect();

View file

@ -128,10 +128,7 @@ impl ApubObject for ApubPost {
} }
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> { fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
Ok(Tombstone::new( Ok(Tombstone::new(self.ap_id.clone().into()))
PageType::Page,
self.updated.unwrap_or(self.published),
))
} }
async fn verify( async fn verify(

View file

@ -14,7 +14,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_parsey_create_or_update() { async fn test_parse_create_or_update() {
test_parse_lemmy_item::<CreateOrUpdatePost>( test_parse_lemmy_item::<CreateOrUpdatePost>(
"assets/lemmy/activities/create_or_update/create_page.json", "assets/lemmy/activities/create_or_update/create_page.json",
); );

View file

@ -1,4 +1,4 @@
use crate::objects::person::ApubPerson; use crate::{objects::person::ApubPerson, protocol::objects::tombstone::Tombstone};
use activitystreams::{activity::kind::DeleteType, unparsed::Unparsed}; use activitystreams::{activity::kind::DeleteType, unparsed::Unparsed};
use lemmy_apub_lib::object_id::ObjectId; use lemmy_apub_lib::object_id::ObjectId;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -11,8 +11,7 @@ use url::Url;
pub struct Delete { pub struct Delete {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
pub(crate) to: Vec<Url>, pub(crate) to: Vec<Url>,
pub(crate) object: Url, pub(crate) object: Tombstone,
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: DeleteType, pub(crate) kind: DeleteType,
/// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user /// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user

View file

@ -11,7 +11,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
#[serial] #[serial]
async fn test_parse_lemmy_voting() { async fn test_parse_lemmy_deletion() {
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/remove_note.json"); test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/remove_note.json");
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json"); test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json");

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
objects::{community::ApubCommunity, person::ApubPerson}, objects::community::ApubCommunity,
protocol::activities::following::follow::FollowCommunity, protocol::activities::following::follow::FollowCommunity,
}; };
use activitystreams::{activity::kind::AcceptType, unparsed::Unparsed}; use activitystreams::{activity::kind::AcceptType, unparsed::Unparsed};
@ -11,7 +11,6 @@ use url::Url;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct AcceptFollowCommunity { pub struct AcceptFollowCommunity {
pub(crate) actor: ObjectId<ApubCommunity>, pub(crate) actor: ObjectId<ApubCommunity>,
pub(crate) to: [ObjectId<ApubPerson>; 1],
pub(crate) object: FollowCommunity, pub(crate) object: FollowCommunity,
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: AcceptType, pub(crate) kind: AcceptType,

View file

@ -8,7 +8,6 @@ use url::Url;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct FollowCommunity { pub struct FollowCommunity {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
pub(crate) to: [ObjectId<ApubCommunity>; 1],
pub(crate) object: ObjectId<ApubCommunity>, pub(crate) object: ObjectId<ApubCommunity>,
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: FollowType, pub(crate) kind: FollowType,

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
objects::{community::ApubCommunity, person::ApubPerson}, objects::person::ApubPerson,
protocol::activities::following::follow::FollowCommunity, protocol::activities::following::follow::FollowCommunity,
}; };
use activitystreams::{activity::kind::UndoType, unparsed::Unparsed}; use activitystreams::{activity::kind::UndoType, unparsed::Unparsed};
@ -11,7 +11,6 @@ use url::Url;
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UndoFollowCommunity { pub struct UndoFollowCommunity {
pub(crate) actor: ObjectId<ApubPerson>, pub(crate) actor: ObjectId<ApubPerson>,
pub(crate) to: [ObjectId<ApubCommunity>; 1],
pub(crate) object: FollowCommunity, pub(crate) object: FollowCommunity,
#[serde(rename = "type")] #[serde(rename = "type")]
pub(crate) kind: UndoType, pub(crate) kind: UndoType,

View file

@ -30,7 +30,9 @@ pub(crate) mod tests {
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned>(path: &str) -> T { pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
path: &str,
) -> T {
let parsed = file_to_json_object::<T>(path); let parsed = file_to_json_object::<T>(path);
// ensure that no field is ignored when parsing // ensure that no field is ignored when parsing

View file

@ -3,7 +3,7 @@ use crate::{
objects::{comment::ApubComment, person::ApubPerson, post::ApubPost}, objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
protocol::Source, protocol::Source,
}; };
use activitystreams::{object::kind::NoteType, unparsed::Unparsed}; use activitystreams::{link::Mention, object::kind::NoteType, unparsed::Unparsed};
use anyhow::anyhow; use anyhow::anyhow;
use chrono::{DateTime, FixedOffset}; use chrono::{DateTime, FixedOffset};
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
@ -38,6 +38,8 @@ pub struct Note {
pub(crate) in_reply_to: ObjectId<PostOrComment>, pub(crate) in_reply_to: ObjectId<PostOrComment>,
pub(crate) published: Option<DateTime<FixedOffset>>, pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>, pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(default)]
pub(crate) tag: Vec<Mention>,
#[serde(flatten)] #[serde(flatten)]
pub(crate) unparsed: Unparsed, pub(crate) unparsed: Unparsed,
} }

View file

@ -1,25 +1,22 @@
use activitystreams::object::kind::TombstoneType; use activitystreams::object::kind::TombstoneType;
use chrono::{DateTime, FixedOffset, NaiveDateTime};
use lemmy_utils::utils::convert_datetime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none; use serde_with::skip_serializing_none;
use url::Url;
#[skip_serializing_none] #[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Tombstone { pub struct Tombstone {
pub(crate) id: Url,
#[serde(rename = "type")] #[serde(rename = "type")]
kind: TombstoneType, kind: TombstoneType,
former_type: String,
deleted: DateTime<FixedOffset>,
} }
impl Tombstone { impl Tombstone {
pub fn new<T: ToString>(former_type: T, updated_time: NaiveDateTime) -> Tombstone { pub fn new(id: Url) -> Tombstone {
Tombstone { Tombstone {
id,
kind: TombstoneType::Tombstone, kind: TombstoneType::Tombstone,
former_type: former_type.to_string(),
deleted: convert_datetime(updated_time),
} }
} }
} }

View file

@ -10,24 +10,26 @@ use background_jobs::{
WorkerConfig, WorkerConfig,
}; };
use lemmy_utils::{location_info, LemmyError}; use lemmy_utils::{location_info, LemmyError};
use log::warn; use log::{info, warn};
use reqwest::Client; use reqwest::Client;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{env, fmt::Debug, future::Future, pin::Pin}; use std::{env, fmt::Debug, future::Future, pin::Pin};
use url::Url; use url::Url;
pub async fn send_activity( pub async fn send_activity(
activity: String, activity_id: &Url,
actor: &dyn ActorType, actor: &dyn ActorType,
inboxes: Vec<&Url>, inboxes: Vec<&Url>,
activity: String,
client: &Client, client: &Client,
activity_queue: &QueueHandle, activity_queue: &QueueHandle,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
for i in inboxes { for i in inboxes {
let message = SendActivityTask { let message = SendActivityTask {
activity: activity.clone(), activity_id: activity_id.clone(),
inbox: i.to_owned(), inbox: i.to_owned(),
actor_id: actor.actor_id(), actor_id: actor.actor_id(),
activity: activity.clone(),
private_key: actor.private_key().context(location_info!())?, private_key: actor.private_key().context(location_info!())?,
}; };
if env::var("APUB_TESTING_SEND_SYNC").is_ok() { if env::var("APUB_TESTING_SEND_SYNC").is_ok() {
@ -42,9 +44,10 @@ pub async fn send_activity(
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
struct SendActivityTask { struct SendActivityTask {
activity: String, activity_id: Url,
inbox: Url, inbox: Url,
actor_id: Url, actor_id: Url,
activity: String,
private_key: String, private_key: String,
} }
@ -64,6 +67,7 @@ impl ActixJob for SendActivityTask {
} }
async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> { async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> {
info!("Sending {} to {}", task.activity_id, task.inbox);
let result = sign_and_send( let result = sign_and_send(
client, client,
&task.inbox, &task.inbox,
@ -73,14 +77,27 @@ async fn do_send(task: SendActivityTask, client: &Client) -> Result<(), Error> {
) )
.await; .await;
if let Err(e) = result { match result {
warn!("{}", e); Ok(o) => {
if !o.status().is_success() {
warn!(
"Send {} to {} failed with status {}: {}",
task.activity_id,
task.inbox,
o.status(),
o.text().await?
);
}
}
Err(e) => {
return Err(anyhow!( return Err(anyhow!(
"Failed to send activity {} to {}", "Failed to send activity {} to {}: {}",
&task.activity, &task.activity_id,
task.inbox task.inbox,
e
)); ));
} }
}
Ok(()) Ok(())
} }

View file

@ -8,6 +8,5 @@ pub mod signatures;
pub mod traits; pub mod traits;
pub mod values; pub mod values;
pub mod verify; pub mod verify;
pub mod webfinger;
pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json"; pub static APUB_JSON_CONTENT_TYPE: &str = "application/activity+json";

View file

@ -44,10 +44,9 @@ pub async fn sign_and_send(
HeaderValue::from_str(APUB_JSON_CONTENT_TYPE)?, HeaderValue::from_str(APUB_JSON_CONTENT_TYPE)?,
); );
headers.insert(HeaderName::from_str("Host")?, HeaderValue::from_str(&host)?); headers.insert(HeaderName::from_str("Host")?, HeaderValue::from_str(&host)?);
headers.insert( // Need to use legacy timezone because mastodon and doesnt understand any new standards
HeaderName::from_str("Date")?, let date = Utc::now().to_rfc2822().replace("+0000", "GMT");
HeaderValue::from_str(&Utc::now().to_rfc2822())?, headers.insert(HeaderName::from_str("Date")?, HeaderValue::from_str(&date)?);
);
let response = client let response = client
.post(&inbox_url.to_string()) .post(&inbox_url.to_string())

View file

@ -1,68 +0,0 @@
use anyhow::anyhow;
use lemmy_utils::{
request::{retry, RecvError},
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,
protocol_string: &str,
) -> Result<Url, LemmyError> {
let webfinger_type = match webfinger_type {
WebfingerType::Person => "acct",
WebfingerType::Group => "group",
};
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource={}:{}@{}",
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

@ -17,7 +17,7 @@ lemmy_db_views = { version = "=0.14.0-rc.1", path = "../db_views" }
lemmy_db_views_actor = { version = "=0.14.0-rc.1", path = "../db_views_actor" } lemmy_db_views_actor = { version = "=0.14.0-rc.1", path = "../db_views_actor" }
lemmy_db_schema = { version = "=0.14.0-rc.1", path = "../db_schema" } lemmy_db_schema = { version = "=0.14.0-rc.1", path = "../db_schema" }
lemmy_api_common = { version = "=0.14.0-rc.1", path = "../api_common" } lemmy_api_common = { version = "=0.14.0-rc.1", path = "../api_common" }
lemmy_apub_lib = { version = "=0.14.0-rc.1", path = "../apub_lib" } lemmy_apub = { version = "=0.14.0-rc.1", path = "../apub" }
diesel = "1.4.8" diesel = "1.4.8"
actix = "0.12.0" actix = "0.12.0"
actix-web = { version = "4.0.0-beta.9", default-features = false, features = ["rustls"] } actix-web = { version = "4.0.0-beta.9", default-features = false, features = ["rustls"] }

View file

@ -1,11 +1,12 @@
use actix_web::{web, web::Query, HttpResponse}; use actix_web::{web, web::Query, HttpResponse};
use anyhow::anyhow; use anyhow::Context;
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_apub_lib::webfinger::{WebfingerLink, WebfingerResponse}; use lemmy_apub::fetcher::webfinger::{WebfingerLink, WebfingerResponse};
use lemmy_db_schema::source::{community::Community, person::Person}; use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_utils::{settings::structs::Settings, ApiError, LemmyError}; use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::Deserialize; use serde::Deserialize;
use url::Url;
#[derive(Deserialize)] #[derive(Deserialize)]
struct Params { struct Params {
@ -31,64 +32,60 @@ async fn get_webfinger_response(
info: Query<Params>, info: Query<Params>,
context: web::Data<LemmyContext>, context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> { ) -> Result<HttpResponse, LemmyError> {
let community_regex_parsed = context let name = context
.settings() .settings()
.webfinger_community_regex() .webfinger_regex()
.captures(&info.resource) .captures(&info.resource)
.map(|c| c.get(1)) .map(|c| c.get(1))
.flatten(); .flatten()
.context(location_info!())?
.as_str()
.to_string();
let username_regex_parsed = context let name_ = name.clone();
.settings() let community_id: Option<Url> = blocking(context.pool(), move |conn| {
.webfinger_username_regex() Community::read_from_name(conn, &name_)
.captures(&info.resource)
.map(|c| c.get(1))
.flatten();
let url = if let Some(community_name) = community_regex_parsed {
let community_name = community_name.as_str().to_owned();
// Make sure the requested community exists.
blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &community_name)
}) })
.await? .await?
.map_err(|e| ApiError::err("not_found", e))? .ok()
.actor_id .map(|c| c.actor_id.into());
} else if let Some(person_name) = username_regex_parsed { let user_id: Option<Url> = blocking(context.pool(), move |conn| {
let person_name = person_name.as_str().to_owned(); Person::find_by_name(conn, &name)
// Make sure the requested person exists.
blocking(context.pool(), move |conn| {
Person::find_by_name(conn, &person_name)
}) })
.await? .await?
.map_err(|e| ApiError::err("not_found", e))? .ok()
.actor_id .map(|c| c.actor_id.into());
} else { let links = vec![
return Err(LemmyError::from(anyhow!("not_found"))); webfinger_link_for_actor(community_id),
}; webfinger_link_for_actor(user_id),
]
.into_iter()
.flatten()
.collect();
let json = WebfingerResponse { let json = WebfingerResponse {
subject: info.resource.to_owned(), subject: info.resource.to_owned(),
aliases: vec![url.to_owned().into()], links,
links: vec![
WebfingerLink {
rel: Some("http://webfinger.net/rel/profile-page".to_string()),
type_: Some("text/html".to_string()),
href: Some(url.to_owned().into()),
template: None,
},
WebfingerLink {
rel: Some("self".to_string()),
type_: Some("application/activity+json".to_string()),
href: Some(url.into()),
template: None,
}, // TODO: this also needs to return the subscribe link once that's implemented
//{
// "rel": "http://ostatus.org/schema/1.0/subscribe",
// "template": "https://my_instance.com/authorize_interaction?uri={uri}"
//}
],
}; };
Ok(HttpResponse::Ok().json(json)) Ok(HttpResponse::Ok().json(json))
} }
fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
if let Some(url) = url {
vec![
WebfingerLink {
rel: Some("http://webfinger.net/rel/profile-page".to_string()),
type_: Some("text/html".to_string()),
href: Some(url.to_owned()),
},
WebfingerLink {
rel: Some("self".to_string()),
type_: Some("application/activity+json".to_string()),
href: Some(url),
},
]
} else {
vec![]
}
}

View file

@ -11,12 +11,7 @@ static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
lazy_static! { lazy_static! {
static ref SETTINGS: RwLock<Settings> = static ref SETTINGS: RwLock<Settings> =
RwLock::new(Settings::init().expect("Failed to load settings file")); RwLock::new(Settings::init().expect("Failed to load settings file"));
static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!( static ref WEBFINGER_REGEX: Regex = Regex::new(&format!(
"^group:([a-z0-9_]{{3,}})@{}$",
Settings::get().hostname
))
.expect("compile webfinger regex");
static ref WEBFINGER_USER_REGEX: Regex = Regex::new(&format!(
"^acct:([a-z0-9_]{{3,}})@{}$", "^acct:([a-z0-9_]{{3,}})@{}$",
Settings::get().hostname Settings::get().hostname
)) ))
@ -105,12 +100,8 @@ impl Settings {
Ok(Self::read_config_file()?) Ok(Self::read_config_file()?)
} }
pub fn webfinger_community_regex(&self) -> Regex { pub fn webfinger_regex(&self) -> Regex {
WEBFINGER_COMMUNITY_REGEX.to_owned() WEBFINGER_REGEX.to_owned()
}
pub fn webfinger_username_regex(&self) -> Regex {
WEBFINGER_USER_REGEX.to_owned()
} }
pub fn slur_regex(&self) -> Option<Regex> { pub fn slur_regex(&self) -> Option<Regex> {