From 6b0bc1029fdedc8318d13cbd78526a8991668113 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 10 Aug 2020 17:21:02 +0200 Subject: [PATCH 1/2] Remove most usage of Option::unwrap() from activitypub code --- server/lemmy_utils/src/lib.rs | 12 ++++ server/src/apub/comment.rs | 27 +++++--- server/src/apub/community.rs | 49 +++++++------ server/src/apub/extensions/signatures.rs | 8 +-- server/src/apub/fetcher.rs | 39 +++++++---- server/src/apub/inbox/activities/announce.rs | 8 ++- server/src/apub/inbox/activities/create.rs | 13 ++-- server/src/apub/inbox/activities/delete.rs | 13 ++-- server/src/apub/inbox/activities/dislike.rs | 22 +++++- server/src/apub/inbox/activities/like.rs | 10 ++- server/src/apub/inbox/activities/remove.rs | 14 ++-- server/src/apub/inbox/activities/undo.rs | 69 +++++++++++++------ server/src/apub/inbox/activities/update.rs | 13 ++-- server/src/apub/inbox/community_inbox.rs | 14 ++-- server/src/apub/inbox/shared_inbox.rs | 34 ++++++--- server/src/apub/inbox/user_inbox.rs | 72 +++++++++++++++----- server/src/apub/mod.rs | 23 +++++-- server/src/apub/post.rs | 33 +++++---- server/src/apub/private_message.rs | 20 ++++-- server/src/apub/user.rs | 24 +++---- 20 files changed, 352 insertions(+), 165 deletions(-) diff --git a/server/lemmy_utils/src/lib.rs b/server/lemmy_utils/src/lib.rs index 6d851b03..fc50e199 100644 --- a/server/lemmy_utils/src/lib.rs +++ b/server/lemmy_utils/src/lib.rs @@ -31,6 +31,18 @@ use regex::{Regex, RegexBuilder}; use std::io::{Error, ErrorKind}; use url::Url; +#[macro_export] +macro_rules! location_info { + () => { + format!( + "None value at {}:{}, column {}", + file!(), + line!(), + column!() + ) + }; +} + pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime { DateTime::::from_utc(ndt, Utc) } diff --git a/server/src/apub/comment.rs b/server/src/apub/comment.rs index fbec5905..a9b4671a 100644 --- a/server/src/apub/comment.rs +++ b/server/src/apub/comment.rs @@ -41,6 +41,7 @@ use activitystreams::{ public, }; use actix_web::{body::Body, client::Client, web::Path, HttpResponse}; +use anyhow::Context; use itertools::Itertools; use lemmy_db::{ comment::{Comment, CommentForm}, @@ -49,7 +50,13 @@ use lemmy_db::{ user::User_, Crud, }; -use lemmy_utils::{convert_datetime, remove_slurs, scrape_text_for_mentions, MentionData}; +use lemmy_utils::{ + convert_datetime, + location_info, + remove_slurs, + scrape_text_for_mentions, + MentionData, +}; use log::debug; use serde::Deserialize; use serde_json::Error; @@ -136,21 +143,21 @@ impl FromApub for CommentForm { ) -> Result { let creator_actor_id = ¬e .attributed_to() - .unwrap() + .context(location_info!())? .as_single_xsd_any_uri() - .unwrap(); + .context(location_info!())?; let creator = get_or_fetch_and_upsert_user(creator_actor_id, client, pool).await?; let mut in_reply_tos = note .in_reply_to() .as_ref() - .unwrap() + .context(location_info!())? .as_many() - .unwrap() + .context(location_info!())? .iter() - .map(|i| i.as_xsd_any_uri().unwrap()); - let post_ap_id = in_reply_tos.next().unwrap(); + .map(|i| i.as_xsd_any_uri().context("")); + let post_ap_id = in_reply_tos.next().context(location_info!())??; // This post, or the parent comment might not yet exist on this server yet, fetch them. let post = get_or_fetch_and_insert_post(&post_ap_id, client, pool).await?; @@ -159,7 +166,7 @@ impl FromApub for CommentForm { // For deeply nested comments, FromApub automatically gets called recursively let parent_id: Option = match in_reply_tos.next() { Some(parent_comment_uri) => { - let parent_comment_ap_id = &parent_comment_uri; + let parent_comment_ap_id = &parent_comment_uri?; let parent_comment = get_or_fetch_and_insert_comment(&parent_comment_ap_id, client, pool).await?; @@ -169,9 +176,9 @@ impl FromApub for CommentForm { }; let content = note .content() - .unwrap() + .context(location_info!())? .as_single_xsd_string() - .unwrap() + .context(location_info!())? .to_string(); let content_slurs_removed = remove_slurs(&content); diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 8b522b44..66a11540 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -40,6 +40,7 @@ use activitystreams::{ }; use activitystreams_ext::Ext2; use actix_web::{body::Body, client::Client, web, HttpResponse}; +use anyhow::Context; use itertools::Itertools; use lemmy_db::{ community::{Community, CommunityForm}, @@ -48,7 +49,7 @@ use lemmy_db::{ post::Post, user::User_, }; -use lemmy_utils::convert_datetime; +use lemmy_utils::{convert_datetime, location_info}; use serde::Deserialize; use url::Url; @@ -142,7 +143,11 @@ impl ActorType for Community { client: &Client, pool: &DbPool, ) -> Result<(), LemmyError> { - let actor_uri = follow.actor()?.as_single_xsd_any_uri().unwrap().to_string(); + let actor_uri = follow + .actor()? + .as_single_xsd_any_uri() + .context(location_info!())? + .to_string(); let mut accept = Accept::new(self.actor_id.to_owned(), follow.into_any_base()?); let to = format!("{}/inbox", actor_uri); @@ -330,44 +335,50 @@ impl FromApub for CommunityForm { pool: &DbPool, expected_domain: Option, ) -> Result { - let creator_and_moderator_uris = group.inner.attributed_to().unwrap(); + let creator_and_moderator_uris = group.inner.attributed_to().context(location_info!())?; let creator_uri = creator_and_moderator_uris .as_many() - .unwrap() + .context(location_info!())? .iter() .next() - .unwrap() + .context(location_info!())? .as_xsd_any_uri() - .unwrap(); + .context(location_info!())?; let creator = get_or_fetch_and_upsert_user(creator_uri, client, pool).await?; let name = group .inner .name() - .unwrap() + .context(location_info!())? .as_one() - .unwrap() + .context(location_info!())? .as_xsd_string() - .unwrap() + .context(location_info!())? + .to_string(); + let title = group + .inner + .preferred_username() + .context(location_info!())? .to_string(); - let title = group.inner.preferred_username().unwrap().to_string(); // TODO: should be parsed as html and tags like