11use chrono:: { NaiveDateTime , Utc } ;
2+ use diesel_async:: AsyncPgConnection ;
23use http:: StatusCode ;
34use secrecy:: SecretString ;
45
@@ -87,30 +88,42 @@ impl CrateOwnerInvitation {
8788 } )
8889 }
8990
90- pub fn find_by_id ( user_id : i32 , crate_id : i32 , conn : & mut impl Conn ) -> QueryResult < Self > {
91- use diesel:: RunQueryDsl ;
91+ pub async fn find_by_id (
92+ user_id : i32 ,
93+ crate_id : i32 ,
94+ conn : & mut AsyncPgConnection ,
95+ ) -> QueryResult < Self > {
96+ use diesel_async:: RunQueryDsl ;
9297
9398 crate_owner_invitations:: table
9499 . find ( ( user_id, crate_id) )
95100 . first :: < Self > ( conn)
101+ . await
96102 }
97103
98- pub fn find_by_token ( token : & str , conn : & mut impl Conn ) -> QueryResult < Self > {
99- use diesel :: RunQueryDsl ;
104+ pub async fn find_by_token ( token : & str , conn : & mut AsyncPgConnection ) -> QueryResult < Self > {
105+ use diesel_async :: RunQueryDsl ;
100106
101107 crate_owner_invitations:: table
102108 . filter ( crate_owner_invitations:: token. eq ( token) )
103109 . first :: < Self > ( conn)
110+ . await
104111 }
105112
106- pub fn accept ( self , conn : & mut impl Conn , config : & config:: Server ) -> AppResult < ( ) > {
107- use diesel:: RunQueryDsl ;
113+ pub async fn accept (
114+ self ,
115+ conn : & mut AsyncPgConnection ,
116+ config : & config:: Server ,
117+ ) -> AppResult < ( ) > {
118+ use diesel_async:: scoped_futures:: ScopedFutureExt ;
119+ use diesel_async:: { AsyncConnection , RunQueryDsl } ;
108120
109121 if self . is_expired ( config) {
110122 let crate_name: String = crates:: table
111123 . find ( self . crate_id )
112124 . select ( crates:: name)
113- . first ( conn) ?;
125+ . first ( conn)
126+ . await ?;
114127
115128 let detail = format ! (
116129 "The invitation to become an owner of the {crate_name} crate expired. \
@@ -121,33 +134,38 @@ impl CrateOwnerInvitation {
121134 }
122135
123136 conn. transaction ( |conn| {
124- diesel:: insert_into ( crate_owners:: table)
125- . values ( & CrateOwner {
126- crate_id : self . crate_id ,
127- owner_id : self . invited_user_id ,
128- created_by : self . invited_by_user_id ,
129- owner_kind : OwnerKind :: User ,
130- email_notifications : true ,
131- } )
132- . on_conflict ( crate_owners:: table. primary_key ( ) )
133- . do_update ( )
134- . set ( crate_owners:: deleted. eq ( false ) )
135- . execute ( conn) ?;
136-
137- diesel:: delete ( & self ) . execute ( conn) ?;
138-
139- Ok ( ( ) )
137+ async move {
138+ diesel:: insert_into ( crate_owners:: table)
139+ . values ( & CrateOwner {
140+ crate_id : self . crate_id ,
141+ owner_id : self . invited_user_id ,
142+ created_by : self . invited_by_user_id ,
143+ owner_kind : OwnerKind :: User ,
144+ email_notifications : true ,
145+ } )
146+ . on_conflict ( crate_owners:: table. primary_key ( ) )
147+ . do_update ( )
148+ . set ( crate_owners:: deleted. eq ( false ) )
149+ . execute ( conn)
150+ . await ?;
151+
152+ diesel:: delete ( & self ) . execute ( conn) . await ?;
153+
154+ Ok ( ( ) )
155+ }
156+ . scope_boxed ( )
140157 } )
158+ . await
141159 }
142160
143- pub fn decline ( self , conn : & mut impl Conn ) -> QueryResult < ( ) > {
144- use diesel :: RunQueryDsl ;
161+ pub async fn decline ( self , conn : & mut AsyncPgConnection ) -> QueryResult < ( ) > {
162+ use diesel_async :: RunQueryDsl ;
145163
146164 // The check to prevent declining expired invitations is *explicitly* missing. We do not
147165 // care if an expired invitation is declined, as that just removes the invitation from the
148166 // database.
149167
150- diesel:: delete ( & self ) . execute ( conn) ?;
168+ diesel:: delete ( & self ) . execute ( conn) . await ?;
151169 Ok ( ( ) )
152170 }
153171
0 commit comments