summaryrefslogtreecommitdiffstats
path: root/store/sqlstore/user_store.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-10-06 08:12:10 -0700
committerJoram Wilander <jwawilander@gmail.com>2017-10-06 11:12:10 -0400
commit363568b4eb3209adb1b88ceb0d8e455e6d4a1073 (patch)
tree419fb74d8e1a2e91f9f77aa5b873c005bcfbff48 /store/sqlstore/user_store.go
parent12501673d0c70120eebeac633e5072b2e7a2174d (diff)
downloadchat-363568b4eb3209adb1b88ceb0d8e455e6d4a1073.tar.gz
chat-363568b4eb3209adb1b88ceb0d8e455e6d4a1073.tar.bz2
chat-363568b4eb3209adb1b88ceb0d8e455e6d4a1073.zip
reduce store boiler plate (#7585)
Diffstat (limited to 'store/sqlstore/user_store.go')
-rw-r--r--store/sqlstore/user_store.go616
1 files changed, 95 insertions, 521 deletions
diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go
index abc2d2d28..5d0e1c50d 100644
--- a/store/sqlstore/user_store.go
+++ b/store/sqlstore/user_store.go
@@ -93,23 +93,14 @@ func (us SqlUserStore) CreateIndexesIfNotExists() {
}
func (us SqlUserStore) Save(user *model.User) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if len(user.Id) > 0 {
result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.existing.app_error", nil, "user_id="+user.Id, http.StatusBadRequest)
- storeChannel <- result
- close(storeChannel)
return
}
user.PreSave()
if result.Err = user.IsValid(); result.Err != nil {
- storeChannel <- result
- close(storeChannel)
return
}
@@ -124,25 +115,14 @@ func (us SqlUserStore) Save(user *model.User) store.StoreChannel {
} else {
result.Data = user
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) Update(user *model.User, trustedUpdateData bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
user.PreUpdate()
if result.Err = user.IsValid(); result.Err != nil {
- storeChannel <- result
- close(storeChannel)
return
}
@@ -176,8 +156,6 @@ func (us SqlUserStore) Update(user *model.User, trustedUpdateData bool) store.St
if user.Username != oldUser.Username ||
user.Email != oldUser.Email {
result.Err = model.NewAppError("SqlUserStore.Update", "store.sql_user.update.can_not_change_ldap.app_error", nil, "user_id="+user.Id, http.StatusBadRequest)
- storeChannel <- result
- close(storeChannel)
return
}
} else if user.Email != oldUser.Email {
@@ -204,20 +182,11 @@ func (us SqlUserStore) Update(user *model.User, trustedUpdateData bool) store.St
result.Data = [2]*model.User{user, oldUser}
}
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdateLastPictureUpdate(userId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
curTime := model.GetMillis()
if _, err := us.GetMaster().Exec("UPDATE Users SET LastPictureUpdate = :Time, UpdateAt = :Time WHERE Id = :UserId", map[string]interface{}{"Time": curTime, "UserId": userId}); err != nil {
@@ -225,20 +194,11 @@ func (us SqlUserStore) UpdateLastPictureUpdate(userId string) store.StoreChannel
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdateUpdateAt(userId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
curTime := model.GetMillis()
if _, err := us.GetMaster().Exec("UPDATE Users SET UpdateAt = :Time WHERE Id = :UserId", map[string]interface{}{"Time": curTime, "UserId": userId}); err != nil {
@@ -246,21 +206,11 @@ func (us SqlUserStore) UpdateUpdateAt(userId string) store.StoreChannel {
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdatePassword(userId, hashedPassword string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
updateAt := model.GetMillis()
if _, err := us.GetMaster().Exec("UPDATE Users SET Password = :Password, LastPasswordUpdate = :LastPasswordUpdate, UpdateAt = :UpdateAt, AuthData = NULL, AuthService = '', EmailVerified = true, FailedAttempts = 0 WHERE Id = :UserId", map[string]interface{}{"Password": hashedPassword, "LastPasswordUpdate": updateAt, "UpdateAt": updateAt, "UserId": userId}); err != nil {
@@ -268,40 +218,21 @@ func (us SqlUserStore) UpdatePassword(userId, hashedPassword string) store.Store
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdateFailedPasswordAttempts(userId string, attempts int) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if _, err := us.GetMaster().Exec("UPDATE Users SET FailedAttempts = :FailedAttempts WHERE Id = :UserId", map[string]interface{}{"FailedAttempts": attempts, "UserId": userId}); err != nil {
result.Err = model.NewAppError("SqlUserStore.UpdateFailedPasswordAttempts", "store.sql_user.update_failed_pwd_attempts.app_error", nil, "user_id="+userId, http.StatusInternalServerError)
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *string, email string, resetMfa bool) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
email = strings.ToLower(email)
updateAt := model.GetMillis()
@@ -336,21 +267,11 @@ func (us SqlUserStore) UpdateAuthData(userId string, service string, authData *s
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdateMfaSecret(userId, secret string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
updateAt := model.GetMillis()
if _, err := us.GetMaster().Exec("UPDATE Users SET MfaSecret = :Secret, UpdateAt = :UpdateAt WHERE Id = :UserId", map[string]interface{}{"Secret": secret, "UpdateAt": updateAt, "UserId": userId}); err != nil {
@@ -358,21 +279,11 @@ func (us SqlUserStore) UpdateMfaSecret(userId, secret string) store.StoreChannel
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) UpdateMfaActive(userId string, active bool) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
updateAt := model.GetMillis()
if _, err := us.GetMaster().Exec("UPDATE Users SET MfaActive = :Active, UpdateAt = :UpdateAt WHERE Id = :UserId", map[string]interface{}{"Active": active, "UpdateAt": updateAt, "UserId": userId}); err != nil {
@@ -380,21 +291,11 @@ func (us SqlUserStore) UpdateMfaActive(userId string, active bool) store.StoreCh
} else {
result.Data = userId
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) Get(id string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if obj, err := us.GetReplica().Get(model.User{}, id); err != nil {
result.Err = model.NewAppError("SqlUserStore.Get", "store.sql_user.get.app_error", nil, "user_id="+id+", "+err.Error(), http.StatusInternalServerError)
} else if obj == nil {
@@ -402,64 +303,33 @@ func (us SqlUserStore) Get(id string) store.StoreChannel {
} else {
result.Data = obj.(*model.User)
}
-
- storeChannel <- result
- close(storeChannel)
-
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetAll() store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var data []*model.User
if _, err := us.GetReplica().Select(&data, "SELECT * FROM Users"); err != nil {
result.Err = model.NewAppError("SqlUserStore.GetAll", "store.sql_user.get.app_error", nil, err.Error(), http.StatusInternalServerError)
}
result.Data = data
-
- storeChannel <- result
- close(storeChannel)
-
- }()
-
- return storeChannel
+ })
}
func (s SqlUserStore) GetEtagForAllProfiles() store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
updateAt, err := s.GetReplica().SelectInt("SELECT UpdateAt FROM Users ORDER BY UpdateAt DESC LIMIT 1")
if err != nil {
result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, model.GetMillis(), utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
} else {
result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, updateAt, utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetAllProfiles(offset int, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
if _, err := us.GetReplica().Select(&users, "SELECT * FROM Users ORDER BY Username ASC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil {
@@ -472,41 +342,22 @@ func (us SqlUserStore) GetAllProfiles(offset int, limit int) store.StoreChannel
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (s SqlUserStore) GetEtagForProfiles(teamId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
updateAt, err := s.GetReplica().SelectInt("SELECT UpdateAt FROM Users, TeamMembers WHERE TeamMembers.TeamId = :TeamId AND Users.Id = TeamMembers.UserId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"TeamId": teamId})
if err != nil {
result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, model.GetMillis(), utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
} else {
result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, updateAt, utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetProfiles(teamId string, offset int, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
if _, err := us.GetReplica().Select(&users, "SELECT Users.* FROM Users, TeamMembers WHERE TeamMembers.TeamId = :TeamId AND Users.Id = TeamMembers.UserId AND TeamMembers.DeleteAt = 0 ORDER BY Users.Username ASC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"TeamId": teamId, "Offset": offset, "Limit": limit}); err != nil {
@@ -519,12 +370,7 @@ func (us SqlUserStore) GetProfiles(teamId string, offset int, limit int) store.S
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) InvalidateProfilesInChannelCacheByUser(userId string) {
@@ -545,12 +391,7 @@ func (us SqlUserStore) InvalidateProfilesInChannelCache(channelId string) {
}
func (us SqlUserStore) GetProfilesInChannel(channelId string, offset int, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
query := "SELECT Users.* FROM Users, ChannelMembers WHERE ChannelMembers.ChannelId = :ChannelId AND Users.Id = ChannelMembers.UserId ORDER BY Users.Username ASC LIMIT :Limit OFFSET :Offset"
@@ -565,29 +406,17 @@ func (us SqlUserStore) GetProfilesInChannel(channelId string, offset int, limit
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetAllProfilesInChannel(channelId string, allowFromCache bool) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if allowFromCache {
if cacheItem, ok := profilesInChannelCache.Get(channelId); ok {
if us.metrics != nil {
us.metrics.IncrementMemCacheHitCounter("Profiles in Channel")
}
result.Data = cacheItem.(map[string]*model.User)
- storeChannel <- result
- close(storeChannel)
return
} else {
if us.metrics != nil {
@@ -621,21 +450,11 @@ func (us SqlUserStore) GetAllProfilesInChannel(channelId string, allowFromCache
profilesInChannelCache.AddWithExpiresInSecs(channelId, userMap, PROFILES_IN_CHANNEL_CACHE_SEC)
}
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetProfilesNotInChannel(teamId string, channelId string, offset int, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
if _, err := us.GetReplica().Select(&users, `
@@ -662,20 +481,11 @@ func (us SqlUserStore) GetProfilesNotInChannel(teamId string, channelId string,
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetProfilesWithoutTeam(offset int, limit int) store.StoreChannel {
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
query := `
@@ -708,20 +518,11 @@ func (us SqlUserStore) GetProfilesWithoutTeam(offset int, limit int) store.Store
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetProfilesByUsernames(usernames []string, teamId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
props := make(map[string]interface{})
idQuery := ""
@@ -749,12 +550,7 @@ func (us SqlUserStore) GetProfilesByUsernames(usernames []string, teamId string)
} else {
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
type UserWithLastActivityAt struct {
@@ -763,12 +559,7 @@ type UserWithLastActivityAt struct {
}
func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string, offset, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*UserWithLastActivityAt
if _, err := us.GetReplica().Select(&users, `
@@ -796,21 +587,11 @@ func (us SqlUserStore) GetRecentlyActiveUsersForTeam(teamId string, offset, limi
result.Data = userList
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetNewUsersForTeam(teamId string, offset, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
if _, err := us.GetReplica().Select(&users, `
@@ -830,21 +611,11 @@ func (us SqlUserStore) GetNewUsersForTeam(teamId string, offset, limit int) stor
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetProfileByIds(userIds []string, allowFromCache bool) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
users := []*model.User{}
props := make(map[string]interface{})
idQuery := ""
@@ -874,8 +645,6 @@ func (us SqlUserStore) GetProfileByIds(userIds []string, allowFromCache bool) st
// If everything came from the cache then just return
if len(remainingUserIds) == 0 {
result.Data = users
- storeChannel <- result
- close(storeChannel)
return
}
@@ -902,21 +671,11 @@ func (us SqlUserStore) GetProfileByIds(userIds []string, allowFromCache bool) st
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetSystemAdminProfiles() store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
if _, err := us.GetReplica().Select(&users, "SELECT * FROM Users WHERE Roles LIKE :Roles", map[string]interface{}{"Roles": "%system_admin%"}); err != nil {
@@ -932,21 +691,11 @@ func (us SqlUserStore) GetSystemAdminProfiles() store.StoreChannel {
result.Data = userMap
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetByEmail(email string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
email = strings.ToLower(email)
user := model.User{}
@@ -956,25 +705,13 @@ func (us SqlUserStore) GetByEmail(email string) store.StoreChannel {
}
result.Data = &user
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetByAuth(authData *string, authService string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if authData == nil || *authData == "" {
result.Err = model.NewAppError("SqlUserStore.GetByAuth", store.MISSING_AUTH_ACCOUNT_ERROR, nil, "authData='', authService="+authService, http.StatusBadRequest)
- storeChannel <- result
- close(storeChannel)
return
}
@@ -989,20 +726,11 @@ func (us SqlUserStore) GetByAuth(authData *string, authService string) store.Sto
}
result.Data = &user
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetAllUsingAuthService(authService string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
+ return store.Do(func(result *store.StoreResult) {
var data []*model.User
if _, err := us.GetReplica().Select(&data, "SELECT * FROM Users WHERE AuthService = :AuthService", map[string]interface{}{"AuthService": authService}); err != nil {
@@ -1010,21 +738,11 @@ func (us SqlUserStore) GetAllUsingAuthService(authService string) store.StoreCha
}
result.Data = data
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetByUsername(username string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
user := model.User{}
if err := us.GetReplica().SelectOne(&user, "SELECT * FROM Users WHERE Username = :Username", map[string]interface{}{"Username": username}); err != nil {
@@ -1032,20 +750,11 @@ func (us SqlUserStore) GetByUsername(username string) store.StoreChannel {
}
result.Data = &user
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetForLogin(loginId string, allowSignInWithUsername, allowSignInWithEmail, ldapEnabled bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
params := map[string]interface{}{
"LoginId": loginId,
"AllowSignInWithUsername": allowSignInWithUsername,
@@ -1073,77 +782,39 @@ func (us SqlUserStore) GetForLogin(loginId string, allowSignInWithUsername, allo
} else {
result.Err = model.NewAppError("SqlUserStore.GetForLogin", "store.sql_user.get_for_login.app_error", nil, "", http.StatusInternalServerError)
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) VerifyEmail(userId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if _, err := us.GetMaster().Exec("UPDATE Users SET EmailVerified = true WHERE Id = :UserId", map[string]interface{}{"UserId": userId}); err != nil {
result.Err = model.NewAppError("SqlUserStore.VerifyEmail", "store.sql_user.verify_email.app_error", nil, "userId="+userId+", "+err.Error(), http.StatusInternalServerError)
}
result.Data = userId
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetTotalUsersCount() store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if count, err := us.GetReplica().SelectInt("SELECT COUNT(Id) FROM Users"); err != nil {
result.Err = model.NewAppError("SqlUserStore.GetTotalUsersCount", "store.sql_user.get_total_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = count
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) PermanentDelete(userId string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if _, err := us.GetMaster().Exec("DELETE FROM Users WHERE Id = :UserId", map[string]interface{}{"UserId": userId}); err != nil {
result.Err = model.NewAppError("SqlUserStore.PermanentDelete", "store.sql_user.permanent_delete.app_error", nil, "userId="+userId+", "+err.Error(), http.StatusInternalServerError)
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) AnalyticsUniqueUserCount(teamId string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
query := ""
if len(teamId) > 0 {
query = "SELECT COUNT(DISTINCT Users.Email) From Users, TeamMembers WHERE TeamMembers.TeamId = :TeamId AND Users.Id = TeamMembers.UserId AND TeamMembers.DeleteAt = 0 AND Users.DeleteAt = 0"
@@ -1157,21 +828,11 @@ func (us SqlUserStore) AnalyticsUniqueUserCount(teamId string) store.StoreChanne
} else {
result.Data = v
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
time := model.GetMillis() - timePeriod
query := "SELECT COUNT(*) FROM Status WHERE LastActivityAt > :Time"
@@ -1182,20 +843,11 @@ func (us SqlUserStore) AnalyticsActiveCount(timePeriod int64) store.StoreChannel
} else {
result.Data = v
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetUnreadCount(userId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if count, err := us.GetReplica().SelectInt(`
SELECT SUM(CASE WHEN c.Type = 'D' THEN (c.TotalMsgCount - cm.MsgCount) ELSE cm.MentionCount END)
FROM Channels c
@@ -1207,37 +859,21 @@ func (us SqlUserStore) GetUnreadCount(userId string) store.StoreChannel {
} else {
result.Data = count
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetUnreadCountForChannel(userId string, channelId string) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if count, err := us.GetReplica().SelectInt("SELECT SUM(CASE WHEN c.Type = 'D' THEN (c.TotalMsgCount - cm.MsgCount) ELSE cm.MentionCount END) FROM Channels c INNER JOIN ChannelMembers cm ON c.Id = :ChannelId AND cm.ChannelId = :ChannelId AND cm.UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}); err != nil {
result.Err = model.NewAppError("SqlUserStore.GetMentionCountForChannel", "store.sql_user.get_unread_count_for_channel.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = count
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) Search(teamId string, term string, options map[string]bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
+ return store.Do(func(result *store.StoreResult) {
searchQuery := ""
if teamId == "" {
@@ -1270,18 +906,13 @@ func (us SqlUserStore) Search(teamId string, term string, options map[string]boo
LIMIT 100`
}
- storeChannel <- us.performSearch(searchQuery, term, options, map[string]interface{}{"TeamId": teamId})
- close(storeChannel)
-
- }()
+ *result = us.performSearch(searchQuery, term, options, map[string]interface{}{"TeamId": teamId})
- return storeChannel
+ })
}
func (us SqlUserStore) SearchWithoutTeam(term string, options map[string]bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
+ return store.Do(func(result *store.StoreResult) {
searchQuery := `
SELECT
*
@@ -1300,18 +931,13 @@ func (us SqlUserStore) SearchWithoutTeam(term string, options map[string]bool) s
ORDER BY Username ASC
LIMIT 100`
- storeChannel <- us.performSearch(searchQuery, term, options, map[string]interface{}{})
- close(storeChannel)
-
- }()
+ *result = us.performSearch(searchQuery, term, options, map[string]interface{}{})
- return storeChannel
+ })
}
func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options map[string]bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
+ return store.Do(func(result *store.StoreResult) {
searchQuery := `
SELECT
Users.*
@@ -1326,18 +952,13 @@ func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options
ORDER BY Users.Username ASC
LIMIT 100`
- storeChannel <- us.performSearch(searchQuery, term, options, map[string]interface{}{"NotInTeamId": notInTeamId})
- close(storeChannel)
+ *result = us.performSearch(searchQuery, term, options, map[string]interface{}{"NotInTeamId": notInTeamId})
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) SearchNotInChannel(teamId string, channelId string, term string, options map[string]bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
+ return store.Do(func(result *store.StoreResult) {
searchQuery := ""
if teamId == "" {
searchQuery = `
@@ -1373,18 +994,13 @@ func (us SqlUserStore) SearchNotInChannel(teamId string, channelId string, term
LIMIT 100`
}
- storeChannel <- us.performSearch(searchQuery, term, options, map[string]interface{}{"TeamId": teamId, "ChannelId": channelId})
- close(storeChannel)
-
- }()
+ *result = us.performSearch(searchQuery, term, options, map[string]interface{}{"TeamId": teamId, "ChannelId": channelId})
- return storeChannel
+ })
}
func (us SqlUserStore) SearchInChannel(channelId string, term string, options map[string]bool) store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
+ return store.Do(func(result *store.StoreResult) {
searchQuery := `
SELECT
Users.*
@@ -1398,12 +1014,9 @@ func (us SqlUserStore) SearchInChannel(channelId string, term string, options ma
ORDER BY Users.Username ASC
LIMIT 100`
- storeChannel <- us.performSearch(searchQuery, term, options, map[string]interface{}{"ChannelId": channelId})
- close(storeChannel)
-
- }()
+ *result = us.performSearch(searchQuery, term, options, map[string]interface{}{"ChannelId": channelId})
- return storeChannel
+ })
}
var escapeUserSearchChar = []string{
@@ -1483,51 +1096,27 @@ func (us SqlUserStore) performSearch(searchQuery string, term string, options ma
}
func (us SqlUserStore) AnalyticsGetInactiveUsersCount() store.StoreChannel {
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if count, err := us.GetReplica().SelectInt("SELECT COUNT(Id) FROM Users WHERE DeleteAt > 0"); err != nil {
result.Err = model.NewAppError("SqlUserStore.AnalyticsGetInactiveUsersCount", "store.sql_user.analytics_get_inactive_users_count.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = count
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) AnalyticsGetSystemAdminCount() store.StoreChannel {
-
- storeChannel := make(store.StoreChannel, 1)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
if count, err := us.GetReplica().SelectInt("SELECT count(*) FROM Users WHERE Roles LIKE :Roles and DeleteAt = 0", map[string]interface{}{"Roles": "%system_admin%"}); err != nil {
result.Err = model.NewAppError("SqlUserStore.AnalyticsGetSystemAdminCount", "store.sql_user.analytics_get_system_admin_count.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = count
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetProfilesNotInTeam(teamId string, offset int, limit int) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
var users []*model.User
if _, err := us.GetReplica().Select(&users, `
@@ -1551,21 +1140,11 @@ func (us SqlUserStore) GetProfilesNotInTeam(teamId string, offset int, limit int
result.Data = users
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}
func (us SqlUserStore) GetEtagForProfilesNotInTeam(teamId string) store.StoreChannel {
-
- storeChannel := make(store.StoreChannel)
-
- go func() {
- result := store.StoreResult{}
-
+ return store.Do(func(result *store.StoreResult) {
updateAt, err := us.GetReplica().SelectInt(`
SELECT
u.UpdateAt
@@ -1584,10 +1163,5 @@ func (us SqlUserStore) GetEtagForProfilesNotInTeam(teamId string) store.StoreCha
} else {
result.Data = fmt.Sprintf("%v.%v.%v.%v", model.CurrentVersion, updateAt, utils.Cfg.PrivacySettings.ShowFullName, utils.Cfg.PrivacySettings.ShowEmailAddress)
}
-
- storeChannel <- result
- close(storeChannel)
- }()
-
- return storeChannel
+ })
}