reduce store boiler plate (#7585)

This commit is contained in:
Chris
2017-10-06 08:12:10 -07:00
committed by Joram Wilander
parent 12501673d0
commit 363568b4eb
35 changed files with 589 additions and 3269 deletions

View File

@@ -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
})
}

File diff suppressed because it is too large Load Diff

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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() {

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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 {

View File

@@ -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
})
}

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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"
)

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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) {

View File

@@ -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
})
}

View File

@@ -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"
)

View File

@@ -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() {

View File

@@ -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
})
}

View File

@@ -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) {

File diff suppressed because it is too large Load Diff

View File

@@ -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
})
}