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