Change order of parameters in some functions

This commit is contained in:
silverpill 2023-02-21 21:23:12 +00:00
parent 2b5d4562aa
commit c796cddff8
13 changed files with 134 additions and 46 deletions

View file

@ -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(),

View file

@ -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<HttpResponse, HttpError> {
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<HttpResponse, HttpError> {
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<Account> = 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<Account> = 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<HttpResponse, HttpError> {
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<Account> = 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<Account> = 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))
}

View file

@ -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<Account> = 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))
}

View file

@ -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,
}

View file

@ -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))
}

View file

@ -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",

View file

@ -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)

View file

@ -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<Account> = 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(&current_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))

View file

@ -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, &current_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))
}

View file

@ -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<Status> = posts
.into_iter()
.map(|post| Status::from_post(post, instance_url))
.map(|post| Status::from_post(instance_url, post))
.collect();
Ok(statuses)
}

View file

@ -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<Attachment> = post.attachments.into_iter()
.map(|item| Attachment::from_db(item, instance_url))
.map(|item| Attachment::from_db(instance_url, item))
.collect();
let mentions: Vec<Mention> = post.mentions.into_iter()
.map(|item| Mention::from_profile(item, instance_url))
.map(|item| Mention::from_profile(instance_url, item))
.collect();
let tags: Vec<Tag> = post.tags.into_iter()
.map(|tag_name| Tag::from_tag_name(instance_url, tag_name))
@ -98,15 +101,18 @@ impl Status {
let emojis: Vec<CustomEmoji> = 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",

View file

@ -190,7 +190,7 @@ async fn create_status(
prepare_create_note(db_client, &instance, &current_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))
}

View file

@ -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))
}