1
1
use std:: format;
2
- use std:: str:: FromStr ;
3
2
4
3
use async_trait:: async_trait;
5
- use chrono:: {
6
- DateTime , Datelike , Days , Duration , Local , NaiveDate , NaiveDateTime , Offset , TimeZone ,
7
- } ;
8
- use chrono_tz:: Tz ;
4
+ use chrono:: { DateTime , Datelike , Days , Duration , Local , NaiveDate , NaiveDateTime } ;
9
5
use collab_database:: database:: timestamp;
10
6
use collab_database:: fields:: { Field , TypeOptionData } ;
11
7
use collab_database:: rows:: { new_cell_builder, Cell , Cells , Row , RowDetail } ;
@@ -84,12 +80,7 @@ impl GroupCustomize for DateGroupController {
84
80
content : & str ,
85
81
cell_data : & <Self :: GroupTypeOption as TypeOption >:: CellData ,
86
82
) -> bool {
87
- content
88
- == group_id (
89
- cell_data,
90
- & self . type_option ,
91
- & self . context . get_setting_content ( ) ,
92
- )
83
+ content == group_id ( cell_data, & self . context . get_setting_content ( ) )
93
84
}
94
85
95
86
fn create_or_delete_group_when_cell_changed (
@@ -102,27 +93,20 @@ impl GroupCustomize for DateGroupController {
102
93
let mut inserted_group = None ;
103
94
if self
104
95
. context
105
- . get_group ( & group_id (
106
- & _cell_data. into ( ) ,
107
- & self . type_option ,
108
- & setting_content,
109
- ) )
96
+ . get_group ( & group_id ( & _cell_data. into ( ) , & setting_content) )
110
97
. is_none ( )
111
98
{
112
- let group =
113
- make_group_from_date_cell ( & _cell_data. into ( ) , & self . type_option , & setting_content) ;
99
+ let group = make_group_from_date_cell ( & _cell_data. into ( ) , & setting_content) ;
114
100
let mut new_group = self . context . add_new_group ( group) ?;
115
101
new_group. group . rows . push ( RowMetaPB :: from ( _row_detail) ) ;
116
102
inserted_group = Some ( new_group) ;
117
103
}
118
104
119
105
// Delete the old group if there are no rows in that group
120
106
let deleted_group = match _old_cell_data. and_then ( |old_cell_data| {
121
- self . context . get_group ( & group_id (
122
- & old_cell_data. into ( ) ,
123
- & self . type_option ,
124
- & setting_content,
125
- ) )
107
+ self
108
+ . context
109
+ . get_group ( & group_id ( & old_cell_data. into ( ) , & setting_content) )
126
110
} ) {
127
111
None => None ,
128
112
Some ( ( _, group) ) => {
@@ -154,7 +138,7 @@ impl GroupCustomize for DateGroupController {
154
138
let setting_content = self . context . get_setting_content ( ) ;
155
139
self . context . iter_mut_status_groups ( |group| {
156
140
let mut changeset = GroupRowsNotificationPB :: new ( group. id . clone ( ) ) ;
157
- if group. id == group_id ( & cell_data. into ( ) , & self . type_option , & setting_content) {
141
+ if group. id == group_id ( & cell_data. into ( ) , & setting_content) {
158
142
if !group. contains_row ( & row_detail. row . id ) {
159
143
changeset
160
144
. inserted_rows
@@ -194,14 +178,13 @@ impl GroupCustomize for DateGroupController {
194
178
} ) ;
195
179
196
180
let setting_content = self . context . get_setting_content ( ) ;
197
- let deleted_group =
198
- match self
199
- . context
200
- . get_group ( & group_id ( cell_data, & self . type_option , & setting_content) )
201
- {
202
- Some ( ( _, group) ) if group. rows . len ( ) == 1 => Some ( group. clone ( ) ) ,
203
- _ => None ,
204
- } ;
181
+ let deleted_group = match self
182
+ . context
183
+ . get_group ( & group_id ( cell_data, & setting_content) )
184
+ {
185
+ Some ( ( _, group) ) if group. rows . len ( ) == 1 => Some ( group. clone ( ) ) ,
186
+ _ => None ,
187
+ } ;
205
188
206
189
let deleted_group = deleted_group. map ( |group| {
207
190
let _ = self . context . delete_group ( & group. id ) ;
@@ -232,11 +215,10 @@ impl GroupCustomize for DateGroupController {
232
215
) -> Option < GroupPB > {
233
216
let mut deleted_group = None ;
234
217
let setting_content = self . context . get_setting_content ( ) ;
235
- if let Some ( ( _, group) ) = self . context . get_group ( & group_id (
236
- & _cell_data. into ( ) ,
237
- & self . type_option ,
238
- & setting_content,
239
- ) ) {
218
+ if let Some ( ( _, group) ) = self
219
+ . context
220
+ . get_group ( & group_id ( & _cell_data. into ( ) , & setting_content) )
221
+ {
240
222
if group. rows . len ( ) == 1 {
241
223
deleted_group = Some ( GroupPB :: from ( group. clone ( ) ) ) ;
242
224
}
@@ -279,7 +261,7 @@ impl GroupsBuilder for DateGroupBuilder {
279
261
async fn build (
280
262
field : & Field ,
281
263
context : & Self :: Context ,
282
- type_option : & Self :: GroupTypeOption ,
264
+ _type_option : & Self :: GroupTypeOption ,
283
265
) -> GeneratedGroups {
284
266
// Read all the cells for the grouping field
285
267
let cells = context. get_all_cells ( ) . await ;
@@ -290,7 +272,7 @@ impl GroupsBuilder for DateGroupBuilder {
290
272
. flat_map ( |value| value. into_date_field_cell_data ( ) )
291
273
. filter ( |cell| cell. timestamp . is_some ( ) )
292
274
. map ( |cell| {
293
- let group = make_group_from_date_cell ( & cell, type_option , & context. get_setting_content ( ) ) ;
275
+ let group = make_group_from_date_cell ( & cell, & context. get_setting_content ( ) ) ;
294
276
GeneratedGroupConfig {
295
277
filter_content : group. id . clone ( ) ,
296
278
group,
@@ -307,27 +289,19 @@ impl GroupsBuilder for DateGroupBuilder {
307
289
}
308
290
}
309
291
310
- fn make_group_from_date_cell (
311
- cell_data : & DateCellData ,
312
- type_option : & DateTypeOption ,
313
- setting_content : & str ,
314
- ) -> Group {
315
- let group_id = group_id ( cell_data, type_option, setting_content) ;
292
+ fn make_group_from_date_cell ( cell_data : & DateCellData , setting_content : & str ) -> Group {
293
+ let group_id = group_id ( cell_data, setting_content) ;
316
294
Group :: new (
317
295
group_id. clone ( ) ,
318
- group_name_from_id ( & group_id, type_option , setting_content) ,
296
+ group_name_from_id ( & group_id, setting_content) ,
319
297
)
320
298
}
321
299
322
300
const GROUP_ID_DATE_FORMAT : & str = "%Y/%m/%d" ;
323
301
324
- fn group_id (
325
- cell_data : & DateCellData ,
326
- type_option : & DateTypeOption ,
327
- setting_content : & str ,
328
- ) -> String {
302
+ fn group_id ( cell_data : & DateCellData , setting_content : & str ) -> String {
329
303
let config = DateGroupConfiguration :: from_json ( setting_content) . unwrap_or_default ( ) ;
330
- let date_time = date_time_from_timestamp ( cell_data. timestamp , & type_option . timezone_id ) ;
304
+ let date_time = date_time_from_timestamp ( cell_data. timestamp ) ;
331
305
332
306
let date_format = GROUP_ID_DATE_FORMAT ;
333
307
let month_format = & date_format. replace ( "%d" , "01" ) ;
@@ -342,7 +316,7 @@ fn group_id(
342
316
. unwrap ( )
343
317
. format ( date_format) ,
344
318
DateCondition :: Relative => {
345
- let now = date_time_from_timestamp ( Some ( timestamp ( ) ) , & type_option . timezone_id ) . date_naive ( ) ;
319
+ let now = date_time_from_timestamp ( Some ( timestamp ( ) ) ) . date_naive ( ) ;
346
320
let date_time = date_time. date_naive ( ) ;
347
321
348
322
let diff = date_time. signed_duration_since ( now) . num_days ( ) ;
@@ -382,11 +356,7 @@ fn group_id(
382
356
date. to_string ( )
383
357
}
384
358
385
- fn group_name_from_id (
386
- group_id : & str ,
387
- type_option : & DateTypeOption ,
388
- setting_content : & str ,
389
- ) -> String {
359
+ fn group_name_from_id ( group_id : & str , setting_content : & str ) -> String {
390
360
let config = DateGroupConfiguration :: from_json ( setting_content) . unwrap_or_default ( ) ;
391
361
let date = NaiveDate :: parse_from_str ( group_id, GROUP_ID_DATE_FORMAT ) . unwrap ( ) ;
392
362
@@ -421,7 +391,7 @@ fn group_name_from_id(
421
391
} ,
422
392
DateCondition :: Year => date. year ( ) . to_string ( ) ,
423
393
DateCondition :: Relative => {
424
- let now = date_time_from_timestamp ( Some ( timestamp ( ) ) , & type_option . timezone_id ) ;
394
+ let now = date_time_from_timestamp ( Some ( timestamp ( ) ) ) ;
425
395
426
396
let diff = date. signed_duration_since ( now. date_naive ( ) ) ;
427
397
let result = match diff. num_days ( ) {
@@ -443,14 +413,11 @@ fn group_name_from_id(
443
413
}
444
414
}
445
415
446
- fn date_time_from_timestamp ( timestamp : Option < i64 > , timezone_id : & str ) -> DateTime < Local > {
416
+ fn date_time_from_timestamp ( timestamp : Option < i64 > ) -> DateTime < Local > {
447
417
match timestamp {
448
418
Some ( timestamp) => {
449
419
let naive = NaiveDateTime :: from_timestamp_opt ( timestamp, 0 ) . unwrap ( ) ;
450
- let offset = match Tz :: from_str ( timezone_id) {
451
- Ok ( timezone) => timezone. offset_from_utc_datetime ( & naive) . fix ( ) ,
452
- Err ( _) => * Local :: now ( ) . offset ( ) ,
453
- } ;
420
+ let offset = * Local :: now ( ) . offset ( ) ;
454
421
455
422
DateTime :: < Local > :: from_naive_utc_and_offset ( naive, offset)
456
423
} ,
@@ -480,12 +447,11 @@ mod tests {
480
447
481
448
#[ test]
482
449
fn group_id_name_test ( ) {
483
- struct GroupIDTest < ' a > {
450
+ struct GroupIDTest {
484
451
cell_data : DateCellData ,
485
452
setting_content : String ,
486
453
exp_group_id : String ,
487
454
exp_group_name : String ,
488
- type_option : & ' a DateTypeOption ,
489
455
}
490
456
491
457
let mar_14_2022 = NaiveDateTime :: from_timestamp_opt ( 1647251762 , 0 ) . unwrap ( ) ;
@@ -505,7 +471,6 @@ mod tests {
505
471
let tests = vec ! [
506
472
GroupIDTest {
507
473
cell_data: mar_14_2022_cd. clone( ) ,
508
- type_option: & local_date_type_option,
509
474
setting_content: r#"{"condition": 0, "hide_empty": false}"# . to_string( ) ,
510
475
exp_group_id: "2022/03/01" . to_string( ) ,
511
476
exp_group_name: "Mar 2022" . to_string( ) ,
@@ -516,7 +481,6 @@ mod tests {
516
481
include_time: false ,
517
482
..Default :: default ( )
518
483
} ,
519
- type_option: & local_date_type_option,
520
484
setting_content: r#"{"condition": 0, "hide_empty": false}"# . to_string( ) ,
521
485
exp_group_id: today. format( GROUP_ID_DATE_FORMAT ) . to_string( ) ,
522
486
exp_group_name: "Today" . to_string( ) ,
@@ -527,7 +491,6 @@ mod tests {
527
491
include_time: false ,
528
492
..Default :: default ( )
529
493
} ,
530
- type_option: & local_date_type_option,
531
494
setting_content: r#"{"condition": 0, "hide_empty": false}"# . to_string( ) ,
532
495
exp_group_id: today
533
496
. checked_sub_days( Days :: new( 7 ) )
@@ -538,7 +501,6 @@ mod tests {
538
501
} ,
539
502
GroupIDTest {
540
503
cell_data: mar_14_2022_cd. clone( ) ,
541
- type_option: & local_date_type_option,
542
504
setting_content: r#"{"condition": 1, "hide_empty": false}"# . to_string( ) ,
543
505
exp_group_id: "2022/03/14" . to_string( ) ,
544
506
exp_group_name: "Mar 14, 2022" . to_string( ) ,
@@ -554,21 +516,18 @@ mod tests {
554
516
include_time: false ,
555
517
..Default :: default ( )
556
518
} ,
557
- type_option: & local_date_type_option,
558
519
setting_content: r#"{"condition": 2, "hide_empty": false}"# . to_string( ) ,
559
520
exp_group_id: "2022/03/14" . to_string( ) ,
560
521
exp_group_name: "Week of Mar 14-20 2022" . to_string( ) ,
561
522
} ,
562
523
GroupIDTest {
563
524
cell_data: mar_14_2022_cd. clone( ) ,
564
- type_option: & local_date_type_option,
565
525
setting_content: r#"{"condition": 3, "hide_empty": false}"# . to_string( ) ,
566
526
exp_group_id: "2022/03/01" . to_string( ) ,
567
527
exp_group_name: "Mar 2022" . to_string( ) ,
568
528
} ,
569
529
GroupIDTest {
570
530
cell_data: mar_14_2022_cd,
571
- type_option: & local_date_type_option,
572
531
setting_content: r#"{"condition": 4, "hide_empty": false}"# . to_string( ) ,
573
532
exp_group_id: "2022/01/01" . to_string( ) ,
574
533
exp_group_name: "2022" . to_string( ) ,
@@ -579,7 +538,6 @@ mod tests {
579
538
include_time: false ,
580
539
..Default :: default ( )
581
540
} ,
582
- type_option: & default_date_type_option,
583
541
setting_content: r#"{"condition": 1, "hide_empty": false}"# . to_string( ) ,
584
542
exp_group_id: "2023/06/02" . to_string( ) ,
585
543
exp_group_name: "" . to_string( ) ,
@@ -590,19 +548,18 @@ mod tests {
590
548
include_time: false ,
591
549
..Default :: default ( )
592
550
} ,
593
- type_option: & default_date_type_option,
594
551
setting_content: r#"{"condition": 1, "hide_empty": false}"# . to_string( ) ,
595
552
exp_group_id: "2023/06/03" . to_string( ) ,
596
553
exp_group_name: "" . to_string( ) ,
597
554
} ,
598
555
] ;
599
556
600
557
for ( i, test) in tests. iter ( ) . enumerate ( ) {
601
- let group_id = group_id ( & test. cell_data , test . type_option , & test. setting_content ) ;
558
+ let group_id = group_id ( & test. cell_data , & test. setting_content ) ;
602
559
assert_eq ! ( test. exp_group_id, group_id, "test {}" , i) ;
603
560
604
561
if !test. exp_group_name . is_empty ( ) {
605
- let group_name = group_name_from_id ( & group_id, test . type_option , & test. setting_content ) ;
562
+ let group_name = group_name_from_id ( & group_id, & test. setting_content ) ;
606
563
assert_eq ! ( test. exp_group_name, group_name, "test {}" , i) ;
607
564
}
608
565
}
0 commit comments