github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/store/sqlstore/command_webhook_store.go (about) 1 // Copyright (c) 2017-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 l4g "github.com/alecthomas/log4go" 11 12 "github.com/mattermost/mattermost-server/model" 13 "github.com/mattermost/mattermost-server/store" 14 ) 15 16 type SqlCommandWebhookStore struct { 17 SqlStore 18 } 19 20 func NewSqlCommandWebhookStore(sqlStore SqlStore) store.CommandWebhookStore { 21 s := &SqlCommandWebhookStore{sqlStore} 22 23 for _, db := range sqlStore.GetAllConns() { 24 tablec := db.AddTableWithName(model.CommandWebhook{}, "CommandWebhooks").SetKeys(false, "Id") 25 tablec.ColMap("Id").SetMaxSize(26) 26 tablec.ColMap("CommandId").SetMaxSize(26) 27 tablec.ColMap("UserId").SetMaxSize(26) 28 tablec.ColMap("ChannelId").SetMaxSize(26) 29 tablec.ColMap("RootId").SetMaxSize(26) 30 tablec.ColMap("ParentId").SetMaxSize(26) 31 } 32 33 return s 34 } 35 36 func (s SqlCommandWebhookStore) CreateIndexesIfNotExists() { 37 s.CreateIndexIfNotExists("idx_command_webhook_create_at", "CommandWebhooks", "CreateAt") 38 } 39 40 func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) store.StoreChannel { 41 return store.Do(func(result *store.StoreResult) { 42 if len(webhook.Id) > 0 { 43 result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.existing.app_error", nil, "id="+webhook.Id, http.StatusBadRequest) 44 return 45 } 46 47 webhook.PreSave() 48 if result.Err = webhook.IsValid(); result.Err != nil { 49 return 50 } 51 52 if err := s.GetMaster().Insert(webhook); err != nil { 53 result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.app_error", nil, "id="+webhook.Id+", "+err.Error(), http.StatusInternalServerError) 54 } else { 55 result.Data = webhook 56 } 57 }) 58 } 59 60 func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel { 61 return store.Do(func(result *store.StoreResult) { 62 var webhook model.CommandWebhook 63 64 exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME 65 if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime", map[string]interface{}{"Id": id, "ExpTime": exptime}); err != nil { 66 result.Err = model.NewAppError("SqlCommandWebhookStore.Get", "store.sql_command_webhooks.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) 67 if err == sql.ErrNoRows { 68 result.Err.StatusCode = http.StatusNotFound 69 } 70 } 71 72 result.Data = &webhook 73 }) 74 } 75 76 func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { 77 return store.Do(func(result *store.StoreResult) { 78 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 { 79 result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) 80 } else if rows, _ := sqlResult.RowsAffected(); rows == 0 { 81 result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.invalid.app_error", nil, "id="+id, http.StatusBadRequest) 82 } 83 84 result.Data = id 85 }) 86 } 87 88 func (s SqlCommandWebhookStore) Cleanup() { 89 l4g.Debug("Cleaning up command webhook store.") 90 exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME 91 if _, err := s.GetMaster().Exec("DELETE FROM CommandWebhooks WHERE CreateAt < :ExpTime", map[string]interface{}{"ExpTime": exptime}); err != nil { 92 l4g.Error("Unable to cleanup command webhook store.") 93 } 94 }