@@ -85,18 +85,72 @@ impl Storage {
8585 . await
8686 . wrap_err ( "failed to create storage directory" ) ?;
8787 }
88- let teams = MirroredToDisk :: read_from_or_create_default ( path. join ( "teams.json" ) )
89- . await
90- . wrap_err ( "failed to read teams.json" ) ?;
91- let users = MirroredToDisk :: read_from_or_create_default ( path. join ( "users.json" ) )
92- . await
93- . wrap_err ( "failed to read users" ) ?;
94- let talks = MirroredToDisk :: read_from_or_create_default ( path. join ( "talks.json" ) )
95- . await
96- . wrap_err ( "failed to read talks" ) ?;
97- let tokens = MirroredToDisk :: read_from_or_create_default ( path. join ( "tokens.json" ) )
98- . await
99- . wrap_err ( "failed to read talks" ) ?;
88+ let mut teams = MirroredToDisk :: < BTreeSet < String > > :: read_from_or_create_default (
89+ path. join ( "teams.json" ) ,
90+ )
91+ . await
92+ . wrap_err ( "failed to read teams.json" ) ?;
93+
94+ let mut users = MirroredToDisk :: < BTreeMap < usize , User > > :: read_from_or_create_default (
95+ path. join ( "users.json" ) ,
96+ )
97+ . await
98+ . wrap_err ( "failed to read users" ) ?;
99+
100+ for ( user_id, user) in users. iter_mut ( ) {
101+ if * user_id != user. id {
102+ tracing:: warn!(
103+ "Inconsistent user id for user {}: key is {}, but user.id is {}. Syncing user.id to {}." ,
104+ user. name,
105+ user_id,
106+ user. id,
107+ user_id,
108+ ) ;
109+ user. id = * user_id;
110+ }
111+ if !teams. value . contains ( & user. team ) {
112+ tracing:: warn!(
113+ "User {} has unknown team {}. Assigning to 'Unknown' team." ,
114+ user. name,
115+ user. team
116+ ) ;
117+ user. team = "Unknown" . to_string ( ) ;
118+ teams. value . insert ( "Unknown" . to_string ( ) ) ;
119+ }
120+ }
121+
122+ let mut talks = MirroredToDisk :: < BTreeMap < usize , Talk > > :: read_from_or_create_default (
123+ path. join ( "talks.json" ) ,
124+ )
125+ . await ?;
126+ let tokens = MirroredToDisk :: read_from_or_create_default ( path. join ( "tokens.json" ) ) . await ?;
127+
128+ let user_ids: BTreeSet < usize > = users. keys ( ) . copied ( ) . collect ( ) ;
129+
130+ talks. value . retain ( |talk_id, talk| {
131+ if !user_ids. contains ( & talk. creator ) {
132+ tracing:: warn!(
133+ "Dropping orphan talk {talk_id} (creator {} missing)" ,
134+ talk. creator
135+ ) ;
136+ return false ;
137+ }
138+
139+ let original_nerds = talk. nerds . len ( ) ;
140+ talk. nerds . retain ( |id| user_ids. contains ( id) ) ;
141+ if talk. nerds . len ( ) != original_nerds {
142+ tracing:: info!( "Cleaned invalid nerds from talk {talk_id}" ) ;
143+ }
144+
145+ let original_noobs = talk. noobs . len ( ) ;
146+ talk. noobs . retain ( |id| user_ids. contains ( id) ) ;
147+ if talk. noobs . len ( ) != original_noobs {
148+ tracing:: info!( "Cleaned invalid noobs from talk {talk_id}" ) ;
149+ }
150+
151+ true
152+ } ) ;
153+
100154 Ok ( Self {
101155 path : path. to_path_buf ( ) ,
102156 teams,
0 commit comments