2019-11-29 12:59:40 +01:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2017-08-16 07:17:57 -05:00
2017-09-25 09:11:25 -05:00
package sqlstore
2017-08-16 07:17:57 -05:00
import (
"database/sql"
2017-09-18 18:31:31 +01:00
"net/http"
2017-08-16 07:17:57 -05:00
2019-11-28 14:39:38 +01:00
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
2017-08-16 07:17:57 -05:00
)
type SqlCommandWebhookStore struct {
SqlStore
}
2020-03-03 11:45:49 +01:00
func newSqlCommandWebhookStore ( sqlStore SqlStore ) store . CommandWebhookStore {
2017-08-16 07:17:57 -05:00
s := & SqlCommandWebhookStore { sqlStore }
for _ , db := range sqlStore . GetAllConns ( ) {
tablec := db . AddTableWithName ( model . CommandWebhook { } , "CommandWebhooks" ) . SetKeys ( false , "Id" )
tablec . ColMap ( "Id" ) . SetMaxSize ( 26 )
tablec . ColMap ( "CommandId" ) . SetMaxSize ( 26 )
tablec . ColMap ( "UserId" ) . SetMaxSize ( 26 )
tablec . ColMap ( "ChannelId" ) . SetMaxSize ( 26 )
tablec . ColMap ( "RootId" ) . SetMaxSize ( 26 )
tablec . ColMap ( "ParentId" ) . SetMaxSize ( 26 )
}
return s
}
2020-03-03 11:45:49 +01:00
func ( s SqlCommandWebhookStore ) createIndexesIfNotExists ( ) {
2017-08-16 07:17:57 -05:00
s . CreateIndexIfNotExists ( "idx_command_webhook_create_at" , "CommandWebhooks" , "CreateAt" )
}
2019-07-09 10:42:10 -04:00
func ( s SqlCommandWebhookStore ) Save ( webhook * model . CommandWebhook ) ( * model . CommandWebhook , * model . AppError ) {
if len ( webhook . Id ) > 0 {
return nil , model . NewAppError ( "SqlCommandWebhookStore.Save" , "store.sql_command_webhooks.save.existing.app_error" , nil , "id=" + webhook . Id , http . StatusBadRequest )
}
2017-08-16 07:17:57 -05:00
2019-07-09 10:42:10 -04:00
webhook . PreSave ( )
if err := webhook . IsValid ( ) ; err != nil {
return nil , err
}
2017-08-16 07:17:57 -05:00
2019-07-09 10:42:10 -04:00
if err := s . GetMaster ( ) . Insert ( webhook ) ; err != nil {
return nil , model . NewAppError ( "SqlCommandWebhookStore.Save" , "store.sql_command_webhooks.save.app_error" , nil , "id=" + webhook . Id + ", " + err . Error ( ) , http . StatusInternalServerError )
}
return webhook , nil
2017-08-16 07:17:57 -05:00
}
2019-07-09 11:54:25 -04:00
func ( s SqlCommandWebhookStore ) Get ( id string ) ( * model . CommandWebhook , * model . AppError ) {
var webhook model . CommandWebhook
exptime := model . GetMillis ( ) - model . COMMAND_WEBHOOK_LIFETIME
var appErr * model . AppError
if err := s . GetReplica ( ) . SelectOne ( & webhook , "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime" , map [ string ] interface { } { "Id" : id , "ExpTime" : exptime } ) ; err != nil {
appErr = model . NewAppError ( "SqlCommandWebhookStore.Get" , "store.sql_command_webhooks.get.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusInternalServerError )
if err == sql . ErrNoRows {
appErr . StatusCode = http . StatusNotFound
2017-08-16 07:17:57 -05:00
}
2019-07-09 11:54:25 -04:00
return nil , appErr
}
2017-08-16 07:17:57 -05:00
2019-07-09 11:54:25 -04:00
return & webhook , nil
2017-08-16 07:17:57 -05:00
}
2019-07-09 16:57:05 -04:00
func ( s SqlCommandWebhookStore ) TryUse ( id string , limit int ) * model . AppError {
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 {
return model . NewAppError ( "SqlCommandWebhookStore.TryUse" , "store.sql_command_webhooks.try_use.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusInternalServerError )
} else if rows , _ := sqlResult . RowsAffected ( ) ; rows == 0 {
return model . NewAppError ( "SqlCommandWebhookStore.TryUse" , "store.sql_command_webhooks.try_use.invalid.app_error" , nil , "id=" + id , http . StatusBadRequest )
}
2017-08-16 07:17:57 -05:00
2019-07-09 16:57:05 -04:00
return nil
2017-08-16 07:17:57 -05:00
}
func ( s SqlCommandWebhookStore ) Cleanup ( ) {
2018-04-27 12:49:45 -07:00
mlog . Debug ( "Cleaning up command webhook store." )
2017-08-16 07:17:57 -05:00
exptime := model . GetMillis ( ) - model . COMMAND_WEBHOOK_LIFETIME
if _ , err := s . GetMaster ( ) . Exec ( "DELETE FROM CommandWebhooks WHERE CreateAt < :ExpTime" , map [ string ] interface { } { "ExpTime" : exptime } ) ; err != nil {
2018-04-27 12:49:45 -07:00
mlog . Error ( "Unable to cleanup command webhook store." )
2017-08-16 07:17:57 -05:00
}
}