github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/terms_of_service_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"database/sql"
     8  	"net/http"
     9  
    10  	"github.com/mattermost/mattermost-server/einterfaces"
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/store"
    13  	"github.com/mattermost/mattermost-server/utils"
    14  )
    15  
    16  type SqlTermsOfServiceStore struct {
    17  	SqlStore
    18  	metrics einterfaces.MetricsInterface
    19  }
    20  
    21  var termsOfServiceCache = utils.NewLru(model.TERMS_OF_SERVICE_CACHE_SIZE)
    22  
    23  const (
    24  	termsOfServiceCacheName = "TermsOfServiceStore"
    25  )
    26  
    27  func NewSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.TermsOfServiceStore {
    28  	s := SqlTermsOfServiceStore{sqlStore, metrics}
    29  
    30  	for _, db := range sqlStore.GetAllConns() {
    31  		table := db.AddTableWithName(model.TermsOfService{}, "TermsOfService").SetKeys(false, "Id")
    32  		table.ColMap("Id").SetMaxSize(26)
    33  		table.ColMap("UserId").SetMaxSize(26)
    34  		table.ColMap("Text").SetMaxSize(model.POST_MESSAGE_MAX_BYTES_V2)
    35  	}
    36  
    37  	return s
    38  }
    39  
    40  func (s SqlTermsOfServiceStore) CreateIndexesIfNotExists() {
    41  }
    42  
    43  func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) store.StoreChannel {
    44  	return store.Do(func(result *store.StoreResult) {
    45  		if len(termsOfService.Id) > 0 {
    46  			result.Err = model.NewAppError(
    47  				"SqlTermsOfServiceStore.Save",
    48  				"store.sql_terms_of_service_store.save.existing.app_error",
    49  				nil,
    50  				"id="+termsOfService.Id, http.StatusBadRequest,
    51  			)
    52  			return
    53  		}
    54  
    55  		termsOfService.PreSave()
    56  
    57  		if result.Err = termsOfService.IsValid(); result.Err != nil {
    58  			return
    59  		}
    60  
    61  		if err := s.GetMaster().Insert(termsOfService); err != nil {
    62  			result.Err = model.NewAppError(
    63  				"SqlTermsOfServiceStore.Save",
    64  				"store.sql_terms_of_service.save.app_error",
    65  				nil,
    66  				"terms_of_service_id="+termsOfService.Id+",err="+err.Error(),
    67  				http.StatusInternalServerError,
    68  			)
    69  		}
    70  
    71  		result.Data = termsOfService
    72  
    73  		termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
    74  	})
    75  }
    76  
    77  func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) store.StoreChannel {
    78  	return store.Do(func(result *store.StoreResult) {
    79  		if allowFromCache {
    80  			if termsOfServiceCache.Len() == 0 {
    81  				if s.metrics != nil {
    82  					s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
    83  				}
    84  			} else {
    85  				if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok {
    86  					if s.metrics != nil {
    87  						s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
    88  					}
    89  
    90  					result.Data = cacheItem.(*model.TermsOfService)
    91  					return
    92  				} else if s.metrics != nil {
    93  					s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
    94  				}
    95  			}
    96  		}
    97  
    98  		var termsOfService *model.TermsOfService
    99  
   100  		err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1")
   101  		if err != nil {
   102  			if err == sql.ErrNoRows {
   103  				result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound)
   104  			} else {
   105  				result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
   106  			}
   107  		} else {
   108  			result.Data = termsOfService
   109  
   110  			if allowFromCache {
   111  				termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
   112  			}
   113  		}
   114  	})
   115  }
   116  
   117  func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) store.StoreChannel {
   118  	return store.Do(func(result *store.StoreResult) {
   119  		if allowFromCache {
   120  			if termsOfServiceCache.Len() == 0 {
   121  				if s.metrics != nil {
   122  					s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
   123  				}
   124  			} else {
   125  				if cacheItem, ok := termsOfServiceCache.Get(id); ok {
   126  					if s.metrics != nil {
   127  						s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
   128  					}
   129  
   130  					result.Data = cacheItem.(*model.TermsOfService)
   131  					return
   132  				} else if s.metrics != nil {
   133  					s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
   134  				}
   135  			}
   136  		}
   137  
   138  		if obj, err := s.GetReplica().Get(model.TermsOfService{}, id); err != nil {
   139  			result.Err = model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
   140  		} else if obj == nil {
   141  			result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
   142  		} else {
   143  			result.Data = obj.(*model.TermsOfService)
   144  		}
   145  	})
   146  }