2017-04-12 08:27:57 -04:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2015-09-21 14:22:23 -04:00
// See License.txt for license information.
2017-09-25 09:11:25 -05:00
package sqlstore
2015-09-21 14:22:23 -04:00
import (
2017-03-12 04:10:56 +05:30
"database/sql"
2019-04-22 23:28:08 +08:00
"net/http"
2017-03-12 04:10:56 +05:30
2017-09-06 23:05:10 -07:00
"github.com/mattermost/mattermost-server/einterfaces"
"github.com/mattermost/mattermost-server/model"
2017-09-25 09:11:25 -05:00
"github.com/mattermost/mattermost-server/store"
2017-09-06 23:05:10 -07:00
"github.com/mattermost/mattermost-server/utils"
2015-09-21 14:22:23 -04:00
)
type SqlWebhookStore struct {
2017-06-27 08:02:08 -07:00
SqlStore
2017-09-14 12:01:44 -05:00
metrics einterfaces . MetricsInterface
2015-09-21 14:22:23 -04:00
}
2017-01-27 14:07:34 -05:00
const (
WEBHOOK_CACHE_SIZE = 25000
WEBHOOK_CACHE_SEC = 900 // 15 minutes
)
var webhookCache = utils . NewLru ( WEBHOOK_CACHE_SIZE )
2018-03-05 10:35:26 -05:00
func ( s SqlWebhookStore ) ClearCaches ( ) {
2017-01-27 14:07:34 -05:00
webhookCache . Purge ( )
2018-03-05 10:35:26 -05:00
if s . metrics != nil {
s . metrics . IncrementMemCacheInvalidationCounter ( "Webhook - Purge" )
}
2017-01-27 14:07:34 -05:00
}
2017-09-25 09:11:25 -05:00
func NewSqlWebhookStore ( sqlStore SqlStore , metrics einterfaces . MetricsInterface ) store . WebhookStore {
2017-09-14 12:01:44 -05:00
s := & SqlWebhookStore {
SqlStore : sqlStore ,
metrics : metrics ,
}
2015-09-21 14:22:23 -04:00
for _ , db := range sqlStore . GetAllConns ( ) {
table := db . AddTableWithName ( model . IncomingWebhook { } , "IncomingWebhooks" ) . SetKeys ( false , "Id" )
table . ColMap ( "Id" ) . SetMaxSize ( 26 )
table . ColMap ( "UserId" ) . SetMaxSize ( 26 )
table . ColMap ( "ChannelId" ) . SetMaxSize ( 26 )
table . ColMap ( "TeamId" ) . SetMaxSize ( 26 )
2016-03-30 10:56:20 -04:00
table . ColMap ( "DisplayName" ) . SetMaxSize ( 64 )
2018-09-04 14:24:41 +03:00
table . ColMap ( "Description" ) . SetMaxSize ( 500 )
2015-10-01 14:07:20 -04:00
tableo := db . AddTableWithName ( model . OutgoingWebhook { } , "OutgoingWebhooks" ) . SetKeys ( false , "Id" )
tableo . ColMap ( "Id" ) . SetMaxSize ( 26 )
tableo . ColMap ( "Token" ) . SetMaxSize ( 26 )
tableo . ColMap ( "CreatorId" ) . SetMaxSize ( 26 )
tableo . ColMap ( "ChannelId" ) . SetMaxSize ( 26 )
tableo . ColMap ( "TeamId" ) . SetMaxSize ( 26 )
tableo . ColMap ( "TriggerWords" ) . SetMaxSize ( 1024 )
tableo . ColMap ( "CallbackURLs" ) . SetMaxSize ( 1024 )
2016-03-30 10:56:20 -04:00
tableo . ColMap ( "DisplayName" ) . SetMaxSize ( 64 )
2018-09-04 14:24:41 +03:00
tableo . ColMap ( "Description" ) . SetMaxSize ( 500 )
2016-05-31 16:51:28 +02:00
tableo . ColMap ( "ContentType" ) . SetMaxSize ( 128 )
2016-07-26 14:04:28 +02:00
tableo . ColMap ( "TriggerWhen" ) . SetMaxSize ( 1 )
2018-07-25 14:31:41 +02:00
tableo . ColMap ( "Username" ) . SetMaxSize ( 64 )
tableo . ColMap ( "IconURL" ) . SetMaxSize ( 1024 )
2015-09-21 14:22:23 -04:00
}
return s
}
func ( s SqlWebhookStore ) CreateIndexesIfNotExists ( ) {
2015-10-01 14:07:20 -04:00
s . CreateIndexIfNotExists ( "idx_incoming_webhook_user_id" , "IncomingWebhooks" , "UserId" )
s . CreateIndexIfNotExists ( "idx_incoming_webhook_team_id" , "IncomingWebhooks" , "TeamId" )
2015-10-19 11:12:40 -04:00
s . CreateIndexIfNotExists ( "idx_outgoing_webhook_team_id" , "OutgoingWebhooks" , "TeamId" )
2016-10-24 17:05:27 -03:00
s . CreateIndexIfNotExists ( "idx_incoming_webhook_update_at" , "IncomingWebhooks" , "UpdateAt" )
s . CreateIndexIfNotExists ( "idx_incoming_webhook_create_at" , "IncomingWebhooks" , "CreateAt" )
s . CreateIndexIfNotExists ( "idx_incoming_webhook_delete_at" , "IncomingWebhooks" , "DeleteAt" )
s . CreateIndexIfNotExists ( "idx_outgoing_webhook_update_at" , "OutgoingWebhooks" , "UpdateAt" )
s . CreateIndexIfNotExists ( "idx_outgoing_webhook_create_at" , "OutgoingWebhooks" , "CreateAt" )
s . CreateIndexIfNotExists ( "idx_outgoing_webhook_delete_at" , "OutgoingWebhooks" , "DeleteAt" )
2015-09-21 14:22:23 -04:00
}
2017-01-27 14:07:34 -05:00
func ( s SqlWebhookStore ) InvalidateWebhookCache ( webhookId string ) {
webhookCache . Remove ( webhookId )
2018-03-05 10:35:26 -05:00
if s . metrics != nil {
s . metrics . IncrementMemCacheInvalidationCounter ( "Webhook - Remove by WebhookId" )
}
2017-01-27 14:07:34 -05:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) SaveIncoming ( webhook * model . IncomingWebhook ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-09-21 14:22:23 -04:00
if len ( webhook . Id ) > 0 {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.SaveIncoming" , "store.sql_webhooks.save_incoming.existing.app_error" , nil , "id=" + webhook . Id , http . StatusBadRequest )
2015-09-21 14:22:23 -04:00
return
}
webhook . PreSave ( )
if result . Err = webhook . IsValid ( ) ; result . Err != nil {
return
}
if err := s . GetMaster ( ) . Insert ( webhook ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.SaveIncoming" , "store.sql_webhooks.save_incoming.app_error" , nil , "id=" + webhook . Id + ", " + err . Error ( ) , http . StatusInternalServerError )
2015-09-21 14:22:23 -04:00
} else {
result . Data = webhook
}
2017-10-06 08:12:10 -07:00
} )
2015-09-21 14:22:23 -04:00
}
2019-04-18 14:03:59 +02:00
func ( s SqlWebhookStore ) UpdateIncoming ( hook * model . IncomingWebhook ) ( * model . IncomingWebhook , * model . AppError ) {
hook . UpdateAt = model . GetMillis ( )
2017-02-27 00:18:20 +05:30
2019-04-18 14:03:59 +02:00
if _ , err := s . GetMaster ( ) . Update ( hook ) ; err != nil {
return nil , model . NewAppError ( "SqlWebhookStore.UpdateIncoming" , "store.sql_webhooks.update_incoming.app_error" , nil , "id=" + hook . Id + ", " + err . Error ( ) , http . StatusInternalServerError )
}
return hook , nil
2017-02-27 00:18:20 +05:30
}
2019-04-16 19:16:30 +02:00
func ( s SqlWebhookStore ) GetIncoming ( id string , allowFromCache bool ) ( * model . IncomingWebhook , * model . AppError ) {
if allowFromCache {
if cacheItem , ok := webhookCache . Get ( id ) ; ok {
if s . metrics != nil {
s . metrics . IncrementMemCacheHitCounter ( "Webhook" )
2017-01-27 14:07:34 -05:00
}
2019-04-16 19:16:30 +02:00
return cacheItem . ( * model . IncomingWebhook ) , nil
2017-01-27 14:07:34 -05:00
}
2019-04-16 19:16:30 +02:00
if s . metrics != nil {
s . metrics . IncrementMemCacheMissCounter ( "Webhook" )
2015-09-21 14:22:23 -04:00
}
2019-04-16 19:16:30 +02:00
}
2015-09-21 14:22:23 -04:00
2019-04-16 19:16:30 +02:00
var webhook model . IncomingWebhook
if err := s . GetReplica ( ) . SelectOne ( & webhook , "SELECT * FROM IncomingWebhooks WHERE Id = :Id AND DeleteAt = 0" , map [ string ] interface { } { "Id" : id } ) ; err != nil {
if err == sql . ErrNoRows {
return nil , model . NewAppError ( "SqlWebhookStore.GetIncoming" , "store.sql_webhooks.get_incoming.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusNotFound )
2017-01-27 14:07:34 -05:00
}
2019-04-16 19:16:30 +02:00
return nil , model . NewAppError ( "SqlWebhookStore.GetIncoming" , "store.sql_webhooks.get_incoming.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusInternalServerError )
}
2017-01-27 14:07:34 -05:00
2019-04-16 19:16:30 +02:00
webhookCache . AddWithExpiresInSecs ( id , & webhook , WEBHOOK_CACHE_SEC )
return & webhook , nil
2015-09-21 14:22:23 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) DeleteIncoming ( webhookId string , time int64 ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-09-21 14:22:23 -04:00
_ , err := s . GetMaster ( ) . Exec ( "Update IncomingWebhooks SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id" , map [ string ] interface { } { "DeleteAt" : time , "UpdateAt" : time , "Id" : webhookId } )
if err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteIncoming" , "store.sql_webhooks.delete_incoming.app_error" , nil , "id=" + webhookId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-09-21 14:22:23 -04:00
}
2017-07-31 08:52:45 -07:00
s . InvalidateWebhookCache ( webhookId )
2017-10-06 08:12:10 -07:00
} )
2015-09-21 14:22:23 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) PermanentDeleteIncomingByUser ( userId string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-11-15 18:18:02 -08:00
_ , err := s . GetMaster ( ) . Exec ( "DELETE FROM IncomingWebhooks WHERE UserId = :UserId" , map [ string ] interface { } { "UserId" : userId } )
if err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteIncomingByUser" , "store.sql_webhooks.permanent_delete_incoming_by_user.app_error" , nil , "id=" + userId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-11-15 18:18:02 -08:00
}
2018-03-05 10:35:26 -05:00
s . ClearCaches ( )
2017-10-06 08:12:10 -07:00
} )
2017-07-31 08:52:45 -07:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) PermanentDeleteIncomingByChannel ( channelId string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-07-31 08:52:45 -07:00
_ , err := s . GetMaster ( ) . Exec ( "DELETE FROM IncomingWebhooks WHERE ChannelId = :ChannelId" , map [ string ] interface { } { "ChannelId" : channelId } )
if err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteIncomingByChannel" , "store.sql_webhooks.permanent_delete_incoming_by_channel.app_error" , nil , "id=" + channelId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2017-07-31 08:52:45 -07:00
}
2018-03-05 10:35:26 -05:00
s . ClearCaches ( )
2017-10-06 08:12:10 -07:00
} )
2015-11-15 18:18:02 -08:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) GetIncomingList ( offset , limit int ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-02-21 19:42:34 -05:00
var webhooks [ ] * model . IncomingWebhook
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM IncomingWebhooks WHERE DeleteAt = 0 LIMIT :Limit OFFSET :Offset" , map [ string ] interface { } { "Limit" : limit , "Offset" : offset } ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.GetIncomingList" , "store.sql_webhooks.get_incoming_by_user.app_error" , nil , "err=" + err . Error ( ) , http . StatusInternalServerError )
}
result . Data = webhooks
2017-10-06 08:12:10 -07:00
} )
2017-02-21 19:42:34 -05:00
}
2019-04-22 08:29:55 -07:00
func ( s SqlWebhookStore ) GetIncomingByTeam ( teamId string , offset , limit int ) ( [ ] * model . IncomingWebhook , * model . AppError ) {
var webhooks [ ] * model . IncomingWebhook
2015-09-21 14:22:23 -04:00
2019-04-22 08:29:55 -07:00
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM IncomingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset" , map [ string ] interface { } { "TeamId" : teamId , "Limit" : limit , "Offset" : offset } ) ; err != nil {
return nil , model . NewAppError ( "SqlWebhookStore.GetIncomingByUser" , "store.sql_webhooks.get_incoming_by_user.app_error" , nil , "teamId=" + teamId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
}
2015-09-21 14:22:23 -04:00
2019-04-22 08:29:55 -07:00
return webhooks , nil
2015-09-21 14:22:23 -04:00
}
2015-10-01 14:07:20 -04:00
2019-04-22 23:28:08 +08:00
func ( s SqlWebhookStore ) GetIncomingByChannel ( channelId string ) ( [ ] * model . IncomingWebhook , * model . AppError ) {
var webhooks [ ] * model . IncomingWebhook
2015-10-22 14:27:47 -04:00
2019-04-22 23:28:08 +08:00
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM IncomingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0" , map [ string ] interface { } { "ChannelId" : channelId } ) ; err != nil {
return nil , model . NewAppError ( "SqlWebhookStore.GetIncomingByChannel" , "store.sql_webhooks.get_incoming_by_channel.app_error" , nil , "channelId=" + channelId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
}
2015-10-22 14:27:47 -04:00
2019-04-22 23:28:08 +08:00
return webhooks , nil
2015-10-22 14:27:47 -04:00
}
2019-04-23 01:39:59 -07:00
func ( s SqlWebhookStore ) SaveOutgoing ( webhook * model . OutgoingWebhook ) ( * model . OutgoingWebhook , * model . AppError ) {
if len ( webhook . Id ) > 0 {
return nil , model . NewAppError ( "SqlWebhookStore.SaveOutgoing" , "store.sql_webhooks.save_outgoing.override.app_error" , nil , "id=" + webhook . Id , http . StatusBadRequest )
}
2015-10-01 14:07:20 -04:00
2019-04-23 01:39:59 -07:00
webhook . PreSave ( )
if err := webhook . IsValid ( ) ; err != nil {
return nil , err
}
2015-10-01 14:07:20 -04:00
2019-04-23 01:39:59 -07:00
if err := s . GetMaster ( ) . Insert ( webhook ) ; err != nil {
return nil , model . NewAppError ( "SqlWebhookStore.SaveOutgoing" , "store.sql_webhooks.save_outgoing.app_error" , nil , "id=" + webhook . Id + ", " + err . Error ( ) , http . StatusInternalServerError )
}
return webhook , nil
2015-10-01 14:07:20 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) GetOutgoing ( id string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-10-01 14:07:20 -04:00
var webhook model . OutgoingWebhook
if err := s . GetReplica ( ) . SelectOne ( & webhook , "SELECT * FROM OutgoingWebhooks WHERE Id = :Id AND DeleteAt = 0" , map [ string ] interface { } { "Id" : id } ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoing" , "store.sql_webhooks.get_outgoing.app_error" , nil , "id=" + id + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-10-01 14:07:20 -04:00
}
result . Data = & webhook
2017-10-06 08:12:10 -07:00
} )
2015-10-01 14:07:20 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) GetOutgoingList ( offset , limit int ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-10-01 14:07:20 -04:00
var webhooks [ ] * model . OutgoingWebhook
2017-03-13 10:40:43 -04:00
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM OutgoingWebhooks WHERE DeleteAt = 0 LIMIT :Limit OFFSET :Offset" , map [ string ] interface { } { "Offset" : offset , "Limit" : limit } ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoingList" , "store.sql_webhooks.get_outgoing_by_channel.app_error" , nil , "err=" + err . Error ( ) , http . StatusInternalServerError )
2017-03-13 10:40:43 -04:00
}
result . Data = webhooks
2017-10-06 08:12:10 -07:00
} )
2017-03-13 10:40:43 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) GetOutgoingByChannel ( channelId string , offset , limit int ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-03-13 10:40:43 -04:00
var webhooks [ ] * model . OutgoingWebhook
query := ""
if limit < 0 || offset < 0 {
query = "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0"
} else {
query = "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset"
}
if _ , err := s . GetReplica ( ) . Select ( & webhooks , query , map [ string ] interface { } { "ChannelId" : channelId , "Offset" : offset , "Limit" : limit } ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoingByChannel" , "store.sql_webhooks.get_outgoing_by_channel.app_error" , nil , "channelId=" + channelId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-10-01 14:07:20 -04:00
}
result . Data = webhooks
2017-10-06 08:12:10 -07:00
} )
2015-10-01 14:07:20 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) GetOutgoingByTeam ( teamId string , offset , limit int ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-10-01 14:07:20 -04:00
var webhooks [ ] * model . OutgoingWebhook
2017-03-13 10:40:43 -04:00
query := ""
if limit < 0 || offset < 0 {
query = "SELECT * FROM OutgoingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0"
} else {
query = "SELECT * FROM OutgoingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0 LIMIT :Limit OFFSET :Offset"
}
if _ , err := s . GetReplica ( ) . Select ( & webhooks , query , map [ string ] interface { } { "TeamId" : teamId , "Offset" : offset , "Limit" : limit } ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoingByTeam" , "store.sql_webhooks.get_outgoing_by_team.app_error" , nil , "teamId=" + teamId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-10-01 14:07:20 -04:00
}
result . Data = webhooks
2017-10-06 08:12:10 -07:00
} )
2015-10-01 14:07:20 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) DeleteOutgoing ( webhookId string , time int64 ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-10-01 14:07:20 -04:00
_ , err := s . GetMaster ( ) . Exec ( "Update OutgoingWebhooks SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id" , map [ string ] interface { } { "DeleteAt" : time , "UpdateAt" : time , "Id" : webhookId } )
if err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteOutgoing" , "store.sql_webhooks.delete_outgoing.app_error" , nil , "id=" + webhookId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-10-01 14:07:20 -04:00
}
2017-10-06 08:12:10 -07:00
} )
2015-10-01 14:07:20 -04:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) PermanentDeleteOutgoingByUser ( userId string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-11-15 18:18:02 -08:00
_ , err := s . GetMaster ( ) . Exec ( "DELETE FROM OutgoingWebhooks WHERE CreatorId = :UserId" , map [ string ] interface { } { "UserId" : userId } )
if err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteOutgoingByUser" , "store.sql_webhooks.permanent_delete_outgoing_by_user.app_error" , nil , "id=" + userId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2015-11-15 18:18:02 -08:00
}
2017-10-06 08:12:10 -07:00
} )
2017-07-31 08:52:45 -07:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) PermanentDeleteOutgoingByChannel ( channelId string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2017-07-31 08:52:45 -07:00
_ , err := s . GetMaster ( ) . Exec ( "DELETE FROM OutgoingWebhooks WHERE ChannelId = :ChannelId" , map [ string ] interface { } { "ChannelId" : channelId } )
if err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteOutgoingByChannel" , "store.sql_webhooks.permanent_delete_outgoing_by_channel.app_error" , nil , "id=" + channelId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2017-07-31 08:52:45 -07:00
}
2018-03-05 10:35:26 -05:00
s . ClearCaches ( )
2017-10-06 08:12:10 -07:00
} )
2015-11-15 18:18:02 -08:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) UpdateOutgoing ( hook * model . OutgoingWebhook ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2015-10-01 14:07:20 -04:00
hook . UpdateAt = model . GetMillis ( )
if _ , err := s . GetMaster ( ) . Update ( hook ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.UpdateOutgoing" , "store.sql_webhooks.update_outgoing.app_error" , nil , "id=" + hook . Id + ", " + err . Error ( ) , http . StatusInternalServerError )
2015-10-01 14:07:20 -04:00
} else {
result . Data = hook
}
2017-10-06 08:12:10 -07:00
} )
2015-10-01 14:07:20 -04:00
}
2016-02-02 08:41:02 -05:00
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) AnalyticsIncomingCount ( teamId string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2016-02-02 08:41:02 -05:00
query :=
` SELECT
COUNT ( * )
FROM
IncomingWebhooks
WHERE
DeleteAt = 0 `
if len ( teamId ) > 0 {
query += " AND TeamId = :TeamId"
}
if v , err := s . GetReplica ( ) . SelectInt ( query , map [ string ] interface { } { "TeamId" : teamId } ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.AnalyticsIncomingCount" , "store.sql_webhooks.analytics_incoming_count.app_error" , nil , "team_id=" + teamId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2016-02-02 08:41:02 -05:00
} else {
result . Data = v
}
2017-10-06 08:12:10 -07:00
} )
2016-02-02 08:41:02 -05:00
}
2017-09-25 09:11:25 -05:00
func ( s SqlWebhookStore ) AnalyticsOutgoingCount ( teamId string ) store . StoreChannel {
2017-10-06 08:12:10 -07:00
return store . Do ( func ( result * store . StoreResult ) {
2016-02-02 08:41:02 -05:00
query :=
` SELECT
COUNT ( * )
FROM
OutgoingWebhooks
WHERE
DeleteAt = 0 `
if len ( teamId ) > 0 {
query += " AND TeamId = :TeamId"
}
if v , err := s . GetReplica ( ) . SelectInt ( query , map [ string ] interface { } { "TeamId" : teamId } ) ; err != nil {
2017-09-18 17:40:56 +01:00
result . Err = model . NewAppError ( "SqlWebhookStore.AnalyticsOutgoingCount" , "store.sql_webhooks.analytics_outgoing_count.app_error" , nil , "team_id=" + teamId + ", err=" + err . Error ( ) , http . StatusInternalServerError )
2016-02-02 08:41:02 -05:00
} else {
result . Data = v
}
2017-10-06 08:12:10 -07:00
} )
2016-02-02 08:41:02 -05:00
}