@@ -38,6 +38,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
38
38
. route ( "{user_id}/organizations" , web:: get ( ) . to ( orgs_list) )
39
39
. route ( "{id}" , web:: patch ( ) . to ( user_edit) )
40
40
. route ( "{id}/icon" , web:: patch ( ) . to ( user_icon_edit) )
41
+ . route ( "{id}/icon" , web:: delete ( ) . to ( user_icon_delete) )
41
42
. route ( "{id}" , web:: delete ( ) . to ( user_delete) )
42
43
. route ( "{id}/follows" , web:: get ( ) . to ( user_follows) )
43
44
. route ( "{id}/notifications" , web:: get ( ) . to ( user_notifications) )
@@ -623,6 +624,59 @@ pub async fn user_icon_edit(
623
624
}
624
625
}
625
626
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
+
626
680
pub async fn user_delete (
627
681
req : HttpRequest ,
628
682
info : web:: Path < ( String , ) > ,
0 commit comments