diff options
author | Harshil Sharma <harshil.sharma@joshtechnologygroup.com> | 2018-10-10 00:55:47 +0000 |
---|---|---|
committer | Jesse Hallam <jesse.hallam@gmail.com> | 2018-10-09 20:55:47 -0400 |
commit | bffcccf99de8a8f3c68cff9e39d2847f0996b67b (patch) | |
tree | 10bf45d87927dc792a233d0653d6ba273c9a7b28 /store/sqlstore/terms_of_service_store.go | |
parent | 59319b7915b8eb4c20a0d4878382cc0e41fc536d (diff) | |
download | chat-bffcccf99de8a8f3c68cff9e39d2847f0996b67b.tar.gz chat-bffcccf99de8a8f3c68cff9e39d2847f0996b67b.tar.bz2 chat-bffcccf99de8a8f3c68cff9e39d2847f0996b67b.zip |
Refactored to rename "service terms" to "terms of service" (#9581)
* #124 renamed identififers from service terms to terms of service
* #124 renamed identififers from service terms to terms of service
* 124 renamed ServiceTerms model to TermsOfService
* 124 Renamed EnableCustomServiceTerms feature flag to EnableCustomTermsOfService
* 124 Renamed EnableCustomServiceTerms feature flag to EnableCustomTermsOfService
* #124 fixed formatting
* #124 fixed formatting
* #132 renamed table ServiceTerms to TermsOfService
* #124 renamed some missed files from 'service_terms' to 'terms_of_service'
* #124 removed fixed TODOs
* drop migrate of ServiceTerms table, since backporting
* s/ServiceTerms/TermsOfService/ in tests
* s/AcceptedServiceTermsId/AcceptedTermsOfServiceId/
Change the model attribute, even though the column name will eventually be removed.
* s/accepted_service_terms_id/accepted_terms_of_service_id/ to match redux
* s/serviceTerms/termsOfService
* rename column too, and add max size constraint
* s/EnableCustomServiceTerms/EnableCustomTermsOfService
Diffstat (limited to 'store/sqlstore/terms_of_service_store.go')
-rw-r--r-- | store/sqlstore/terms_of_service_store.go | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/store/sqlstore/terms_of_service_store.go b/store/sqlstore/terms_of_service_store.go new file mode 100644 index 000000000..dc9ce5b5c --- /dev/null +++ b/store/sqlstore/terms_of_service_store.go @@ -0,0 +1,143 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package sqlstore + +import ( + "database/sql" + "github.com/mattermost/mattermost-server/einterfaces" + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/store" + "github.com/mattermost/mattermost-server/utils" + "net/http" +) + +type SqlTermsOfServiceStore struct { + SqlStore + metrics einterfaces.MetricsInterface +} + +var termsOfServiceCache = utils.NewLru(model.TERMS_OF_SERVICE_CACHE_SIZE) + +const termsOfServiceCacheName = "TermsOfServiceStore" + +func NewSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.TermsOfServiceStore { + s := SqlTermsOfServiceStore{sqlStore, metrics} + + for _, db := range sqlStore.GetAllConns() { + table := db.AddTableWithName(model.TermsOfService{}, "TermsOfService").SetKeys(false, "Id") + table.ColMap("Id").SetMaxSize(26) + table.ColMap("UserId").SetMaxSize(26) + table.ColMap("Text").SetMaxSize(model.POST_MESSAGE_MAX_BYTES_V2) + } + + return s +} + +func (s SqlTermsOfServiceStore) CreateIndexesIfNotExists() { +} + +func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) store.StoreChannel { + return store.Do(func(result *store.StoreResult) { + if len(termsOfService.Id) > 0 { + result.Err = model.NewAppError( + "SqlTermsOfServiceStore.Save", + "store.sql_terms_of_service_store.save.existing.app_error", + nil, + "id="+termsOfService.Id, http.StatusBadRequest, + ) + return + } + + termsOfService.PreSave() + + if result.Err = termsOfService.IsValid(); result.Err != nil { + return + } + + if err := s.GetMaster().Insert(termsOfService); err != nil { + result.Err = model.NewAppError( + "SqlTermsOfServiceStore.Save", + "store.sql_terms_of_service.save.app_error", + nil, + "terms_of_service_id="+termsOfService.Id+",err="+err.Error(), + http.StatusInternalServerError, + ) + } + + result.Data = termsOfService + + termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService) + }) +} + +func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) store.StoreChannel { + return store.Do(func(result *store.StoreResult) { + if allowFromCache { + if termsOfServiceCache.Len() == 0 { + if s.metrics != nil { + s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName) + } + } else { + if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok { + if s.metrics != nil { + s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName) + } + + result.Data = cacheItem.(*model.TermsOfService) + return + } else if s.metrics != nil { + s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName) + } + } + } + + var termsOfService *model.TermsOfService + + err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1") + if err != nil { + if err == sql.ErrNoRows { + result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound) + } else { + result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) + } + } else { + result.Data = termsOfService + + if allowFromCache { + termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService) + } + } + }) +} + +func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) store.StoreChannel { + return store.Do(func(result *store.StoreResult) { + if allowFromCache { + if termsOfServiceCache.Len() == 0 { + if s.metrics != nil { + s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName) + } + } else { + if cacheItem, ok := termsOfServiceCache.Get(id); ok { + if s.metrics != nil { + s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName) + } + + result.Data = cacheItem.(*model.TermsOfService) + return + } else if s.metrics != nil { + s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName) + } + } + } + + if obj, err := s.GetReplica().Get(model.TermsOfService{}, id); err != nil { + result.Err = model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) + } else if obj == nil { + result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound) + } else { + result.Data = obj.(*model.TermsOfService) + } + }) +} |