Skip to content

Commit

Permalink
refactor: reduce use of unwrap (#54)
Browse files Browse the repository at this point in the history
* refactor: reduce use of unwrap

* refactor: reduce use of unwrap

* chore: lint code

* refactor: reduce use of unwrap
  • Loading branch information
kwaa authored Jul 16, 2024
1 parent 387b653 commit eaa8730
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 57 deletions.
3 changes: 2 additions & 1 deletion crates/api_apub/src/users/user_followers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ pub async fn handler(
.fetch_page(page - 1)
.await?
.into_iter()
.map(|follow| Url::parse(&follow.id).unwrap())
.map(|follow| Url::parse(&follow.id))
.filter_map(Result::ok)
.collect(),
total.number_of_pages,
page,
Expand Down
3 changes: 2 additions & 1 deletion crates/api_apub/src/users/user_outbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ pub async fn handler(
.into_iter()
.map(|activity| {
let activity: ApubActivity = activity.into();
activity.into_json().unwrap()
activity.into_json()
})
.filter_map(Result::ok)
.collect(),
total.number_of_pages,
page,
Expand Down
32 changes: 14 additions & 18 deletions crates/api_mastodon/src/entities/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use activitypub_federation::{config::Data, traits::Object};
use futures::future::TryJoinAll;
use hatsu_apub::objects::{ApubPost, Note};
use hatsu_db_schema::{post, prelude::Post};
use hatsu_utils::{AppData, AppError};
Expand All @@ -24,26 +25,21 @@ impl Context {
.await?
{
Some(post) => {
// https://www.sea-ql.org/SeaORM/docs/relation/chained-relations/
let handles = post
.find_linked(post::SelfReferencingLink)
.all(&data.conn)
.await?
.into_iter()
.map(|post| async move {
let apub_post: ApubPost = post.clone().into();
// TODO: remove unwrap
let note: Note = apub_post.into_json(data).await.unwrap();

Status::from_json(note, data).await.unwrap()
})
.collect::<Vec<_>>();

let descendants: Vec<Status> = futures::future::join_all(handles).await;

Ok(Self {
ancestors: vec![],
descendants,
// https://www.sea-ql.org/SeaORM/docs/relation/chained-relations/
descendants: post
.find_linked(post::SelfReferencingLink)
.all(&data.conn)
.await?
.into_iter()
.map(|post| async move {
let apub_post: ApubPost = post.clone().into();
let note: Note = apub_post.into_json(data).await?;
Status::from_json(note, data).await
})
.collect::<TryJoinAll<_>>()
.await?,
})
},
None => Err(AppError::not_found("Record", post_id.as_ref())),
Expand Down
18 changes: 8 additions & 10 deletions crates/api_mastodon/src/routes/statuses/status_favourited_by.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use activitypub_federation::config::Data;
use axum::{debug_handler, extract::Path, Json};
use futures::future::TryJoinAll;
use hatsu_db_schema::prelude::{Post, ReceivedLike};
use hatsu_utils::{AppData, AppError};
use sea_orm::{EntityTrait, ModelTrait};
Expand Down Expand Up @@ -37,20 +38,17 @@ pub async fn status_favourited_by(
.one(&data.conn)
.await?
{
Some(post) => {
let handles = post
.find_related(ReceivedLike)
Some(post) => Ok(Json(
post.find_related(ReceivedLike)
.all(&data.conn)
.await
.unwrap()
.await?
.into_iter()
.map(|received_like| async {
Account::from_id(received_like.actor, &data).await.unwrap()
Account::from_id(received_like.actor, &data).await
})
.collect::<Vec<_>>();

Ok(Json(futures::future::join_all(handles).await))
},
.collect::<TryJoinAll<_>>()
.await?,
)),
_ => Err(AppError::not_found("Record", &base64_url)),
}
},
Expand Down
18 changes: 8 additions & 10 deletions crates/api_mastodon/src/routes/statuses/status_reblogged_by.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use activitypub_federation::config::Data;
use axum::{debug_handler, extract::Path, Json};
use futures::future::TryJoinAll;
use hatsu_db_schema::prelude::{Post, ReceivedAnnounce};
use hatsu_utils::{AppData, AppError};
use sea_orm::{EntityTrait, ModelTrait};
Expand Down Expand Up @@ -37,20 +38,17 @@ pub async fn status_reblogged_by(
.one(&data.conn)
.await?
{
Some(post) => {
let handles = post
.find_related(ReceivedAnnounce)
Some(post) => Ok(Json(
post.find_related(ReceivedAnnounce)
.all(&data.conn)
.await
.unwrap()
.await?
.into_iter()
.map(|received_like| async {
Account::from_id(received_like.actor, &data).await.unwrap()
Account::from_id(received_like.actor, &data).await
})
.collect::<Vec<_>>();

Ok(Json(futures::future::join_all(handles).await))
},
.collect::<TryJoinAll<_>>()
.await?,
)),
_ => Err(AppError::not_found("Record", &base64_url)),
}
},
Expand Down
8 changes: 4 additions & 4 deletions crates/apub/src/actors/db_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ impl Object for ApubUser {
icon: self
.icon
.clone()
.map(|icon| UserImage::new(Url::parse(&icon).unwrap())),
.and_then(|icon| Url::parse(&icon).map_or(None, |url| Some(UserImage::new(url)))),
image: self.hatsu.clone().and_then(|hatsu| {
hatsu
.banner_image
.map(|image| UserImage::new(Url::parse(&image).unwrap()))
hatsu.banner_image.and_then(|image| {
Url::parse(&image).map_or(None, |url| Some(UserImage::new(url)))
})
}),
attachment: self
.feed
Expand Down
21 changes: 10 additions & 11 deletions crates/apub/src/actors/db_user_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use activitypub_federation::{
http_signatures::generate_actor_keypair,
traits::{ActivityHandler, Actor},
};
use futures::future::TryJoinAll;
use hatsu_db_schema::{prelude::ReceivedFollow, user::Model as DbUser};
use hatsu_feed::{UserFeed, UserFeedHatsu, UserFeedTopLevel};
use hatsu_utils::{AppData, AppError};
Expand Down Expand Up @@ -92,24 +93,22 @@ impl ApubUser {
let inboxes = if let Some(inboxes) = inboxes {
inboxes
} else {
// 获取 followers inbox
let handles = self
.find_related(ReceivedFollow)
// get followers inbox
self.find_related(ReceivedFollow)
.all(&data.conn)
.await?
.into_iter()
.map(|received_follow| async move {
let follower: ObjectId<Self> =
Url::parse(&received_follow.actor).unwrap().into();
let follower: Self = follower.dereference_local(data).await.unwrap();
follower.shared_inbox_or_inbox()
})
.collect::<Vec<_>>();
let follower: ObjectId<Self> = Url::parse(&received_follow.actor)?.into();
let follower: Self = follower.dereference_local(data).await?;

futures::future::join_all(handles).await
Ok::<Url, AppError>(follower.shared_inbox_or_inbox())
})
.collect::<TryJoinAll<_>>()
.await?
};

// 发送
// send
queue_activity(&activity, self, inboxes, data).await?;

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/feed/src/user_feed_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl WrappedUserFeedItem {
tags: self
.tags
.clone()
.map(|tags| serde_json::from_str(&tags).unwrap()),
.and_then(|tags| serde_json::from_str(&tags).ok()),
date_published: self.date_published.clone(),
date_modified: self.date_modified.clone(),
})
Expand Down Expand Up @@ -98,7 +98,7 @@ impl WrappedUserFeedItem {
language: json.language,
tags: json
.tags
.map(|tags| serde_json::to_string::<Vec<String>>(&tags).unwrap()),
.and_then(|tags| serde_json::to_string::<Vec<String>>(&tags).ok()),
date_published: json.date_published,
date_modified: json.date_modified,
};
Expand Down

0 comments on commit eaa8730

Please sign in to comment.