From 5f49b2aaec2af04dcc5acf8ada63ce5ee571401d Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 18 Dec 2024 11:26:59 +0100 Subject: [PATCH] add upload user banner --- crates/api/src/local_user/save_settings.rs | 9 +-------- crates/api_common/src/person.rs | 3 --- crates/routes/src/images/upload.rs | 19 +++++++++++++++++++ src/api_routes_v4.rs | 3 ++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index bcf1b02b6..930f6bf48 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -3,12 +3,10 @@ use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, person::SaveUserSettings, - request::replace_image, utils::{ get_url_blocklist, local_site_to_slur_regex, process_markdown_opt, - proxy_image_link_opt_api, send_verification_email, }, SuccessResponse, @@ -21,7 +19,7 @@ use lemmy_db_schema::{ person::{Person, PersonUpdateForm}, }, traits::Crud, - utils::{diesel_string_update, diesel_url_update}, + utils::diesel_string_update, }; use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_utils::{ @@ -46,10 +44,6 @@ pub async fn save_user_settings( .as_deref(), ); - let banner = diesel_url_update(data.banner.as_deref())?; - replace_image(&banner, &local_user_view.person.banner, &context).await?; - let banner = proxy_image_link_opt_api(banner, &context).await?; - let display_name = diesel_string_update(data.display_name.as_deref()); let matrix_user_id = diesel_string_update(data.matrix_user_id.as_deref()); let email_deref = data.email.as_deref().map(str::to_lowercase); @@ -104,7 +98,6 @@ pub async fn save_user_settings( bio, matrix_user_id, bot_account: data.bot_account, - banner, ..Default::default() }; diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 9c8a1d087..5e7902377 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -114,9 +114,6 @@ pub struct SaveUserSettings { /// The language of the lemmy interface #[cfg_attr(feature = "full", ts(optional))] pub interface_language: Option, - /// A URL for your banner. - #[cfg_attr(feature = "full", ts(optional))] - pub banner: Option, /// Your display name, which can contain strange characters, and does not need to be unique. #[cfg_attr(feature = "full", ts(optional))] pub display_name: Option, diff --git a/crates/routes/src/images/upload.rs b/crates/routes/src/images/upload.rs index 735a18d15..6b24940b9 100644 --- a/crates/routes/src/images/upload.rs +++ b/crates/routes/src/images/upload.rs @@ -22,6 +22,7 @@ use UploadType::*; pub enum UploadType { Avatar, + Banner, Other, } @@ -58,6 +59,24 @@ pub async fn upload_user_avatar( Ok(Json(SuccessResponse::default())) } +pub async fn upload_user_banner( + req: HttpRequest, + body: Payload, + local_user_view: LocalUserView, + context: Data, +) -> LemmyResult> { + let image = do_upload_image(req, body, Banner, &local_user_view, &context).await?; + delete_old_image(&local_user_view.person.banner, &context).await?; + + let person_form = PersonUpdateForm { + banner: Some(Some(image.image_url.into())), + ..Default::default() + }; + Person::update(&mut context.pool(), local_user_view.person.id, &person_form).await?; + + Ok(Json(SuccessResponse::default())) +} + pub async fn do_upload_image( req: HttpRequest, body: Payload, diff --git a/src/api_routes_v4.rs b/src/api_routes_v4.rs index 90f751b55..f725b66ca 100644 --- a/src/api_routes_v4.rs +++ b/src/api_routes_v4.rs @@ -162,7 +162,7 @@ use lemmy_routes::images::{ delete_image, download::{get_image, image_proxy}, pictrs_health, - upload::{upload_image, upload_user_avatar}, + upload::{upload_image, upload_user_avatar, upload_user_banner}, }; use lemmy_utils::rate_limit::RateLimitCell; @@ -320,6 +320,7 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) { .route("/list_logins", get().to(list_logins)) .route("/validate_auth", get().to(validate_auth)) .route("/avatar", post().to(upload_user_avatar)) + .route("/banner", post().to(upload_user_banner)) .service( scope("/block") .route("/person", post().to(user_block_person))