@@ -5,23 +5,24 @@ use crate::auth::AuthCheck;
55use crate :: controllers:: helpers:: ok_true;
66use crate :: models:: { Crate , Follow } ;
77use crate :: schema:: * ;
8- use crate :: tasks:: spawn_blocking;
9- use crate :: util:: diesel:: prelude:: * ;
10- use crate :: util:: diesel:: Conn ;
118use crate :: util:: errors:: { crate_not_found, AppResult } ;
129use axum:: extract:: Path ;
1310use axum:: response:: Response ;
1411use axum:: Json ;
15- use diesel_async:: async_connection_wrapper:: AsyncConnectionWrapper ;
12+ use diesel:: prelude:: * ;
13+ use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
1614use http:: request:: Parts ;
1715use serde_json:: Value ;
1816
19- fn follow_target ( crate_name : & str , conn : & mut impl Conn , user_id : i32 ) -> AppResult < Follow > {
20- use diesel:: RunQueryDsl ;
21-
17+ async fn follow_target (
18+ crate_name : & str ,
19+ conn : & mut AsyncPgConnection ,
20+ user_id : i32 ,
21+ ) -> AppResult < Follow > {
2222 let crate_id = Crate :: by_name ( crate_name)
2323 . select ( crates:: id)
2424 . first ( conn)
25+ . await
2526 . optional ( ) ?
2627 . ok_or_else ( || crate_not_found ( crate_name) ) ?;
2728
@@ -36,20 +37,14 @@ pub async fn follow(
3637) -> AppResult < Response > {
3738 let mut conn = app. db_write ( ) . await ?;
3839 let user_id = AuthCheck :: default ( ) . check ( & req, & mut conn) . await ?. user_id ( ) ;
39- spawn_blocking ( move || {
40- use diesel:: RunQueryDsl ;
41-
42- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
43-
44- let follow = follow_target ( & crate_name, conn, user_id) ?;
45- diesel:: insert_into ( follows:: table)
46- . values ( & follow)
47- . on_conflict_do_nothing ( )
48- . execute ( conn) ?;
49-
50- ok_true ( )
51- } )
52- . await
40+ let follow = follow_target ( & crate_name, & mut conn, user_id) . await ?;
41+ diesel:: insert_into ( follows:: table)
42+ . values ( & follow)
43+ . on_conflict_do_nothing ( )
44+ . execute ( & mut conn)
45+ . await ?;
46+
47+ ok_true ( )
5348}
5449
5550/// Handles the `DELETE /crates/:crate_id/follow` route.
@@ -60,17 +55,10 @@ pub async fn unfollow(
6055) -> AppResult < Response > {
6156 let mut conn = app. db_write ( ) . await ?;
6257 let user_id = AuthCheck :: default ( ) . check ( & req, & mut conn) . await ?. user_id ( ) ;
63- spawn_blocking ( move || {
64- use diesel:: RunQueryDsl ;
65-
66- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
58+ let follow = follow_target ( & crate_name, & mut conn, user_id) . await ?;
59+ diesel:: delete ( & follow) . execute ( & mut conn) . await ?;
6760
68- let follow = follow_target ( & crate_name, conn, user_id) ?;
69- diesel:: delete ( & follow) . execute ( conn) ?;
70-
71- ok_true ( )
72- } )
73- . await
61+ ok_true ( )
7462}
7563
7664/// Handles the `GET /crates/:crate_id/following` route.
@@ -79,23 +67,18 @@ pub async fn following(
7967 Path ( crate_name) : Path < String > ,
8068 req : Parts ,
8169) -> AppResult < Json < Value > > {
70+ use diesel:: dsl:: exists;
71+
8272 let mut conn = app. db_read_prefer_primary ( ) . await ?;
8373 let user_id = AuthCheck :: only_cookie ( )
8474 . check ( & req, & mut conn)
8575 . await ?
8676 . user_id ( ) ;
87- spawn_blocking ( move || {
88- use diesel:: RunQueryDsl ;
89-
90- let conn: & mut AsyncConnectionWrapper < _ > = & mut conn. into ( ) ;
91-
92- use diesel:: dsl:: exists;
9377
94- let follow = follow_target ( & crate_name, conn, user_id) ?;
95- let following =
96- diesel:: select ( exists ( follows:: table. find ( follow. id ( ) ) ) ) . get_result :: < bool > ( conn) ?;
78+ let follow = follow_target ( & crate_name, & mut conn, user_id) . await ?;
79+ let following = diesel:: select ( exists ( follows:: table. find ( follow. id ( ) ) ) )
80+ . get_result :: < bool > ( & mut conn)
81+ . await ?;
9782
98- Ok ( Json ( json ! ( { "following" : following } ) ) )
99- } )
100- . await
83+ Ok ( Json ( json ! ( { "following" : following } ) ) )
10184}
0 commit comments