github.com/mad-app/mattermost-server@v5.11.1+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  	"github.com/mattermost/mattermost-server/mlog"
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/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("ChannelId").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) store.StoreChannel {
    40  	return store.Do(func(result *store.StoreResult) {
    41  		if len(webhook.Id) > 0 {
    42  			result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.existing.app_error", nil, "id="+webhook.Id, http.StatusBadRequest)
    43  			return
    44  		}
    45  
    46  		webhook.PreSave()
    47  		if result.Err = webhook.IsValid(); result.Err != nil {
    48  			return
    49  		}
    50  
    51  		if err := s.GetMaster().Insert(webhook); err != nil {
    52  			result.Err = model.NewAppError("SqlCommandWebhookStore.Save", "store.sql_command_webhooks.save.app_error", nil, "id="+webhook.Id+", "+err.Error(), http.StatusInternalServerError)
    53  		} else {
    54  			result.Data = webhook
    55  		}
    56  	})
    57  }
    58  
    59  func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel {
    60  	return store.Do(func(result *store.StoreResult) {
    61  		var webhook model.CommandWebhook
    62  
    63  		exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
    64  		if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime", map[string]interface{}{"Id": id, "ExpTime": exptime}); err != nil {
    65  			result.Err = model.NewAppError("SqlCommandWebhookStore.Get", "store.sql_command_webhooks.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
    66  			if err == sql.ErrNoRows {
    67  				result.Err.StatusCode = http.StatusNotFound
    68  			}
    69  		}
    70  
    71  		result.Data = &webhook
    72  	})
    73  }
    74  
    75  func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel {
    76  	return store.Do(func(result *store.StoreResult) {
    77  		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 {
    78  			result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
    79  		} else if rows, _ := sqlResult.RowsAffected(); rows == 0 {
    80  			result.Err = model.NewAppError("SqlCommandWebhookStore.TryUse", "store.sql_command_webhooks.try_use.invalid.app_error", nil, "id="+id, http.StatusBadRequest)
    81  		}
    82  
    83  		result.Data = id
    84  	})
    85  }
    86  
    87  func (s SqlCommandWebhookStore) Cleanup() {
    88  	mlog.Debug("Cleaning up command webhook store.")
    89  	exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
    90  	if _, err := s.GetMaster().Exec("DELETE FROM CommandWebhooks WHERE CreateAt < :ExpTime", map[string]interface{}{"ExpTime": exptime}); err != nil {
    91  		mlog.Error("Unable to cleanup command webhook store.")
    92  	}
    93  }