github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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  
     9  	"github.com/mattermost/mattermost-server/v5/mlog"
    10  	"github.com/mattermost/mattermost-server/v5/model"
    11  	"github.com/mattermost/mattermost-server/v5/store"
    12  
    13  	"github.com/pkg/errors"
    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) (*model.CommandWebhook, error) {
    41  	if len(webhook.Id) > 0 {
    42  		return nil, store.NewErrInvalidInput("CommandWebhook", "id", webhook.Id)
    43  	}
    44  
    45  	webhook.PreSave()
    46  	if err := webhook.IsValid(); err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	if err := s.GetMaster().Insert(webhook); err != nil {
    51  		return nil, errors.Wrapf(err, "save: id=%s", webhook.Id)
    52  	}
    53  
    54  	return webhook, nil
    55  }
    56  
    57  func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, error) {
    58  	var webhook model.CommandWebhook
    59  
    60  	exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
    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  		if err == sql.ErrNoRows {
    63  			return nil, store.NewErrNotFound("CommandWebhook", id)
    64  		}
    65  		return nil, errors.Wrapf(err, "get: id=%s", id)
    66  	}
    67  
    68  	return &webhook, nil
    69  }
    70  
    71  func (s SqlCommandWebhookStore) TryUse(id string, limit int) error {
    72  	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 {
    73  		return errors.Wrapf(err, "tryuse: id=%s limit=%d", id, limit)
    74  	} else if rows, _ := sqlResult.RowsAffected(); rows == 0 {
    75  		return store.NewErrInvalidInput("CommandWebhook", "id", id)
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func (s SqlCommandWebhookStore) Cleanup() {
    82  	mlog.Debug("Cleaning up command webhook store.")
    83  	exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
    84  	if _, err := s.GetMaster().Exec("DELETE FROM CommandWebhooks WHERE CreateAt < :ExpTime", map[string]interface{}{"ExpTime": exptime}); err != nil {
    85  		mlog.Error("Unable to cleanup command webhook store.")
    86  	}
    87  }