From: Felix Ableitner Date: Tue, 14 Jul 2020 14:09:13 +0000 (+0200) Subject: Convert comments to new apub lib (including comment activities) X-Git-Url: http://these/git/%22%7Burl%7D/%22https:/image.com/%7B%60%24%7BghostArchiveUrl%7D/%7Biframely.thumbnail_url%7D?a=commitdiff_plain;h=2d4c41d2be2c98f23389af1c952c071c643648e1;p=lemmy.git Convert comments to new apub lib (including comment activities) --- diff --git a/server/lemmy_db/src/user_view.rs b/server/lemmy_db/src/user_view.rs index 6de6d6b4..d61fe9c5 100644 --- a/server/lemmy_db/src/user_view.rs +++ b/server/lemmy_db/src/user_view.rs @@ -157,10 +157,28 @@ impl UserView { pub fn admins(conn: &PgConnection) -> Result, Error> { use super::user_view::user_fast::dsl::*; - use diesel::sql_types::{Text, Nullable}; + use diesel::sql_types::{Nullable, Text}; user_fast // The select is necessary here to not get back emails - .select((id, actor_id, name, avatar, "".into_sql::>(), matrix_user_id, bio, local, admin, banned, show_avatars, send_notifications_to_email, published, number_of_posts, post_score, number_of_comments, comment_score)) + .select(( + id, + actor_id, + name, + avatar, + "".into_sql::>(), + matrix_user_id, + bio, + local, + admin, + banned, + show_avatars, + send_notifications_to_email, + published, + number_of_posts, + post_score, + number_of_comments, + comment_score, + )) .filter(admin.eq(true)) .order_by(published) .load::(conn) @@ -168,9 +186,28 @@ impl UserView { pub fn banned(conn: &PgConnection) -> Result, Error> { use super::user_view::user_fast::dsl::*; - use diesel::sql_types::{Text, Nullable}; + use diesel::sql_types::{Nullable, Text}; user_fast - .select((id, actor_id, name, avatar, "".into_sql::>(), matrix_user_id, bio, local, admin, banned, show_avatars, send_notifications_to_email, published, number_of_posts, post_score, number_of_comments, comment_score)) - .filter(banned.eq(true)).load::(conn) + .select(( + id, + actor_id, + name, + avatar, + "".into_sql::>(), + matrix_user_id, + bio, + local, + admin, + banned, + show_avatars, + send_notifications_to_email, + published, + number_of_posts, + post_score, + number_of_comments, + comment_score, + )) + .filter(banned.eq(true)) + .load::(conn) } } diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index 9e5e53a7..975a2a76 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -1,33 +1,27 @@ use crate::{ apub::{ - activities::{populate_object_props, send_activity_to_community}, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, - fetch_webfinger_url, + activities::send_activity_to_community, + create_apub_response, create_apub_tombstone_response, create_tombstone, fetch_webfinger_url, fetcher::{ - get_or_fetch_and_insert_remote_comment, - get_or_fetch_and_insert_remote_post, + get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post, get_or_fetch_and_upsert_remote_user, }, - ActorType, - ApubLikeableType, - ApubObjectType, - FromApub, - ToApub, + ActorType, ApubLikeableType, ApubObjectType, FromApub, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; -use activitystreams::{ +use activitystreams_new::{ activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, + base::AnyBase, context, link::Mention, - object::{kind::NoteType, properties::ObjectProperties, Note}, + object::{kind::NoteType, Note, Tombstone}, + prelude::*, + primitives::XsdAnyUri, + public, }; -use activitystreams_new::object::Tombstone; use actix_web::{body::Body, client::Client, web::Path, HttpResponse}; use itertools::Itertools; use lemmy_db::{ @@ -40,6 +34,8 @@ use lemmy_db::{ use lemmy_utils::{convert_datetime, scrape_text_for_mentions, MentionData}; use log::debug; use serde::Deserialize; +use serde_json::Error; +use std::str::FromStr; #[derive(Deserialize)] pub struct CommentQuery { @@ -66,8 +62,7 @@ impl ToApub for Comment { type Response = Note; async fn to_apub(&self, pool: &DbPool) -> Result { - let mut comment = Note::default(); - let oprops: &mut ObjectProperties = comment.as_mut(); + let mut comment = Note::new(); let creator_id = self.creator_id; let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??; @@ -88,18 +83,18 @@ impl ToApub for Comment { in_reply_to_vec.push(parent_comment.ap_id); } - oprops + comment // Not needed when the Post is embedded in a collection (like for community outbox) - .set_context_xsd_any_uri(context())? - .set_id(self.ap_id.to_owned())? - .set_published(convert_datetime(self.published))? - .set_to_xsd_any_uri(community.actor_id)? - .set_many_in_reply_to_xsd_any_uris(in_reply_to_vec)? - .set_content_xsd_string(self.content.to_owned())? - .set_attributed_to_xsd_any_uri(creator.actor_id)?; + .set_context(context()) + .set_id(self.ap_id.parse::()?) + .set_published(convert_datetime(self.published).into()) + .set_to(community.actor_id) + .set_many_in_reply_tos(in_reply_to_vec) + .set_content(self.content.to_owned()) + .set_attributed_to(creator.actor_id); if let Some(u) = self.updated { - oprops.set_updated(convert_datetime(u))?; + comment.set_updated(convert_datetime(u).into()); } Ok(comment) @@ -125,12 +120,22 @@ impl FromApub for CommentForm { client: &Client, pool: &DbPool, ) -> Result { - let oprops = ¬e.object_props; - let creator_actor_id = &oprops.get_attributed_to_xsd_any_uri().unwrap().to_string(); - - let creator = get_or_fetch_and_upsert_remote_user(&creator_actor_id, client, pool).await?; - - let mut in_reply_tos = oprops.get_many_in_reply_to_xsd_any_uris().unwrap(); + let creator_actor_id = ¬e + .attributed_to() + .unwrap() + .as_single_xsd_any_uri() + .unwrap(); + + let creator = get_or_fetch_and_upsert_remote_user(creator_actor_id, client, pool).await?; + + let mut in_reply_tos = note + .in_reply_to + .as_ref() + .unwrap() + .as_many() + .unwrap() + .iter() + .map(|i| i.as_xsd_any_uri().unwrap()); let post_ap_id = in_reply_tos.next().unwrap().to_string(); // This post, or the parent comment might not yet exist on this server yet, fetch them. @@ -153,20 +158,20 @@ impl FromApub for CommentForm { creator_id: creator.id, post_id: post.id, parent_id, - content: oprops - .get_content_xsd_string() - .map(|c| c.to_string()) - .unwrap(), + content: note + .content() + .unwrap() + .as_single_xsd_string() + .unwrap() + .to_string(), removed: None, read: None, - published: oprops - .get_published() - .map(|u| u.as_ref().to_owned().naive_local()), - updated: oprops - .get_updated() + published: note + .published() .map(|u| u.as_ref().to_owned().naive_local()), + updated: note.updated().map(|u| u.as_ref().to_owned().naive_local()), deleted: None, - ap_id: oprops.get_id().unwrap().to_string(), + ap_id: note.id().unwrap().to_string(), local: false, }) } @@ -193,16 +198,14 @@ impl ApubObjectType for Comment { collect_non_local_mentions_and_addresses(&self.content, &community, client, pool).await?; let id = format!("{}/create/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut create = Create::new(); - populate_object_props(&mut create.object_props, maa.addressed_ccs, &id)?; - - // Set the mention tags - create.object_props.set_many_tag_base_boxes(maa.tags)?; - + let mut create = Create::new(creator.actor_id.to_owned(), note.into_any_base()?); create - .create_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(maa.addressed_ccs.to_owned()) + // Set the mention tags + .set_many_tags(maa.get_tags()?); send_activity_to_community(&creator, &community, maa.inboxes, create, client, pool).await?; Ok(()) @@ -227,16 +230,14 @@ impl ApubObjectType for Comment { collect_non_local_mentions_and_addresses(&self.content, &community, client, pool).await?; let id = format!("{}/update/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut update = Update::new(); - populate_object_props(&mut update.object_props, maa.addressed_ccs, &id)?; - - // Set the mention tags - update.object_props.set_many_tag_base_boxes(maa.tags)?; - + let mut update = Update::new(creator.actor_id.to_owned(), note.into_any_base()?); update - .update_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(maa.addressed_ccs.to_owned()) + // Set the mention tags + .set_many_tags(maa.get_tags()?); send_activity_to_community(&creator, &community, maa.inboxes, update, client, pool).await?; Ok(()) @@ -257,18 +258,12 @@ impl ApubObjectType for Comment { let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut delete = Delete::default(); - - populate_object_props( - &mut delete.object_props, - vec![community.get_followers_url()], - &id, - )?; - + let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); delete - .delete_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &creator, @@ -298,34 +293,22 @@ impl ApubObjectType for Comment { // Generate a fake delete activity, with the correct object let id = format!("{}/delete/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut delete = Delete::default(); - - populate_object_props( - &mut delete.object_props, - vec![community.get_followers_url()], - &id, - )?; - + let mut delete = Delete::new(creator.actor_id.to_owned(), note.into_any_base()?); delete - .delete_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); // TODO // Undo that fake activity let undo_id = format!("{}/undo/delete/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut undo = Undo::default(); - - populate_object_props( - &mut undo.object_props, - vec![community.get_followers_url()], - &undo_id, - )?; - + let mut undo = Undo::new(creator.actor_id.to_owned(), delete.into_any_base()?); undo - .undo_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(delete)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&undo_id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &creator, @@ -354,18 +337,12 @@ impl ApubObjectType for Comment { let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??; let id = format!("{}/remove/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut remove = Remove::default(); - - populate_object_props( - &mut remove.object_props, - vec![community.get_followers_url()], - &id, - )?; - + let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?); remove - .remove_props - .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &mod_, @@ -395,33 +372,21 @@ impl ApubObjectType for Comment { // Generate a fake delete activity, with the correct object let id = format!("{}/remove/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut remove = Remove::default(); - - populate_object_props( - &mut remove.object_props, - vec![community.get_followers_url()], - &id, - )?; - + let mut remove = Remove::new(mod_.actor_id.to_owned(), note.into_any_base()?); remove - .remove_props - .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); // Undo that fake activity let undo_id = format!("{}/undo/remove/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut undo = Undo::default(); - - populate_object_props( - &mut undo.object_props, - vec![community.get_followers_url()], - &undo_id, - )?; - + let mut undo = Undo::new(mod_.actor_id.to_owned(), remove.into_any_base()?); undo - .undo_props - .set_actor_xsd_any_uri(mod_.actor_id.to_owned())? - .set_object_base_box(remove)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&undo_id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &mod_, @@ -454,16 +419,12 @@ impl ApubLikeableType for Comment { let id = format!("{}/like/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut like = Like::new(); - populate_object_props( - &mut like.object_props, - vec![community.get_followers_url()], - &id, - )?; + let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); like - .like_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &creator, @@ -493,16 +454,12 @@ impl ApubLikeableType for Comment { let id = format!("{}/dislike/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut dislike = Dislike::new(); - populate_object_props( - &mut dislike.object_props, - vec![community.get_followers_url()], - &id, - )?; + let mut dislike = Dislike::new(creator.actor_id.to_owned(), note.into_any_base()?); dislike - .dislike_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &creator, @@ -532,32 +489,22 @@ impl ApubLikeableType for Comment { let id = format!("{}/dislike/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut like = Like::new(); - populate_object_props( - &mut like.object_props, - vec![community.get_followers_url()], - &id, - )?; + let mut like = Like::new(creator.actor_id.to_owned(), note.into_any_base()?); like - .like_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(note)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); // TODO // Undo that fake activity let undo_id = format!("{}/undo/like/{}", self.ap_id, uuid::Uuid::new_v4()); - let mut undo = Undo::default(); - - populate_object_props( - &mut undo.object_props, - vec![community.get_followers_url()], - &undo_id, - )?; - + let mut undo = Undo::new(creator.actor_id.to_owned(), like.into_any_base()?); undo - .undo_props - .set_actor_xsd_any_uri(creator.actor_id.to_owned())? - .set_object_base_box(like)?; + .set_context(context()) + .set_id(XsdAnyUri::from_str(&undo_id)?) + .set_to(public()) + .set_many_ccs(vec![community.get_followers_url()]); send_activity_to_community( &creator, @@ -578,6 +525,16 @@ struct MentionsAndAddresses { tags: Vec, } +impl MentionsAndAddresses { + fn get_tags(&self) -> Result, Error> { + self + .tags + .iter() + .map(|t| t.to_owned().into_any_base()) + .collect::, Error>>() + } +} + /// 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 users / addresses that go in the cc field. @@ -604,17 +561,14 @@ async fn collect_non_local_mentions_and_addresses( // TODO should it be fetching it every time? if let Ok(actor_id) = fetch_webfinger_url(mention, client).await { debug!("mention actor_id: {}", actor_id); - addressed_ccs.push(actor_id.to_owned()); + addressed_ccs.push(actor_id.to_owned().to_string()); let mention_user = get_or_fetch_and_upsert_remote_user(&actor_id, client, pool).await?; let shared_inbox = mention_user.get_shared_inbox_url(); mention_inboxes.push(shared_inbox); let mut mention_tag = Mention::new(); - mention_tag - .link_props - .set_href(actor_id)? - .set_name_xsd_string(mention.full_name())?; + mention_tag.set_href(actor_id).set_name(mention.full_name()); tags.push(mention_tag); } } diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 529039fc..d6869b2e 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -1,28 +1,18 @@ use crate::{ apub::{ activities::{populate_object_props, send_activity}, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, + create_apub_response, create_apub_tombstone_response, create_tombstone, extensions::group_extensions::GroupExtension, fetcher::get_or_fetch_and_upsert_remote_user, - get_shared_inbox, - insert_activity, - ActorType, - FromApub, - GroupExt, - ToApub, + get_shared_inbox, insert_activity, ActorType, FromApub, GroupExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams::{ activity::{Accept, Announce, Delete, Remove, Undo}, - Activity, - Base, - BaseBox, + Activity, Base, BaseBox, }; use activitystreams_ext::Ext2; use activitystreams_new::{ @@ -378,7 +368,7 @@ impl FromApub for CommunityForm { .as_xsd_any_uri() .unwrap(); - let creator = get_or_fetch_and_upsert_remote_user(creator_uri.as_str(), client, pool).await?; + let creator = get_or_fetch_and_upsert_remote_user(creator_uri, client, pool).await?; Ok(CommunityForm { name: group.name().unwrap().as_single_xsd_string().unwrap().into(), diff --git a/server/src/apub/community_inbox.rs b/server/src/apub/community_inbox.rs index dfb30f30..e9788c66 100644 --- a/server/src/apub/community_inbox.rs +++ b/server/src/apub/community_inbox.rs @@ -71,7 +71,7 @@ pub async fn community_inbox( &community.name, &input ); let follow = input.follow()?; - let user_uri = follow.actor.as_single_xsd_any_uri().unwrap().to_string(); + let user_uri = follow.actor.as_single_xsd_any_uri().unwrap(); let community_uri = follow.object.as_single_xsd_any_uri().unwrap().to_string(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, &client, &db).await?; diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index f20c9eab..5d9a8604 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -4,11 +4,9 @@ use crate::{ blocking, request::{retry, RecvError}, routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}, - DbPool, - LemmyError, + DbPool, LemmyError, }; -use activitystreams::object::Note; -use activitystreams_new::{base::BaseExt, prelude::*, primitives::XsdAnyUri}; +use activitystreams_new::{base::BaseExt, object::Note, prelude::*, primitives::XsdAnyUri}; use actix_web::client::Client; use chrono::NaiveDateTime; use diesel::{result::Error::NotFound, PgConnection}; @@ -22,9 +20,7 @@ use lemmy_db::{ post_view::PostView, user::{UserForm, User_}, user_view::UserView, - Crud, - Joinable, - SearchType, + Crud, Joinable, SearchType, }; use lemmy_utils::get_apub_protocol_string; use log::debug; @@ -139,7 +135,7 @@ pub async fn search_by_apub_id( let response = match fetch_remote_object::(client, &query_url).await? { SearchAcceptedObjects::Person(p) => { - let user_uri = p.inner.id().unwrap().to_string(); + let user_uri = p.inner.id().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -173,16 +169,11 @@ pub async fn search_by_apub_id( response } SearchAcceptedObjects::Comment(c) => { - let post_url = c - .object_props - .get_many_in_reply_to_xsd_any_uris() - .unwrap() - .next() - .unwrap() - .to_string(); + let post_url = c.in_reply_to.as_ref().unwrap().as_many().unwrap(); // TODO: also fetch parent comments if any - let post = fetch_remote_object(client, &Url::parse(&post_url)?).await?; + let x = post_url.first().unwrap().as_xsd_any_uri().unwrap(); + let post = fetch_remote_object(client, x.as_url()).await?; let post_form = PostForm::from_apub(&post, client, pool).await?; let comment_form = CommentForm::from_apub(&c, client, pool).await?; @@ -200,13 +191,13 @@ pub async fn search_by_apub_id( /// Check if a remote user exists, create if not found, if its too old update it.Fetch a user, insert/update it in the database and return the user. pub async fn get_or_fetch_and_upsert_remote_user( - apub_id: &str, + apub_id: &XsdAnyUri, client: &Client, pool: &DbPool, ) -> Result { let apub_id_owned = apub_id.to_owned(); let user = blocking(pool, move |conn| { - User_::read_from_actor_id(conn, &apub_id_owned) + User_::read_from_actor_id(conn, apub_id_owned.as_str()) }) .await?; @@ -214,7 +205,7 @@ pub async fn get_or_fetch_and_upsert_remote_user( // If its older than a day, re-fetch it Ok(u) if !u.local && should_refetch_actor(u.last_refreshed_at) => { debug!("Fetching and updating from remote user: {}", apub_id); - let person = fetch_remote_object::(client, &Url::parse(apub_id)?).await?; + let person = fetch_remote_object::(client, apub_id.as_url()).await?; let mut uf = UserForm::from_apub(&person, client, pool).await?; uf.last_refreshed_at = Some(naive_now()); @@ -225,7 +216,7 @@ pub async fn get_or_fetch_and_upsert_remote_user( Ok(u) => Ok(u), Err(NotFound {}) => { debug!("Fetching and creating remote user: {}", apub_id); - let person = fetch_remote_object::(client, &Url::parse(apub_id)?).await?; + let person = fetch_remote_object::(client, apub_id.as_url()).await?; let uf = UserForm::from_apub(&person, client, pool).await?; let user = blocking(pool, move |conn| User_::create(conn, &uf)).await??; @@ -293,7 +284,7 @@ pub async fn get_or_fetch_and_upsert_remote_community( let mut creator_and_moderators = Vec::new(); for uri in creator_and_moderator_uris { - let c_or_m = get_or_fetch_and_upsert_remote_user(uri.as_str(), client, pool).await?; + let c_or_m = get_or_fetch_and_upsert_remote_user(uri, client, pool).await?; creator_and_moderators.push(c_or_m); } diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index a912504b..a4e452e4 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -19,8 +19,7 @@ use crate::{ blocking, request::{retry, RecvError}, routes::webfinger::WebFingerResponse, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_ext::{Ext1, Ext2}; use activitystreams_new::{ @@ -28,6 +27,7 @@ use activitystreams_new::{ actor::{ApActor, Group, Person}, object::{Page, Tombstone}, prelude::*, + primitives::XsdAnyUri, }; use actix_web::{body::Body, client::Client, HttpResponse}; use chrono::NaiveDateTime; @@ -36,6 +36,7 @@ use lemmy_db::{activity::do_insert_activity, user::User_}; use lemmy_utils::{convert_datetime, get_apub_protocol_string, settings::Settings, MentionData}; use log::debug; use serde::Serialize; +use std::str::FromStr; use url::Url; type GroupExt = Ext2, GroupExtension, PublicKeyExtension>; @@ -310,7 +311,7 @@ pub trait ActorType { pub async fn fetch_webfinger_url( mention: &MentionData, client: &Client, -) -> Result { +) -> Result { let fetch_url = format!( "{}://{}/.well-known/webfinger?resource=acct:{}@{}", get_apub_protocol_string(), @@ -335,6 +336,8 @@ pub async fn fetch_webfinger_url( link .href .to_owned() + .map(|u| XsdAnyUri::from_str(&u)) + .transpose()? .ok_or_else(|| format_err!("No href found.").into()) } diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 3455fbfa..ce13ff6e 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -1,22 +1,14 @@ use crate::{ apub::{ activities::{populate_object_props, send_activity_to_community}, - create_apub_response, - create_apub_tombstone_response, - create_tombstone, + create_apub_response, create_apub_tombstone_response, create_tombstone, extensions::page_extension::PageExtension, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - ActorType, - ApubLikeableType, - ApubObjectType, - FromApub, - PageExt, - ToApub, + ActorType, ApubLikeableType, ApubObjectType, FromApub, PageExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams::{ activity::{Create, Delete, Dislike, Like, Remove, Undo, Update}, @@ -166,8 +158,7 @@ impl FromApub for PostForm { .as_ref() .unwrap() .as_single_xsd_any_uri() - .unwrap() - .as_str(); + .unwrap(); let creator = get_or_fetch_and_upsert_remote_user(creator_actor_id, client, pool).await?; diff --git a/server/src/apub/private_message.rs b/server/src/apub/private_message.rs index bc685b23..b40ac009 100644 --- a/server/src/apub/private_message.rs +++ b/server/src/apub/private_message.rs @@ -1,16 +1,9 @@ use crate::{ apub::{ - activities::send_activity, - create_tombstone, - fetcher::get_or_fetch_and_upsert_remote_user, - insert_activity, - ApubObjectType, - FromApub, - ToApub, + activities::send_activity, create_tombstone, fetcher::get_or_fetch_and_upsert_remote_user, + insert_activity, ApubObjectType, FromApub, ToApub, }, - blocking, - DbPool, - LemmyError, + blocking, DbPool, LemmyError, }; use activitystreams::{ activity::{Create, Delete, Undo, Update}, @@ -76,11 +69,11 @@ impl FromApub for PrivateMessageForm { pool: &DbPool, ) -> Result { let oprops = ¬e.object_props; - let creator_actor_id = &oprops.get_attributed_to_xsd_any_uri().unwrap().to_string(); + let creator_actor_id = &oprops.get_attributed_to_xsd_any_uri().unwrap(); let creator = get_or_fetch_and_upsert_remote_user(&creator_actor_id, client, pool).await?; - let recipient_actor_id = &oprops.get_to_xsd_any_uri().unwrap().to_string(); + let recipient_actor_id = &oprops.get_to_xsd_any_uri().unwrap(); let recipient = get_or_fetch_and_upsert_remote_user(&recipient_actor_id, client, pool).await?; diff --git a/server/src/apub/shared_inbox.rs b/server/src/apub/shared_inbox.rs index 7319f1ae..d8c0ac17 100644 --- a/server/src/apub/shared_inbox.rs +++ b/server/src/apub/shared_inbox.rs @@ -8,15 +8,10 @@ use crate::{ community::do_announce, extensions::signatures::verify, fetcher::{ - get_or_fetch_and_insert_remote_comment, - get_or_fetch_and_insert_remote_post, - get_or_fetch_and_upsert_remote_community, - get_or_fetch_and_upsert_remote_user, + get_or_fetch_and_insert_remote_comment, get_or_fetch_and_insert_remote_post, + get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user, }, - insert_activity, - FromApub, - GroupExt, - PageExt, + insert_activity, FromApub, GroupExt, PageExt, }, blocking, routes::{ChatServerParam, DbPoolParam}, @@ -24,16 +19,13 @@ use crate::{ server::{SendComment, SendCommunityRoomMessage, SendPost}, UserOperation, }, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams::{ activity::{Announce, Create, Delete, Dislike, Like, Remove, Undo, Update}, - object::Note, - Activity, - Base, - BaseBox, + Activity, Base, BaseBox, }; +use activitystreams_new::{object::Note, primitives::XsdAnyUri}; use actix_web::{client::Client, web, HttpRequest, HttpResponse}; use lemmy_db::{ comment::{Comment, CommentForm, CommentLike, CommentLikeForm}, @@ -43,8 +35,7 @@ use lemmy_db::{ naive_now, post::{Post, PostForm, PostLike, PostLikeForm}, post_view::PostView, - Crud, - Likeable, + Crud, Likeable, }; use lemmy_utils::scrape_text_for_mentions; use log::debug; @@ -77,7 +68,7 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Announce(a) => a.announce_props.get_object_base_box(), } } - fn sender(&self) -> String { + fn sender(&self) -> XsdAnyUri { let uri = match self { SharedAcceptedObjects::Create(c) => c.create_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Update(u) => u.update_props.get_actor_xsd_any_uri(), @@ -88,7 +79,7 @@ impl SharedAcceptedObjects { SharedAcceptedObjects::Remove(r) => r.remove_props.get_actor_xsd_any_uri(), SharedAcceptedObjects::Announce(a) => a.announce_props.get_actor_xsd_any_uri(), }; - uri.unwrap().clone().to_string() + uri.unwrap().clone() } fn cc(&self) -> String { // TODO: there is probably an easier way to do this @@ -133,7 +124,7 @@ pub async fn shared_inbox( let to = cc.replace("/followers", ""); // TODO: this is ugly - match get_or_fetch_and_upsert_remote_user(&sender.to_string(), &client, pool).await { + match get_or_fetch_and_upsert_remote_user(sender, &client, pool).await { Ok(u) => verify(&request, &u)?, Err(_) => { let c = get_or_fetch_and_upsert_remote_community(&sender.to_string(), &client, pool).await?; @@ -219,7 +210,7 @@ pub async fn shared_inbox( async fn announce_activity_if_valid( activity: A, community_uri: &str, - sender: &str, + sender: &XsdAnyUri, client: &Client, pool: &DbPool, ) -> Result @@ -344,11 +335,7 @@ async fn receive_create_post( .to_owned() .into_concrete::()?; - let user_uri = create - .create_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = create.create_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -390,11 +377,7 @@ async fn receive_create_comment( .to_owned() .into_concrete::()?; - let user_uri = create - .create_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = create.create_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -449,11 +432,7 @@ async fn receive_update_post( .to_owned() .into_concrete::()?; - let user_uri = update - .update_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = update.update_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -495,7 +474,7 @@ async fn receive_like_post( .to_owned() .into_concrete::()?; - let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); + let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -546,11 +525,7 @@ async fn receive_dislike_post( .to_owned() .into_concrete::()?; - let user_uri = dislike - .dislike_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = dislike.dislike_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -601,11 +576,7 @@ async fn receive_update_comment( .to_owned() .into_concrete::()?; - let user_uri = update - .update_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = update.update_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -660,7 +631,7 @@ async fn receive_like_comment( .to_owned() .into_concrete::()?; - let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); + let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -718,11 +689,7 @@ async fn receive_dislike_comment( .to_owned() .into_concrete::()?; - let user_uri = dislike - .dislike_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = dislike.dislike_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -772,11 +739,7 @@ async fn receive_delete_community( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let group = delete .delete_props @@ -849,11 +812,7 @@ async fn receive_remove_community( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let mod_uri = remove - .remove_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let mod_uri = remove.remove_props.get_actor_xsd_any_uri().unwrap(); let group = remove .remove_props @@ -926,11 +885,7 @@ async fn receive_delete_post( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let page = delete .delete_props @@ -992,11 +947,7 @@ async fn receive_remove_post( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let mod_uri = remove - .remove_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let mod_uri = remove.remove_props.get_actor_xsd_any_uri().unwrap(); let page = remove .remove_props @@ -1058,11 +1009,7 @@ async fn receive_delete_comment( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let note = delete .delete_props @@ -1126,11 +1073,7 @@ async fn receive_remove_comment( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let mod_uri = remove - .remove_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let mod_uri = remove.remove_props.get_actor_xsd_any_uri().unwrap(); let note = remove .remove_props @@ -1254,11 +1197,7 @@ async fn receive_undo_delete_comment( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let note = delete .delete_props @@ -1322,11 +1261,7 @@ async fn receive_undo_remove_comment( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let mod_uri = remove - .remove_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let mod_uri = remove.remove_props.get_actor_xsd_any_uri().unwrap(); let note = remove .remove_props @@ -1390,11 +1325,7 @@ async fn receive_undo_delete_post( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let page = delete .delete_props @@ -1456,11 +1387,7 @@ async fn receive_undo_remove_post( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let mod_uri = remove - .remove_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let mod_uri = remove.remove_props.get_actor_xsd_any_uri().unwrap(); let page = remove .remove_props @@ -1522,11 +1449,7 @@ async fn receive_undo_delete_community( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let group = delete .delete_props @@ -1599,11 +1522,7 @@ async fn receive_undo_remove_community( pool: &DbPool, chat_server: ChatServerParam, ) -> Result { - let mod_uri = remove - .remove_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let mod_uri = remove.remove_props.get_actor_xsd_any_uri().unwrap(); let group = remove .remove_props @@ -1713,7 +1632,7 @@ async fn receive_undo_like_comment( .to_owned() .into_concrete::()?; - let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); + let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; @@ -1767,7 +1686,7 @@ async fn receive_undo_like_post( .to_owned() .into_concrete::()?; - let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap().to_string(); + let user_uri = like.like_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 2b02486d..997c03ea 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -1,18 +1,12 @@ use crate::{ api::claims::Claims, apub::{ - activities::send_activity, - create_apub_response, - insert_activity, - ActorType, - FromApub, - PersonExt, - ToApub, + activities::send_activity, create_apub_response, insert_activity, ActorType, FromApub, + PersonExt, ToApub, }, blocking, routes::DbPoolParam, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams_ext::Ext1; use activitystreams_new::{ diff --git a/server/src/apub/user_inbox.rs b/server/src/apub/user_inbox.rs index 9bc102a7..0ce2f942 100644 --- a/server/src/apub/user_inbox.rs +++ b/server/src/apub/user_inbox.rs @@ -3,14 +3,12 @@ use crate::{ apub::{ extensions::signatures::verify, fetcher::{get_or_fetch_and_upsert_remote_community, get_or_fetch_and_upsert_remote_user}, - insert_activity, - FromApub, + insert_activity, FromApub, }, blocking, routes::{ChatServerParam, DbPoolParam}, websocket::{server::SendUserRoomMessage, UserOperation}, - DbPool, - LemmyError, + DbPool, LemmyError, }; use activitystreams::{ activity::{Accept, Create, Delete, Undo, Update}, @@ -23,8 +21,7 @@ use lemmy_db::{ private_message::{PrivateMessage, PrivateMessageForm}, private_message_view::PrivateMessageView, user::User_, - Crud, - Followable, + Crud, Followable, }; use log::debug; use serde::Deserialize; @@ -124,11 +121,7 @@ async fn receive_create_private_message( .to_owned() .into_concrete::()?; - let user_uri = create - .create_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = create.create_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; verify(request, &user)?; @@ -176,11 +169,7 @@ async fn receive_update_private_message( .to_owned() .into_concrete::()?; - let user_uri = update - .update_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = update.update_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; verify(request, &user)?; @@ -236,11 +225,7 @@ async fn receive_delete_private_message( .to_owned() .into_concrete::()?; - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; verify(request, &user)?; @@ -316,11 +301,7 @@ async fn receive_undo_delete_private_message( .to_owned() .into_concrete::()?; - let user_uri = delete - .delete_props - .get_actor_xsd_any_uri() - .unwrap() - .to_string(); + let user_uri = delete.delete_props.get_actor_xsd_any_uri().unwrap(); let user = get_or_fetch_and_upsert_remote_user(&user_uri, client, pool).await?; verify(request, &user)?;