2017-08-16 07:17:57 -05:00
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
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
l4g "github.com/alecthomas/log4go"
2017-09-06 23:05:10 -07:00
"github.com/mattermost/mattermost-server/model"
2017-09-25 09:11:25 -05:00
"github.com/mattermost/mattermost-server/store"
2017-08-16 07:17:57 -05:00
)
type SqlCommandWebhookStore struct {
SqlStore
}
2017-09-25 09:11:25 -05: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
}
func ( s SqlCommandWebhookStore ) CreateIndexesIfNotExists ( ) {
s . CreateIndexIfNotExists ( "idx_command_webhook_create_at" , "CommandWebhooks" , "CreateAt" )
}
2017-09-25 09:11:25 -05:00
func ( s SqlCommandWebhookStore ) Save ( webhook * model . CommandWebhook ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-08-16 07:17:57 -05:00
if len ( webhook . Id ) > 0 {
2017-09-18 18:31:31 +01:00
result . Err = 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
return
}
webhook . PreSave ( )
if result . Err = webhook . IsValid ( ) ; result . Err != nil {
return
}
if err := s . GetMaster ( ) . Insert ( webhook ) ; err != nil {
2017-09-18 18:31:31 +01:00
result . Err = model . NewAppError ( "SqlCommandWebhookStore.Save" , "store.sql_command_webhooks.save.app_error" , nil , "id=" + webhook . Id + ", " + err . Error ( ) , http . StatusInternalServerError )
2017-08-16 07:17:57 -05:00
} else {
result . Data = webhook
}
2017-10-06 08:12:10 -07:00
} )
2017-08-16 07:17:57 -05:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlCommandWebhookStore ) Get ( id string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-08-16 07:17:57 -05:00
var webhook model . CommandWebhook
exptime := model . GetMillis ( ) - model . COMMAND_WEBHOOK_LIFETIME
if err := s . GetReplica ( ) . SelectOne ( & webhook , "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime" , map [ string ] interface { } { "Id" : id , "ExpTime" : exptime } ) ; err != nil {
2017-09-18 18:31:31 +01:00
result . Err = model . NewAppError ( "SqlCommandWebhookStore.Get" , "store.sql_command_webhooks.get.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2017-08-16 07:17:57 -05:00
if err == sql . ErrNoRows {
result . Err . StatusCode = http . StatusNotFound
}
}
result . Data = & webhook
2017-10-06 08:12:10 -07:00
} )
2017-08-16 07:17:57 -05:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlCommandWebhookStore ) TryUse ( id string , limit int ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-08-16 07:17:57 -05:00
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 {
2017-09-18 18:31:31 +01:00
result . Err = model . NewAppError ( "SqlCommandWebhookStore.TryUse" , "store.sql_command_webhooks.try_use.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2017-08-16 07:17:57 -05:00
} else if rows , _ := sqlResult . RowsAffected ( ) ; rows == 0 {
result . Err = model . NewAppError ( "SqlCommandWebhookStore.TryUse" , "store.sql_command_webhooks.try_use.invalid.app_error" , nil , "id=" + id , http . StatusBadRequest )
}
result . Data = id
2017-10-06 08:12:10 -07:00
} )
2017-08-16 07:17:57 -05:00
}
func ( s SqlCommandWebhookStore ) Cleanup ( ) {
l4g . Debug ( "Cleaning up command webhook store." )
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 {
l4g . Error ( "Unable to cleanup command webhook store." )
}
}