github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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  	sq "github.com/Masterminds/squirrel"
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/mlog"
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  	"github.com/mattermost/mattermost-server/v5/store"
    15  )
    16  
    17  type SqlCommandWebhookStore struct {
    18  	*SqlStore
    19  }
    20  
    21  func newSqlCommandWebhookStore(sqlStore *SqlStore) store.CommandWebhookStore {
    22  	s := &SqlCommandWebhookStore{sqlStore}
    23  
    24  	for _, db := range sqlStore.GetAllConns() {
    25  		tablec := db.AddTableWithName(model.CommandWebhook{}, "CommandWebhooks").SetKeys(false, "Id")
    26  		tablec.ColMap("Id").SetMaxSize(26)
    27  		tablec.ColMap("CommandId").SetMaxSize(26)
    28  		tablec.ColMap("UserId").SetMaxSize(26)
    29  		tablec.ColMap("ChannelId").SetMaxSize(26)
    30  		tablec.ColMap("RootId").SetMaxSize(26)
    31  		tablec.ColMap("ParentId").SetMaxSize(26)
    32  	}
    33  
    34  	return s
    35  }
    36  
    37  func (s SqlCommandWebhookStore) createIndexesIfNotExists() {
    38  	s.CreateIndexIfNotExists("idx_command_webhook_create_at", "CommandWebhooks", "CreateAt")
    39  }
    40  
    41  func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) (*model.CommandWebhook, error) {
    42  	if webhook.Id != "" {
    43  		return nil, store.NewErrInvalidInput("CommandWebhook", "id", webhook.Id)
    44  	}
    45  
    46  	webhook.PreSave()
    47  	if err := webhook.IsValid(); err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	if err := s.GetMaster().Insert(webhook); err != nil {
    52  		return nil, errors.Wrapf(err, "save: id=%s", webhook.Id)
    53  	}
    54  
    55  	return webhook, nil
    56  }
    57  
    58  func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, error) {
    59  	var webhook model.CommandWebhook
    60  
    61  	exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
    62  
    63  	query := s.getQueryBuilder().
    64  		Select("*").
    65  		From("CommandWebhooks").
    66  		Where(sq.Eq{"Id": id}).
    67  		Where(sq.Gt{"CreateAt": exptime})
    68  
    69  	queryString, args, err := query.ToSql()
    70  	if err != nil {
    71  		return nil, errors.Wrap(err, "get_tosql")
    72  	}
    73  
    74  	if err := s.GetReplica().SelectOne(&webhook, queryString, args...); err != nil {
    75  		if err == sql.ErrNoRows {
    76  			return nil, store.NewErrNotFound("CommandWebhook", id)
    77  		}
    78  		return nil, errors.Wrapf(err, "get: id=%s", id)
    79  	}
    80  
    81  	return &webhook, nil
    82  }
    83  
    84  func (s SqlCommandWebhookStore) TryUse(id string, limit int) error {
    85  	query := s.getQueryBuilder().
    86  		Update("CommandWebhooks").
    87  		Set("UseCount", sq.Expr("UseCount + 1")).
    88  		Where(sq.Eq{"Id": id}).
    89  		Where(sq.Lt{"UseCount": limit})
    90  
    91  	queryString, args, err := query.ToSql()
    92  	if err != nil {
    93  		return errors.Wrap(err, "tryuse_tosql")
    94  	}
    95  
    96  	if sqlResult, err := s.GetMaster().Exec(queryString, args...); err != nil {
    97  		return errors.Wrapf(err, "tryuse: id=%s limit=%d", id, limit)
    98  	} else if rows, _ := sqlResult.RowsAffected(); rows == 0 {
    99  		return store.NewErrInvalidInput("CommandWebhook", "id", id)
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func (s SqlCommandWebhookStore) Cleanup() {
   106  	mlog.Debug("Cleaning up command webhook store.")
   107  	exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
   108  
   109  	query := s.getQueryBuilder().
   110  		Delete("CommandWebhooks").
   111  		Where(sq.Lt{"CreateAt": exptime})
   112  
   113  	queryString, args, err := query.ToSql()
   114  	if err != nil {
   115  		mlog.Error("Failed to build query when trying to perform a cleanup in command webhook store.")
   116  		return
   117  	}
   118  
   119  	if _, err := s.GetMaster().Exec(queryString, args...); err != nil {
   120  		mlog.Error("Unable to cleanup command webhook store.")
   121  	}
   122  }