From 363568b4eb3209adb1b88ceb0d8e455e6d4a1073 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 6 Oct 2017 08:12:10 -0700 Subject: reduce store boiler plate (#7585) --- store/sqlstore/audit_store.go | 57 +-- store/sqlstore/channel_store.go | 549 ++++------------------ store/sqlstore/cluster_discovery_store.go | 81 +--- store/sqlstore/cluster_discovery_store_test.go | 2 +- store/sqlstore/command_store.go | 121 +---- store/sqlstore/command_webhook_store.go | 43 +- store/sqlstore/compliance_store.go | 74 +-- store/sqlstore/compliance_store_test.go | 2 +- store/sqlstore/emoji_store.go | 69 +-- store/sqlstore/emoji_store_test.go | 2 +- store/sqlstore/file_info_store.go | 108 +---- store/sqlstore/file_info_store_test.go | 8 +- store/sqlstore/job_store.go | 155 +------ store/sqlstore/license_store.go | 31 +- store/sqlstore/license_store_test.go | 2 +- store/sqlstore/oauth_store.go | 266 ++--------- store/sqlstore/oauth_store_test.go | 2 +- store/sqlstore/post_store.go | 380 +++------------ store/sqlstore/post_store_test.go | 2 +- store/sqlstore/preference_store.go | 131 +----- store/sqlstore/preference_store_test.go | 2 +- store/sqlstore/reaction_store_test.go | 2 +- store/sqlstore/session_store.go | 159 +------ store/sqlstore/session_store_test.go | 2 +- store/sqlstore/status_store.go | 117 +---- store/sqlstore/status_store_test.go | 2 +- store/sqlstore/system_store.go | 70 +-- store/sqlstore/system_store_test.go | 2 +- store/sqlstore/team_store.go | 393 +++------------- store/sqlstore/team_store_test.go | 2 +- store/sqlstore/tokens_store.go | 44 +- store/sqlstore/user_access_token_store.go | 90 +--- store/sqlstore/user_access_token_store_test.go | 2 +- store/sqlstore/user_store.go | 616 ++++--------------------- store/sqlstore/webhook_store.go | 270 ++--------- 35 files changed, 589 insertions(+), 3269 deletions(-) (limited to 'store') diff --git a/store/sqlstore/audit_store.go b/store/sqlstore/audit_store.go index 4910db79b..3cc0758cc 100644 --- a/store/sqlstore/audit_store.go +++ b/store/sqlstore/audit_store.go @@ -36,38 +36,21 @@ func (s SqlAuditStore) CreateIndexesIfNotExists() { } func (s SqlAuditStore) Save(audit *model.Audit) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { audit.Id = model.NewId() audit.CreateAt = model.GetMillis() if err := s.GetMaster().Insert(audit); err != nil { result.Err = model.NewAppError("SqlAuditStore.Save", "store.sql_audit.save.saving.app_error", nil, "user_id="+audit.UserId+" action="+audit.Action, http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlAuditStore) Get(user_id string, offset int, limit int) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if limit > 1000 { limit = 1000 result.Err = model.NewAppError("SqlAuditStore.Get", "store.sql_audit.get.limit.app_error", nil, "user_id="+user_id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } @@ -85,39 +68,20 @@ func (s SqlAuditStore) Get(user_id string, offset int, limit int) store.StoreCha } else { result.Data = audits } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlAuditStore) PermanentDeleteByUser(userId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM Audits WHERE UserId = :userId", map[string]interface{}{"userId": userId}); err != nil { result.Err = model.NewAppError("SqlAuditStore.Delete", "store.sql_audit.permanent_delete_by_user.app_error", nil, "user_id="+userId, http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlAuditStore) PermanentDeleteBatch(endTime int64, limit int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var query string if *utils.Cfg.SqlSettings.DriverName == "postgres" { query = "DELETE from Audits WHERE Id = any (array (SELECT Id FROM Audits WHERE CreateAt < :EndTime LIMIT :Limit))" @@ -137,10 +101,5 @@ func (s SqlAuditStore) PermanentDeleteBatch(endTime int64, limit int64) store.St result.Data = rowsAffected } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index c9a4f695e..1ddc887bd 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -92,17 +92,14 @@ func (s SqlChannelStore) CreateIndexesIfNotExists() { } func (s SqlChannelStore) Save(channel *model.Channel) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - var result store.StoreResult + return store.Do(func(result *store.StoreResult) { if channel.Type == model.CHANNEL_DIRECT { result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save.direct_channel.app_error", nil, "", http.StatusBadRequest) } else { if transaction, err := s.GetMaster().Begin(); err != nil { result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) } else { - result = s.saveChannelT(transaction, channel) + *result = s.saveChannelT(transaction, channel) if result.Err != nil { transaction.Rollback() } else { @@ -112,12 +109,7 @@ func (s SqlChannelStore) Save(channel *model.Channel) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) CreateDirectChannel(userId string, otherUserId string) store.StoreChannel { @@ -144,11 +136,7 @@ func (s SqlChannelStore) CreateDirectChannel(userId string, otherUserId string) } func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1 *model.ChannelMember, member2 *model.ChannelMember) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - var result store.StoreResult - + return store.Do(func(result *store.StoreResult) { if directchannel.Type != model.CHANNEL_DIRECT { result.Err = model.NewAppError("SqlChannelStore.SaveDirectChannel", "store.sql_channel.save_direct_channel.not_direct.app_error", nil, "", http.StatusBadRequest) } else { @@ -185,18 +173,13 @@ func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1 if err := transaction.Commit(); err != nil { result.Err = model.NewAppError("SqlChannelStore.SaveDirectChannel", "store.sql_channel.save_direct_channel.commit.app_error", nil, err.Error(), http.StatusInternalServerError) } else { - result = channelResult + *result = channelResult } } } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *model.Channel) store.StoreResult { @@ -243,17 +226,10 @@ func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *mo } func (s SqlChannelStore) Update(channel *model.Channel) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { channel.PreUpdate() if result.Err = channel.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -274,20 +250,11 @@ func (s SqlChannelStore) Update(channel *model.Channel) store.StoreChannel { } else { result.Data = channel } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) extraUpdated(channel *model.Channel) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { channel.ExtraUpdated() _, err := s.GetMaster().Exec( @@ -302,20 +269,11 @@ func (s SqlChannelStore) extraUpdated(channel *model.Channel) store.StoreChannel if err != nil { result.Err = model.NewAppError("SqlChannelStore.extraUpdated", "store.sql_channel.extra_updated.app_error", nil, "id="+channel.Id+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetChannelUnread(channelId, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var unreadChannel model.ChannelUnread err := s.GetReplica().SelectOne(&unreadChannel, `SELECT @@ -337,12 +295,7 @@ func (s SqlChannelStore) GetChannelUnread(channelId, userId string) store.StoreC } else { result.Data = &unreadChannel } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (us SqlChannelStore) InvalidateChannel(id string) { @@ -358,10 +311,7 @@ func (s SqlChannelStore) Get(id string, allowFromCache bool) store.StoreChannel } func (s SqlChannelStore) GetPinnedPosts(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { pl := model.NewPostList() var posts []*model.Post @@ -375,12 +325,7 @@ func (s SqlChannelStore) GetPinnedPosts(channelId string) store.StoreChannel { } result.Data = pl - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetFromMaster(id string) store.StoreChannel { @@ -388,11 +333,7 @@ func (s SqlChannelStore) GetFromMaster(id string) store.StoreChannel { } func (s SqlChannelStore) get(id string, master bool, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var db *gorp.DbMap if master { db = s.GetMaster() @@ -406,8 +347,6 @@ func (s SqlChannelStore) get(id string, master bool, allowFromCache bool) store. s.metrics.IncrementMemCacheHitCounter("Channel") } result.Data = (cacheItem.(*model.Channel)).DeepCopy() - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -428,12 +367,7 @@ func (s SqlChannelStore) get(id string, master bool, allowFromCache bool) store. result.Data = obj.(*model.Channel) channelCache.AddWithExpiresInSecs(id, obj.(*model.Channel), CHANNEL_CACHE_SEC) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) Delete(channelId string, time int64) store.StoreChannel { @@ -445,73 +379,37 @@ func (s SqlChannelStore) Restore(channelId string, time int64) store.StoreChanne } func (s SqlChannelStore) SetDeleteAt(channelId string, deleteAt int64, updateAt int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("Update Channels SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :ChannelId", map[string]interface{}{"DeleteAt": deleteAt, "UpdateAt": updateAt, "ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.Delete", "store.sql_channel.delete.channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) PermanentDeleteByTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM Channels WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}); err != nil { result.Err = model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) PermanentDelete(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM Channels WHERE Id = :ChannelId", map[string]interface{}{"ChannelId": channelId}); err != nil { result.Err = model.NewAppError("SqlChannelStore.PermanentDelete", "store.sql_channel.permanent_delete.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) PermanentDeleteMembersByChannel(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.RemoveAllMembersByChannel", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } type channelWithMember struct { @@ -520,11 +418,7 @@ type channelWithMember struct { } func (s SqlChannelStore) GetChannels(teamId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { data := &model.ChannelList{} _, err := s.GetReplica().Select(data, "SELECT Channels.* FROM Channels, ChannelMembers WHERE Id = ChannelId AND UserId = :UserId AND DeleteAt = 0 AND (TeamId = :TeamId OR TeamId = '') ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) @@ -537,20 +431,11 @@ func (s SqlChannelStore) GetChannels(teamId string, userId string) store.StoreCh result.Data = data } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset int, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { data := &model.ChannelList{} _, err := s.GetReplica().Select(data, `SELECT @@ -581,20 +466,11 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset in } else { result.Data = data } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetPublicChannelsForTeam(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) { data := &model.ChannelList{} _, err := s.GetReplica().Select(data, `SELECT @@ -615,20 +491,11 @@ func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, lim } else { result.Data = data } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { props := make(map[string]interface{}) props["teamId"] = teamId @@ -666,11 +533,7 @@ func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds } result.Data = data - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } type channelIdWithCountAndUpdateAt struct { @@ -680,11 +543,7 @@ type channelIdWithCountAndUpdateAt struct { } func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []channelIdWithCountAndUpdateAt _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount, UpdateAt FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND (TeamId = :TeamId OR TeamId = '') AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) @@ -700,20 +559,11 @@ func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) store.St result.Data = counts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetTeamChannels(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { data := &model.ChannelList{} _, err := s.GetReplica().Select(data, "SELECT * FROM Channels WHERE TeamId = :TeamId And Type != 'D' ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId}) @@ -726,12 +576,7 @@ func (s SqlChannelStore) GetTeamChannels(teamId string) store.StoreChannel { result.Data = data } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetByName(teamId string, name string, allowFromCache bool) store.StoreChannel { @@ -743,18 +588,13 @@ func (s SqlChannelStore) GetByNameIncludeDeleted(teamId string, name string, all } func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bool, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - var query string if includeDeleted { query = "SELECT * FROM Channels WHERE (TeamId = :TeamId OR TeamId = '') AND Name = :Name" } else { query = "SELECT * FROM Channels WHERE (TeamId = :TeamId OR TeamId = '') AND Name = :Name AND DeleteAt = 0" } - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { channel := model.Channel{} if allowFromCache { @@ -763,8 +603,6 @@ func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bo s.metrics.IncrementMemCacheHitCounter("Channel By Name") } result.Data = cacheItem.(*model.Channel) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -782,20 +620,11 @@ func (s SqlChannelStore) getByName(teamId string, name string, includeDeleted bo result.Data = &channel channelByNameCache.AddWithExpiresInSecs(teamId+name, &channel, CHANNEL_CACHE_SEC) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetDeletedByName(teamId string, name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { channel := model.Channel{} if err := s.GetReplica().SelectOne(&channel, "SELECT * FROM Channels WHERE (TeamId = :TeamId OR TeamId = '') AND Name = :Name AND DeleteAt != 0", map[string]interface{}{"TeamId": teamId, "Name": name}); err != nil { @@ -807,20 +636,11 @@ func (s SqlChannelStore) GetDeletedByName(teamId string, name string) store.Stor } else { result.Data = &channel } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetDeleted(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) { channels := &model.ChannelList{} if _, err := s.GetReplica().Select(channels, "SELECT * FROM Channels WHERE (TeamId = :TeamId OR TeamId = '') AND DeleteAt != 0 ORDER BY DisplayName LIMIT :Limit OFFSET :Offset", map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset}); err != nil { @@ -832,19 +652,11 @@ func (s SqlChannelStore) GetDeleted(teamId string, offset int, limit int) store. } else { result.Data = channels } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) SaveMember(member *model.ChannelMember) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - var result store.StoreResult + return store.Do(func(result *store.StoreResult) { // Grab the channel we are saving this member to if cr := <-s.GetFromMaster(member.ChannelId); cr.Err != nil { result.Err = cr.Err @@ -854,7 +666,7 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) store.StoreChan if transaction, err := s.GetMaster().Begin(); err != nil { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) } else { - result = s.saveMemberT(transaction, member, channel) + *result = s.saveMemberT(transaction, member, channel) if result.Err != nil { transaction.Rollback() } else { @@ -870,12 +682,7 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) store.StoreChan } s.InvalidateAllChannelMembersForUser(member.UserId) - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *model.ChannelMember, channel *model.Channel) store.StoreResult { @@ -900,16 +707,10 @@ func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *mode } func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { member.PreUpdate() if result.Err = member.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -918,20 +719,11 @@ func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) store.StoreCh } else { result.Data = member } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetMembers(channelId string, offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var members model.ChannelMembers _, err := s.GetReplica().Select(&members, "SELECT * FROM ChannelMembers WHERE ChannelId = :ChannelId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Limit": limit, "Offset": offset}) if err != nil { @@ -939,20 +731,11 @@ func (s SqlChannelStore) GetMembers(channelId string, offset, limit int) store.S } else { result.Data = &members } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetMember(channelId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var member model.ChannelMember if err := s.GetReplica().SelectOne(&member, "SELECT * FROM ChannelMembers WHERE ChannelId = :ChannelId AND UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}); err != nil { @@ -964,12 +747,7 @@ func (s SqlChannelStore) GetMember(channelId string, userId string) store.StoreC } else { result.Data = &member } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (us SqlChannelStore) InvalidateAllChannelMembersForUser(userId string) { @@ -1007,11 +785,7 @@ func (us SqlChannelStore) IsUserInChannelUseCache(userId string, channelId strin } func (s SqlChannelStore) GetMemberForPost(postId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { member := &model.ChannelMember{} if err := s.GetReplica().SelectOne( member, @@ -1028,12 +802,7 @@ func (s SqlChannelStore) GetMemberForPost(postId string, userId string) store.St } else { result.Data = member } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } type allChannelMember struct { @@ -1042,19 +811,13 @@ type allChannelMember struct { } func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := allChannelMembersForUserCache.Get(userId); ok { if s.metrics != nil { s.metrics.IncrementMemCacheHitCounter("All Channel Members for User") } result.Data = cacheItem.(map[string]string) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -1085,12 +848,7 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac allChannelMembersForUserCache.AddWithExpiresInSecs(userId, ids, ALL_CHANNEL_MEMBERS_FOR_USER_CACHE_SEC) } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (us SqlChannelStore) InvalidateCacheForChannelMembersNotifyProps(channelId string) { @@ -1103,19 +861,13 @@ type allChannelMemberNotifyProps struct { } func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := allChannelMembersNotifyPropsForChannelCache.Get(channelId); ok { if s.metrics != nil { s.metrics.IncrementMemCacheHitCounter("All Channel Members Notify Props for Channel") } result.Data = cacheItem.(map[string]model.StringMap) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -1147,12 +899,7 @@ func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId str allChannelMembersNotifyPropsForChannelCache.AddWithExpiresInSecs(channelId, props, ALL_CHANNEL_MEMBERS_NOTIFY_PROPS_FOR_CHANNEL_CACHE_SEC) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (us SqlChannelStore) InvalidateMemberCount(channelId string) { @@ -1179,19 +926,13 @@ func (s SqlChannelStore) GetMemberCountFromCache(channelId string) int64 { } func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := channelMemberCountsCache.Get(channelId); ok { if s.metrics != nil { s.metrics.IncrementMemCacheHitCounter("Channel Member Counts") } result.Data = cacheItem.(int64) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -1223,20 +964,11 @@ func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) s channelMemberCountsCache.AddWithExpiresInSecs(channelId, count, CHANNEL_MEMBERS_COUNTS_CACHE_SEC) } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) RemoveMember(channelId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { // Grab the channel we are saving this member to if cr := <-s.Get(channelId, true); cr.Err != nil { result.Err = cr.Err @@ -1253,37 +985,19 @@ func (s SqlChannelStore) RemoveMember(channelId string, userId string) store.Sto } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) PermanentDeleteMembersByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}); err != nil { result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_channel.permanent_delete_members_by_user.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { props := make(map[string]interface{}) updateIdQuery := "" @@ -1308,8 +1022,6 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) if _, err := s.GetMaster().Select(&lastPostAtTimes, selectQuery, props); err != nil { result.Err = model.NewAppError("SqlChannelStore.UpdateLastViewedAt", "store.sql_channel.update_last_viewed_at.app_error", nil, "channel_ids="+strings.Join(channelIds, ",")+", user_id="+userId+", "+err.Error(), http.StatusInternalServerError) - storeChannel <- result - close(storeChannel) return } @@ -1361,20 +1073,11 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) } else { result.Data = times } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec( `UPDATE ChannelMembers @@ -1388,20 +1091,11 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) if err != nil { result.Err = model.NewAppError("SqlChannelStore.IncrementMentionCount", "store.sql_channel.increment_mention_count.app_error", nil, "channel_id="+channelId+", user_id="+userId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetAll(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []*model.Channel _, err := s.GetReplica().Select(&data, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Type != 'D' ORDER BY Name", map[string]interface{}{"TeamId": teamId}) @@ -1410,20 +1104,11 @@ func (s SqlChannelStore) GetAll(teamId string) store.StoreChannel { } else { result.Data = data } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetForPost(postId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { channel := &model.Channel{} if err := s.GetReplica().SelectOne( channel, @@ -1439,20 +1124,11 @@ func (s SqlChannelStore) GetForPost(postId string) store.StoreChannel { } else { result.Data = channel } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType" if len(teamId) > 0 { @@ -1465,20 +1141,11 @@ func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) s } else { result.Data = v } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType AND DeleteAt > 0" if len(teamId) > 0 { @@ -1491,20 +1158,11 @@ func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType st } else { result.Data = v } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) ExtraUpdateByUser(userId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec( `UPDATE Channels SET ExtraUpdateAt = :Time WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId);`, @@ -1513,20 +1171,11 @@ func (s SqlChannelStore) ExtraUpdateByUser(userId string, time int64) store.Stor if err != nil { result.Err = model.NewAppError("SqlChannelStore.extraUpdated", "store.sql_channel.extra_updated.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { members := &model.ChannelMembers{} _, err := s.GetReplica().Select(members, ` SELECT cm.* @@ -1543,18 +1192,11 @@ func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) store.S } else { result.Data = members } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlChannelStore) SearchInTeam(teamId string, term string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { + return store.Do(func(result *store.StoreResult) { searchQuery := ` SELECT * @@ -1568,17 +1210,12 @@ func (s SqlChannelStore) SearchInTeam(teamId string, term string) store.StoreCha ORDER BY DisplayName LIMIT 100` - storeChannel <- s.performSearch(searchQuery, term, map[string]interface{}{"TeamId": teamId}) - close(storeChannel) - }() - - return storeChannel + *result = s.performSearch(searchQuery, term, map[string]interface{}{"TeamId": teamId}) + }) } func (s SqlChannelStore) SearchMore(userId string, teamId string, term string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { + return store.Do(func(result *store.StoreResult) { searchQuery := ` SELECT * @@ -1602,11 +1239,8 @@ func (s SqlChannelStore) SearchMore(userId string, teamId string, term string) s ORDER BY DisplayName LIMIT 100` - storeChannel <- s.performSearch(searchQuery, term, map[string]interface{}{"TeamId": teamId, "UserId": userId}) - close(storeChannel) - }() - - return storeChannel + *result = s.performSearch(searchQuery, term, map[string]interface{}{"TeamId": teamId, "UserId": userId}) + }) } func (s SqlChannelStore) performSearch(searchQuery string, term string, parameters map[string]interface{}) store.StoreResult { @@ -1659,11 +1293,7 @@ func (s SqlChannelStore) performSearch(searchQuery string, term string, paramete } func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var members model.ChannelMembers props := make(map[string]interface{}) idQuery := "" @@ -1685,10 +1315,5 @@ func (s SqlChannelStore) GetMembersByIds(channelId string, userIds []string) sto result.Data = &members } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/cluster_discovery_store.go b/store/sqlstore/cluster_discovery_store.go index 4c7d2706b..94639f74c 100644 --- a/store/sqlstore/cluster_discovery_store.go +++ b/store/sqlstore/cluster_discovery_store.go @@ -29,35 +29,20 @@ func NewSqlClusterDiscoveryStore(sqlStore SqlStore) store.ClusterDiscoveryStore } func (s sqlClusterDiscoveryStore) Save(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { ClusterDiscovery.PreSave() if result.Err = ClusterDiscovery.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } if err := s.GetMaster().Insert(ClusterDiscovery); err != nil { result.Err = model.NewAppError("SqlClusterDiscoveryStore.Save", "Failed to save ClusterDiscovery row", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { result.Data = false if count, err := s.GetMaster().SelectInt( @@ -82,19 +67,11 @@ func (s sqlClusterDiscoveryStore) Delete(ClusterDiscovery *model.ClusterDiscover result.Data = true } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { result.Data = false if count, err := s.GetMaster().SelectInt( @@ -120,21 +97,11 @@ func (s sqlClusterDiscoveryStore) Exists(ClusterDiscovery *model.ClusterDiscover result.Data = true } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { lastPingAt := model.GetMillis() - model.CDS_OFFLINE_AFTER_MILLIS var list []*model.ClusterDiscovery @@ -160,20 +127,11 @@ func (s sqlClusterDiscoveryStore) GetAll(ClusterDiscoveryType, clusterName strin } else { result.Data = list } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterDiscovery) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec( ` UPDATE ClusterDiscovery @@ -193,21 +151,11 @@ func (s sqlClusterDiscoveryStore) SetLastPingAt(ClusterDiscovery *model.ClusterD ); err != nil { result.Err = model.NewAppError("SqlClusterDiscoveryStore.GetAllForType", "Failed to update last ping at", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s sqlClusterDiscoveryStore) Cleanup() store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec( ` DELETE FROM ClusterDiscovery @@ -220,10 +168,5 @@ func (s sqlClusterDiscoveryStore) Cleanup() store.StoreChannel { ); err != nil { result.Err = model.NewAppError("SqlClusterDiscoveryStore.Save", "Failed to save ClusterDiscovery row", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/cluster_discovery_store_test.go b/store/sqlstore/cluster_discovery_store_test.go index 35207cd56..ce361d59d 100644 --- a/store/sqlstore/cluster_discovery_store_test.go +++ b/store/sqlstore/cluster_discovery_store_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestSqlClusterDiscoveryStore(t *testing.T) { diff --git a/store/sqlstore/command_store.go b/store/sqlstore/command_store.go index 8284f889b..d7c53e7e2 100644 --- a/store/sqlstore/command_store.go +++ b/store/sqlstore/command_store.go @@ -45,22 +45,14 @@ func (s SqlCommandStore) CreateIndexesIfNotExists() { } func (s SqlCommandStore) Save(command *model.Command) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(command.Id) > 0 { result.Err = model.NewAppError("SqlCommandStore.Save", "store.sql_command.save.saving_overwrite.app_error", nil, "id="+command.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } command.PreSave() if result.Err = command.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -69,20 +61,11 @@ func (s SqlCommandStore) Save(command *model.Command) store.StoreChannel { } else { result.Data = command } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var command model.Command if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil { @@ -90,20 +73,11 @@ func (s SqlCommandStore) Get(id string) store.StoreChannel { } result.Data = &command - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) GetByTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var commands []*model.Command if _, err := s.GetReplica().Select(&commands, "SELECT * FROM Commands WHERE TeamId = :TeamId AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId}); err != nil { @@ -111,20 +85,11 @@ func (s SqlCommandStore) GetByTeam(teamId string) store.StoreChannel { } result.Data = commands - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var command model.Command if err := s.GetReplica().SelectOne(&command, "SELECT * FROM Commands WHERE TeamId = :TeamId AND `Trigger` = :Trigger AND DeleteAt = 0", map[string]interface{}{"TeamId": teamId, "Trigger": trigger}); err != nil { @@ -132,74 +97,38 @@ func (s SqlCommandStore) GetByTrigger(teamId string, trigger string) store.Store } result.Data = &command - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) Delete(commandId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("Update Commands SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": commandId}) if err != nil { result.Err = model.NewAppError("SqlCommandStore.Delete", "store.sql_command.save.delete.app_error", nil, "id="+commandId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) PermanentDeleteByTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}) if err != nil { result.Err = model.NewAppError("SqlCommandStore.DeleteByTeam", "store.sql_command.save.delete_perm.app_error", nil, "id="+teamId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) PermanentDeleteByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM Commands WHERE CreatorId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlCommandStore.DeleteByUser", "store.sql_command.save.delete_perm.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) Update(cmd *model.Command) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { cmd.UpdateAt = model.GetMillis() if _, err := s.GetMaster().Update(cmd); err != nil { @@ -207,20 +136,11 @@ func (s SqlCommandStore) Update(cmd *model.Command) store.StoreChannel { } else { result.Data = cmd } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandStore) AnalyticsCommandCount(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT COUNT(*) @@ -238,10 +158,5 @@ func (s SqlCommandStore) AnalyticsCommandCount(teamId string) store.StoreChannel } else { result.Data = c } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/command_webhook_store.go b/store/sqlstore/command_webhook_store.go index dc1ad0732..40fa8577c 100644 --- a/store/sqlstore/command_webhook_store.go +++ b/store/sqlstore/command_webhook_store.go @@ -38,22 +38,14 @@ func (s SqlCommandWebhookStore) CreateIndexesIfNotExists() { } func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(webhook.Id) > 0 { result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.existing.app_error", nil, "id="+webhook.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } webhook.PreSave() if result.Err = webhook.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -62,20 +54,11 @@ func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) store.StoreC } else { result.Data = webhook } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhook model.CommandWebhook exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME @@ -87,20 +70,11 @@ func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel { } result.Data = &webhook - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if sqlResult, err := s.GetMaster().Exec("UPDATE CommandWebhooks SET UseCount = UseCount + 1 WHERE Id = :Id AND UseCount < :UseLimit", map[string]interface{}{"Id": id, "UseLimit": limit}); err != nil { result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) } else if rows, _ := sqlResult.RowsAffected(); rows == 0 { @@ -108,12 +82,7 @@ func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel } result.Data = id - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlCommandWebhookStore) Cleanup() { diff --git a/store/sqlstore/compliance_store.go b/store/sqlstore/compliance_store.go index 95da94673..3d638b1fd 100644 --- a/store/sqlstore/compliance_store.go +++ b/store/sqlstore/compliance_store.go @@ -37,16 +37,9 @@ func (s SqlComplianceStore) CreateIndexesIfNotExists() { } func (s SqlComplianceStore) Save(compliance *model.Compliance) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { compliance.PreSave() if result.Err = compliance.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -55,24 +48,12 @@ func (s SqlComplianceStore) Save(compliance *model.Compliance) store.StoreChanne } else { result.Data = compliance } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (us SqlComplianceStore) Update(compliance *model.Compliance) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if result.Err = compliance.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -81,21 +62,11 @@ func (us SqlComplianceStore) Update(compliance *model.Compliance) store.StoreCha } else { result.Data = compliance } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlComplianceStore) GetAll(offset, limit int) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := "SELECT * FROM Compliances ORDER BY CreateAt DESC LIMIT :Limit OFFSET :Offset" var compliances model.Compliances @@ -104,21 +75,11 @@ func (s SqlComplianceStore) GetAll(offset, limit int) store.StoreChannel { } else { result.Data = compliances } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (us SqlComplianceStore) 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.Compliance{}, id); err != nil { result.Err = model.NewAppError("SqlComplianceStore.Get", "store.sql_compliance.get.finding.app_error", nil, err.Error(), http.StatusInternalServerError) } else if obj == nil { @@ -126,21 +87,11 @@ func (us SqlComplianceStore) Get(id string) store.StoreChannel { } else { result.Data = obj.(*model.Compliance) } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (s SqlComplianceStore) ComplianceExport(job *model.Compliance) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { props := map[string]interface{}{"StartTime": job.StartAt, "EndTime": job.EndAt} keywordQuery := "" @@ -258,10 +209,5 @@ func (s SqlComplianceStore) ComplianceExport(job *model.Compliance) store.StoreC } else { result.Data = cposts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/compliance_store_test.go b/store/sqlstore/compliance_store_test.go index 7c7bfaf8f..16d5e18b3 100644 --- a/store/sqlstore/compliance_store_test.go +++ b/store/sqlstore/compliance_store_test.go @@ -8,7 +8,7 @@ import ( "time" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestSqlComplianceStore(t *testing.T) { diff --git a/store/sqlstore/emoji_store.go b/store/sqlstore/emoji_store.go index 5842af2f3..9ef071f02 100644 --- a/store/sqlstore/emoji_store.go +++ b/store/sqlstore/emoji_store.go @@ -49,15 +49,9 @@ func (es SqlEmojiStore) CreateIndexesIfNotExists() { } func (es SqlEmojiStore) Save(emoji *model.Emoji) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { emoji.PreSave() if result.Err = emoji.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -66,28 +60,17 @@ func (es SqlEmojiStore) Save(emoji *model.Emoji) store.StoreChannel { } else { result.Data = emoji } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (es SqlEmojiStore) Get(id string, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := emojiCache.Get(id); ok { if es.metrics != nil { es.metrics.IncrementMemCacheHitCounter("Emoji") } result.Data = cacheItem.(*model.Emoji) - storeChannel <- result - close(storeChannel) return } else { if es.metrics != nil { @@ -118,20 +101,11 @@ func (es SqlEmojiStore) Get(id string, allowFromCache bool) store.StoreChannel { emojiCache.AddWithExpiresInSecs(id, emoji, EMOJI_CACHE_SEC) } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (es SqlEmojiStore) GetByName(name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var emoji *model.Emoji if err := es.GetReplica().SelectOne(&emoji, @@ -146,20 +120,11 @@ func (es SqlEmojiStore) GetByName(name string) store.StoreChannel { } else { result.Data = emoji } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (es SqlEmojiStore) GetList(offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var emoji []*model.Emoji if _, err := es.GetReplica().Select(&emoji, @@ -174,20 +139,11 @@ func (es SqlEmojiStore) GetList(offset, limit int) store.StoreChannel { } else { result.Data = emoji } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (es SqlEmojiStore) Delete(id string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if sqlResult, err := es.GetMaster().Exec( `Update Emoji @@ -203,10 +159,5 @@ func (es SqlEmojiStore) Delete(id string, time int64) store.StoreChannel { } emojiCache.Remove(id) - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/emoji_store_test.go b/store/sqlstore/emoji_store_test.go index 39673a503..9754e2b47 100644 --- a/store/sqlstore/emoji_store_test.go +++ b/store/sqlstore/emoji_store_test.go @@ -8,7 +8,7 @@ import ( "time" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestEmojiSaveDelete(t *testing.T) { diff --git a/store/sqlstore/file_info_store.go b/store/sqlstore/file_info_store.go index cc8b4fb2f..18e3a2a1c 100644 --- a/store/sqlstore/file_info_store.go +++ b/store/sqlstore/file_info_store.go @@ -58,15 +58,9 @@ func (fs SqlFileInfoStore) CreateIndexesIfNotExists() { } func (fs SqlFileInfoStore) Save(info *model.FileInfo) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { info.PreSave() if result.Err = info.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -75,20 +69,11 @@ func (fs SqlFileInfoStore) Save(info *model.FileInfo) store.StoreChannel { } else { result.Data = info } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (fs SqlFileInfoStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { info := &model.FileInfo{} if err := fs.GetReplica().SelectOne(info, @@ -107,20 +92,11 @@ func (fs SqlFileInfoStore) Get(id string) store.StoreChannel { } else { result.Data = info } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (fs SqlFileInfoStore) GetByPath(path string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { info := &model.FileInfo{} if err := fs.GetReplica().SelectOne(info, @@ -136,12 +112,7 @@ func (fs SqlFileInfoStore) GetByPath(path string) store.StoreChannel { } else { result.Data = info } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (fs SqlFileInfoStore) InvalidateFileInfosForPostCache(postId string) { @@ -149,11 +120,7 @@ func (fs SqlFileInfoStore) InvalidateFileInfosForPostCache(postId string) { } func (fs SqlFileInfoStore) GetForPost(postId string, readFromMaster bool, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := fileInfoCache.Get(postId); ok { if fs.metrics != nil { @@ -161,8 +128,6 @@ func (fs SqlFileInfoStore) GetForPost(postId string, readFromMaster bool, allowF } result.Data = cacheItem.([]*model.FileInfo) - storeChannel <- result - close(storeChannel) return } else { if fs.metrics != nil { @@ -202,20 +167,11 @@ func (fs SqlFileInfoStore) GetForPost(postId string, readFromMaster bool, allowF result.Data = infos } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (fs SqlFileInfoStore) AttachToPost(fileId, postId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := fs.GetMaster().Exec( `UPDATE FileInfo @@ -227,20 +183,11 @@ func (fs SqlFileInfoStore) AttachToPost(fileId, postId string) store.StoreChanne result.Err = model.NewAppError("SqlFileInfoStore.AttachToPost", "store.sql_file_info.attach_to_post.app_error", nil, "post_id="+postId+", file_id="+fileId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (fs SqlFileInfoStore) DeleteForPost(postId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := fs.GetMaster().Exec( `UPDATE FileInfo @@ -253,20 +200,11 @@ func (fs SqlFileInfoStore) DeleteForPost(postId string) store.StoreChannel { } else { result.Data = postId } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (fs SqlFileInfoStore) PermanentDelete(fileId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := fs.GetMaster().Exec( `DELETE FROM FileInfo @@ -275,20 +213,11 @@ func (fs SqlFileInfoStore) PermanentDelete(fileId string) store.StoreChannel { result.Err = model.NewAppError("SqlFileInfoStore.PermanentDelete", "store.sql_file_info.permanent_delete.app_error", nil, "file_id="+fileId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlFileInfoStore) PermanentDeleteBatch(endTime int64, limit int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var query string if *utils.Cfg.SqlSettings.DriverName == "postgres" { query = "DELETE from FileInfo WHERE Id = any (array (SELECT Id FROM FileInfo WHERE CreateAt < :EndTime LIMIT :Limit))" @@ -308,10 +237,5 @@ func (s SqlFileInfoStore) PermanentDeleteBatch(endTime int64, limit int64) store result.Data = rowsAffected } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/file_info_store_test.go b/store/sqlstore/file_info_store_test.go index 626fe8c6a..8ddf34e19 100644 --- a/store/sqlstore/file_info_store_test.go +++ b/store/sqlstore/file_info_store_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestFileInfoSaveGet(t *testing.T) { @@ -267,21 +267,21 @@ func TestFileInfoPermanentDeleteBatch(t *testing.T) { PostId: postId, CreatorId: model.NewId(), Path: "file.txt", - CreateAt: 1000, + CreateAt: 1000, })) store.Must(ss.FileInfo().Save(&model.FileInfo{ PostId: postId, CreatorId: model.NewId(), Path: "file.txt", - CreateAt: 1200, + CreateAt: 1200, })) store.Must(ss.FileInfo().Save(&model.FileInfo{ PostId: postId, CreatorId: model.NewId(), Path: "file.txt", - CreateAt: 2000, + CreateAt: 2000, })) if result := <-ss.FileInfo().GetForPost(postId, true, false); result.Err != nil { diff --git a/store/sqlstore/job_store.go b/store/sqlstore/job_store.go index 8cd921217..691b09638 100644 --- a/store/sqlstore/job_store.go +++ b/store/sqlstore/job_store.go @@ -35,29 +35,17 @@ func (jss SqlJobStore) CreateIndexesIfNotExists() { } func (jss SqlJobStore) Save(job *model.Job) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { if err := jss.GetMaster().Insert(job); err != nil { result.Err = model.NewAppError("SqlJobStore.Save", "store.sql_job.save.app_error", nil, "id="+job.Id+", "+err.Error(), http.StatusInternalServerError) } else { result.Data = job } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) UpdateOptimistically(job *model.Job, currentStatus string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if sqlResult, err := jss.GetMaster().Exec( `UPDATE Jobs @@ -92,20 +80,11 @@ func (jss SqlJobStore) UpdateOptimistically(job *model.Job, currentStatus string } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) UpdateStatus(id string, status string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { job := &model.Job{ Id: id, Status: status, @@ -121,20 +100,11 @@ func (jss SqlJobStore) UpdateStatus(id string, status string) store.StoreChannel if result.Err == nil { result.Data = job } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) UpdateStatusOptimistically(id string, currentStatus string, newStatus string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var startAtClause string if newStatus == model.JOB_STATUS_IN_PROGRESS { startAtClause = `StartAt = :StartAt,` @@ -164,20 +134,11 @@ func (jss SqlJobStore) UpdateStatusOptimistically(id string, currentStatus strin } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var status *model.Job if err := jss.GetReplica().SelectOne(&status, @@ -195,20 +156,11 @@ func (jss SqlJobStore) Get(id string) store.StoreChannel { } else { result.Data = status } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) GetAllPage(offset int, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var statuses []*model.Job if _, err := jss.GetReplica().Select(&statuses, @@ -226,20 +178,11 @@ func (jss SqlJobStore) GetAllPage(offset int, limit int) store.StoreChannel { } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) GetAllByType(jobType string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var statuses []*model.Job if _, err := jss.GetReplica().Select(&statuses, @@ -255,20 +198,11 @@ func (jss SqlJobStore) GetAllByType(jobType string) store.StoreChannel { } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) GetAllByTypePage(jobType 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 statuses []*model.Job if _, err := jss.GetReplica().Select(&statuses, @@ -288,20 +222,11 @@ func (jss SqlJobStore) GetAllByTypePage(jobType string, offset int, limit int) s } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) GetAllByStatus(status string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var statuses []*model.Job if _, err := jss.GetReplica().Select(&statuses, @@ -317,20 +242,11 @@ func (jss SqlJobStore) GetAllByStatus(status string) store.StoreChannel { } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) GetNewestJobByStatusAndType(status string, jobType string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var job *model.Job if err := jss.GetReplica().SelectOne(&job, @@ -349,20 +265,11 @@ func (jss SqlJobStore) GetNewestJobByStatusAndType(status string, jobType string } else { result.Data = job } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) GetCountByStatusAndType(status string, jobType string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if count, err := jss.GetReplica().SelectInt(`SELECT COUNT(*) FROM @@ -375,20 +282,11 @@ func (jss SqlJobStore) GetCountByStatusAndType(status string, jobType string) st } else { result.Data = count } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (jss SqlJobStore) Delete(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := jss.GetMaster().Exec( `DELETE FROM Jobs @@ -398,10 +296,5 @@ func (jss SqlJobStore) Delete(id string) store.StoreChannel { } else { result.Data = id } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/license_store.go b/store/sqlstore/license_store.go index 5f3e91e88..0a1293dee 100644 --- a/store/sqlstore/license_store.go +++ b/store/sqlstore/license_store.go @@ -30,16 +30,9 @@ func (ls SqlLicenseStore) CreateIndexesIfNotExists() { } func (ls SqlLicenseStore) Save(license *model.LicenseRecord) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { license.PreSave() if result.Err = license.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -51,21 +44,11 @@ func (ls SqlLicenseStore) Save(license *model.LicenseRecord) store.StoreChannel result.Data = license } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (ls SqlLicenseStore) 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 := ls.GetReplica().Get(model.LicenseRecord{}, id); err != nil { result.Err = model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError) } else if obj == nil { @@ -73,11 +56,5 @@ func (ls SqlLicenseStore) Get(id string) store.StoreChannel { } else { result.Data = obj.(*model.LicenseRecord) } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } diff --git a/store/sqlstore/license_store_test.go b/store/sqlstore/license_store_test.go index 053b0ede1..99b13a423 100644 --- a/store/sqlstore/license_store_test.go +++ b/store/sqlstore/license_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestLicenseStoreSave(t *testing.T) { diff --git a/store/sqlstore/oauth_store.go b/store/sqlstore/oauth_store.go index f93f47821..7644ac5dc 100644 --- a/store/sqlstore/oauth_store.go +++ b/store/sqlstore/oauth_store.go @@ -61,23 +61,14 @@ func (as SqlOAuthStore) CreateIndexesIfNotExists() { } func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(app.Id) > 0 { result.Err = model.NewAppError("SqlOAuthStore.SaveApp", "store.sql_oauth.save_app.existing.app_error", nil, "app_id="+app.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } app.PreSave() if result.Err = app.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -86,26 +77,14 @@ func (as SqlOAuthStore) SaveApp(app *model.OAuthApp) store.StoreChannel { } else { result.Data = app } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { app.PreUpdate() if result.Err = app.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -126,21 +105,11 @@ func (as SqlOAuthStore) UpdateApp(app *model.OAuthApp) store.StoreChannel { result.Data = [2]*model.OAuthApp{app, oldApp} } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetApp(id string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if obj, err := as.GetReplica().Get(model.OAuthApp{}, id); err != nil { result.Err = model.NewAppError("SqlOAuthStore.GetApp", "store.sql_oauth.get_app.finding.app_error", nil, "app_id="+id+", "+err.Error(), http.StatusInternalServerError) } else if obj == nil { @@ -148,22 +117,11 @@ func (as SqlOAuthStore) GetApp(id string) store.StoreChannel { } else { result.Data = obj.(*model.OAuthApp) } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetAppByUser(userId string, offset, limit int) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var apps []*model.OAuthApp if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps WHERE CreatorId = :UserId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil { @@ -171,21 +129,11 @@ func (as SqlOAuthStore) GetAppByUser(userId string, offset, limit int) store.Sto } result.Data = apps - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetApps(offset, limit int) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var apps []*model.OAuthApp if _, err := as.GetReplica().Select(&apps, "SELECT * FROM OAuthApps LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil { @@ -193,20 +141,11 @@ func (as SqlOAuthStore) GetApps(offset, limit int) store.StoreChannel { } result.Data = apps - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetAuthorizedApps(userId string, offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var apps []*model.OAuthApp if _, err := as.GetReplica().Select(&apps, @@ -216,27 +155,18 @@ func (as SqlOAuthStore) GetAuthorizedApps(userId string, offset, limit int) stor } result.Data = apps - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) DeleteApp(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { // wrap in a transaction so that if one fails, everything fails transaction, err := as.GetMaster().Begin() if err != nil { result.Err = model.NewAppError("SqlOAuthStore.DeleteApp", "store.sql_oauth.delete.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) } else { if extrasResult := as.deleteApp(transaction, id); extrasResult.Err != nil { - result = extrasResult + *result = extrasResult } if result.Err == nil { @@ -250,24 +180,12 @@ func (as SqlOAuthStore) DeleteApp(id string) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) SaveAccessData(accessData *model.AccessData) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if result.Err = accessData.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -276,21 +194,11 @@ func (as SqlOAuthStore) SaveAccessData(accessData *model.AccessData) store.Store } else { result.Data = accessData } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetAccessData(token string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { accessData := model.AccessData{} if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { @@ -298,22 +206,11 @@ func (as SqlOAuthStore) GetAccessData(token string) store.StoreChannel { } else { result.Data = &accessData } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var accessData []*model.AccessData if _, err := as.GetReplica().Select(&accessData, @@ -323,22 +220,11 @@ func (as SqlOAuthStore) GetAccessDataByUserForApp(userId, clientId string) store } else { result.Data = accessData } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { accessData := model.AccessData{} if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE RefreshToken = :Token", map[string]interface{}{"Token": token}); err != nil { @@ -346,22 +232,11 @@ func (as SqlOAuthStore) GetAccessDataByRefreshToken(token string) store.StoreCha } else { result.Data = &accessData } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { accessData := model.AccessData{} if err := as.GetReplica().SelectOne(&accessData, "SELECT * FROM OAuthAccessData WHERE ClientId = :ClientId AND UserId = :UserId", @@ -374,24 +249,12 @@ func (as SqlOAuthStore) GetPreviousAccessData(userId, clientId string) store.Sto } else { result.Data = &accessData } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if result.Err = accessData.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -402,42 +265,21 @@ func (as SqlOAuthStore) UpdateAccessData(accessData *model.AccessData) store.Sto } else { result.Data = accessData } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) RemoveAccessData(token string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { result.Err = model.NewAppError("SqlOAuthStore.RemoveAccessData", "store.sql_oauth.remove_access_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) SaveAuthData(authData *model.AuthData) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { authData.PreSave() if result.Err = authData.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -446,21 +288,11 @@ func (as SqlOAuthStore) SaveAuthData(authData *model.AuthData) store.StoreChanne } else { result.Data = authData } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) GetAuthData(code string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if obj, err := as.GetReplica().Get(model.AuthData{}, code); err != nil { result.Err = model.NewAppError("SqlOAuthStore.GetAuthData", "store.sql_oauth.get_auth_data.finding.app_error", nil, err.Error(), http.StatusInternalServerError) } else if obj == nil { @@ -468,49 +300,25 @@ func (as SqlOAuthStore) GetAuthData(code string) store.StoreChannel { } else { result.Data = obj.(*model.AuthData) } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (as SqlOAuthStore) RemoveAuthData(code string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := as.GetMaster().Exec("DELETE FROM OAuthAuthData WHERE Code = :Code", map[string]interface{}{"Code": code}) if err != nil { result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthData", "store.sql_oauth.remove_auth_data.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) PermanentDeleteAuthDataByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := as.GetMaster().Exec("DELETE FROM OAuthAccessData WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlOAuthStore.RemoveAuthDataByUserId", "store.sql_oauth.permanent_delete_auth_data_by_user.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (as SqlOAuthStore) deleteApp(transaction *gorp.Transaction, clientId string) store.StoreResult { diff --git a/store/sqlstore/oauth_store_test.go b/store/sqlstore/oauth_store_test.go index 1e4aacfe5..b79414abf 100644 --- a/store/sqlstore/oauth_store_test.go +++ b/store/sqlstore/oauth_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestOAuthStoreSaveApp(t *testing.T) { diff --git a/store/sqlstore/post_store.go b/store/sqlstore/post_store.go index b3e0bdbb0..e6311a5a0 100644 --- a/store/sqlstore/post_store.go +++ b/store/sqlstore/post_store.go @@ -78,22 +78,14 @@ func (s SqlPostStore) CreateIndexesIfNotExists() { } func (s SqlPostStore) Save(post *model.Post) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(post.Id) > 0 { result.Err = model.NewAppError("SqlPostStore.Save", "store.sql_post.save.existing.app_error", nil, "id="+post.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } post.PreSave() if result.Err = post.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -116,20 +108,11 @@ func (s SqlPostStore) Save(post *model.Post) store.StoreChannel { result.Data = post } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) Update(newPost *model.Post, oldPost *model.Post) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { newPost.UpdateAt = model.GetMillis() newPost.PreCommit() @@ -140,8 +123,6 @@ func (s SqlPostStore) Update(newPost *model.Post, oldPost *model.Post) store.Sto oldPost.PreCommit() if result.Err = newPost.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -160,25 +141,14 @@ func (s SqlPostStore) Update(newPost *model.Post, oldPost *model.Post) store.Sto result.Data = newPost } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) Overwrite(post *model.Post) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { post.UpdateAt = model.GetMillis() if result.Err = post.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -187,18 +157,11 @@ func (s SqlPostStore) Overwrite(post *model.Post) store.StoreChannel { } else { result.Data = post } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetFlaggedPosts(userId string, offset int, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { pl := model.NewPostList() var posts []*model.Post @@ -212,18 +175,11 @@ func (s SqlPostStore) GetFlaggedPosts(userId string, offset int, limit int) stor } result.Data = pl - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetFlaggedPostsForTeam(userId, 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) { pl := model.NewPostList() var posts []*model.Post @@ -264,18 +220,11 @@ func (s SqlPostStore) GetFlaggedPostsForTeam(userId, teamId string, offset int, } result.Data = pl - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetFlaggedPostsForChannel(userId, channelId string, offset int, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { pl := model.NewPostList() var posts []*model.Post @@ -300,25 +249,15 @@ func (s SqlPostStore) GetFlaggedPostsForChannel(userId, channelId string, offset } result.Data = pl - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) Get(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { pl := model.NewPostList() if len(id) == 0 { result.Err = model.NewAppError("SqlPostStore.GetPost", "store.sql_post.get.app_error", nil, "id="+id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } @@ -326,8 +265,6 @@ func (s SqlPostStore) Get(id string) store.StoreChannel { err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}) if err != nil { result.Err = model.NewAppError("SqlPostStore.GetPost", "store.sql_post.get.app_error", nil, "id="+id+err.Error(), http.StatusNotFound) - storeChannel <- result - close(storeChannel) return } @@ -342,8 +279,6 @@ func (s SqlPostStore) Get(id string) store.StoreChannel { if len(rootId) == 0 { result.Err = model.NewAppError("SqlPostStore.GetPost", "store.sql_post.get.app_error", nil, "root_id="+rootId, http.StatusInternalServerError) - storeChannel <- result - close(storeChannel) return } @@ -351,8 +286,6 @@ func (s SqlPostStore) Get(id string) store.StoreChannel { _, err = s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE (Id = :Id OR RootId = :RootId) AND DeleteAt = 0", map[string]interface{}{"Id": rootId, "RootId": rootId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.GetPost", "store.sql_post.get.app_error", nil, "root_id="+rootId+err.Error(), http.StatusInternalServerError) - storeChannel <- result - close(storeChannel) return } else { for _, p := range posts { @@ -361,20 +294,11 @@ func (s SqlPostStore) Get(id string) store.StoreChannel { } result.Data = pl - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetSingle(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var post model.Post err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}) if err != nil { @@ -382,12 +306,7 @@ func (s SqlPostStore) GetSingle(id string) store.StoreChannel { } result.Data = &post - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } type etagPosts struct { @@ -401,19 +320,13 @@ func (s SqlPostStore) InvalidateLastPostTimeCache(channelId string) { } func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := lastPostTimeCache.Get(channelId); ok { if s.metrics != nil { s.metrics.IncrementMemCacheHitCounter("Last Post Time") } result.Data = fmt.Sprintf("%v.%v", model.CurrentVersion, cacheItem.(int64)) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -435,79 +348,41 @@ func (s SqlPostStore) GetEtag(channelId string, allowFromCache bool) store.Store } lastPostTimeCache.AddWithExpiresInSecs(channelId, et.UpdateAt, LAST_POST_TIME_CACHE_SEC) - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) Delete(postId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("Update Posts SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id OR RootId = :RootId", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": postId, "RootId": postId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.Delete", "store.sql_post.delete.app_error", nil, "id="+postId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) permanentDelete(postId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM Posts WHERE Id = :Id OR RootId = :RootId", map[string]interface{}{"Id": postId, "RootId": postId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.Delete", "store.sql_post.permanent_delete.app_error", nil, "id="+postId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) permanentDeleteAllCommentByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM Posts WHERE UserId = :UserId AND RootId != ''", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.permanentDeleteAllCommentByUser", "store.sql_post.permanent_delete_all_comments_by_user.app_error", nil, "userId="+userId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) PermanentDeleteByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { // First attempt to delete all the comments for a user if r := <-s.permanentDeleteAllCommentByUser(userId); r.Err != nil { result.Err = r.Err - storeChannel <- result - close(storeChannel) return } @@ -521,8 +396,6 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) store.StoreChannel { _, err := s.GetMaster().Select(&ids, "SELECT Id FROM Posts WHERE UserId = :UserId LIMIT 1000", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.PermanentDeleteByUser.select", "store.sql_post.permanent_delete_by_user.app_error", nil, "userId="+userId+", err="+err.Error(), http.StatusInternalServerError) - storeChannel <- result - close(storeChannel) return } else { found = false @@ -530,8 +403,6 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) store.StoreChannel { found = true if r := <-s.permanentDelete(id); r.Err != nil { result.Err = r.Err - storeChannel <- result - close(storeChannel) return } } @@ -541,46 +412,24 @@ func (s SqlPostStore) PermanentDeleteByUser(userId string) store.StoreChannel { count = count + 1 if count >= 10 { result.Err = model.NewAppError("SqlPostStore.PermanentDeleteByUser.toolarge", "store.sql_post.permanent_delete_by_user.too_many.app_error", nil, "userId="+userId, http.StatusInternalServerError) - storeChannel <- result - close(storeChannel) return } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) PermanentDeleteByChannel(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM Posts WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}); err != nil { result.Err = model.NewAppError("SqlPostStore.PermanentDeleteByChannel", "store.sql_post.permanent_delete_by_channel.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if limit > 1000 { result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_posts.app_error", nil, "channelId="+channelId, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } @@ -591,8 +440,6 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFro } result.Data = cacheItem.(*model.PostList) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -635,20 +482,11 @@ func (s SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFro result.Data = list } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetPostsSince(channelId string, time int64, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { // If the last post in the channel's time is less than or equal to the time we are getting posts since, // we can safely return no posts. @@ -658,8 +496,6 @@ func (s SqlPostStore) GetPostsSince(channelId string, time int64, allowFromCache } list := model.NewPostList() result.Data = list - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -723,12 +559,7 @@ func (s SqlPostStore) GetPostsSince(channelId string, time int64, allowFromCache result.Data = list } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetPostsBefore(channelId string, postId string, numPosts int, offset int) store.StoreChannel { @@ -740,11 +571,7 @@ func (s SqlPostStore) GetPostsAfter(channelId string, postId string, numPosts in } func (s SqlPostStore) getPostsAround(channelId string, postId string, numPosts int, offset int, before bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var direction string var sort string if before { @@ -821,20 +648,11 @@ func (s SqlPostStore) getPostsAround(channelId string, postId string, numPosts i result.Data = list } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) getRootPosts(channelId 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 posts []*model.Post _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit}) if err != nil { @@ -842,20 +660,11 @@ func (s SqlPostStore) getRootPosts(channelId string, offset int, limit int) stor } else { result.Data = posts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) getParentsPosts(channelId 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 posts []*model.Post _, err := s.GetReplica().Select(&posts, `SELECT @@ -887,12 +696,7 @@ func (s SqlPostStore) getParentsPosts(channelId string, offset int, limit int) s } else { result.Data = posts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } var specialSearchChar = []string{ @@ -908,18 +712,12 @@ var specialSearchChar = []string{ } func (s SqlPostStore) Search(teamId string, userId string, params *model.SearchParams) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if !*utils.Cfg.ServiceSettings.EnablePostSearch { list := model.NewPostList() result.Data = list result.Err = model.NewAppError("SqlPostStore.Search", "store.sql_post.search.disabled", nil, fmt.Sprintf("teamId=%v userId=%v params=%v", teamId, userId, params.ToJson()), http.StatusNotImplemented) - storeChannel <- result - close(storeChannel) return } @@ -933,8 +731,6 @@ func (s SqlPostStore) Search(teamId string, userId string, params *model.SearchP if terms == "" && len(params.InChannels) == 0 && len(params.FromUsers) == 0 { result.Data = []*model.Post{} - storeChannel <- result - close(storeChannel) return } @@ -1094,20 +890,11 @@ func (s SqlPostStore) Search(teamId string, userId string, params *model.SearchP list.MakeNonNil() result.Data = list - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) AnalyticsUserCountsWithPostsByDay(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT DISTINCT DATE(FROM_UNIXTIME(Posts.CreateAt / 1000)) AS Name, @@ -1156,20 +943,11 @@ func (s SqlPostStore) AnalyticsUserCountsWithPostsByDay(teamId string) store.Sto } else { result.Data = rows } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) AnalyticsPostCountsByDay(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT DATE(FROM_UNIXTIME(Posts.CreateAt / 1000)) AS Name, @@ -1220,20 +998,11 @@ func (s SqlPostStore) AnalyticsPostCountsByDay(teamId string) store.StoreChannel } else { result.Data = rows } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) AnalyticsPostCount(teamId string, mustHaveFile bool, mustHaveHashtag bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT COUNT(Posts.Id) AS Value @@ -1260,20 +1029,11 @@ func (s SqlPostStore) AnalyticsPostCount(teamId string, mustHaveFile bool, mustH } else { result.Data = v } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetPostsCreatedAt(channelId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT * FROM Posts WHERE CreateAt = :CreateAt` var posts []*model.Post @@ -1284,20 +1044,11 @@ func (s SqlPostStore) GetPostsCreatedAt(channelId string, time int64) store.Stor } else { result.Data = posts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetPostsByIds(postIds []string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { keys := bytes.Buffer{} params := make(map[string]interface{}) for i, postId := range postIds { @@ -1321,20 +1072,11 @@ func (s SqlPostStore) GetPostsByIds(postIds []string) store.StoreChannel { } else { result.Data = posts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) GetPostsBatchForIndexing(startTime int64, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var posts []*model.PostForIndexing _, err1 := s.GetSearchReplica().Select(&posts, `(SELECT @@ -1362,20 +1104,11 @@ func (s SqlPostStore) GetPostsBatchForIndexing(startTime int64, limit int) store } else { result.Data = posts } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPostStore) PermanentDeleteBatch(endTime int64, limit int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var query string if *utils.Cfg.SqlSettings.DriverName == "postgres" { query = "DELETE from Posts WHERE Id = any (array (SELECT Id FROM Posts WHERE CreateAt < :EndTime LIMIT :Limit))" @@ -1395,10 +1128,5 @@ func (s SqlPostStore) PermanentDeleteBatch(endTime int64, limit int64) store.Sto result.Data = rowsAffected } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/post_store_test.go b/store/sqlstore/post_store_test.go index 7be37584b..aa70f1fc7 100644 --- a/store/sqlstore/post_store_test.go +++ b/store/sqlstore/post_store_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/utils" ) diff --git a/store/sqlstore/preference_store.go b/store/sqlstore/preference_store.go index 2aab91386..6765a74f8 100644 --- a/store/sqlstore/preference_store.go +++ b/store/sqlstore/preference_store.go @@ -60,11 +60,7 @@ func (s SqlPreferenceStore) DeleteUnusedFeatures() { } func (s SqlPreferenceStore) Save(preferences *model.Preferences) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { // wrap in a transaction so that if one fails, everything fails transaction, err := s.GetMaster().Begin() if err != nil { @@ -72,7 +68,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) store.StoreChan } else { for _, preference := range *preferences { if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil { - result = upsertResult + *result = upsertResult break } } @@ -90,12 +86,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) store.StoreChan } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) store.StoreResult { @@ -181,11 +172,7 @@ func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *mo } func (s SqlPreferenceStore) Get(userId string, category string, name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var preference model.Preference if err := s.GetReplica().SelectOne(&preference, @@ -201,20 +188,11 @@ func (s SqlPreferenceStore) Get(userId string, category string, name string) sto } else { result.Data = preference } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) GetCategory(userId string, category string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var preferences model.Preferences if _, err := s.GetReplica().Select(&preferences, @@ -229,20 +207,11 @@ func (s SqlPreferenceStore) GetCategory(userId string, category string) store.St } else { result.Data = preferences } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) GetAll(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var preferences model.Preferences if _, err := s.GetReplica().Select(&preferences, @@ -256,37 +225,20 @@ func (s SqlPreferenceStore) GetAll(userId string) store.StoreChannel { } else { result.Data = preferences } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec( `DELETE FROM Preferences WHERE UserId = :UserId`, map[string]interface{}{"UserId": userId}); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.Delete", "store.sql_preference.permanent_delete_by_user.app_error", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) IsFeatureEnabled(feature, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { if value, err := s.GetReplica().SelectStr(`SELECT value FROM @@ -299,20 +251,11 @@ func (s SqlPreferenceStore) IsFeatureEnabled(feature, userId string) store.Store } else { result.Data = value == "true" } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) Delete(userId, category, name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec( `DELETE FROM Preferences @@ -322,20 +265,11 @@ func (s SqlPreferenceStore) Delete(userId, category, name string) store.StoreCha AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.Delete", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) DeleteCategory(userId string, category string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec( `DELETE FROM Preferences @@ -344,20 +278,11 @@ func (s SqlPreferenceStore) DeleteCategory(userId string, category string) store AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.DeleteCategory", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec( `DELETE FROM Preferences @@ -366,20 +291,11 @@ func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string) AND Category = :Category`, map[string]interface{}{"Name": name, "Category": category}); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.DeleteCategoryAndName", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `DELETE FROM Preferences @@ -418,10 +334,5 @@ func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) store.StoreChannel { result.Data = rowsAffected } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/preference_store_test.go b/store/sqlstore/preference_store_test.go index f1cebf379..ff415038d 100644 --- a/store/sqlstore/preference_store_test.go +++ b/store/sqlstore/preference_store_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestPreferenceSave(t *testing.T) { diff --git a/store/sqlstore/reaction_store_test.go b/store/sqlstore/reaction_store_test.go index 276bcbffa..28338307b 100644 --- a/store/sqlstore/reaction_store_test.go +++ b/store/sqlstore/reaction_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestReactionSave(t *testing.T) { diff --git a/store/sqlstore/session_store.go b/store/sqlstore/session_store.go index 09193f595..1f8799cdf 100644 --- a/store/sqlstore/session_store.go +++ b/store/sqlstore/session_store.go @@ -42,16 +42,9 @@ func (me SqlSessionStore) CreateIndexesIfNotExists() { } func (me SqlSessionStore) Save(session *model.Session) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(session.Id) > 0 { result.Err = model.NewAppError("SqlSessionStore.Save", "store.sql_session.save.existing.app_error", nil, "id="+session.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } @@ -82,21 +75,11 @@ func (me SqlSessionStore) Save(session *model.Session) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) Get(sessionIdOrToken string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var sessions []*model.Session if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE Token = :Token OR Id = :Id LIMIT 1", map[string]interface{}{"Token": sessionIdOrToken, "Id": sessionIdOrToken}); err != nil { @@ -120,25 +103,15 @@ func (me SqlSessionStore) Get(sessionIdOrToken string) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - - }() - - return storeChannel + }) } func (me SqlSessionStore) GetSessions(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - + return store.Do(func(result *store.StoreResult) { if cur := <-me.CleanUpExpiredSessions(userId); cur.Err != nil { l4g.Error(utils.T("store.sql_session.get_sessions.error"), cur.Err) } - result := store.StoreResult{} var sessions []*model.Session tcs := me.Team().GetTeamsForUser(userId) @@ -164,20 +137,11 @@ func (me SqlSessionStore) GetSessions(userId string) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { var sessions []*model.Session if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE UserId = :UserId AND ExpiresAt != 0 AND :ExpiresAt <= ExpiresAt AND DeviceId != ''", map[string]interface{}{"UserId": userId, "ExpiresAt": model.GetMillis()}); err != nil { @@ -186,148 +150,78 @@ func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) store.St result.Data = sessions } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) Remove(sessionIdOrToken string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE Id = :Id Or Token = :Token", map[string]interface{}{"Id": sessionIdOrToken, "Token": sessionIdOrToken}) if err != nil { result.Err = model.NewAppError("SqlSessionStore.RemoveSession", "store.sql_session.remove.app_error", nil, "id="+sessionIdOrToken+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) RemoveAllSessions() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := me.GetMaster().Exec("DELETE FROM Sessions") if err != nil { result.Err = model.NewAppError("SqlSessionStore.RemoveAllSessions", "store.sql_session.remove_all_sessions_for_team.app_error", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlSessionStore.RemoveAllSessionsForUser", "store.sql_session.permanent_delete_sessions_by_user.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) CleanUpExpiredSessions(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE UserId = :UserId AND ExpiresAt != 0 AND :ExpiresAt > ExpiresAt", map[string]interface{}{"UserId": userId, "ExpiresAt": model.GetMillis()}); err != nil { result.Err = model.NewAppError("SqlSessionStore.CleanUpExpiredSessions", "store.sql_session.cleanup_expired_sessions.app_error", nil, err.Error(), http.StatusInternalServerError) } else { result.Data = userId } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := me.GetMaster().Exec("UPDATE Sessions SET LastActivityAt = :LastActivityAt WHERE Id = :Id", map[string]interface{}{"LastActivityAt": time, "Id": sessionId}); err != nil { result.Err = model.NewAppError("SqlSessionStore.UpdateLastActivityAt", "store.sql_session.update_last_activity.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError) } else { result.Data = sessionId } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) UpdateRoles(userId, roles string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { if _, err := me.GetMaster().Exec("UPDATE Sessions SET Roles = :Roles WHERE UserId = :UserId", map[string]interface{}{"Roles": roles, "UserId": userId}); err != nil { result.Err = model.NewAppError("SqlSessionStore.UpdateRoles", "store.sql_session.update_roles.app_error", nil, "userId="+userId, http.StatusInternalServerError) } else { result.Data = userId } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} + return store.Do(func(result *store.StoreResult) { if _, err := me.GetMaster().Exec("UPDATE Sessions SET DeviceId = :DeviceId, ExpiresAt = :ExpiresAt WHERE Id = :Id", map[string]interface{}{"DeviceId": deviceId, "Id": id, "ExpiresAt": expiresAt}); err != nil { result.Err = model.NewAppError("SqlSessionStore.UpdateDeviceId", "store.sql_session.update_device_id.app_error", nil, err.Error(), http.StatusInternalServerError) } else { result.Data = deviceId } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (me SqlSessionStore) AnalyticsSessionCount() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT COUNT(*) @@ -340,10 +234,5 @@ func (me SqlSessionStore) AnalyticsSessionCount() store.StoreChannel { } else { result.Data = c } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/session_store_test.go b/store/sqlstore/session_store_test.go index ab6ffec95..1eed17b51 100644 --- a/store/sqlstore/session_store_test.go +++ b/store/sqlstore/session_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestSessionStoreSave(t *testing.T) { diff --git a/store/sqlstore/status_store.go b/store/sqlstore/status_store.go index 43dab0c34..94e324c11 100644 --- a/store/sqlstore/status_store.go +++ b/store/sqlstore/status_store.go @@ -40,11 +40,7 @@ func (s SqlStatusStore) CreateIndexesIfNotExists() { } func (s SqlStatusStore) SaveOrUpdate(status *model.Status) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if err := s.GetReplica().SelectOne(&model.Status{}, "SELECT * FROM Status WHERE UserId = :UserId", map[string]interface{}{"UserId": status.UserId}); err == nil { if _, err := s.GetMaster().Update(status); err != nil { result.Err = model.NewAppError("SqlStatusStore.SaveOrUpdate", "store.sql_status.update.app_error", nil, err.Error(), http.StatusInternalServerError) @@ -56,20 +52,11 @@ func (s SqlStatusStore) SaveOrUpdate(status *model.Status) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) Get(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var status model.Status if err := s.GetReplica().SelectOne(&status, @@ -87,20 +74,11 @@ func (s SqlStatusStore) Get(userId string) store.StoreChannel { } else { result.Data = &status } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) GetByIds(userIds []string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { props := make(map[string]interface{}) idQuery := "" @@ -119,60 +97,33 @@ func (s SqlStatusStore) GetByIds(userIds []string) store.StoreChannel { } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) GetOnlineAway() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var statuses []*model.Status if _, err := s.GetReplica().Select(&statuses, "SELECT * FROM Status WHERE Status = :Online OR Status = :Away LIMIT 300", map[string]interface{}{"Online": model.STATUS_ONLINE, "Away": model.STATUS_AWAY}); err != nil { result.Err = model.NewAppError("SqlStatusStore.GetOnlineAway", "store.sql_status.get_online_away.app_error", nil, err.Error(), http.StatusInternalServerError) } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) GetOnline() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var statuses []*model.Status if _, err := s.GetReplica().Select(&statuses, "SELECT * FROM Status WHERE Status = :Online", map[string]interface{}{"Online": model.STATUS_ONLINE}); err != nil { result.Err = model.NewAppError("SqlStatusStore.GetOnline", "store.sql_status.get_online.app_error", nil, err.Error(), http.StatusInternalServerError) } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) GetAllFromTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var statuses []*model.Status if _, err := s.GetReplica().Select(&statuses, `SELECT s.* FROM Status AS s INNER JOIN @@ -181,37 +132,19 @@ func (s SqlStatusStore) GetAllFromTeam(teamId string) store.StoreChannel { } else { result.Data = statuses } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) ResetAll() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("UPDATE Status SET Status = :Status WHERE Manual = false", map[string]interface{}{"Status": model.STATUS_OFFLINE}); err != nil { result.Err = model.NewAppError("SqlStatusStore.ResetAll", "store.sql_status.reset_all.app_error", nil, "", http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) GetTotalActiveUsersCount() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { time := model.GetMillis() - (1000 * 60 * 60 * 24) if count, err := s.GetReplica().SelectInt("SELECT COUNT(UserId) FROM Status WHERE LastActivityAt > :Time", map[string]interface{}{"Time": time}); err != nil { @@ -219,27 +152,13 @@ func (s SqlStatusStore) GetTotalActiveUsersCount() store.StoreChannel { } else { result.Data = count } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlStatusStore) UpdateLastActivityAt(userId string, lastActivityAt int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("UPDATE Status SET LastActivityAt = :Time WHERE UserId = :UserId", map[string]interface{}{"UserId": userId, "Time": lastActivityAt}); err != nil { result.Err = model.NewAppError("SqlStatusStore.UpdateLastActivityAt", "store.sql_status.update_last_activity_at.app_error", nil, "", http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/status_store_test.go b/store/sqlstore/status_store_test.go index 3f3a99837..92374ff7a 100644 --- a/store/sqlstore/status_store_test.go +++ b/store/sqlstore/status_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestSqlStatusStore(t *testing.T) { diff --git a/store/sqlstore/system_store.go b/store/sqlstore/system_store.go index 8d863701a..496ff2ced 100644 --- a/store/sqlstore/system_store.go +++ b/store/sqlstore/system_store.go @@ -30,30 +30,15 @@ func (s SqlSystemStore) CreateIndexesIfNotExists() { } func (s SqlSystemStore) Save(system *model.System) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if err := s.GetMaster().Insert(system); err != nil { result.Err = model.NewAppError("SqlSystemStore.Save", "store.sql_system.save.app_error", nil, err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlSystemStore) SaveOrUpdate(system *model.System) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if err := s.GetReplica().SelectOne(&model.System{}, "SELECT * FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": system.Name}); err == nil { if _, err := s.GetMaster().Update(system); err != nil { result.Err = model.NewAppError("SqlSystemStore.SaveOrUpdate", "store.sql_system.update.app_error", nil, "", http.StatusInternalServerError) @@ -63,39 +48,19 @@ func (s SqlSystemStore) SaveOrUpdate(system *model.System) store.StoreChannel { result.Err = model.NewAppError("SqlSystemStore.SaveOrUpdate", "store.sql_system.save.app_error", nil, "", http.StatusInternalServerError) } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlSystemStore) Update(system *model.System) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Update(system); err != nil { result.Err = model.NewAppError("SqlSystemStore.Update", "store.sql_system.update.app_error", nil, "", http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlSystemStore) Get() store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var systems []model.System props := make(model.StringMap) if _, err := s.GetReplica().Select(&systems, "SELECT * FROM Systems"); err != nil { @@ -107,31 +72,16 @@ func (s SqlSystemStore) Get() store.StoreChannel { result.Data = props } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlSystemStore) GetByName(name string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var system model.System if err := s.GetReplica().SelectOne(&system, "SELECT * FROM Systems WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil { result.Err = model.NewAppError("SqlSystemStore.GetByName", "store.sql_system.get_by_name.app_error", nil, "", http.StatusInternalServerError) } result.Data = &system - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/system_store_test.go b/store/sqlstore/system_store_test.go index 752a4daf4..3a0b593d9 100644 --- a/store/sqlstore/system_store_test.go +++ b/store/sqlstore/system_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestSqlSystemStore(t *testing.T) { diff --git a/store/sqlstore/team_store.go b/store/sqlstore/team_store.go index 1b899da46..c819ec61a 100644 --- a/store/sqlstore/team_store.go +++ b/store/sqlstore/team_store.go @@ -58,24 +58,16 @@ func (s SqlTeamStore) CreateIndexesIfNotExists() { } func (s SqlTeamStore) Save(team *model.Team) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(team.Id) > 0 { result.Err = model.NewAppError("SqlTeamStore.Save", "store.sql_team.save.existing.app_error", nil, "id="+team.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } team.PreSave() if result.Err = team.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -88,26 +80,14 @@ func (s SqlTeamStore) Save(team *model.Team) store.StoreChannel { } else { result.Data = team } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) Update(team *model.Team) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { team.PreUpdate() if result.Err = team.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -129,40 +109,21 @@ func (s SqlTeamStore) Update(team *model.Team) store.StoreChannel { result.Data = team } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) UpdateDisplayName(name string, teamId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("UPDATE Teams SET DisplayName = :Name WHERE Id = :Id", map[string]interface{}{"Name": name, "Id": teamId}); err != nil { result.Err = model.NewAppError("SqlTeamStore.UpdateName", "store.sql_team.update_display_name.app_error", nil, "team_id="+teamId, http.StatusInternalServerError) } else { result.Data = teamId } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) 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 := s.GetReplica().Get(model.Team{}, id); err != nil { result.Err = model.NewAppError("SqlTeamStore.Get", "store.sql_team.get.finding.app_error", nil, "id="+id+", "+err.Error(), http.StatusInternalServerError) } else if obj == nil { @@ -175,20 +136,11 @@ func (s SqlTeamStore) Get(id string) store.StoreChannel { result.Data = team } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetByInviteId(inviteId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { team := model.Team{} if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Id = :InviteId OR InviteId = :InviteId", map[string]interface{}{"InviteId": inviteId}); err != nil { @@ -204,20 +156,11 @@ func (s SqlTeamStore) GetByInviteId(inviteId string) store.StoreChannel { } result.Data = &team - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetByName(name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { team := model.Team{} if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil { @@ -229,20 +172,11 @@ func (s SqlTeamStore) GetByName(name string) store.StoreChannel { } result.Data = &team - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) SearchByName(name string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var teams []*model.Team if _, err := s.GetReplica().Select(&teams, "SELECT * FROM Teams WHERE Name LIKE :Name", map[string]interface{}{"Name": name + "%"}); err != nil { @@ -250,20 +184,11 @@ func (s SqlTeamStore) SearchByName(name string) store.StoreChannel { } result.Data = teams - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) SearchAll(term string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var teams []*model.Team if _, err := s.GetReplica().Select(&teams, "SELECT * FROM Teams WHERE Name LIKE :Term OR DisplayName LIKE :Term", map[string]interface{}{"Term": term + "%"}); err != nil { @@ -271,20 +196,11 @@ func (s SqlTeamStore) SearchAll(term string) store.StoreChannel { } result.Data = teams - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) SearchOpen(term string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var teams []*model.Team if _, err := s.GetReplica().Select(&teams, "SELECT * FROM Teams WHERE Type = 'O' AND AllowOpenInvite = true AND (Name LIKE :Term OR DisplayName LIKE :Term)", map[string]interface{}{"Term": term + "%"}); err != nil { @@ -292,20 +208,11 @@ func (s SqlTeamStore) SearchOpen(term string) store.StoreChannel { } result.Data = teams - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetAll() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []*model.Team if _, err := s.GetReplica().Select(&data, "SELECT * FROM Teams"); err != nil { result.Err = model.NewAppError("SqlTeamStore.GetAllTeams", "store.sql_team.get_all.app_error", nil, err.Error(), http.StatusInternalServerError) @@ -318,20 +225,11 @@ func (s SqlTeamStore) GetAll() store.StoreChannel { } result.Data = data - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetAllPage(offset int, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []*model.Team if _, err := s.GetReplica().Select(&data, "SELECT * FROM Teams LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil { result.Err = model.NewAppError("SqlTeamStore.GetAllTeams", "store.sql_team.get_all.app_error", nil, err.Error(), http.StatusInternalServerError) @@ -344,20 +242,11 @@ func (s SqlTeamStore) GetAllPage(offset int, limit int) store.StoreChannel { } result.Data = data - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetTeamsByUserId(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []*model.Team if _, err := s.GetReplica().Select(&data, "SELECT Teams.* FROM Teams, TeamMembers WHERE TeamMembers.TeamId = Teams.Id AND TeamMembers.UserId = :UserId AND TeamMembers.DeleteAt = 0 AND Teams.DeleteAt = 0", map[string]interface{}{"UserId": userId}); err != nil { result.Err = model.NewAppError("SqlTeamStore.GetTeamsByUserId", "store.sql_team.get_all.app_error", nil, err.Error(), http.StatusInternalServerError) @@ -370,20 +259,11 @@ func (s SqlTeamStore) GetTeamsByUserId(userId string) store.StoreChannel { } result.Data = data - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetAllTeamListing() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := "SELECT * FROM Teams WHERE AllowOpenInvite = 1" if *utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { @@ -402,20 +282,11 @@ func (s SqlTeamStore) GetAllTeamListing() store.StoreChannel { } result.Data = data - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetAllTeamPageListing(offset int, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := "SELECT * FROM Teams WHERE AllowOpenInvite = 1 LIMIT :Limit OFFSET :Offset" if *utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { @@ -434,59 +305,30 @@ func (s SqlTeamStore) GetAllTeamPageListing(offset int, limit int) store.StoreCh } result.Data = data - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) PermanentDelete(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM Teams WHERE Id = :TeamId", map[string]interface{}{"TeamId": teamId}); err != nil { result.Err = model.NewAppError("SqlTeamStore.Delete", "store.sql_team.permanent_delete.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) AnalyticsTeamCount() store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if c, err := s.GetReplica().SelectInt("SELECT COUNT(*) FROM Teams WHERE DeleteAt = 0", map[string]interface{}{}); err != nil { result.Err = model.NewAppError("SqlTeamStore.AnalyticsTeamCount", "store.sql_team.analytics_team_count.app_error", nil, err.Error(), http.StatusInternalServerError) } else { result.Data = c } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) SaveMember(member *model.TeamMember) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if result.Err = member.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -504,13 +346,9 @@ func (s SqlTeamStore) SaveMember(member *model.TeamMember) store.StoreChannel { AND TeamMembers.DeleteAt = 0 AND Users.DeleteAt = 0`, map[string]interface{}{"TeamId": member.TeamId}); err != nil { result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.member_count.app_error", nil, "teamId="+member.TeamId+", "+err.Error(), http.StatusInternalServerError) - storeChannel <- result - close(storeChannel) return } else if int(count) >= *utils.Cfg.TeamSettings.MaxUsersPerTeam { result.Err = model.NewAppError("SqlUserStore.Save", "store.sql_user.save.max_accounts.app_error", nil, "teamId="+member.TeamId, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } @@ -523,25 +361,14 @@ func (s SqlTeamStore) SaveMember(member *model.TeamMember) store.StoreChannel { } else { result.Data = member } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) UpdateMember(member *model.TeamMember) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { member.PreUpdate() if result.Err = member.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -550,20 +377,11 @@ func (s SqlTeamStore) UpdateMember(member *model.TeamMember) store.StoreChannel } else { result.Data = member } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetMember(teamId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var member model.TeamMember err := s.GetReplica().SelectOne(&member, "SELECT * FROM TeamMembers WHERE TeamId = :TeamId AND UserId = :UserId", map[string]interface{}{"TeamId": teamId, "UserId": userId}) if err != nil { @@ -575,20 +393,11 @@ func (s SqlTeamStore) GetMember(teamId string, userId string) store.StoreChannel } else { result.Data = &member } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetMembers(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 members []*model.TeamMember _, err := s.GetReplica().Select(&members, "SELECT * FROM TeamMembers WHERE TeamId = :TeamId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset", map[string]interface{}{"TeamId": teamId, "Offset": offset, "Limit": limit}) if err != nil { @@ -596,20 +405,11 @@ func (s SqlTeamStore) GetMembers(teamId string, offset int, limit int) store.Sto } else { result.Data = members } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetTotalMemberCount(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { count, err := s.GetReplica().SelectInt(` SELECT count(*) @@ -625,20 +425,11 @@ func (s SqlTeamStore) GetTotalMemberCount(teamId string) store.StoreChannel { } else { result.Data = count } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetActiveMemberCount(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { count, err := s.GetReplica().SelectInt(` SELECT count(*) @@ -655,20 +446,11 @@ func (s SqlTeamStore) GetActiveMemberCount(teamId string) store.StoreChannel { } else { result.Data = count } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetMembersByIds(teamId string, userIds []string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var members []*model.TeamMember props := make(map[string]interface{}) idQuery := "" @@ -689,20 +471,11 @@ func (s SqlTeamStore) GetMembersByIds(teamId string, userIds []string) store.Sto } else { result.Data = members } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetTeamsForUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var members []*model.TeamMember _, err := s.GetReplica().Select(&members, "SELECT * FROM TeamMembers WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { @@ -710,20 +483,11 @@ func (s SqlTeamStore) GetTeamsForUser(userId string) store.StoreChannel { } else { result.Data = members } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetChannelUnreadsForAllTeams(excludeTeamId, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []*model.ChannelUnread _, err := s.GetReplica().Select(&data, `SELECT @@ -742,20 +506,11 @@ func (s SqlTeamStore) GetChannelUnreadsForAllTeams(excludeTeamId, userId string) } else { result.Data = data } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) GetChannelUnreadsForTeam(teamId, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var data []*model.ChannelUnread _, err := s.GetReplica().Select(&data, `SELECT @@ -774,64 +529,32 @@ func (s SqlTeamStore) GetChannelUnreadsForTeam(teamId, userId string) store.Stor } else { result.Data = data } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) RemoveMember(teamId string, userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM TeamMembers WHERE TeamId = :TeamId AND UserId = :UserId", map[string]interface{}{"TeamId": teamId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_team.remove_member.app_error", nil, "team_id="+teamId+", user_id="+userId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) RemoveAllMembersByTeam(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM TeamMembers WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_team.remove_member.app_error", nil, "team_id="+teamId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTeamStore) RemoveAllMembersByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM TeamMembers WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_team.remove_member.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/team_store_test.go b/store/sqlstore/team_store_test.go index 0cfe483f7..9ba9da97a 100644 --- a/store/sqlstore/team_store_test.go +++ b/store/sqlstore/team_store_test.go @@ -8,7 +8,7 @@ import ( "time" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/utils" ) diff --git a/store/sqlstore/tokens_store.go b/store/sqlstore/tokens_store.go index 31e39f1fe..ccb58cef1 100644 --- a/store/sqlstore/tokens_store.go +++ b/store/sqlstore/tokens_store.go @@ -34,54 +34,27 @@ func (s SqlTokenStore) CreateIndexesIfNotExists() { } func (s SqlTokenStore) Save(token *model.Token) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if result.Err = token.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } if err := s.GetMaster().Insert(token); err != nil { result.Err = model.NewAppError("SqlTokenStore.Save", "store.sql_recover.save.app_error", nil, "", http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTokenStore) Delete(token string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM Tokens WHERE Token = :Token", map[string]interface{}{"Token": token}); err != nil { result.Err = model.NewAppError("SqlTokenStore.Delete", "store.sql_recover.delete.app_error", nil, "", http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTokenStore) GetByToken(tokenString string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { token := model.Token{} if err := s.GetReplica().SelectOne(&token, "SELECT * FROM Tokens WHERE Token = :Token", map[string]interface{}{"Token": tokenString}); err != nil { @@ -93,12 +66,7 @@ func (s SqlTokenStore) GetByToken(tokenString string) store.StoreChannel { } result.Data = &token - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlTokenStore) Cleanup() { diff --git a/store/sqlstore/user_access_token_store.go b/store/sqlstore/user_access_token_store.go index 5186ead41..558b01cd6 100644 --- a/store/sqlstore/user_access_token_store.go +++ b/store/sqlstore/user_access_token_store.go @@ -37,17 +37,10 @@ func (s SqlUserAccessTokenStore) CreateIndexesIfNotExists() { } func (s SqlUserAccessTokenStore) Save(token *model.UserAccessToken) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { token.PreSave() if result.Err = token.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -56,27 +49,17 @@ func (s SqlUserAccessTokenStore) Save(token *model.UserAccessToken) store.StoreC } else { result.Data = token } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlUserAccessTokenStore) Delete(tokenId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { transaction, err := s.GetMaster().Begin() if err != nil { result.Err = model.NewAppError("SqlUserAccessTokenStore.Delete", "store.sql_user_access_token.delete.app_error", nil, err.Error(), http.StatusInternalServerError) } else { if extrasResult := s.deleteSessionsAndTokensById(transaction, tokenId); extrasResult.Err != nil { - result = extrasResult + *result = extrasResult } if result.Err == nil { @@ -90,12 +73,7 @@ func (s SqlUserAccessTokenStore) Delete(tokenId string) store.StoreChannel { } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlUserAccessTokenStore) deleteSessionsAndTokensById(transaction *gorp.Transaction, tokenId string) store.StoreResult { @@ -127,18 +105,13 @@ func (s SqlUserAccessTokenStore) deleteTokensById(transaction *gorp.Transaction, } func (s SqlUserAccessTokenStore) DeleteAllForUser(userId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { transaction, err := s.GetMaster().Begin() if err != nil { result.Err = model.NewAppError("SqlUserAccessTokenStore.DeleteAllForUser", "store.sql_user_access_token.delete.app_error", nil, err.Error(), http.StatusInternalServerError) } else { if extrasResult := s.deleteSessionsandTokensByUser(transaction, userId); extrasResult.Err != nil { - result = extrasResult + *result = extrasResult } if result.Err == nil { @@ -152,12 +125,7 @@ func (s SqlUserAccessTokenStore) DeleteAllForUser(userId string) store.StoreChan } } } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlUserAccessTokenStore) deleteSessionsandTokensByUser(transaction *gorp.Transaction, userId string) store.StoreResult { @@ -189,12 +157,7 @@ func (s SqlUserAccessTokenStore) deleteTokensByUser(transaction *gorp.Transactio } func (s SqlUserAccessTokenStore) Get(tokenId string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { token := model.UserAccessToken{} if err := s.GetReplica().SelectOne(&token, "SELECT * FROM UserAccessTokens WHERE Id = :Id", map[string]interface{}{"Id": tokenId}); err != nil { @@ -206,21 +169,11 @@ func (s SqlUserAccessTokenStore) Get(tokenId string) store.StoreChannel { } result.Data = &token - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlUserAccessTokenStore) GetByToken(tokenString string) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { token := model.UserAccessToken{} if err := s.GetReplica().SelectOne(&token, "SELECT * FROM UserAccessTokens WHERE Token = :Token", map[string]interface{}{"Token": tokenString}); err != nil { @@ -232,21 +185,11 @@ func (s SqlUserAccessTokenStore) GetByToken(tokenString string) store.StoreChann } result.Data = &token - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlUserAccessTokenStore) GetByUser(userId string, offset, limit int) store.StoreChannel { - - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { tokens := []*model.UserAccessToken{} if _, err := s.GetReplica().Select(&tokens, "SELECT * FROM UserAccessTokens WHERE UserId = :UserId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"UserId": userId, "Offset": offset, "Limit": limit}); err != nil { @@ -254,10 +197,5 @@ func (s SqlUserAccessTokenStore) GetByUser(userId string, offset, limit int) sto } result.Data = tokens - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } diff --git a/store/sqlstore/user_access_token_store_test.go b/store/sqlstore/user_access_token_store_test.go index e160ddfa1..74ce53b64 100644 --- a/store/sqlstore/user_access_token_store_test.go +++ b/store/sqlstore/user_access_token_store_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/mattermost/mattermost-server/model" -"github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/store" ) func TestUserAccessTokenSaveGetDelete(t *testing.T) { 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 + }) } diff --git a/store/sqlstore/webhook_store.go b/store/sqlstore/webhook_store.go index 705cd40bc..8a3720fa0 100644 --- a/store/sqlstore/webhook_store.go +++ b/store/sqlstore/webhook_store.go @@ -81,22 +81,14 @@ func (s SqlWebhookStore) InvalidateWebhookCache(webhookId string) { } func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(webhook.Id) > 0 { result.Err = model.NewAppError("SqlWebhookStore.SaveIncoming", "store.sql_webhooks.save_incoming.existing.app_error", nil, "id="+webhook.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } webhook.PreSave() if result.Err = webhook.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -105,20 +97,11 @@ func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) store.Stor } else { result.Data = webhook } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) UpdateIncoming(hook *model.IncomingWebhook) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { hook.UpdateAt = model.GetMillis() if _, err := s.GetMaster().Update(hook); err != nil { @@ -126,28 +109,17 @@ func (s SqlWebhookStore) UpdateIncoming(hook *model.IncomingWebhook) store.Store } else { result.Data = hook } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetIncoming(id string, allowFromCache bool) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if allowFromCache { if cacheItem, ok := webhookCache.Get(id); ok { if s.metrics != nil { s.metrics.IncrementMemCacheHitCounter("Webhook") } result.Data = cacheItem.(*model.IncomingWebhook) - storeChannel <- result - close(storeChannel) return } else { if s.metrics != nil { @@ -171,80 +143,44 @@ func (s SqlWebhookStore) GetIncoming(id string, allowFromCache bool) store.Store } result.Data = &webhook - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) DeleteIncoming(webhookId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("Update IncomingWebhooks SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": webhookId}) if err != nil { result.Err = model.NewAppError("SqlWebhookStore.DeleteIncoming", "store.sql_webhooks.delete_incoming.app_error", nil, "id="+webhookId+", err="+err.Error(), http.StatusInternalServerError) } s.InvalidateWebhookCache(webhookId) - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) PermanentDeleteIncomingByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM IncomingWebhooks WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlWebhookStore.DeleteIncomingByUser", "store.sql_webhooks.permanent_delete_incoming_by_user.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) } ClearWebhookCaches() - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) PermanentDeleteIncomingByChannel(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM IncomingWebhooks WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlWebhookStore.DeleteIncomingByChannel", "store.sql_webhooks.permanent_delete_incoming_by_channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError) } ClearWebhookCaches() - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetIncomingList(offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhooks []*model.IncomingWebhook if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE DeleteAt = 0 LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Limit": limit, "Offset": offset}); err != nil { @@ -252,20 +188,11 @@ func (s SqlWebhookStore) GetIncomingList(offset, limit int) store.StoreChannel { } result.Data = webhooks - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhooks []*model.IncomingWebhook if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset", map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset}); err != nil { @@ -273,20 +200,11 @@ func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) sto } result.Data = webhooks - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetIncomingByChannel(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhooks []*model.IncomingWebhook if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0", map[string]interface{}{"ChannelId": channelId}); err != nil { @@ -294,31 +212,18 @@ func (s SqlWebhookStore) GetIncomingByChannel(channelId string) store.StoreChann } result.Data = webhooks - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) SaveOutgoing(webhook *model.OutgoingWebhook) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { if len(webhook.Id) > 0 { result.Err = model.NewAppError("SqlWebhookStore.SaveOutgoing", "store.sql_webhooks.save_outgoing.override.app_error", nil, "id="+webhook.Id, http.StatusBadRequest) - storeChannel <- result - close(storeChannel) return } webhook.PreSave() if result.Err = webhook.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) return } @@ -327,20 +232,11 @@ func (s SqlWebhookStore) SaveOutgoing(webhook *model.OutgoingWebhook) store.Stor } else { result.Data = webhook } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetOutgoing(id string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhook model.OutgoingWebhook if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM OutgoingWebhooks WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}); err != nil { @@ -348,20 +244,11 @@ func (s SqlWebhookStore) GetOutgoing(id string) store.StoreChannel { } result.Data = &webhook - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetOutgoingList(offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhooks []*model.OutgoingWebhook if _, err := s.GetReplica().Select(&webhooks, "SELECT * FROM OutgoingWebhooks WHERE DeleteAt = 0 LIMIT :Limit OFFSET :Offset", map[string]interface{}{"Offset": offset, "Limit": limit}); err != nil { @@ -369,20 +256,11 @@ func (s SqlWebhookStore) GetOutgoingList(offset, limit int) store.StoreChannel { } result.Data = webhooks - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetOutgoingByChannel(channelId string, offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhooks []*model.OutgoingWebhook query := "" @@ -397,20 +275,11 @@ func (s SqlWebhookStore) GetOutgoingByChannel(channelId string, offset, limit in } result.Data = webhooks - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) GetOutgoingByTeam(teamId string, offset, limit int) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { var webhooks []*model.OutgoingWebhook query := "" @@ -425,76 +294,40 @@ func (s SqlWebhookStore) GetOutgoingByTeam(teamId string, offset, limit int) sto } result.Data = webhooks - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) DeleteOutgoing(webhookId string, time int64) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("Update OutgoingWebhooks SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": webhookId}) if err != nil { result.Err = model.NewAppError("SqlWebhookStore.DeleteOutgoing", "store.sql_webhooks.delete_outgoing.app_error", nil, "id="+webhookId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) PermanentDeleteOutgoingByUser(userId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM OutgoingWebhooks WHERE CreatorId = :UserId", map[string]interface{}{"UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlWebhookStore.DeleteOutgoingByUser", "store.sql_webhooks.permanent_delete_outgoing_by_user.app_error", nil, "id="+userId+", err="+err.Error(), http.StatusInternalServerError) } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) PermanentDeleteOutgoingByChannel(channelId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { _, err := s.GetMaster().Exec("DELETE FROM OutgoingWebhooks WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId}) if err != nil { result.Err = model.NewAppError("SqlWebhookStore.DeleteOutgoingByChannel", "store.sql_webhooks.permanent_delete_outgoing_by_channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError) } ClearWebhookCaches() - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) UpdateOutgoing(hook *model.OutgoingWebhook) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { hook.UpdateAt = model.GetMillis() if _, err := s.GetMaster().Update(hook); err != nil { @@ -502,20 +335,11 @@ func (s SqlWebhookStore) UpdateOutgoing(hook *model.OutgoingWebhook) store.Store } else { result.Data = hook } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) AnalyticsIncomingCount(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT COUNT(*) @@ -533,20 +357,11 @@ func (s SqlWebhookStore) AnalyticsIncomingCount(teamId string) store.StoreChanne } else { result.Data = v } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } func (s SqlWebhookStore) AnalyticsOutgoingCount(teamId string) store.StoreChannel { - storeChannel := make(store.StoreChannel, 1) - - go func() { - result := store.StoreResult{} - + return store.Do(func(result *store.StoreResult) { query := `SELECT COUNT(*) @@ -564,10 +379,5 @@ func (s SqlWebhookStore) AnalyticsOutgoingCount(teamId string) store.StoreChanne } else { result.Data = v } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel + }) } -- cgit v1.2.3-1-g7c22