@@ -4,7 +4,6 @@ use diesel_async::scoped_futures::ScopedFutureExt;
4
4
use diesel_async:: { AsyncConnection , AsyncPgConnection , RunQueryDsl } ;
5
5
use secrecy:: SecretString ;
6
6
7
- use crate :: config;
8
7
use crate :: models:: CrateOwner ;
9
8
use crate :: schema:: { crate_owner_invitations, crate_owners, crates} ;
10
9
@@ -27,32 +26,16 @@ impl NewCrateOwnerInvitation {
27
26
pub async fn create (
28
27
& self ,
29
28
conn : & mut AsyncPgConnection ,
30
- config : & config:: Server ,
31
29
) -> QueryResult < NewCrateOwnerInvitationOutcome > {
32
30
// Before actually creating the invite, check if an expired invitation already exists
33
31
// and delete it from the database. This allows obtaining a new invite if the old one
34
32
// expired, instead of returning "already exists".
35
- conn. transaction ( |conn| {
36
- async move {
37
- // This does a SELECT FOR UPDATE + DELETE instead of a DELETE with a WHERE clause to
38
- // use the model's `is_expired` method, centralizing our expiration checking logic.
39
- let existing: Option < CrateOwnerInvitation > = crate_owner_invitations:: table
40
- . find ( ( self . invited_user_id , self . crate_id ) )
41
- . for_update ( )
42
- . first ( conn)
43
- . await
44
- . optional ( ) ?;
45
-
46
- if let Some ( existing) = existing {
47
- if existing. is_expired ( config) {
48
- diesel:: delete ( & existing) . execute ( conn) . await ?;
49
- }
50
- }
51
- QueryResult :: Ok ( ( ) )
52
- }
53
- . scope_boxed ( )
54
- } )
55
- . await ?;
33
+ diesel:: delete ( crate_owner_invitations:: table)
34
+ . filter ( crate_owner_invitations:: invited_user_id. eq ( self . invited_user_id ) )
35
+ . filter ( crate_owner_invitations:: crate_id. eq ( self . crate_id ) )
36
+ . filter ( crate_owner_invitations:: expires_at. le ( Utc :: now ( ) ) )
37
+ . execute ( conn)
38
+ . await ?;
56
39
57
40
let res: Option < CrateOwnerInvitation > = diesel:: insert_into ( crate_owner_invitations:: table)
58
41
. values ( self )
@@ -83,7 +66,7 @@ pub struct CrateOwnerInvitation {
83
66
pub created_at : NaiveDateTime ,
84
67
#[ diesel( deserialize_as = String ) ]
85
68
pub token : SecretString ,
86
- pub expires_at : Option < DateTime < Utc > > ,
69
+ pub expires_at : DateTime < Utc > ,
87
70
}
88
71
89
72
impl CrateOwnerInvitation {
@@ -105,15 +88,8 @@ impl CrateOwnerInvitation {
105
88
. await
106
89
}
107
90
108
- pub async fn accept (
109
- self ,
110
- conn : & mut AsyncPgConnection ,
111
- config : & config:: Server ,
112
- ) -> Result < ( ) , AcceptError > {
113
- use diesel_async:: scoped_futures:: ScopedFutureExt ;
114
- use diesel_async:: { AsyncConnection , RunQueryDsl } ;
115
-
116
- if self . is_expired ( config) {
91
+ pub async fn accept ( self , conn : & mut AsyncPgConnection ) -> Result < ( ) , AcceptError > {
92
+ if self . is_expired ( ) {
117
93
let crate_name: String = crates:: table
118
94
. find ( self . crate_id )
119
95
. select ( crates:: name)
@@ -151,12 +127,8 @@ impl CrateOwnerInvitation {
151
127
Ok ( ( ) )
152
128
}
153
129
154
- pub fn is_expired ( & self , config : & config:: Server ) -> bool {
155
- self . expires_at ( config) <= Utc :: now ( ) . naive_utc ( )
156
- }
157
-
158
- pub fn expires_at ( & self , config : & config:: Server ) -> NaiveDateTime {
159
- self . created_at + config. ownership_invitations_expiration
130
+ pub fn is_expired ( & self ) -> bool {
131
+ self . expires_at <= Utc :: now ( )
160
132
}
161
133
}
162
134
0 commit comments