UserStore migration (#15563)
* Migration completed * Fix tests * Fix tests * Fix tests * Suggestions * Trigger CI * Suggestions * Merge with master * Trigger CI Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
d51d843fcd
Коммит
96f1739f8f
@@ -21,7 +21,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
var systemUserCount int64
|
||||
systemUserCount, err := a.Srv().Store.User().Count(model.UserCountOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetAnalytics", "app.user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if systemUserCount > int64(*a.Config().AnalyticsSettings.MaxUsersForStatistics) {
|
||||
@@ -62,14 +62,14 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
userInactiveChan = make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
count, err2 := a.Srv().Store.User().AnalyticsGetInactiveUsersCount()
|
||||
userInactiveChan <- store.StoreResult{Data: count, Err: err2}
|
||||
userInactiveChan <- store.StoreResult{Data: count, NErr: err2}
|
||||
close(userInactiveChan)
|
||||
}()
|
||||
} else {
|
||||
userChan = make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
count, err2 := a.Srv().Store.User().Count(model.UserCountOptions{TeamId: teamId})
|
||||
userChan <- store.StoreResult{Data: count, Err: err2}
|
||||
userChan <- store.StoreResult{Data: count, NErr: err2}
|
||||
close(userChan)
|
||||
}()
|
||||
}
|
||||
@@ -94,14 +94,14 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
dailyActiveChan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
dailyActive, err2 := a.Srv().Store.User().AnalyticsActiveCount(DAY_MILLISECONDS, model.UserCountOptions{IncludeBotAccounts: false, IncludeDeleted: false})
|
||||
dailyActiveChan <- store.StoreResult{Data: dailyActive, Err: err2}
|
||||
dailyActiveChan <- store.StoreResult{Data: dailyActive, NErr: err2}
|
||||
close(dailyActiveChan)
|
||||
}()
|
||||
|
||||
monthlyActiveChan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
monthlyActive, err2 := a.Srv().Store.User().AnalyticsActiveCount(MONTH_MILLISECONDS, model.UserCountOptions{IncludeBotAccounts: false, IncludeDeleted: false})
|
||||
monthlyActiveChan <- store.StoreResult{Data: monthlyActive, Err: err2}
|
||||
monthlyActiveChan <- store.StoreResult{Data: monthlyActive, NErr: err2}
|
||||
close(monthlyActiveChan)
|
||||
}()
|
||||
|
||||
@@ -131,8 +131,8 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
rows[3].Value = float64(systemUserCount)
|
||||
} else {
|
||||
r = <-userChan
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
if r.NErr != nil {
|
||||
return nil, model.NewAppError("GetAnalytics", "app.user.get_total_users_count.app_error", nil, r.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
rows[3].Value = float64(r.Data.(int64))
|
||||
}
|
||||
@@ -141,8 +141,8 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
rows[10].Value = -1
|
||||
} else {
|
||||
r = <-userInactiveChan
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
if r.NErr != nil {
|
||||
return nil, model.NewAppError("GetAnalytics", "app.user.analytics_get_inactive_users_count.app_error", nil, r.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
rows[10].Value = float64(r.Data.(int64))
|
||||
}
|
||||
@@ -181,14 +181,14 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
}
|
||||
|
||||
r = <-dailyActiveChan
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
if r.NErr != nil {
|
||||
return nil, model.NewAppError("GetAnalytics", "app.user.analytics_daily_active_users.app_error", nil, r.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
rows[8].Value = float64(r.Data.(int64))
|
||||
|
||||
r = <-monthlyActiveChan
|
||||
if r.Err != nil {
|
||||
return nil, r.Err
|
||||
if r.NErr != nil {
|
||||
return nil, model.NewAppError("GetAnalytics", "app.user.analytics_daily_active_users.app_error", nil, r.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
rows[9].Value = float64(r.Data.(int64))
|
||||
|
||||
@@ -325,7 +325,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
|
||||
r = <-commandChan
|
||||
if r.NErr != nil {
|
||||
return nil, model.NewAppError("GetAnalytics", "app.analytics.getanalytics.internal_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, model.NewAppError("GetAnalytics", "app.analytics.getanalytics.internal_error", nil, r.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
rows[4].Value = float64(r.Data.(int64))
|
||||
|
||||
@@ -344,7 +344,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
|
||||
func (a *App) GetRecentlyActiveUsersForTeam(teamId string) (map[string]*model.User, *model.AppError) {
|
||||
users, err := a.Srv().Store.User().GetRecentlyActiveUsersForTeam(teamId, 0, 100, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetRecentlyActiveUsersForTeam", "app.user.get_recently_active_users.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
userMap := make(map[string]*model.User)
|
||||
@@ -359,7 +359,7 @@ func (a *App) GetRecentlyActiveUsersForTeam(teamId string) (map[string]*model.Us
|
||||
func (a *App) GetRecentlyActiveUsersForTeamPage(teamId string, page, perPage int, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
|
||||
users, err := a.Srv().Store.User().GetRecentlyActiveUsersForTeam(teamId, page*perPage, perPage, viewRestrictions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetRecentlyActiveUsersForTeamPage", "app.user.get_recently_active_users.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
@@ -368,7 +368,7 @@ func (a *App) GetRecentlyActiveUsersForTeamPage(teamId string, page, perPage int
|
||||
func (a *App) GetNewUsersForTeamPage(teamId string, page, perPage int, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
|
||||
users, err := a.Srv().Store.User().GetNewUsersForTeam(teamId, page*perPage, perPage, viewRestrictions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetNewUsersForTeamPage", "app.user.get_new_users.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
|
||||
@@ -52,7 +52,7 @@ func (a *App) CheckPasswordAndAllCriteria(user *model.User, password string, mfa
|
||||
|
||||
if err := a.checkUserPassword(user, password); err != nil {
|
||||
if passErr := a.Srv().Store.User().UpdateFailedPasswordAttempts(user.Id, user.FailedAttempts+1); passErr != nil {
|
||||
return passErr
|
||||
return model.NewAppError("CheckPasswordAndAllCriteria", "app.user.update_failed_pwd_attempts.app_error", nil, passErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
@@ -65,7 +65,7 @@ func (a *App) CheckPasswordAndAllCriteria(user *model.User, password string, mfa
|
||||
// about the MFA state of the user in question
|
||||
if mfaToken != "" {
|
||||
if passErr := a.Srv().Store.User().UpdateFailedPasswordAttempts(user.Id, user.FailedAttempts+1); passErr != nil {
|
||||
return passErr
|
||||
return model.NewAppError("CheckPasswordAndAllCriteria", "app.user.update_failed_pwd_attempts.app_error", nil, passErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func (a *App) CheckPasswordAndAllCriteria(user *model.User, password string, mfa
|
||||
}
|
||||
|
||||
if passErr := a.Srv().Store.User().UpdateFailedPasswordAttempts(user.Id, 0); passErr != nil {
|
||||
return passErr
|
||||
return model.NewAppError("CheckPasswordAndAllCriteria", "app.user.update_failed_pwd_attempts.app_error", nil, passErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
@@ -95,7 +95,7 @@ func (a *App) DoubleCheckPassword(user *model.User, password string) *model.AppE
|
||||
|
||||
if err := a.checkUserPassword(user, password); err != nil {
|
||||
if passErr := a.Srv().Store.User().UpdateFailedPasswordAttempts(user.Id, user.FailedAttempts+1); passErr != nil {
|
||||
return passErr
|
||||
return model.NewAppError("DoubleCheckPassword", "app.user.update_failed_pwd_attempts.app_error", nil, passErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
@@ -104,7 +104,7 @@ func (a *App) DoubleCheckPassword(user *model.User, password string) *model.AppE
|
||||
}
|
||||
|
||||
if passErr := a.Srv().Store.User().UpdateFailedPasswordAttempts(user.Id, 0); passErr != nil {
|
||||
return passErr
|
||||
return model.NewAppError("DoubleCheckPassword", "app.user.update_failed_pwd_attempts.app_error", nil, passErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
86
app/bot.go
86
app/bot.go
@@ -18,9 +18,18 @@ import (
|
||||
|
||||
// CreateBot creates the given bot and corresponding user.
|
||||
func (a *App) CreateBot(bot *model.Bot) (*model.Bot, *model.AppError) {
|
||||
user, err := a.Srv().Store.User().Save(model.UserFromBot(bot))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Save(model.UserFromBot(bot))
|
||||
if nErr != nil {
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(nErr, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(nErr, &invErr):
|
||||
return nil, model.NewAppError("CreateBot", "app.user.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("CreateBot", "app.user.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
bot.UserId = user.Id
|
||||
|
||||
@@ -38,9 +47,10 @@ func (a *App) CreateBot(bot *model.Bot) (*model.Bot, *model.AppError) {
|
||||
|
||||
// Get the owner of the bot, if one exists. If not, don't send a message
|
||||
ownerUser, err := a.Srv().Store.User().Get(bot.OwnerId)
|
||||
if err != nil && err.Id != store.MISSING_ACCOUNT_ERROR {
|
||||
var nfErr *store.ErrNotFound
|
||||
if err != nil && !errors.As(err, &nfErr) {
|
||||
mlog.Error(err.Error())
|
||||
return nil, err
|
||||
return nil, model.NewAppError("CreateBot", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
} else if ownerUser != nil {
|
||||
// Send a message to the bot's creator to inform them that the bot needs to be added
|
||||
// to a team and channel after it's created
|
||||
@@ -74,10 +84,19 @@ func (a *App) getOrCreateWarnMetricsBot(botDef *model.Bot) (*model.Bot, *model.A
|
||||
}
|
||||
|
||||
// cannot find this bot user, save the user
|
||||
user, err := a.Srv().Store.User().Save(model.UserFromBot(botDef))
|
||||
if err != nil {
|
||||
mlog.Error(err.Error())
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Save(model.UserFromBot(botDef))
|
||||
if nErr != nil {
|
||||
mlog.Error(nErr.Error())
|
||||
var appError *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(nErr, &appError):
|
||||
return nil, appError
|
||||
case errors.As(nErr, &invErr):
|
||||
return nil, model.NewAppError("getOrCreateWarnMetricsBot", "app.user.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("getOrCreateWarnMetricsBot", "app.user.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
botDef.UserId = user.Id
|
||||
|
||||
@@ -119,9 +138,15 @@ func (a *App) PatchBot(botUserId string, botPatch *model.BotPatch) (*model.Bot,
|
||||
|
||||
bot.Patch(botPatch)
|
||||
|
||||
user, err := a.Srv().Store.User().Get(botUserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(botUserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("PatchBot", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("PatchBot", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
patchedUser := model.UserFromBot(bot)
|
||||
@@ -130,16 +155,25 @@ func (a *App) PatchBot(botUserId string, botPatch *model.BotPatch) (*model.Bot,
|
||||
user.Email = patchedUser.Email
|
||||
user.FirstName = patchedUser.FirstName
|
||||
|
||||
userUpdate, err := a.Srv().Store.User().Update(user, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
userUpdate, nErr := a.Srv().Store.User().Update(user, true)
|
||||
if nErr != nil {
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(nErr, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(nErr, &invErr):
|
||||
return nil, model.NewAppError("PatchBot", "app.user.update.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("PatchBot", "app.user.update.finding.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
|
||||
ruser := userUpdate.New
|
||||
a.sendUpdatedUserEvent(*ruser)
|
||||
|
||||
bot, nErr := a.Srv().Store.Bot().Update(bot)
|
||||
bot, nErr = a.Srv().Store.Bot().Update(bot)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
var appErr *model.AppError
|
||||
@@ -181,12 +215,18 @@ func (a *App) GetBots(options *model.BotGetOptions) (model.BotList, *model.AppEr
|
||||
|
||||
// UpdateBotActive marks a bot as active or inactive, along with its corresponding user.
|
||||
func (a *App) UpdateBotActive(botUserId string, active bool) (*model.Bot, *model.AppError) {
|
||||
user, err := a.Srv().Store.User().Get(botUserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(botUserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("PatchBot", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("PatchBot", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = a.UpdateActive(user, active); err != nil {
|
||||
if _, err := a.UpdateActive(user, active); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -242,7 +282,7 @@ func (a *App) PermanentDeleteBot(botUserId string) *model.AppError {
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.User().PermanentDelete(botUserId); err != nil {
|
||||
return err
|
||||
return model.NewAppError("PermanentDeleteBot", "app.user.permanent_delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -499,8 +539,8 @@ func (a *App) DeleteBotIconImage(botUserId string) *model.AppError {
|
||||
return model.NewAppError("DeleteBotIconImage", "api.bot.delete_bot_icon_image.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err = a.Srv().Store.User().UpdateLastPictureUpdate(botUserId); err != nil {
|
||||
mlog.Error(err.Error())
|
||||
if nErr := a.Srv().Store.User().UpdateLastPictureUpdate(botUserId); nErr != nil {
|
||||
mlog.Error(nErr.Error())
|
||||
}
|
||||
|
||||
bot.LastIconUpdate = int64(0)
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestCreateBot(t *testing.T) {
|
||||
OwnerId: th.BasicUser.Id,
|
||||
})
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "store.sql_user.save.username_exists.app_error", err.Id)
|
||||
require.Equal(t, "app.user.save.existing.app_error", err.Id)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -201,7 +201,7 @@ func TestPatchBot(t *testing.T) {
|
||||
|
||||
_, err = th.App.PatchBot(bot.UserId, botPatch)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "store.sql_user.update.username_taken.app_error", err.Id)
|
||||
require.Equal(t, "app.user.update.find.app_error", err.Id)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ func TestUpdateBotActive(t *testing.T) {
|
||||
|
||||
_, err := th.App.UpdateBotActive(model.NewId(), false)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "store.sql_user.missing_account.const", err.Id)
|
||||
require.Equal(t, "app.user.missing_account.const", err.Id)
|
||||
})
|
||||
|
||||
t.Run("disable/enable bot", func(t *testing.T) {
|
||||
|
||||
107
app/channel.go
107
app/channel.go
@@ -65,16 +65,21 @@ func (a *App) DefaultChannelNames() []string {
|
||||
|
||||
func (a *App) JoinDefaultChannels(teamId string, user *model.User, shouldBeAdmin bool, userRequestorId string) *model.AppError {
|
||||
var requestor *model.User
|
||||
var nErr error
|
||||
if userRequestorId != "" {
|
||||
var err *model.AppError
|
||||
requestor, err = a.Srv().Store.User().Get(userRequestorId)
|
||||
if err != nil {
|
||||
return err
|
||||
requestor, nErr = a.Srv().Store.User().Get(userRequestorId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return model.NewAppError("JoinDefaultChannels", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("JoinDefaultChannels", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var err *model.AppError
|
||||
var nErr error
|
||||
for _, channelName := range a.DefaultChannelNames() {
|
||||
channel, channelErr := a.Srv().Store.Channel().GetByName(teamId, channelName, true)
|
||||
if channelErr != nil {
|
||||
@@ -256,9 +261,15 @@ func (a *App) CreateChannel(channel *model.Channel, addMember bool) (*model.Chan
|
||||
}
|
||||
|
||||
if addMember {
|
||||
user, err := a.Srv().Store.User().Get(channel.CreatorId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(channel.CreatorId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("CreateChannel", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("CreateChannel", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
cm := &model.ChannelMember{
|
||||
@@ -352,23 +363,23 @@ func (a *App) createDirectChannel(userId string, otherUserId string) (*model.Cha
|
||||
uc2 := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
uc1 <- store.StoreResult{Data: user, Err: err}
|
||||
uc1 <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uc1)
|
||||
}()
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(otherUserId)
|
||||
uc2 <- store.StoreResult{Data: user, Err: err}
|
||||
uc2 <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uc2)
|
||||
}()
|
||||
|
||||
result := <-uc1
|
||||
if result.Err != nil {
|
||||
if result.NErr != nil {
|
||||
return nil, model.NewAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, userId, http.StatusBadRequest)
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
result = <-uc2
|
||||
if result.Err != nil {
|
||||
if result.NErr != nil {
|
||||
return nil, model.NewAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, otherUserId, http.StatusBadRequest)
|
||||
}
|
||||
otherUser := result.Data.(*model.User)
|
||||
@@ -478,7 +489,7 @@ func (a *App) createGroupChannel(userIds []string, creatorId string) (*model.Cha
|
||||
|
||||
users, err := a.Srv().Store.User().GetProfileByIds(userIds, nil, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("createGroupChannel", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if len(users) != len(userIds) {
|
||||
@@ -558,16 +569,16 @@ func (a *App) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError
|
||||
|
||||
users, err := a.Srv().Store.User().GetProfileByIds(userIds, nil, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetGroupChannel", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if len(users) != len(userIds) {
|
||||
return nil, model.NewAppError("GetGroupChannel", "api.channel.create_group.bad_user.app_error", nil, "user_ids="+model.ArrayToJson(userIds), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
channel, err := a.GetChannelByName(model.GetGroupNameFromUserIds(userIds), "", true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
channel, appErr := a.GetChannelByName(model.GetGroupNameFromUserIds(userIds), "", true)
|
||||
if appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
@@ -702,9 +713,15 @@ func (a *App) RestoreChannel(channel *model.Channel, userId string) (*model.Chan
|
||||
message.Add("channel_id", channel.Id)
|
||||
a.Publish(message)
|
||||
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(userId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("RestoreChannel", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("RestoreChannel", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if user != nil {
|
||||
@@ -1209,10 +1226,16 @@ func (a *App) DeleteChannel(channel *model.Channel, userId string) *model.AppErr
|
||||
|
||||
var user *model.User
|
||||
if userId != "" {
|
||||
var err *model.AppError
|
||||
user, err = a.Srv().Store.User().Get(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
var nErr error
|
||||
user, nErr = a.Srv().Store.User().Get(userId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return model.NewAppError("DeleteChannel", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("DeleteChannel", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1876,7 +1899,7 @@ func (a *App) JoinChannel(channel *model.Channel, userId string) *model.AppError
|
||||
memberChan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
userChan <- store.StoreResult{Data: user, Err: err}
|
||||
userChan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(userChan)
|
||||
}()
|
||||
go func() {
|
||||
@@ -1886,8 +1909,14 @@ func (a *App) JoinChannel(channel *model.Channel, userId string) *model.AppError
|
||||
}()
|
||||
|
||||
uresult := <-userChan
|
||||
if uresult.Err != nil {
|
||||
return uresult.Err
|
||||
if uresult.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(uresult.NErr, &nfErr):
|
||||
return model.NewAppError("CreateChannel", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("CreateChannel", "app.user.get.app_error", nil, uresult.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
mresult := <-memberChan
|
||||
@@ -1979,7 +2008,7 @@ func (a *App) LeaveChannel(channelId string, userId string) *model.AppError {
|
||||
uc := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
uc <- store.StoreResult{Data: user, Err: err}
|
||||
uc <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uc)
|
||||
}()
|
||||
|
||||
@@ -2001,8 +2030,14 @@ func (a *App) LeaveChannel(channelId string, userId string) *model.AppError {
|
||||
}
|
||||
}
|
||||
uresult := <-uc
|
||||
if uresult.Err != nil {
|
||||
return uresult.Err
|
||||
if uresult.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(uresult.NErr, &nfErr):
|
||||
return model.NewAppError("LeaveChannel", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("LeaveChannel", "app.user.get.app_error", nil, uresult.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
ccresult := <-mcc
|
||||
if ccresult.NErr != nil {
|
||||
@@ -2134,9 +2169,15 @@ func (a *App) postRemoveFromChannelMessage(removerUserId string, removedUser *mo
|
||||
}
|
||||
|
||||
func (a *App) removeUserFromChannel(userIdToRemove string, removerUserId string, channel *model.Channel) *model.AppError {
|
||||
user, err := a.Srv().Store.User().Get(userIdToRemove)
|
||||
if err != nil {
|
||||
return err
|
||||
user, nErr := a.Srv().Store.User().Get(userIdToRemove)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return model.NewAppError("removeUserFromChannel", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("removeUserFromChannel", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
isGuest := user.IsGuest()
|
||||
|
||||
|
||||
@@ -1859,7 +1859,7 @@ func TestMarkChannelsAsViewedPanic(t *testing.T) {
|
||||
|
||||
mockStore := th.App.Srv().Store.(*mocks.Store)
|
||||
mockUserStore := mocks.UserStore{}
|
||||
mockUserStore.On("Get", "userID").Return(nil, model.NewAppError("SqlUserStore.Get", "store.sql_user.get.app_error", nil, "user_id=userID", http.StatusInternalServerError))
|
||||
mockUserStore.On("Get", "userID").Return(nil, model.NewAppError("SqlUserStore.Get", "app.user.get.app_error", nil, "user_id=userID", http.StatusInternalServerError))
|
||||
mockChannelStore := mocks.ChannelStore{}
|
||||
mockChannelStore.On("Get", "channelID", true).Return(&model.Channel{}, nil)
|
||||
mockChannelStore.On("GetMember", "channelID", "userID").Return(&model.ChannelMember{
|
||||
|
||||
@@ -231,28 +231,29 @@ func (a *App) MentionsToTeamMembers(message, teamId string) model.UserMentionMap
|
||||
wg.Add(1)
|
||||
go func(mention string) {
|
||||
defer wg.Done()
|
||||
user, err := a.Srv().Store.User().GetByUsername(mention)
|
||||
user, nErr := a.Srv().Store.User().GetByUsername(mention)
|
||||
|
||||
if err != nil && err.StatusCode != http.StatusNotFound {
|
||||
mlog.Warn("Failed to retrieve user @"+mention, mlog.Err(err))
|
||||
var nfErr *store.ErrNotFound
|
||||
if nErr != nil && !errors.As(nErr, &nfErr) {
|
||||
mlog.Warn("Failed to retrieve user @"+mention, mlog.Err(nErr))
|
||||
return
|
||||
}
|
||||
|
||||
// If it's a http.StatusNotFound error, check for usernames in substrings
|
||||
// without trailing punctuation
|
||||
if err != nil {
|
||||
if nErr != nil {
|
||||
trimmed, ok := model.TrimUsernameSpecialChar(mention)
|
||||
for ; ok; trimmed, ok = model.TrimUsernameSpecialChar(trimmed) {
|
||||
userFromTrimmed, userErr := a.Srv().Store.User().GetByUsername(trimmed)
|
||||
if userErr != nil && err.StatusCode != http.StatusNotFound {
|
||||
userFromTrimmed, nErr := a.Srv().Store.User().GetByUsername(trimmed)
|
||||
if nErr != nil && !errors.As(nErr, &nfErr) {
|
||||
return
|
||||
}
|
||||
|
||||
if userErr != nil {
|
||||
if nErr != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = a.GetTeamMember(teamId, userFromTrimmed.Id)
|
||||
_, err := a.GetTeamMember(teamId, userFromTrimmed.Id)
|
||||
if err != nil {
|
||||
// The user is not in the team, so we should ignore it
|
||||
return
|
||||
@@ -265,7 +266,7 @@ func (a *App) MentionsToTeamMembers(message, teamId string) model.UserMentionMap
|
||||
return
|
||||
}
|
||||
|
||||
_, err = a.GetTeamMember(teamId, user.Id)
|
||||
_, err := a.GetTeamMember(teamId, user.Id)
|
||||
if err != nil {
|
||||
// The user is not in the team, so we should ignore it
|
||||
return
|
||||
@@ -367,7 +368,7 @@ func (a *App) tryExecuteCustomCommand(args *model.CommandArgs, trigger string, m
|
||||
userChan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(args.UserId)
|
||||
userChan <- store.StoreResult{Data: user, Err: err}
|
||||
userChan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(userChan)
|
||||
}()
|
||||
|
||||
@@ -389,8 +390,14 @@ func (a *App) tryExecuteCustomCommand(args *model.CommandArgs, trigger string, m
|
||||
team := tr.Data.(*model.Team)
|
||||
|
||||
ur := <-userChan
|
||||
if ur.Err != nil {
|
||||
return nil, nil, ur.Err
|
||||
if ur.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(ur.NErr, &nfErr):
|
||||
return nil, nil, model.NewAppError("tryExecuteCustomCommand", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, nil, model.NewAppError("tryExecuteCustomCommand", "app.user.get.app_error", nil, ur.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := ur.Data.(*model.User)
|
||||
|
||||
|
||||
@@ -247,9 +247,9 @@ func (s *Server) ensureInstallationDate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
installDate, appErr := s.Store.User().InferSystemInstallDate()
|
||||
installDate, nErr := s.Store.User().InferSystemInstallDate()
|
||||
var installationDate int64
|
||||
if appErr == nil && installDate > 0 {
|
||||
if nErr == nil && installDate > 0 {
|
||||
installationDate = installDate
|
||||
} else {
|
||||
installationDate = utils.MillisFromTime(time.Now())
|
||||
|
||||
@@ -4,3 +4,5 @@
|
||||
package app
|
||||
|
||||
const MISSING_CHANNEL_MEMBER_ERROR = "app.channel.get_member.missing.app_error"
|
||||
const MISSING_ACCOUNT_ERROR = "app.user.missing_account.const"
|
||||
const MISSING_AUTH_ACCOUNT_ERROR = "app.user.get_by_auth.missing_account.app_error"
|
||||
|
||||
@@ -187,7 +187,7 @@ func (a *App) exportAllUsers(writer io.Writer) *model.AppError {
|
||||
users, err := a.Srv().Store.User().GetAllAfter(1000, afterId)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("exportAllUsers", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if len(users) == 0 {
|
||||
@@ -412,11 +412,12 @@ func (a *App) BuildPostReactions(postId string) (*[]ReactionImportData, *model.A
|
||||
for _, reaction := range reactions {
|
||||
user, err := a.Srv().Store.User().Get(reaction.UserId)
|
||||
if err != nil {
|
||||
if err.Id == store.MISSING_ACCOUNT_ERROR { // this is a valid case, the user that reacted might've been deleted by now
|
||||
var nfErr *store.ErrNotFound
|
||||
if errors.As(err, &nfErr) { // this is a valid case, the user that reacted might've been deleted by now
|
||||
mlog.Info("Skipping reactions by user since the entity doesn't exist anymore", mlog.String("user_id", reaction.UserId))
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
return nil, model.NewAppError("BuildPostReactions", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
reactionsOfPost = append(reactionsOfPost, *ImportReactionFromPost(user, reaction))
|
||||
}
|
||||
|
||||
@@ -296,9 +296,9 @@ func (a *App) importUser(data *UserImportData, dryRun bool) *model.AppError {
|
||||
hasUserEmailVerifiedChanged := false
|
||||
|
||||
var user *model.User
|
||||
var err *model.AppError
|
||||
user, err = a.Srv().Store.User().GetByUsername(*data.Username)
|
||||
if err != nil {
|
||||
var nErr error
|
||||
user, nErr = a.Srv().Store.User().GetByUsername(*data.Username)
|
||||
if nErr != nil {
|
||||
user = &model.User{}
|
||||
user.MakeNonNil()
|
||||
user.SetDefaultNotifications()
|
||||
@@ -475,6 +475,7 @@ func (a *App) importUser(data *UserImportData, dryRun bool) *model.AppError {
|
||||
}
|
||||
|
||||
var savedUser *model.User
|
||||
var err *model.AppError
|
||||
if user.Id == "" {
|
||||
if savedUser, err = a.createUser(user); err != nil {
|
||||
return err
|
||||
@@ -501,8 +502,14 @@ func (a *App) importUser(data *UserImportData, dryRun bool) *model.AppError {
|
||||
}
|
||||
} else {
|
||||
if hasUserAuthDataChanged {
|
||||
if _, err = a.Srv().Store.User().UpdateAuthData(user.Id, authService, authData, user.Email, false); err != nil {
|
||||
return err
|
||||
if _, nErr := a.Srv().Store.User().UpdateAuthData(user.Id, authService, authData, user.Email, false); nErr != nil {
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(nErr, &invErr):
|
||||
return model.NewAppError("importUser", "app.user.update_auth_data.email_exists.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return model.NewAppError("importUser", "app.user.update_auth_data.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -976,15 +983,14 @@ func (a *App) importUserChannels(user *model.User, team *model.Team, teamMember
|
||||
}
|
||||
|
||||
func (a *App) importReaction(data *ReactionImportData, post *model.Post, dryRun bool) *model.AppError {
|
||||
var err *model.AppError
|
||||
if err = validateReactionImportData(data, post.CreateAt); err != nil {
|
||||
if err := validateReactionImportData(data, post.CreateAt); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
user, err = a.Srv().Store.User().GetByUsername(*data.User)
|
||||
if err != nil {
|
||||
return model.NewAppError("BulkImport", "app.import.import_post.user_not_found.error", map[string]interface{}{"Username": data.User}, err.Error(), http.StatusBadRequest)
|
||||
var nErr error
|
||||
if user, nErr = a.Srv().Store.User().GetByUsername(*data.User); nErr != nil {
|
||||
return model.NewAppError("BulkImport", "app.import.import_post.user_not_found.error", map[string]interface{}{"Username": data.User}, nErr.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
reaction := &model.Reaction{
|
||||
@@ -993,7 +999,7 @@ func (a *App) importReaction(data *ReactionImportData, post *model.Post, dryRun
|
||||
EmojiName: *data.EmojiName,
|
||||
CreateAt: *data.CreateAt,
|
||||
}
|
||||
if _, nErr := a.Srv().Store.Reaction().Save(reaction); nErr != nil {
|
||||
if _, nErr = a.Srv().Store.Reaction().Save(reaction); nErr != nil {
|
||||
var appErr *model.AppError
|
||||
switch {
|
||||
case errors.As(nErr, &appErr):
|
||||
|
||||
@@ -85,7 +85,7 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
|
||||
userChan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(upstreamRequest.UserId)
|
||||
userChan <- store.StoreResult{Data: user, Err: err}
|
||||
userChan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(userChan)
|
||||
}()
|
||||
|
||||
@@ -188,8 +188,14 @@ func (a *App) DoPostActionWithCookie(postId, actionId, userId, selectedOption st
|
||||
}()
|
||||
|
||||
ur := <-userChan
|
||||
if ur.Err != nil {
|
||||
return "", ur.Err
|
||||
if ur.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(ur.NErr, &nfErr):
|
||||
return "", model.NewAppError("DoPostActionWithCookie", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return "", model.NewAppError("DoPostActionWithCookie", "app.user.get.app_error", nil, ur.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := ur.Data.(*model.User)
|
||||
upstreamRequest.UserName = user.Username
|
||||
|
||||
@@ -96,7 +96,7 @@ func (s *Server) SaveLicense(licenseBytes []byte) (*model.License, *model.AppErr
|
||||
case errors.As(nErr, &appErr):
|
||||
return nil, appErr
|
||||
default:
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ func (a *App) GetUserForLogin(id, loginId string) (*model.User, *model.AppError)
|
||||
if len(id) != 0 {
|
||||
user, err := a.GetUser(id)
|
||||
if err != nil {
|
||||
if err.Id != store.MISSING_ACCOUNT_ERROR {
|
||||
if err.Id != MISSING_ACCOUNT_ERROR {
|
||||
err.StatusCode = http.StatusInternalServerError
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
||||
pchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
props, err := a.Srv().Store.User().GetAllProfilesInChannel(channel.Id, true)
|
||||
pchan <- store.StoreResult{Data: props, Err: err}
|
||||
pchan <- store.StoreResult{Data: props, NErr: err}
|
||||
close(pchan)
|
||||
}()
|
||||
|
||||
@@ -58,8 +58,8 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
||||
}
|
||||
|
||||
result := <-pchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
if result.NErr != nil {
|
||||
return nil, result.NErr
|
||||
}
|
||||
profileMap := result.Data.(map[string]*model.User)
|
||||
|
||||
@@ -504,9 +504,9 @@ func (a *App) filterOutOfChannelMentions(sender *model.User, post *model.Post, c
|
||||
// Filter out inactive users and bots
|
||||
allUsers := model.UserSlice(users).FilterByActive(true)
|
||||
allUsers = allUsers.FilterWithoutBots()
|
||||
allUsers, err = a.FilterUsersByVisible(sender, allUsers)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
allUsers, appErr := a.FilterUsersByVisible(sender, allUsers)
|
||||
if appErr != nil {
|
||||
return nil, nil, appErr
|
||||
}
|
||||
|
||||
if len(allUsers) == 0 {
|
||||
|
||||
@@ -211,7 +211,7 @@ func (a *App) clearPushNotificationSync(currentSessionId, userId, channelId stri
|
||||
|
||||
unreadCount, err := a.Srv().Store.User().GetUnreadCount(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("clearPushNotificationSync", "app.user.get_unread_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
msg.Badge = int(unreadCount)
|
||||
@@ -242,7 +242,7 @@ func (a *App) updateMobileAppBadgeSync(userId string) *model.AppError {
|
||||
|
||||
unreadCount, err := a.Srv().Store.User().GetUnreadCount(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("updateMobileAppBadgeSync", "app.user.get_unread_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
msg.Badge = int(unreadCount)
|
||||
@@ -525,7 +525,7 @@ func (a *App) BuildPushNotificationMessage(contentsConfig string, post *model.Po
|
||||
|
||||
unreadCount, err := a.Srv().Store.User().GetUnreadCount(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("BuildPushNotificationMessage", "app.user.get_unread_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
msg.Badge = int(unreadCount)
|
||||
|
||||
|
||||
39
app/oauth.go
39
app/oauth.go
@@ -269,6 +269,7 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
|
||||
var accessData *model.AccessData
|
||||
var accessRsp *model.AccessResponse
|
||||
var user *model.User
|
||||
if grantType == model.ACCESS_TOKEN_GRANT_TYPE {
|
||||
var authData *model.AuthData
|
||||
authData, nErr = a.Srv().Store.OAuth().GetAuthData(code)
|
||||
@@ -287,8 +288,8 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.redirect_uri.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
user, err := a.Srv().Store.User().Get(authData.UserId)
|
||||
if err != nil {
|
||||
user, nErr = a.Srv().Store.User().Get(authData.UserId)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_user.app_error", nil, "", http.StatusNotFound)
|
||||
}
|
||||
|
||||
@@ -300,7 +301,7 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
if accessData != nil {
|
||||
if accessData.IsExpired() {
|
||||
var access *model.AccessResponse
|
||||
access, err = a.newSessionUpdateToken(oauthApp.Name, accessData, user)
|
||||
access, err := a.newSessionUpdateToken(oauthApp.Name, accessData, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -317,7 +318,7 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
} else {
|
||||
var session *model.Session
|
||||
// Create a new session and return new access token
|
||||
session, err = a.newSession(oauthApp.Name, user)
|
||||
session, err := a.newSession(oauthApp.Name, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -347,8 +348,8 @@ func (a *App) GetOAuthAccessTokenForCodeFlow(clientId, grantType, redirectUri, c
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.refresh_token.app_error", nil, "", http.StatusNotFound)
|
||||
}
|
||||
|
||||
user, err := a.Srv().Store.User().Get(accessData.UserId)
|
||||
if err != nil {
|
||||
user, nErr := a.Srv().Store.User().Get(accessData.UserId)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("GetOAuthAccessToken", "api.oauth.get_access_token.internal_user.app_error", nil, "", http.StatusNotFound)
|
||||
}
|
||||
|
||||
@@ -587,7 +588,7 @@ func (a *App) LoginByOAuth(service string, userData io.Reader, teamId string) (*
|
||||
}
|
||||
user, err := a.GetUserByAuth(model.NewString(*authUser.AuthData), service)
|
||||
if err != nil {
|
||||
if err.Id == store.MISSING_AUTH_ACCOUNT_ERROR {
|
||||
if err.Id == MISSING_AUTH_ACCOUNT_ERROR {
|
||||
user, err = a.CreateOAuthUser(service, bytes.NewReader(buf.Bytes()), teamId)
|
||||
} else {
|
||||
return nil, err
|
||||
@@ -637,21 +638,27 @@ func (a *App) CompleteSwitchWithOAuth(service string, userData io.Reader, email
|
||||
map[string]interface{}{"Service": service}, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
user, err := a.Srv().Store.User().GetByEmail(email)
|
||||
if err != nil {
|
||||
user, nErr := a.Srv().Store.User().GetByEmail(email)
|
||||
if nErr != nil {
|
||||
return nil, model.NewAppError("CompleteSwitchWithOAuth", MISSING_ACCOUNT_ERROR, nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := a.RevokeAllSessions(user.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = a.RevokeAllSessions(user.Id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err = a.Srv().Store.User().UpdateAuthData(user.Id, service, model.NewString(*ssoUser.AuthData), ssoUser.Email, true); err != nil {
|
||||
return nil, err
|
||||
if _, nErr := a.Srv().Store.User().UpdateAuthData(user.Id, service, ssoUser.AuthData, ssoUser.Email, true); nErr != nil {
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(nErr, &invErr):
|
||||
return nil, model.NewAppError("importUser", "app.user.update_auth_data.email_exists.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("importUser", "app.user.update_auth_data.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
a.Srv().Go(func() {
|
||||
if err = a.Srv().EmailService.SendSignInChangeEmail(user.Email, strings.Title(service)+" SSO", user.Locale, a.GetSiteURL()); err != nil {
|
||||
if err := a.Srv().EmailService.SendSignInChangeEmail(user.Email, strings.Title(service)+" SSO", user.Locale, a.GetSiteURL()); err != nil {
|
||||
mlog.Error("error sending signin change email", mlog.Err(err))
|
||||
}
|
||||
})
|
||||
|
||||
@@ -30,7 +30,7 @@ func (a *App) ResetPermissionsSystem() *model.AppError {
|
||||
|
||||
// Reset all Custom Role assignments to Users.
|
||||
if err := a.Srv().Store.User().ClearAllCustomRoleAssignments(); err != nil {
|
||||
return err
|
||||
return model.NewAppError("ResetPermissionsSystem", "app.user.clear_all_custom_role_assignments.select.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Reset all Custom Role assignments to TeamMembers.
|
||||
|
||||
24
app/post.go
24
app/post.go
@@ -53,9 +53,15 @@ func (a *App) CreatePostAsUser(post *model.Post, currentSessionId string, setOnl
|
||||
}
|
||||
|
||||
if err.Id == "api.post.create_post.town_square_read_only" {
|
||||
user, userErr := a.Srv().Store.User().Get(post.UserId)
|
||||
if userErr != nil {
|
||||
return nil, userErr
|
||||
user, nErr := a.Srv().Store.User().Get(post.UserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("CreatePostAsUser", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("CreatePostAsUser", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
T := utils.GetUserTranslations(user.Locale)
|
||||
@@ -185,9 +191,15 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
|
||||
}()
|
||||
}
|
||||
|
||||
user, err := a.Srv().Store.User().Get(post.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(post.UserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("CreatePost", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("CreatePost", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if user.IsBot {
|
||||
|
||||
@@ -16,10 +16,10 @@ import (
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
"github.com/mattermost/mattermost-server/v5/utils"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
date_constraints "github.com/reflog/dateconstraints"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/reflog/dateconstraints"
|
||||
)
|
||||
|
||||
const MAX_REPEAT_VIEWINGS = 3
|
||||
@@ -301,16 +301,15 @@ func (a *App) UpdateProductNotices() *model.AppError {
|
||||
url := *a.Srv().Config().AnnouncementSettings.NoticesURL
|
||||
skip := *a.Srv().Config().AnnouncementSettings.NoticesSkipCache
|
||||
mlog.Debug("Will fetch notices from", mlog.String("url", url), mlog.Bool("skip_cache", skip))
|
||||
var appErr *model.AppError
|
||||
var err error
|
||||
cachedPostCount, err = a.Srv().Store.Post().AnalyticsPostCount("", false, false)
|
||||
if err != nil {
|
||||
mlog.Error("Failed to fetch post count", mlog.String("error", err.Error()))
|
||||
}
|
||||
|
||||
cachedUserCount, appErr = a.Srv().Store.User().Count(model.UserCountOptions{IncludeDeleted: true})
|
||||
if appErr != nil {
|
||||
mlog.Error("Failed to fetch user count", mlog.String("error", appErr.Error()))
|
||||
cachedUserCount, err = a.Srv().Store.User().Count(model.UserCountOptions{IncludeDeleted: true})
|
||||
if err != nil {
|
||||
mlog.Error("Failed to fetch user count", mlog.String("error", err.Error()))
|
||||
}
|
||||
|
||||
data, err := utils.GetUrlWithCache(url, ¬icesCache, skip)
|
||||
|
||||
@@ -399,9 +399,15 @@ func (a *App) SetSessionExpireInDays(session *model.Session, days int) {
|
||||
|
||||
func (a *App) CreateUserAccessToken(token *model.UserAccessToken) (*model.UserAccessToken, *model.AppError) {
|
||||
|
||||
user, err := a.Srv().Store.User().Get(token.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(token.UserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("CreateUserAccessToken", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("CreateUserAccessToken", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if !*a.Config().ServiceSettings.EnableUserAccessTokens && !user.IsBot {
|
||||
@@ -410,14 +416,14 @@ func (a *App) CreateUserAccessToken(token *model.UserAccessToken) (*model.UserAc
|
||||
|
||||
token.Token = model.NewId()
|
||||
|
||||
token, nErr := a.Srv().Store.UserAccessToken().Save(token)
|
||||
token, nErr = a.Srv().Store.UserAccessToken().Save(token)
|
||||
if nErr != nil {
|
||||
var appErr *model.AppError
|
||||
switch {
|
||||
case errors.As(nErr, &appErr):
|
||||
return nil, appErr
|
||||
default:
|
||||
return nil, model.NewAppError("CreateUserAccessToken", "app.user_access_token.save.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, model.NewAppError("CreateUserAccessToken", "app.user_access_token.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -442,9 +448,15 @@ func (a *App) createSessionForUserAccessToken(tokenString string) (*model.Sessio
|
||||
return nil, model.NewAppError("createSessionForUserAccessToken", "app.user_access_token.invalid_or_missing", nil, "inactive_token", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
user, err := a.Srv().Store.User().Get(token.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(token.UserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("createSessionForUserAccessToken", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("createSessionForUserAccessToken", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if !*a.Config().ServiceSettings.EnableUserAccessTokens && !user.IsBot {
|
||||
|
||||
@@ -56,7 +56,7 @@ func CreateBasicUser(a *app.App, client *model.Client4) *model.AppError {
|
||||
}
|
||||
_, err := a.Srv().Store.User().VerifyEmail(ruser.Id, ruser.Email)
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("CreateBasicUser", "app.user.verify_email.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
if _, nErr := a.Srv().Store.Team().SaveMember(&model.TeamMember{TeamId: basicteam.Id, UserId: ruser.Id}, *a.Config().TeamSettings.MaxUsersPerTeam); nErr != nil {
|
||||
var appErr *model.AppError
|
||||
|
||||
@@ -48,8 +48,8 @@ func (me *groupmsgProvider) DoCommand(a *app.App, args *model.CommandArgs, messa
|
||||
for _, username := range users {
|
||||
username = strings.TrimSpace(username)
|
||||
username = strings.TrimPrefix(username, "@")
|
||||
targetUser, err := a.Srv().Store.User().GetByUsername(username)
|
||||
if err != nil {
|
||||
targetUser, nErr := a.Srv().Store.User().GetByUsername(username)
|
||||
if nErr != nil {
|
||||
invalidUsernames = append(invalidUsernames, username)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -50,9 +50,9 @@ func (me *InviteProvider) DoCommand(a *app.App, args *model.CommandArgs, message
|
||||
targetUsername := splitMessage[0]
|
||||
targetUsername = strings.TrimPrefix(targetUsername, "@")
|
||||
|
||||
userProfile, err := a.Srv().Store.User().GetByUsername(targetUsername)
|
||||
if err != nil {
|
||||
mlog.Error(err.Error())
|
||||
userProfile, nErr := a.Srv().Store.User().GetByUsername(targetUsername)
|
||||
if nErr != nil {
|
||||
mlog.Error(nErr.Error())
|
||||
return &model.CommandResponse{
|
||||
Text: args.T("api.command_invite.missing_user.app_error"),
|
||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
||||
@@ -67,6 +67,7 @@ func (me *InviteProvider) DoCommand(a *app.App, args *model.CommandArgs, message
|
||||
}
|
||||
|
||||
var channelToJoin *model.Channel
|
||||
var err *model.AppError
|
||||
// User set a channel to add the invited user
|
||||
if len(splitMessage) > 1 && splitMessage[1] != "" {
|
||||
targetChannelName := strings.TrimPrefix(strings.TrimSpace(splitMessage[1]), "~")
|
||||
|
||||
@@ -51,9 +51,9 @@ func (me *msgProvider) DoCommand(a *app.App, args *model.CommandArgs, message st
|
||||
targetUsername = strings.SplitN(message, " ", 2)[0]
|
||||
targetUsername = strings.TrimPrefix(targetUsername, "@")
|
||||
|
||||
userProfile, err := a.Srv().Store.User().GetByUsername(targetUsername)
|
||||
if err != nil {
|
||||
mlog.Error(err.Error())
|
||||
userProfile, nErr := a.Srv().Store.User().GetByUsername(targetUsername)
|
||||
if nErr != nil {
|
||||
mlog.Error(nErr.Error())
|
||||
return &model.CommandResponse{Text: args.T("api.command_msg.missing.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,9 +108,9 @@ func doCommand(a *app.App, args *model.CommandArgs, message string) *model.Comma
|
||||
targetUsername = strings.SplitN(message, " ", 2)[0]
|
||||
targetUsername = strings.TrimPrefix(targetUsername, "@")
|
||||
|
||||
userProfile, err := a.Srv().Store.User().GetByUsername(targetUsername)
|
||||
if err != nil {
|
||||
mlog.Error(err.Error())
|
||||
userProfile, nErr := a.Srv().Store.User().GetByUsername(targetUsername)
|
||||
if nErr != nil {
|
||||
mlog.Error(nErr.Error())
|
||||
return &model.CommandResponse{
|
||||
Text: args.T("api.command_remove.missing.app_error"),
|
||||
ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL,
|
||||
|
||||
88
app/team.go
88
app/team.go
@@ -488,7 +488,7 @@ func (a *App) AddUserToTeam(teamId string, userId string, userRequestorId string
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -505,8 +505,14 @@ func (a *App) AddUserToTeam(teamId string, userId string, userRequestorId string
|
||||
team := result.Data.(*model.Team)
|
||||
|
||||
result = <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
if result.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(result.NErr, &nfErr):
|
||||
return nil, model.NewAppError("AddUserToTeam", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("AddUserToTeam", "app.user.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
@@ -559,7 +565,7 @@ func (a *App) AddUserToTeamByToken(userId string, tokenId string) (*model.Team,
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -580,8 +586,14 @@ func (a *App) AddUserToTeamByToken(userId string, tokenId string) (*model.Team,
|
||||
}
|
||||
|
||||
result = <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
if result.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(result.NErr, &nfErr):
|
||||
return nil, model.NewAppError("AddUserToTeamByToken", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("AddUserToTeamByToken", "app.user.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
@@ -628,7 +640,7 @@ func (a *App) AddUserToTeamByInviteId(inviteId string, userId string) (*model.Te
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -645,8 +657,14 @@ func (a *App) AddUserToTeamByInviteId(inviteId string, userId string) (*model.Te
|
||||
team := result.Data.(*model.Team)
|
||||
|
||||
result = <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
if result.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(result.NErr, &nfErr):
|
||||
return nil, model.NewAppError("AddUserToTeamByInviteId", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("AddUserToTeamByInviteId", "app.user.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
@@ -760,7 +778,7 @@ func (a *App) JoinUserToTeam(team *model.Team, user *model.User, userRequestorId
|
||||
}
|
||||
|
||||
if _, err := a.Srv().Store.User().UpdateUpdateAt(user.Id); err != nil {
|
||||
return err
|
||||
return model.NewAppError("JoinUserToTeam", "app.user.update_update.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := a.createInitialSidebarCategories(user.Id, team.Id); err != nil {
|
||||
@@ -1144,7 +1162,7 @@ func (a *App) RemoveUserFromTeam(teamId string, userId string, requestorId strin
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -1161,8 +1179,14 @@ func (a *App) RemoveUserFromTeam(teamId string, userId string, requestorId strin
|
||||
team := result.Data.(*model.Team)
|
||||
|
||||
result = <-uchan
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
if result.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(result.NErr, &nfErr):
|
||||
return model.NewAppError("RemoveUserFromTeam", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("RemoveUserFromTeam", "app.user.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
@@ -1180,9 +1204,15 @@ func (a *App) RemoveTeamMemberFromTeam(teamMember *model.TeamMember, requestorId
|
||||
message.Add("team_id", teamMember.TeamId)
|
||||
a.Publish(message)
|
||||
|
||||
user, err := a.Srv().Store.User().Get(teamMember.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
user, nErr := a.Srv().Store.User().Get(teamMember.UserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return model.NewAppError("RemoveTeamMemberFromTeam", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("RemoveTeamMemberFromTeam", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
teamMember.Roles = ""
|
||||
@@ -1214,7 +1244,7 @@ func (a *App) RemoveTeamMemberFromTeam(teamMember *model.TeamMember, requestorId
|
||||
}
|
||||
|
||||
if _, err := a.Srv().Store.User().UpdateUpdateAt(user.Id); err != nil {
|
||||
return err
|
||||
return model.NewAppError("RemoveTeamMemberFromTeam", "app.user.update_update.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.Channel().ClearSidebarOnTeamLeave(user.Id, teamMember.TeamId); err != nil {
|
||||
@@ -1337,7 +1367,7 @@ func (a *App) prepareInviteNewUsersToTeam(teamId, senderId string) (*model.User,
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(senderId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -1354,8 +1384,14 @@ func (a *App) prepareInviteNewUsersToTeam(teamId, senderId string) (*model.User,
|
||||
team := result.Data.(*model.Team)
|
||||
|
||||
result = <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, nil, result.Err
|
||||
if result.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(result.NErr, &nfErr):
|
||||
return nil, nil, model.NewAppError("prepareInviteNewUsersToTeam", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, nil, model.NewAppError("prepareInviteNewUsersToTeam", "app.user.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
return user, team, nil
|
||||
@@ -1462,7 +1498,7 @@ func (a *App) prepareInviteGuestsToChannels(teamId string, guestsInvite *model.G
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(senderId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -1473,8 +1509,14 @@ func (a *App) prepareInviteGuestsToChannels(teamId string, guestsInvite *model.G
|
||||
channels := result.Data.([]*model.Channel)
|
||||
|
||||
result = <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, nil, nil, result.Err
|
||||
if result.NErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(result.NErr, &nfErr):
|
||||
return nil, nil, nil, model.NewAppError("prepareInviteGuestsToChannels", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, nil, nil, model.NewAppError("prepareInviteGuestsToChannels", "app.user.get.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
user := result.Data.(*model.User)
|
||||
|
||||
|
||||
306
app/user.go
306
app/user.go
@@ -258,7 +258,7 @@ func (a *App) createUserOrGuest(user *model.User, guest bool) (*model.User, *mod
|
||||
// system is granted the system_admin role
|
||||
count, err := a.Srv().Store.User().Count(model.UserCountOptions{IncludeDeleted: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("createUserOrGuest", "app.user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
if count <= 0 {
|
||||
user.Roles = model.SYSTEM_ADMIN_ROLE_ID + " " + model.SYSTEM_USER_ROLE_ID
|
||||
@@ -268,9 +268,9 @@ func (a *App) createUserOrGuest(user *model.User, guest bool) (*model.User, *mod
|
||||
user.Locale = *a.Config().LocalizationSettings.DefaultClientLocale
|
||||
}
|
||||
|
||||
ruser, err := a.createUser(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
ruser, appErr := a.createUser(user)
|
||||
if appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
// This message goes to everyone, so the teamId, channelId and userId are irrelevant
|
||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_NEW_USER, "", "", "", nil)
|
||||
@@ -297,10 +297,19 @@ func (a *App) createUser(user *model.User) (*model.User, *model.AppError) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ruser, err := a.Srv().Store.User().Save(user)
|
||||
if err != nil {
|
||||
mlog.Error("Couldn't save the user", mlog.Err(err))
|
||||
return nil, err
|
||||
ruser, nErr := a.Srv().Store.User().Save(user)
|
||||
if nErr != nil {
|
||||
mlog.Error("Couldn't save the user", mlog.Err(nErr))
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(nErr, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(nErr, &invErr):
|
||||
return nil, model.NewAppError("createUser", "app.user.save.existing.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("createUser", "app.user.save.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if user.EmailVerified {
|
||||
@@ -337,12 +346,12 @@ func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string)
|
||||
euchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
userByAuth, err := a.Srv().Store.User().GetByAuth(user.AuthData, service)
|
||||
suchan <- store.StoreResult{Data: userByAuth, Err: err}
|
||||
suchan <- store.StoreResult{Data: userByAuth, NErr: err}
|
||||
close(suchan)
|
||||
}()
|
||||
go func() {
|
||||
userByEmail, err := a.Srv().Store.User().GetByEmail(user.Email)
|
||||
euchan <- store.StoreResult{Data: userByEmail, Err: err}
|
||||
euchan <- store.StoreResult{Data: userByEmail, NErr: err}
|
||||
close(euchan)
|
||||
}()
|
||||
|
||||
@@ -355,11 +364,11 @@ func (a *App) CreateOAuthUser(service string, userData io.Reader, teamId string)
|
||||
}
|
||||
}
|
||||
|
||||
if result := <-suchan; result.Err == nil {
|
||||
if result := <-suchan; result.NErr == nil {
|
||||
return result.Data.(*model.User), nil
|
||||
}
|
||||
|
||||
if result := <-euchan; result.Err == nil {
|
||||
if result := <-euchan; result.NErr == nil {
|
||||
authService := result.Data.(*model.User).AuthService
|
||||
if authService == "" {
|
||||
return nil, model.NewAppError("CreateOAuthUser", "api.user.create_oauth_user.already_attached.app_error", map[string]interface{}{"Service": service, "Auth": model.USER_AUTH_SERVICE_EMAIL}, "email="+user.Email, http.StatusBadRequest)
|
||||
@@ -425,14 +434,30 @@ func (a *App) IsUsernameTaken(name string) bool {
|
||||
}
|
||||
|
||||
func (a *App) GetUser(userId string) (*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().Get(userId)
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
if err != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("GetUser", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("GetUser", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUserByUsername(username string) (*model.User, *model.AppError) {
|
||||
result, err := a.Srv().Store.User().GetByUsername(username)
|
||||
if err != nil && err.Id == "store.sql_user.get_by_username.app_error" {
|
||||
err.StatusCode = http.StatusNotFound
|
||||
return nil, err
|
||||
if err != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("GetUserByUsername", "app.user.get_by_username.app_error", nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("GetUserByUsername", "app.user.get_by_username.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -440,22 +465,42 @@ func (a *App) GetUserByUsername(username string) (*model.User, *model.AppError)
|
||||
func (a *App) GetUserByEmail(email string) (*model.User, *model.AppError) {
|
||||
user, err := a.Srv().Store.User().GetByEmail(email)
|
||||
if err != nil {
|
||||
if err.Id == "store.sql_user.missing_account.const" {
|
||||
err.StatusCode = http.StatusNotFound
|
||||
return nil, err
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("GetUserByEmail", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("GetUserByEmail", MISSING_ACCOUNT_ERROR, nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUserByAuth(authData *string, authService string) (*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetByAuth(authData, authService)
|
||||
user, err := a.Srv().Store.User().GetByAuth(authData, authService)
|
||||
if err != nil {
|
||||
var invErr *store.ErrInvalidInput
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("GetUserByAuth", MISSING_AUTH_ACCOUNT_ERROR, nil, invErr.Error(), http.StatusBadRequest)
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("GetUserByAuth", MISSING_AUTH_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusInternalServerError)
|
||||
default:
|
||||
return nil, model.NewAppError("GetUserByAuth", "app.user.get_by_auth.other.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsers(options *model.UserGetOptions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetAllProfiles(options)
|
||||
users, err := a.Srv().Store.User().GetAllProfiles(options)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsers", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersPage(options *model.UserGetOptions, asAdmin bool) ([]*model.User, *model.AppError) {
|
||||
@@ -472,11 +517,21 @@ func (a *App) GetUsersEtag(restrictionsHash string) string {
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInTeam(options *model.UserGetOptions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetProfiles(options)
|
||||
users, err := a.Srv().Store.User().GetProfiles(options)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsersInTeam", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersNotInTeam(teamId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetProfilesNotInTeam(teamId, groupConstrained, offset, limit, viewRestrictions)
|
||||
users, err := a.Srv().Store.User().GetProfilesNotInTeam(teamId, groupConstrained, offset, limit, viewRestrictions)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsersNotInTeam", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInTeamPage(options *model.UserGetOptions, asAdmin bool) ([]*model.User, *model.AppError) {
|
||||
@@ -506,11 +561,21 @@ func (a *App) GetUsersNotInTeamEtag(teamId string, restrictionsHash string) stri
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInChannel(options *model.UserGetOptions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetProfilesInChannel(options)
|
||||
users, err := a.Srv().Store.User().GetProfilesInChannel(options)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsersInChannel", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInChannelByStatus(options *model.UserGetOptions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetProfilesInChannelByStatus(options)
|
||||
users, err := a.Srv().Store.User().GetProfilesInChannelByStatus(options)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsersInChannelByStatus", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersInChannelMap(options *model.UserGetOptions, asAdmin bool) (map[string]*model.User, *model.AppError) {
|
||||
@@ -546,7 +611,12 @@ func (a *App) GetUsersInChannelPageByStatus(options *model.UserGetOptions, asAdm
|
||||
}
|
||||
|
||||
func (a *App) GetUsersNotInChannel(teamId string, channelId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetProfilesNotInChannel(teamId, channelId, groupConstrained, offset, limit, viewRestrictions)
|
||||
users, err := a.Srv().Store.User().GetProfilesNotInChannel(teamId, channelId, groupConstrained, offset, limit, viewRestrictions)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsersNotInChannel", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersNotInChannelMap(teamId string, channelId string, groupConstrained bool, offset int, limit int, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) (map[string]*model.User, *model.AppError) {
|
||||
@@ -584,17 +654,32 @@ func (a *App) GetUsersWithoutTeamPage(options *model.UserGetOptions, asAdmin boo
|
||||
}
|
||||
|
||||
func (a *App) GetUsersWithoutTeam(options *model.UserGetOptions) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetProfilesWithoutTeam(options)
|
||||
users, err := a.Srv().Store.User().GetProfilesWithoutTeam(options)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetUsersWithoutTeam", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// GetTeamGroupUsers returns the users who are associated to the team via GroupTeams and GroupMembers.
|
||||
func (a *App) GetTeamGroupUsers(teamID string) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetTeamGroupUsers(teamID)
|
||||
users, err := a.Srv().Store.User().GetTeamGroupUsers(teamID)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetTeamGroupUsers", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// GetChannelGroupUsers returns the users who are associated to the channel via GroupChannels and GroupMembers.
|
||||
func (a *App) GetChannelGroupUsers(channelID string) ([]*model.User, *model.AppError) {
|
||||
return a.Srv().Store.User().GetChannelGroupUsers(channelID)
|
||||
users, err := a.Srv().Store.User().GetChannelGroupUsers(channelID)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetChannelGroupUsers", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (a *App) GetUsersByIds(userIds []string, options *store.UserGetByIdsOpts) ([]*model.User, *model.AppError) {
|
||||
@@ -602,7 +687,7 @@ func (a *App) GetUsersByIds(userIds []string, options *store.UserGetByIdsOpts) (
|
||||
|
||||
users, err := a.Srv().Store.User().GetProfileByIds(userIds, options, allowFromCache)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetUsersByIds", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return a.sanitizeProfiles(users, options.IsAdmin), nil
|
||||
@@ -611,7 +696,7 @@ func (a *App) GetUsersByIds(userIds []string, options *store.UserGetByIdsOpts) (
|
||||
func (a *App) GetUsersByGroupChannelIds(channelIds []string, asAdmin bool) (map[string][]*model.User, *model.AppError) {
|
||||
usersByChannelId, err := a.Srv().Store.User().GetProfileByGroupChannelIdsForUser(a.Session().UserId, channelIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetUsersByGroupChannelIds", "app.user.get_profile_by_group_channel_ids_for_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
for channelId, userList := range usersByChannelId {
|
||||
usersByChannelId[channelId] = a.sanitizeProfiles(userList, asAdmin)
|
||||
@@ -623,7 +708,7 @@ func (a *App) GetUsersByGroupChannelIds(channelIds []string, asAdmin bool) (map[
|
||||
func (a *App) GetUsersByUsernames(usernames []string, asAdmin bool, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) {
|
||||
users, err := a.Srv().Store.User().GetProfilesByUsernames(usernames, viewRestrictions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetUsersByUsernames", "app.user.get_profiles.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return a.sanitizeProfiles(users, asAdmin), nil
|
||||
}
|
||||
@@ -658,7 +743,13 @@ func (a *App) GenerateMfaSecret(userId string) (*model.MfaSecret, *model.AppErro
|
||||
func (a *App) ActivateMfa(userId, token string) *model.AppError {
|
||||
user, err := a.Srv().Store.User().Get(userId)
|
||||
if err != nil {
|
||||
return err
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &nfErr):
|
||||
return model.NewAppError("ActivateMfa", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return model.NewAppError("ActivateMfa", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if len(user.AuthService) > 0 && user.AuthService != model.USER_AUTH_SERVICE_LDAP {
|
||||
@@ -1003,7 +1094,16 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A
|
||||
|
||||
userUpdate, err := a.Srv().Store.User().Update(user, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("UpdateActive", "app.user.update.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateActive", "app.user.update.finding.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
ruser := userUpdate.New
|
||||
|
||||
@@ -1024,7 +1124,7 @@ func (a *App) UpdateActive(user *model.User, active bool) (*model.User, *model.A
|
||||
func (a *App) DeactivateGuests() *model.AppError {
|
||||
userIds, err := a.Srv().Store.User().DeactivateGuests()
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("DeactivateGuests", "app.user.update_active_for_multiple_users.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, userId := range userIds {
|
||||
@@ -1098,13 +1198,19 @@ func (a *App) UpdateUserAuth(userId string, userAuth *model.UserAuth) (*model.Us
|
||||
password := model.HashPassword(userAuth.Password)
|
||||
|
||||
if err := a.Srv().Store.User().UpdatePassword(userId, password); err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("UpdateUserAuth", "app.user.update_password.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
userAuth.Password = ""
|
||||
|
||||
if _, err := a.Srv().Store.User().UpdateAuthData(userId, userAuth.AuthService, userAuth.AuthData, "", false); err != nil {
|
||||
return nil, err
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("UpdateUserAuth", "app.user.update_auth_data.email_exists.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateUserAuth", "app.user.update_auth_data.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1129,7 +1235,13 @@ func (a *App) sendUpdatedUserEvent(user model.User) {
|
||||
func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User, *model.AppError) {
|
||||
prev, err := a.Srv().Store.User().Get(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("UpdateUser", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateUser", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if !CheckUserDomain(user, *a.Config().TeamSettings.RestrictCreationToDomains) {
|
||||
@@ -1150,8 +1262,8 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User,
|
||||
if *a.Config().EmailSettings.RequireEmailVerification && prev.Email != user.Email {
|
||||
newEmail = user.Email
|
||||
|
||||
_, err = a.GetUserByEmail(newEmail)
|
||||
if err == nil {
|
||||
_, appErr := a.GetUserByEmail(newEmail)
|
||||
if appErr == nil {
|
||||
return nil, model.NewAppError("UpdateUser", "store.sql_user.update.email_taken.app_error", nil, "user_id="+user.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
@@ -1166,7 +1278,16 @@ func (a *App) UpdateUser(user *model.User, sendNotifications bool) (*model.User,
|
||||
|
||||
userUpdate, err := a.Srv().Store.User().Update(user, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("UpdateUser", "app.user.update.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateUser", "app.user.update.finding.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if sendNotifications {
|
||||
@@ -1440,7 +1561,7 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
userUpdate, err := a.Srv().Store.User().Update(user, true)
|
||||
uchan <- store.StoreResult{Data: userUpdate, Err: err}
|
||||
uchan <- store.StoreResult{Data: userUpdate, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -1452,8 +1573,17 @@ func (a *App) UpdateUserRoles(userId string, newRoles string, sendWebSocketEvent
|
||||
}()
|
||||
|
||||
result := <-uchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
if result.NErr != nil {
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(result.NErr, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(result.NErr, &invErr):
|
||||
return nil, model.NewAppError("UpdateUserRoles", "app.user.update.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return nil, model.NewAppError("UpdateUserRoles", "app.user.update.finding.app_error", nil, result.NErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
ruser := result.Data.(*model.UserUpdate).New
|
||||
|
||||
@@ -1572,7 +1702,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.User().PermanentDelete(user.Id); err != nil {
|
||||
return err
|
||||
return model.NewAppError("PermanentDeleteUser", "app.user.permanent_delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if err := a.Srv().Store.Audit().PermanentDeleteByUser(user.Id); err != nil {
|
||||
@@ -1591,7 +1721,7 @@ func (a *App) PermanentDeleteUser(user *model.User) *model.AppError {
|
||||
func (a *App) PermanentDeleteAllUsers() *model.AppError {
|
||||
users, err := a.Srv().Store.User().GetAll()
|
||||
if err != nil {
|
||||
return err
|
||||
return model.NewAppError("PermanentDeleteAllUsers", "app.user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
for _, user := range users {
|
||||
a.PermanentDeleteUser(user)
|
||||
@@ -1674,7 +1804,7 @@ func (a *App) GetTotalUsersStats(viewRestrictions *model.ViewUsersRestrictions)
|
||||
ViewRestrictions: viewRestrictions,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetTotalUsersStats", "app.user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
stats := &model.UsersStats{
|
||||
TotalUsersCount: count,
|
||||
@@ -1686,7 +1816,7 @@ func (a *App) GetTotalUsersStats(viewRestrictions *model.ViewUsersRestrictions)
|
||||
func (a *App) GetFilteredUsersStats(options *model.UserCountOptions) (*model.UsersStats, *model.AppError) {
|
||||
count, err := a.Srv().Store.User().Count(*options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("GetFilteredUsersStats", "app.user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
stats := &model.UsersStats{
|
||||
TotalUsersCount: count,
|
||||
@@ -1696,7 +1826,7 @@ func (a *App) GetFilteredUsersStats(options *model.UserCountOptions) (*model.Use
|
||||
|
||||
func (a *App) VerifyUserEmail(userId, email string) *model.AppError {
|
||||
if _, err := a.Srv().Store.User().VerifyEmail(userId, email); err != nil {
|
||||
return err
|
||||
return model.NewAppError("VerifyUserEmail", "app.user.verify_email.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForUser(userId)
|
||||
@@ -1735,7 +1865,7 @@ func (a *App) SearchUsersInChannel(channelId string, term string, options *model
|
||||
term = strings.TrimSpace(term)
|
||||
users, err := a.Srv().Store.User().SearchInChannel(channelId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("SearchUsersInChannel", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
for _, user := range users {
|
||||
a.SanitizeProfile(user, options.IsAdmin)
|
||||
@@ -1748,7 +1878,7 @@ func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term stri
|
||||
term = strings.TrimSpace(term)
|
||||
users, err := a.Srv().Store.User().SearchNotInChannel(teamId, channelId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("SearchUsersNotInChannel", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1759,13 +1889,11 @@ func (a *App) SearchUsersNotInChannel(teamId string, channelId string, term stri
|
||||
}
|
||||
|
||||
func (a *App) SearchUsersInTeam(teamId, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
|
||||
var users []*model.User
|
||||
var err *model.AppError
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
users, err = a.Srv().Store.User().Search(teamId, term, options)
|
||||
users, err := a.Srv().Store.User().Search(teamId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("SearchUsersInTeam", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1779,7 +1907,7 @@ func (a *App) SearchUsersNotInTeam(notInTeamId string, term string, options *mod
|
||||
term = strings.TrimSpace(term)
|
||||
users, err := a.Srv().Store.User().SearchNotInTeam(notInTeamId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("SearchUsersNotInTeam", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1793,7 +1921,7 @@ func (a *App) SearchUsersWithoutTeam(term string, options *model.UserSearchOptio
|
||||
term = strings.TrimSpace(term)
|
||||
users, err := a.Srv().Store.User().SearchWithoutTeam(term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("SearchUsersWithoutTeam", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1807,7 +1935,7 @@ func (a *App) SearchUsersInGroup(groupID string, term string, options *model.Use
|
||||
term = strings.TrimSpace(term)
|
||||
users, err := a.Srv().Store.User().SearchInGroup(groupID, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("SearchUsersInGroup", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1822,7 +1950,7 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
|
||||
|
||||
autocomplete, err := a.Srv().Store.User().AutocompleteUsersInChannel(teamId, channelId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("AutocompleteUsersInChannel", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range autocomplete.InChannel {
|
||||
@@ -1837,13 +1965,11 @@ func (a *App) AutocompleteUsersInChannel(teamId string, channelId string, term s
|
||||
}
|
||||
|
||||
func (a *App) AutocompleteUsersInTeam(teamId string, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInTeam, *model.AppError) {
|
||||
var err *model.AppError
|
||||
|
||||
term = strings.TrimSpace(term)
|
||||
|
||||
users, err := a.Srv().Store.User().Search(teamId, term, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, model.NewAppError("AutocompleteUsersInTeam", "app.user.search.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, user := range users {
|
||||
@@ -1892,7 +2018,16 @@ func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provide
|
||||
if userAttrsChanged {
|
||||
users, err := a.Srv().Store.User().Update(user, true)
|
||||
if err != nil {
|
||||
return err
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return appErr
|
||||
case errors.As(err, &invErr):
|
||||
return model.NewAppError("UpdateOAuthUserAttrs", "app.user.update.find.app_error", nil, invErr.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
return model.NewAppError("UpdateOAuthUserAttrs", "app.user.update.finding.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
user = users.New
|
||||
@@ -2047,10 +2182,10 @@ func (a *App) GetViewUsersRestrictions(userId string) (*model.ViewUsersRestricti
|
||||
// PromoteGuestToUser Convert user's roles and all his mermbership's roles from
|
||||
// guest roles to regular user roles.
|
||||
func (a *App) PromoteGuestToUser(user *model.User, requestorId string) *model.AppError {
|
||||
err := a.Srv().Store.User().PromoteGuestToUser(user.Id)
|
||||
nErr := a.Srv().Store.User().PromoteGuestToUser(user.Id)
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
if nErr != nil {
|
||||
return model.NewAppError("PromoteGuestToUser", "app.user.promote_guest.user_update.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
userTeams, nErr := a.Srv().Store.Team().GetTeamsByUserId(user.Id)
|
||||
if nErr != nil {
|
||||
@@ -2059,7 +2194,7 @@ func (a *App) PromoteGuestToUser(user *model.User, requestorId string) *model.Ap
|
||||
|
||||
for _, team := range userTeams {
|
||||
// Soft error if there is an issue joining the default channels
|
||||
if err = a.JoinDefaultChannels(team.Id, user, false, requestorId); err != nil {
|
||||
if err := a.JoinDefaultChannels(team.Id, user, false, requestorId); err != nil {
|
||||
mlog.Error("Failed to join default channels", mlog.String("user_id", user.Id), mlog.String("team_id", team.Id), mlog.String("requestor_id", requestorId), mlog.Err(err))
|
||||
}
|
||||
}
|
||||
@@ -2101,10 +2236,10 @@ func (a *App) PromoteGuestToUser(user *model.User, requestorId string) *model.Ap
|
||||
// DemoteUserToGuest Convert user's roles and all his mermbership's roles from
|
||||
// regular user roles to guest roles.
|
||||
func (a *App) DemoteUserToGuest(user *model.User) *model.AppError {
|
||||
err := a.Srv().Store.User().DemoteUserToGuest(user.Id)
|
||||
nErr := a.Srv().Store.User().DemoteUserToGuest(user.Id)
|
||||
a.InvalidateCacheForUser(user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
if nErr != nil {
|
||||
return model.NewAppError("DemoteUserToGuest", "app.user.demote_user_to_guest.user_update.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
demotedUser, err := a.GetUser(user.Id)
|
||||
@@ -2176,29 +2311,40 @@ func (a *App) invalidateUserCacheAndPublish(userId string) {
|
||||
// relationship with a user. That means any user sharing any channel, including
|
||||
// direct and group channels.
|
||||
func (a *App) GetKnownUsers(userID string) ([]string, *model.AppError) {
|
||||
return a.Srv().Store.User().GetKnownUsers(userID)
|
||||
users, err := a.Srv().Store.User().GetKnownUsers(userID)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("GetKnownUsers", "app.user.get_known_users.get_users.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// ConvertBotToUser converts a bot to user.
|
||||
func (a *App) ConvertBotToUser(bot *model.Bot, userPatch *model.UserPatch, sysadmin bool) (*model.User, *model.AppError) {
|
||||
user, err := a.Srv().Store.User().Get(bot.UserId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
user, nErr := a.Srv().Store.User().Get(bot.UserId)
|
||||
if nErr != nil {
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(nErr, &nfErr):
|
||||
return nil, model.NewAppError("ConvertBotToUser", MISSING_ACCOUNT_ERROR, nil, nfErr.Error(), http.StatusNotFound)
|
||||
default:
|
||||
return nil, model.NewAppError("ConvertBotToUser", "app.user.get.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
if sysadmin && !user.IsInRole(model.SYSTEM_ADMIN_ROLE_ID) {
|
||||
_, err = a.UpdateUserRoles(
|
||||
_, appErr := a.UpdateUserRoles(
|
||||
user.Id,
|
||||
fmt.Sprintf("%s %s", user.Roles, model.SYSTEM_ADMIN_ROLE_ID),
|
||||
false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if appErr != nil {
|
||||
return nil, appErr
|
||||
}
|
||||
}
|
||||
|
||||
user.Patch(userPatch)
|
||||
|
||||
user, err = a.UpdateUser(user, false)
|
||||
user, err := a.UpdateUser(user, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -398,8 +398,8 @@ func TestUpdateUserEmail(t *testing.T) {
|
||||
Username: model.NewId(),
|
||||
IsBot: true,
|
||||
}
|
||||
_, err = th.App.Srv().Store.User().Save(&botuser)
|
||||
assert.Nil(t, err)
|
||||
_, nErr := th.App.Srv().Store.User().Save(&botuser)
|
||||
assert.Nil(t, nErr)
|
||||
|
||||
newBotEmail := th.MakeEmail()
|
||||
botuser.Email = newBotEmail
|
||||
@@ -441,8 +441,8 @@ func TestUpdateUserEmail(t *testing.T) {
|
||||
Username: model.NewId(),
|
||||
IsBot: true,
|
||||
}
|
||||
_, err = th.App.Srv().Store.User().Save(&botuser)
|
||||
assert.Nil(t, err)
|
||||
_, nErr := th.App.Srv().Store.User().Save(&botuser)
|
||||
assert.Nil(t, nErr)
|
||||
|
||||
newBotEmail := th.MakeEmail()
|
||||
botuser.Email = newBotEmail
|
||||
|
||||
@@ -676,7 +676,7 @@ func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookReq
|
||||
uchan := make(chan store.StoreResult, 1)
|
||||
go func() {
|
||||
user, err := a.Srv().Store.User().Get(hook.UserId)
|
||||
uchan <- store.StoreResult{Data: user, Err: err}
|
||||
uchan <- store.StoreResult{Data: user, NErr: err}
|
||||
close(uchan)
|
||||
}()
|
||||
|
||||
@@ -700,7 +700,7 @@ func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookReq
|
||||
if len(channelName) != 0 {
|
||||
if channelName[0] == '@' {
|
||||
if result, err := a.Srv().Store.User().GetByUsername(channelName[1:]); err != nil {
|
||||
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+err.Message, http.StatusBadRequest)
|
||||
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
if ch, err := a.GetOrCreateDirectChannel(hook.UserId, result.Id); err != nil {
|
||||
return err
|
||||
@@ -757,8 +757,8 @@ func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookReq
|
||||
}
|
||||
|
||||
var user *model.User
|
||||
if result := <-uchan; result.Err != nil {
|
||||
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, "err="+result.Err.Message, http.StatusForbidden)
|
||||
if result := <-uchan; result.NErr != nil {
|
||||
return model.NewAppError("HandleIncomingWebhook", "web.incoming_webhook.user.app_error", nil, result.NErr.Error(), http.StatusForbidden)
|
||||
} else {
|
||||
user = result.Data.(*model.User)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user