From c796cddff85dda0cf0e1e42e624f2d3529afd1e6 Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 21 Feb 2023 21:23:12 +0000 Subject: [PATCH] Change order of parameters in some functions --- src/mastodon_api/accounts/types.rs | 30 ++++++++--- src/mastodon_api/accounts/views.rs | 68 ++++++++++++++++++++----- src/mastodon_api/directory/views.rs | 6 ++- src/mastodon_api/media/types.rs | 12 +++-- src/mastodon_api/media/views.rs | 2 +- src/mastodon_api/notifications/types.rs | 9 ++-- src/mastodon_api/notifications/views.rs | 2 +- src/mastodon_api/search/views.rs | 10 ++-- src/mastodon_api/settings/views.rs | 10 +++- src/mastodon_api/statuses/helpers.rs | 4 +- src/mastodon_api/statuses/types.rs | 20 +++++--- src/mastodon_api/statuses/views.rs | 2 +- src/mastodon_api/subscriptions/views.rs | 5 +- 13 files changed, 134 insertions(+), 46 deletions(-) diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index d2f813e..2477d93 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -116,7 +116,10 @@ pub struct Account { } impl Account { - pub fn from_profile(profile: DbActorProfile, instance_url: &str) -> Self { + pub fn from_profile( + instance_url: &str, + profile: DbActorProfile, + ) -> Self { let profile_url = profile.actor_url(instance_url); let avatar_url = profile.avatar .map(|image| get_file_url(instance_url, &image.file_name)); @@ -203,7 +206,10 @@ impl Account { } } - pub fn from_user(user: User, instance_url: &str) -> Self { + pub fn from_user( + instance_url: &str, + user: User, + ) -> Self { let fields_sources = user.profile.extra_fields.clone() .into_inner().into_iter() .map(|field| AccountField { @@ -217,7 +223,10 @@ impl Account { fields: fields_sources, }; let role = ApiRole::from_db(user.role); - let mut account = Self::from_profile(user.profile, instance_url); + let mut account = Self::from_profile( + instance_url, + user.profile, + ); account.source = Some(source); account.role = Some(role); account @@ -496,7 +505,10 @@ impl ApiSubscription { instance_url: &str, subscription: Subscription, ) -> Self { - let sender = Account::from_profile(subscription.sender, instance_url); + let sender = Account::from_profile( + instance_url, + subscription.sender, + ); Self { id: subscription.id, sender, @@ -532,7 +544,10 @@ mod tests { avatar: Some(ProfileImage::new("test".to_string(), 1000, None)), ..Default::default() }; - let account = Account::from_profile(profile, INSTANCE_URL); + let account = Account::from_profile( + INSTANCE_URL, + profile, + ); assert_eq!( account.avatar.unwrap(), @@ -554,7 +569,10 @@ mod tests { profile, ..Default::default() }; - let account = Account::from_user(user, INSTANCE_URL); + let account = Account::from_user( + INSTANCE_URL, + user, + ); assert_eq!( account.source.unwrap().note.unwrap(), diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index c0c5d73..892e5cb 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -194,7 +194,10 @@ pub async fn create_account( Err(other_error) => return Err(other_error.into()), }; log::warn!("created user {}", user.id); - let account = Account::from_user(user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + user, + ); Ok(HttpResponse::Created().json(account)) } @@ -206,7 +209,10 @@ async fn verify_credentials( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let user = get_current_user(db_client, auth.token()).await?; - let account = Account::from_user(user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + user, + ); Ok(HttpResponse::Ok().json(account)) } @@ -239,7 +245,10 @@ async fn update_credentials( None, ).await?.enqueue(db_client).await?; - let account = Account::from_user(current_user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + current_user, + ); Ok(HttpResponse::Ok().json(account)) } @@ -313,7 +322,10 @@ async fn send_signed_activity( outgoing_activity.enqueue(db_client).await?; - let account = Account::from_user(current_user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + current_user, + ); Ok(HttpResponse::Ok().json(account)) } @@ -428,7 +440,10 @@ async fn create_identity_proof( None, ).await?.enqueue(db_client).await?; - let account = Account::from_user(current_user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + current_user, + ); Ok(HttpResponse::Ok().json(account)) } @@ -456,7 +471,10 @@ async fn lookup_acct( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let profile = get_profile_by_acct(db_client, &query_params.acct).await?; - let account = Account::from_profile(profile, &config.instance_url()); + let account = Account::from_profile( + &config.instance_url(), + profile, + ); Ok(HttpResponse::Ok().json(account)) } @@ -472,8 +490,12 @@ async fn search_by_acct( &query_params.q, query_params.limit.inner(), ).await?; + let instance_url = config.instance().url(); let accounts: Vec = profiles.into_iter() - .map(|profile| Account::from_profile(profile, &config.instance_url())) + .map(|profile| Account::from_profile( + &instance_url, + profile, + )) .collect(); Ok(HttpResponse::Ok().json(accounts)) } @@ -488,8 +510,12 @@ async fn search_by_did( let did: Did = query_params.did.parse() .map_err(|_| ValidationError("invalid DID"))?; let profiles = search_profiles_by_did(db_client, &did, false).await?; + let instance_url = config.instance().url(); let accounts: Vec = profiles.into_iter() - .map(|profile| Account::from_profile(profile, &config.instance_url())) + .map(|profile| Account::from_profile( + &instance_url, + profile, + )) .collect(); Ok(HttpResponse::Ok().json(accounts)) } @@ -502,7 +528,10 @@ async fn get_account( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let profile = get_profile_by_id(db_client, &account_id).await?; - let account = Account::from_profile(profile, &config.instance_url()); + let account = Account::from_profile( + &config.instance_url(), + profile, + ); Ok(HttpResponse::Ok().json(account)) } @@ -639,11 +668,15 @@ async fn get_account_followers( ).await?; let max_index = usize::from(query_params.limit.inner().saturating_sub(1)); let maybe_last_id = followers.get(max_index).map(|item| item.relationship_id); + let instance_url = config.instance().url(); let accounts: Vec = followers.into_iter() - .map(|item| Account::from_profile(item.profile, &config.instance_url())) + .map(|item| Account::from_profile( + &instance_url, + item.profile, + )) .collect(); let response = get_paginated_response( - &config.instance_url(), + &instance_url, request.uri().path(), accounts, maybe_last_id, @@ -676,11 +709,15 @@ async fn get_account_following( ).await?; let max_index = usize::from(query_params.limit.inner().saturating_sub(1)); let maybe_last_id = following.get(max_index).map(|item| item.relationship_id); + let instance_url = config.instance().url(); let accounts: Vec = following.into_iter() - .map(|item| Account::from_profile(item.profile, &config.instance_url())) + .map(|item| Account::from_profile( + &instance_url, + item.profile, + )) .collect(); let response = get_paginated_response( - &config.instance_url(), + &instance_url, request.uri().path(), accounts, maybe_last_id, @@ -713,7 +750,10 @@ async fn get_account_subscribers( ) .await? .into_iter() - .map(|item| ApiSubscription::from_subscription(&instance_url, item)) + .map(|subscription| ApiSubscription::from_subscription( + &instance_url, + subscription, + )) .collect(); Ok(HttpResponse::Ok().json(subscriptions)) } diff --git a/src/mastodon_api/directory/views.rs b/src/mastodon_api/directory/views.rs index 24b6828..8b39678 100644 --- a/src/mastodon_api/directory/views.rs +++ b/src/mastodon_api/directory/views.rs @@ -28,9 +28,13 @@ async fn profile_directory( query_params.offset, query_params.limit.inner(), ).await?; + let instance_url = config.instance().url(); let accounts: Vec = profiles .into_iter() - .map(|profile| Account::from_profile(profile, &config.instance_url())) + .map(|profile| Account::from_profile( + &instance_url, + profile, + )) .collect(); Ok(HttpResponse::Ok().json(accounts)) } diff --git a/src/mastodon_api/media/types.rs b/src/mastodon_api/media/types.rs index a67172f..4d331aa 100644 --- a/src/mastodon_api/media/types.rs +++ b/src/mastodon_api/media/types.rs @@ -26,16 +26,20 @@ pub struct Attachment { } impl Attachment { - pub fn from_db(db_object: DbMediaAttachment, instance_url: &str) -> Self { - let attachment_type = AttachmentType::from_media_type(db_object.media_type); + pub fn from_db(instance_url: &str, db_attachment: DbMediaAttachment) -> Self { + let attachment_type = + AttachmentType::from_media_type(db_attachment.media_type); let attachment_type_mastodon = match attachment_type { AttachmentType::Unknown => "unknown", AttachmentType::Image => "image", AttachmentType::Video => "video", }; - let attachment_url = get_file_url(instance_url, &db_object.file_name); + let attachment_url = get_file_url( + instance_url, + &db_attachment.file_name, + ); Self { - id: db_object.id, + id: db_attachment.id, attachment_type: attachment_type_mastodon.to_string(), url: attachment_url, } diff --git a/src/mastodon_api/media/views.rs b/src/mastodon_api/media/views.rs index 3563964..2bda465 100644 --- a/src/mastodon_api/media/views.rs +++ b/src/mastodon_api/media/views.rs @@ -36,8 +36,8 @@ async fn create_attachment_view( Some(media_type), ).await?; let attachment = Attachment::from_db( - db_attachment, &config.instance_url(), + db_attachment, ); Ok(HttpResponse::Ok().json(attachment)) } diff --git a/src/mastodon_api/notifications/types.rs b/src/mastodon_api/notifications/types.rs index c3e5bb0..eeb16e6 100644 --- a/src/mastodon_api/notifications/types.rs +++ b/src/mastodon_api/notifications/types.rs @@ -34,13 +34,16 @@ pub struct ApiNotification { } impl ApiNotification { - pub fn from_db(notification: Notification, instance_url: &str) -> Self { + pub fn from_db( + instance_url: &str, + notification: Notification, + ) -> Self { let account = Account::from_profile( - notification.sender, instance_url, + notification.sender, ); let status = notification.post.map(|post| { - Status::from_post(post, instance_url) + Status::from_post(instance_url, post) }); let event_type_mastodon = match notification.event_type { EventType::Follow => "follow", diff --git a/src/mastodon_api/notifications/views.rs b/src/mastodon_api/notifications/views.rs index 275da2a..b2ca767 100644 --- a/src/mastodon_api/notifications/views.rs +++ b/src/mastodon_api/notifications/views.rs @@ -34,7 +34,7 @@ async fn get_notifications_view( query_params.limit.inner(), ).await? .into_iter() - .map(|item| ApiNotification::from_db(item, &config.instance_url())) + .map(|item| ApiNotification::from_db(&config.instance_url(), item)) .collect(); let max_index = usize::from(query_params.limit.inner().saturating_sub(1)); let maybe_last_id = notifications.get(max_index) diff --git a/src/mastodon_api/search/views.rs b/src/mastodon_api/search/views.rs index e84a5ec..751f025 100644 --- a/src/mastodon_api/search/views.rs +++ b/src/mastodon_api/search/views.rs @@ -31,17 +31,21 @@ async fn search_view( query_params.q.trim(), query_params.limit.inner(), ).await?; + let instance_url = config.instance().url(); let accounts: Vec = profiles.into_iter() - .map(|profile| Account::from_profile(profile, &config.instance_url())) + .map(|profile| Account::from_profile( + &instance_url, + profile, + )) .collect(); let statuses = build_status_list( db_client, - &config.instance_url(), + &instance_url, Some(¤t_user), posts, ).await?; let hashtags = tags.into_iter() - .map(|tag_name| Tag::from_tag_name(&config.instance_url(), tag_name)) + .map(|tag_name| Tag::from_tag_name(&instance_url, tag_name)) .collect(); let results = SearchResults { accounts, statuses, hashtags }; Ok(HttpResponse::Ok().json(results)) diff --git a/src/mastodon_api/settings/views.rs b/src/mastodon_api/settings/views.rs index b276377..8d4107e 100644 --- a/src/mastodon_api/settings/views.rs +++ b/src/mastodon_api/settings/views.rs @@ -46,7 +46,10 @@ async fn change_password_view( let password_hash = hash_password(&request_data.new_password) .map_err(|_| HttpError::InternalError)?; set_user_password(db_client, ¤t_user.id, password_hash).await?; - let account = Account::from_user(current_user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + current_user, + ); Ok(HttpResponse::Ok().json(account)) } @@ -190,7 +193,10 @@ async fn move_followers( None, ).enqueue(db_client).await?; - let account = Account::from_user(current_user, &instance.url()); + let account = Account::from_user( + &instance.url(), + current_user, + ); Ok(HttpResponse::Ok().json(account)) } diff --git a/src/mastodon_api/statuses/helpers.rs b/src/mastodon_api/statuses/helpers.rs index 9c1eb1a..a3eb656 100644 --- a/src/mastodon_api/statuses/helpers.rs +++ b/src/mastodon_api/statuses/helpers.rs @@ -83,7 +83,7 @@ pub async fn build_status( if let Some(user) = user { add_user_actions(db_client, &user.id, vec![&mut post]).await?; }; - let status = Status::from_post(post, instance_url); + let status = Status::from_post(instance_url, post); Ok(status) } @@ -99,7 +99,7 @@ pub async fn build_status_list( }; let statuses: Vec = posts .into_iter() - .map(|post| Status::from_post(post, instance_url)) + .map(|post| Status::from_post(instance_url, post)) .collect(); Ok(statuses) } diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index fe574e6..3d25fb9 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -24,7 +24,7 @@ pub struct Mention { } impl Mention { - fn from_profile(profile: DbActorProfile, instance_url: &str) -> Self { + fn from_profile(instance_url: &str, profile: DbActorProfile) -> Self { Mention { id: profile.id.to_string(), username: profile.username.clone(), @@ -84,13 +84,16 @@ pub struct Status { } impl Status { - pub fn from_post(post: Post, instance_url: &str) -> Self { + pub fn from_post( + instance_url: &str, + post: Post, + ) -> Self { let object_id = post.object_id(instance_url); let attachments: Vec = post.attachments.into_iter() - .map(|item| Attachment::from_db(item, instance_url)) + .map(|item| Attachment::from_db(instance_url, item)) .collect(); let mentions: Vec = post.mentions.into_iter() - .map(|item| Mention::from_profile(item, instance_url)) + .map(|item| Mention::from_profile(instance_url, item)) .collect(); let tags: Vec = post.tags.into_iter() .map(|tag_name| Tag::from_tag_name(instance_url, tag_name)) @@ -98,15 +101,18 @@ impl Status { let emojis: Vec = post.emojis.into_iter() .map(|emoji| CustomEmoji::from_db(instance_url, emoji)) .collect(); - let account = Account::from_profile(post.author, instance_url); + let account = Account::from_profile( + instance_url, + post.author, + ); let reblog = if let Some(repost_of) = post.repost_of { - let status = Status::from_post(*repost_of, instance_url); + let status = Status::from_post(instance_url, *repost_of); Some(Box::new(status)) } else { None }; let links = post.linked.into_iter().map(|post| { - Status::from_post(post, instance_url) + Status::from_post(instance_url, post) }).collect(); let visibility = match post.visibility { Visibility::Public => "public", diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 7446f8e..3085835 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -190,7 +190,7 @@ async fn create_status( prepare_create_note(db_client, &instance, ¤t_user, &post) .await?.enqueue(db_client).await?; - let status = Status::from_post(post, &instance.url()); + let status = Status::from_post(&instance.url(), post); Ok(HttpResponse::Ok().json(status)) } diff --git a/src/mastodon_api/subscriptions/views.rs b/src/mastodon_api/subscriptions/views.rs index 121a798..d42980b 100644 --- a/src/mastodon_api/subscriptions/views.rs +++ b/src/mastodon_api/subscriptions/views.rs @@ -165,7 +165,10 @@ pub async fn register_subscription_option( ).await?.enqueue(db_client).await?; }; - let account = Account::from_user(current_user, &config.instance_url()); + let account = Account::from_user( + &config.instance_url(), + current_user, + ); Ok(HttpResponse::Ok().json(account)) }