@@ -436,46 +436,6 @@ func (s SqlChannelStore) createIndexesIfNotExists() {
436
436
s .CreateIndexIfNotExists ("idx_channels_scheme_id" , "Channels" , "SchemeId" )
437
437
}
438
438
439
- // MigrateSidebarCategories creates 3 initial categories for all existing user/team pairs
440
- // **IMPORTANT** This function should only be called from the migration task and shouldn't be used by itself
441
- func (s SqlChannelStore ) MigrateSidebarCategories (fromTeamId , fromUserId string ) (map [string ]interface {}, error ) {
442
- var userTeamMap []struct {
443
- UserId string
444
- TeamId string
445
- }
446
-
447
- transaction , err := s .GetMaster ().Begin ()
448
- if err != nil {
449
- return nil , err
450
- }
451
-
452
- defer finalizeTransaction (transaction )
453
-
454
- if _ , err := transaction .Select (& userTeamMap , "SELECT TeamId, UserId FROM TeamMembers LEFT JOIN Users ON Users.Id=UserId WHERE (TeamId, UserId) > (:FromTeamId, :FromUserId) ORDER BY TeamId, UserId LIMIT 100" , map [string ]interface {}{"FromTeamId" : fromTeamId , "FromUserId" : fromUserId }); err != nil {
455
- return nil , err
456
- }
457
-
458
- if len (userTeamMap ) == 0 {
459
- // No more team members in query result means that the migration has finished.
460
- return nil , nil
461
- }
462
-
463
- for _ , u := range userTeamMap {
464
- if err := s .createInitialSidebarCategoriesT (transaction , u .UserId , u .TeamId ); err != nil {
465
- return nil , err
466
- }
467
- }
468
- if err := transaction .Commit (); err != nil {
469
- return nil , err
470
- }
471
-
472
- data := make (map [string ]interface {})
473
- data ["TeamId" ] = userTeamMap [len (userTeamMap )- 1 ].TeamId
474
- data ["UserId" ] = userTeamMap [len (userTeamMap )- 1 ].UserId
475
-
476
- return data , nil
477
- }
478
-
479
439
func (s SqlChannelStore ) CreateInitialSidebarCategories (userId , teamId string ) error {
480
440
transaction , err := s .GetMaster ().Begin ()
481
441
if err != nil {
@@ -510,20 +470,22 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
510
470
return errors .Wrap (err , "createInitialSidebarCategoriesT: failed to select existing categories" )
511
471
}
512
472
513
- hasCategoryOfType := func (categoryType model.SidebarCategoryType ) bool {
514
- for _ , existingType := range existingTypes {
515
- if categoryType == existingType {
516
- return true
517
- }
518
- }
519
-
520
- return false
473
+ hasCategoryOfType := make (map [model.SidebarCategoryType ]bool , len (existingTypes ))
474
+ for _ , existingType := range existingTypes {
475
+ hasCategoryOfType [existingType ] = true
521
476
}
522
477
523
- if ! hasCategoryOfType (model .SidebarCategoryFavorites ) {
478
+ if ! hasCategoryOfType [model .SidebarCategoryFavorites ] {
479
+ favoritesCategoryId := model .NewId ()
480
+
481
+ // Create the SidebarChannels first since there's more opportunity for something to fail here
482
+ if err := s .migrateFavoritesToSidebarT (transaction , userId , teamId , favoritesCategoryId ); err != nil {
483
+ return errors .Wrap (err , "createInitialSidebarCategoriesT: failed to migrate favorites to sidebar" )
484
+ }
485
+
524
486
if err := transaction .Insert (& model.SidebarCategory {
525
487
DisplayName : "Favorites" , // This will be retranslated by the client into the user's locale
526
- Id : model . NewId () ,
488
+ Id : favoritesCategoryId ,
527
489
UserId : userId ,
528
490
TeamId : teamId ,
529
491
Sorting : model .SidebarCategorySortDefault ,
@@ -534,7 +496,7 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
534
496
}
535
497
}
536
498
537
- if ! hasCategoryOfType ( model .SidebarCategoryChannels ) {
499
+ if ! hasCategoryOfType [ model .SidebarCategoryChannels ] {
538
500
if err := transaction .Insert (& model.SidebarCategory {
539
501
DisplayName : "Channels" , // This will be retranslateed by the client into the user's locale
540
502
Id : model .NewId (),
@@ -548,7 +510,7 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *gorp.Trans
548
510
}
549
511
}
550
512
551
- if ! hasCategoryOfType ( model .SidebarCategoryDirectMessages ) {
513
+ if ! hasCategoryOfType [ model .SidebarCategoryDirectMessages ] {
552
514
if err := transaction .Insert (& model.SidebarCategory {
553
515
DisplayName : "Direct Messages" , // This will be retranslateed by the client into the user's locale
554
516
Id : model .NewId (),
@@ -595,6 +557,45 @@ func (s SqlChannelStore) migrateMembershipToSidebar(transaction *gorp.Transactio
595
557
return memberships , nil
596
558
}
597
559
560
+ func (s SqlChannelStore ) migrateFavoritesToSidebarT (transaction * gorp.Transaction , userId , teamId , favoritesCategoryId string ) error {
561
+ favoritesQuery , favoritesParams , _ := s .getQueryBuilder ().
562
+ Select ("Preferences.Name" ).
563
+ From ("Preferences" ).
564
+ Join ("Channels on Preferences.Name = Channels.Id" ).
565
+ Join ("ChannelMembers on Preferences.Name = ChannelMembers.ChannelId and Preferences.UserId = ChannelMembers.UserId" ).
566
+ Where (sq.Eq {
567
+ "Preferences.UserId" : userId ,
568
+ "Preferences.Category" : model .PREFERENCE_CATEGORY_FAVORITE_CHANNEL ,
569
+ "Preferences.Value" : "true" ,
570
+ }).
571
+ Where (sq.Or {
572
+ sq.Eq {"Channels.TeamId" : teamId },
573
+ sq.Eq {"Channels.TeamId" : "" },
574
+ }).
575
+ OrderBy (
576
+ "Channels.DisplayName" ,
577
+ "Channels.Name ASC" ,
578
+ ).ToSql ()
579
+
580
+ var favoriteChannelIds []string
581
+ if _ , err := transaction .Select (& favoriteChannelIds , favoritesQuery , favoritesParams ... ); err != nil {
582
+ return errors .Wrap (err , "migrateFavoritesToSidebarT: unable to get favorite channel IDs" )
583
+ }
584
+
585
+ for i , channelId := range favoriteChannelIds {
586
+ if err := transaction .Insert (& model.SidebarChannel {
587
+ ChannelId : channelId ,
588
+ CategoryId : favoritesCategoryId ,
589
+ UserId : userId ,
590
+ SortOrder : int64 (i * model .MinimalSidebarSortDistance ),
591
+ }); err != nil {
592
+ return errors .Wrap (err , "migrateFavoritesToSidebarT: unable to insert SidebarChannel" )
593
+ }
594
+ }
595
+
596
+ return nil
597
+ }
598
+
598
599
// MigrateFavoritesToSidebarChannels populates the SidebarChannels table by analyzing existing user preferences for favorites
599
600
// **IMPORTANT** This function should only be called from the migration task and shouldn't be used by itself
600
601
func (s SqlChannelStore ) MigrateFavoritesToSidebarChannels (lastUserId string , runningOrder int64 ) (map [string ]interface {}, error ) {
0 commit comments