Skip to content

Commit 1e35acd

Browse files
committed
Add user icon delete route
By request of moderation, but also just generally nice to have
1 parent d6c8af7 commit 1e35acd

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

apps/labrinth/.sqlx/query-483cb875ba81c7563a2f7220158cfcb9e6a117a4efc070438606e4c94103a9a4.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/labrinth/src/routes/v3/users.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
3838
.route("{user_id}/organizations", web::get().to(orgs_list))
3939
.route("{id}", web::patch().to(user_edit))
4040
.route("{id}/icon", web::patch().to(user_icon_edit))
41+
.route("{id}/icon", web::delete().to(user_icon_delete))
4142
.route("{id}", web::delete().to(user_delete))
4243
.route("{id}/follows", web::get().to(user_follows))
4344
.route("{id}/notifications", web::get().to(user_notifications))
@@ -623,6 +624,59 @@ pub async fn user_icon_edit(
623624
}
624625
}
625626

627+
pub async fn user_icon_delete(
628+
req: HttpRequest,
629+
info: web::Path<(String,)>,
630+
pool: web::Data<PgPool>,
631+
redis: web::Data<RedisPool>,
632+
file_host: web::Data<Arc<dyn FileHost + Send + Sync>>,
633+
session_queue: web::Data<AuthQueue>,
634+
) -> Result<HttpResponse, ApiError> {
635+
let user = get_user_from_headers(
636+
&req,
637+
&**pool,
638+
&redis,
639+
&session_queue,
640+
Some(&[Scopes::USER_WRITE]),
641+
)
642+
.await?
643+
.1;
644+
let id_option = User::get(&info.into_inner().0, &**pool, &redis).await?;
645+
646+
if let Some(actual_user) = id_option {
647+
if user.id != actual_user.id.into() && !user.role.is_mod() {
648+
return Err(ApiError::CustomAuthentication(
649+
"You don't have permission to edit this user's icon."
650+
.to_string(),
651+
));
652+
}
653+
654+
delete_old_images(
655+
actual_user.avatar_url,
656+
actual_user.raw_avatar_url,
657+
&***file_host,
658+
)
659+
.await?;
660+
661+
sqlx::query!(
662+
"
663+
UPDATE users
664+
SET avatar_url = NULL, raw_avatar_url = NULL
665+
WHERE (id = $1)
666+
",
667+
actual_user.id as crate::database::models::ids::UserId,
668+
)
669+
.execute(&**pool)
670+
.await?;
671+
672+
User::clear_caches(&[(actual_user.id, None)], &redis).await?;
673+
674+
Ok(HttpResponse::NoContent().body(""))
675+
} else {
676+
Err(ApiError::NotFound)
677+
}
678+
}
679+
626680
pub async fn user_delete(
627681
req: HttpRequest,
628682
info: web::Path<(String,)>,

0 commit comments

Comments
 (0)