[MM-46694] A/B Test: welcome post (#20926)

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Julien Tant
2022-10-13 14:23:22 -07:00
коммит произвёл GitHub
родитель a6a44b5824
Коммит 3747d99806
14 изменённых файлов: 230 добавлений и 0 удалений

Просмотреть файл

@@ -871,6 +871,7 @@ type AppIface interface {
InviteNewUsersToTeam(emailList []string, teamID, senderId string) *model.AppError
InviteNewUsersToTeamGracefully(memberInvite *model.MemberInvite, teamID, senderId string, reminderInterval string) ([]*model.EmailInviteWithError, *model.AppError)
IsCRTEnabledForUser(c request.CTX, userID string) bool
IsFirstAdmin(user *model.User) bool
IsFirstUserAccount() bool
IsLeader() bool
IsPasswordValid(password string) *model.AppError

Просмотреть файл

@@ -12,6 +12,7 @@ import (
"strings"
"time"
"github.com/mattermost/logr/v2"
"github.com/mattermost/mattermost-server/v6/app/request"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
@@ -130,6 +131,35 @@ func (a *App) JoinDefaultChannels(c request.CTX, teamID string, user *model.User
message.Add("user_id", user.Id)
message.Add("team_id", channel.TeamId)
a.Publish(message)
// A/B Test on the welcome post
if a.Config().FeatureFlags.SendWelcomePost && channelName == model.DefaultChannelName {
nbTeams, err := a.Srv().Store().Team().AnalyticsTeamCount(&model.TeamSearch{
IncludeDeleted: model.NewBool(true),
})
if err != nil {
c.Logger().Warn("unable to get number of teams", logr.Err(err))
return nil
}
if nbTeams == 1 && a.IsFirstAdmin(user) {
// Post the welcome message
if _, err := a.CreatePost(c, &model.Post{
ChannelId: channel.Id,
Type: model.PostTypeWelcomePost,
UserId: user.Id,
}, channel, false, false); err != nil {
c.Logger().Warn("unable to post welcome message", logr.Err(err))
return nil
}
ts := a.Srv().GetTelemetryService()
if ts != nil {
ts.SendTelemetry("welcome-message-sent", map[string]any{
"category": "growth",
})
}
}
}
}
if nErr != nil {

Просмотреть файл

@@ -11626,6 +11626,23 @@ func (a *OpenTracingAppLayer) IsCRTEnabledForUser(c request.CTX, userID string)
return resultVar0
}
func (a *OpenTracingAppLayer) IsFirstAdmin(user *model.User) bool {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.IsFirstAdmin")
a.ctx = newCtx
a.app.Srv().Store().SetContext(newCtx)
defer func() {
a.app.Srv().Store().SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.IsFirstAdmin(user)
return resultVar0
}
func (a *OpenTracingAppLayer) IsFirstUserAccount() bool {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.IsFirstUserAccount")

Просмотреть файл

@@ -209,6 +209,19 @@ func (a *App) IsFirstUserAccount() bool {
return a.ch.srv.platform.IsFirstUserAccount()
}
func (a *App) IsFirstAdmin(user *model.User) bool {
if !user.IsSystemAdmin() {
return false
}
adminID, err := a.Srv().Store().User().GetFirstSystemAdminID()
if err != nil {
return false
}
return adminID == user.Id
}
// CreateUser creates a user and sets several fields of the returned User struct to
// their zero values.
func (a *App) CreateUser(c request.CTX, user *model.User) (*model.User, *model.AppError) {

Просмотреть файл

@@ -1801,3 +1801,54 @@ func TestCreateUserWithInitialPreferences(t *testing.T) {
assert.Equal(t, "false", recommendedNextStepsPref[0].Value)
})
}
func TestIsFirstAdmin(t *testing.T) {
t.Run("should return false if user is not sysadmin", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
Id := model.NewId()
isFirstAdmin := th.App.IsFirstAdmin(&model.User{
Id: Id,
Roles: model.SystemUserRoleId,
})
require.False(t, isFirstAdmin)
})
t.Run("should return false if user is sysadmin but not the first one", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
Id := model.NewId()
mockUserStore := storemocks.UserStore{}
mockUserStore.On("GetFirstSystemAdminID").Return(model.NewId(), nil)
mockStore := th.App.Srv().Store().(*storemocks.Store)
mockStore.On("User").Return(&mockUserStore)
isFirstAdmin := th.App.IsFirstAdmin(&model.User{
Id: Id,
Roles: model.SystemAdminRoleId,
})
require.False(t, isFirstAdmin)
})
t.Run("should return true if user is sysadmin and the first one", func(t *testing.T) {
th := SetupWithStoreMock(t)
defer th.TearDown()
Id := model.NewId()
mockStore := th.App.Srv().Store().(*storemocks.Store)
mockUserStore := storemocks.UserStore{}
mockUserStore.On("GetFirstSystemAdminID").Return(Id, nil)
mockStore.On("User").Return(&mockUserStore)
isFirstAdmin := th.App.IsFirstAdmin(&model.User{
Id: Id,
Roles: model.SystemAdminRoleId,
})
require.True(t, isFirstAdmin)
})
}