MM-26753 Create initial sidebar categories on demand if migration hasn't ran (#14981)
* MM-26753 Change CreateInitialSidebarCategories to only take a user ID * MM-26753 Create initial sidebar categories on demand if migration hasn't ran * Wait for sidebar categories to be loaded in case of replication lag
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
31200fc657
Коммит
302e59a0fe
@@ -2603,8 +2603,8 @@ func (a *App) ClearChannelMembersCache(channelID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) createInitialSidebarCategories(user *model.User, team *model.Team) *model.AppError {
|
||||
nErr := a.Srv().Store.Channel().CreateInitialSidebarCategories(user, team.Id)
|
||||
func (a *App) createInitialSidebarCategories(userId, teamId string) *model.AppError {
|
||||
nErr := a.Srv().Store.Channel().CreateInitialSidebarCategories(userId, teamId)
|
||||
|
||||
if nErr != nil {
|
||||
return model.NewAppError("createInitialSidebarCategories", "app.channel.create_initial_sidebar_categories.internal_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
@@ -2614,7 +2614,45 @@ func (a *App) createInitialSidebarCategories(user *model.User, team *model.Team)
|
||||
}
|
||||
|
||||
func (a *App) GetSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, *model.AppError) {
|
||||
return a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
|
||||
categories, err := a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
|
||||
|
||||
if len(categories.Categories) == 0 && err == nil {
|
||||
// A user must always have categories, so migration must not have happened yet, and we should run it ourselves
|
||||
nErr := a.createInitialSidebarCategories(userId, teamId)
|
||||
if nErr != nil {
|
||||
return nil, nErr
|
||||
}
|
||||
|
||||
categories, err = a.waitForSidebarCategories(userId, teamId)
|
||||
}
|
||||
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// waitForSidebarCategories is used to get a user's sidebar categories after they've been created since there may be
|
||||
// replication lag if any database replicas exist. It will wait until results are available to return them.
|
||||
func (a *App) waitForSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, *model.AppError) {
|
||||
if len(a.Config().SqlSettings.DataSourceReplicas) == 0 {
|
||||
// The categories should be available immediately on a single database
|
||||
return a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
|
||||
}
|
||||
|
||||
now := model.GetMillis()
|
||||
|
||||
for model.GetMillis()-now < 12000 {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
categories, err := a.Srv().Store.Channel().GetSidebarCategories(userId, teamId)
|
||||
|
||||
if err != nil || len(categories.Categories) > 0 {
|
||||
// We've found something, so return
|
||||
return categories, err
|
||||
}
|
||||
}
|
||||
|
||||
mlog.Error("waitForSidebarCategories giving up", mlog.String("user_id", userId), mlog.String("team_id", teamId))
|
||||
|
||||
return &model.OrderedSidebarCategories{}, nil
|
||||
}
|
||||
|
||||
func (a *App) GetSidebarCategoryOrder(userId, teamId string) ([]string, *model.AppError) {
|
||||
|
||||
@@ -1858,3 +1858,42 @@ func TestSidebarCategory(t *testing.T) {
|
||||
require.Equal(t, catOrder[1], createdCategory.Id, "the newly created category should be after favorites")
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetSidebarCategories(t *testing.T) {
|
||||
t.Run("should return the sidebar categories for the given user/team", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
_, err := th.App.CreateSidebarCategory(th.BasicUser.Id, th.BasicTeam.Id, &model.SidebarCategoryWithChannels{
|
||||
SidebarCategory: model.SidebarCategory{
|
||||
UserId: th.BasicUser.Id,
|
||||
TeamId: th.BasicTeam.Id,
|
||||
DisplayName: "new category",
|
||||
},
|
||||
})
|
||||
require.Nil(t, err)
|
||||
|
||||
categories, err := th.App.GetSidebarCategories(th.BasicUser.Id, th.BasicTeam.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, categories.Categories, 4)
|
||||
})
|
||||
|
||||
t.Run("should create the initial categories even if migration hasn't ran yet", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
// Manually add the user to the team without going through the app layer to simulate a pre-existing user/team
|
||||
// relationship that hasn't been migrated yet
|
||||
team := th.CreateTeam()
|
||||
_, err := th.App.Srv().Store.Team().SaveMember(&model.TeamMember{
|
||||
TeamId: team.Id,
|
||||
UserId: th.BasicUser.Id,
|
||||
SchemeUser: true,
|
||||
}, 100)
|
||||
require.Nil(t, err)
|
||||
|
||||
categories, err := th.App.GetSidebarCategories(th.BasicUser.Id, team.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.Len(t, categories.Categories, 3)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -653,7 +653,7 @@ func (a *App) JoinUserToTeam(team *model.Team, user *model.User, userRequestorId
|
||||
return err
|
||||
}
|
||||
|
||||
if err := a.createInitialSidebarCategories(user, team); err != nil {
|
||||
if err := a.createInitialSidebarCategories(user.Id, team.Id); err != nil {
|
||||
mlog.Error(
|
||||
"Encountered an issue creating default sidebar categories.",
|
||||
mlog.String("user_id", user.Id),
|
||||
|
||||
Ссылка в новой задаче
Block a user