44// SPDX-License-Identifier: AGPL-3.0-only
55// Please see LICENSE in the repository root for full details.
66
7- use std:: { net:: IpAddr , ops :: Deref } ;
7+ use std:: net:: IpAddr ;
88
9- use chrono:: { DateTime , Duration , Utc } ;
10- use rand:: { Rng , SeedableRng } ;
9+ use chrono:: { DateTime , Utc } ;
10+ use rand:: Rng ;
1111use serde:: Serialize ;
1212use ulid:: Ulid ;
1313
@@ -171,7 +171,6 @@ pub struct UserEmail {
171171 pub user_id : Ulid ,
172172 pub email : String ,
173173 pub created_at : DateTime < Utc > ,
174- pub confirmed_at : Option < DateTime < Utc > > ,
175174}
176175
177176impl UserEmail {
@@ -183,79 +182,13 @@ impl UserEmail {
183182 user_id: Ulid :: from_datetime_with_source( now. into( ) , rng) ,
184183 email
: "[email protected] " . to_owned
( ) , 185184 created_at: now,
186- confirmed_at: Some ( now) ,
187185 } ,
188186 Self {
189187 id: Ulid :: from_datetime_with_source( now. into( ) , rng) ,
190188 user_id: Ulid :: from_datetime_with_source( now. into( ) , rng) ,
191189 email
: "[email protected] " . to_owned
( ) , 192190 created_at: now,
193- confirmed_at: None ,
194191 } ,
195192 ]
196193 }
197194}
198-
199- #[ derive( Debug , Clone , PartialEq , Eq , Serialize ) ]
200- pub enum UserEmailVerificationState {
201- AlreadyUsed { when : DateTime < Utc > } ,
202- Expired { when : DateTime < Utc > } ,
203- Valid ,
204- }
205-
206- impl UserEmailVerificationState {
207- #[ must_use]
208- pub fn is_valid ( & self ) -> bool {
209- matches ! ( self , Self :: Valid )
210- }
211- }
212-
213- #[ derive( Debug , Clone , PartialEq , Eq , Serialize ) ]
214- pub struct UserEmailVerification {
215- pub id : Ulid ,
216- pub user_email_id : Ulid ,
217- pub code : String ,
218- pub created_at : DateTime < Utc > ,
219- pub state : UserEmailVerificationState ,
220- }
221-
222- impl Deref for UserEmailVerification {
223- type Target = UserEmailVerificationState ;
224-
225- fn deref ( & self ) -> & Self :: Target {
226- & self . state
227- }
228- }
229-
230- impl UserEmailVerification {
231- #[ doc( hidden) ]
232- #[ must_use]
233- pub fn samples ( now : chrono:: DateTime < Utc > , rng : & mut impl Rng ) -> Vec < Self > {
234- let states = [
235- UserEmailVerificationState :: AlreadyUsed {
236- when : now - Duration :: microseconds ( 5 * 60 * 1000 * 1000 ) ,
237- } ,
238- UserEmailVerificationState :: Expired {
239- when : now - Duration :: microseconds ( 5 * 60 * 1000 * 1000 ) ,
240- } ,
241- UserEmailVerificationState :: Valid ,
242- ] ;
243-
244- states
245- . into_iter ( )
246- . flat_map ( move |state| {
247- let mut rng =
248- rand_chacha:: ChaChaRng :: from_rng ( & mut * rng) . expect ( "could not seed rng" ) ;
249- UserEmail :: samples ( now, & mut rng)
250- . into_iter ( )
251- . map ( move |email| Self {
252- id : Ulid :: from_datetime_with_source ( now. into ( ) , & mut rng) ,
253- user_email_id : email. id ,
254- code : "123456" . to_owned ( ) ,
255- created_at : now - Duration :: microseconds ( 10 * 60 * 1000 * 1000 ) ,
256- state : state. clone ( ) ,
257- } )
258- } )
259- . collect ( )
260- }
261- }
0 commit comments