2015-10-08 12:27:09 -04:00
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-09-21 14:22:23 -04:00
// See License.txt for license information.
package store
import (
"github.com/mattermost/platform/model"
)
type SqlWebhookStore struct {
* SqlStore
}
func NewSqlWebhookStore ( sqlStore * SqlStore ) WebhookStore {
s := & SqlWebhookStore { sqlStore }
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 )
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 )
2015-09-21 14:22:23 -04:00
}
return s
}
func ( s SqlWebhookStore ) UpgradeSchemaIfNeeded ( ) {
}
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" )
2015-09-21 14:22:23 -04:00
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) SaveIncoming ( webhook * model . IncomingWebhook ) StoreChannel {
2015-09-21 14:22:23 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
if len ( webhook . Id ) > 0 {
result . Err = model . NewAppError ( "SqlWebhookStore.SaveIncoming" ,
"You cannot overwrite an existing IncomingWebhook" , "id=" + webhook . Id )
storeChannel <- result
close ( storeChannel )
return
}
webhook . PreSave ( )
if result . Err = webhook . IsValid ( ) ; result . Err != nil {
storeChannel <- result
close ( storeChannel )
return
}
if err := s . GetMaster ( ) . Insert ( webhook ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.SaveIncoming" , "We couldn't save the IncomingWebhook" , "id=" + webhook . Id + ", " + err . Error ( ) )
} else {
result . Data = webhook
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetIncoming ( id string ) StoreChannel {
2015-09-21 14:22:23 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
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 {
result . Err = model . NewAppError ( "SqlWebhookStore.GetIncoming" , "We couldn't get the webhook" , "id=" + id + ", err=" + err . Error ( ) )
}
result . Data = & webhook
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) DeleteIncoming ( webhookId string , time int64 ) StoreChannel {
2015-09-21 14:22:23 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
_ , 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 {
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteIncoming" , "We couldn't delete the webhook" , "id=" + webhookId + ", err=" + err . Error ( ) )
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) PermanentDeleteIncomingByUser ( userId string ) StoreChannel {
2015-11-15 18:18:02 -08:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
_ , err := s . GetMaster ( ) . Exec ( "DELETE FROM IncomingWebhooks WHERE UserId = :UserId" , map [ string ] interface { } { "UserId" : userId } )
if err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteIncomingByUser" , "We couldn't delete the webhook" , "id=" + userId + ", err=" + err . Error ( ) )
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetIncomingByUser ( userId string ) StoreChannel {
2015-09-21 14:22:23 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
var webhooks [ ] * model . IncomingWebhook
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM IncomingWebhooks WHERE UserId = :UserId AND DeleteAt = 0" , map [ string ] interface { } { "UserId" : userId } ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.GetIncomingByUser" , "We couldn't get the webhook" , "userId=" + userId + ", err=" + err . Error ( ) )
}
result . Data = webhooks
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2015-10-01 14:07:20 -04:00
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetIncomingByChannel ( channelId string ) StoreChannel {
2015-10-22 14:27:47 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
var webhooks [ ] * model . IncomingWebhook
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM IncomingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0" , map [ string ] interface { } { "ChannelId" : channelId } ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.GetIncomingByChannel" , "We couldn't get the webhooks" , "channelId=" + channelId + ", err=" + err . Error ( ) )
}
result . Data = webhooks
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) SaveOutgoing ( webhook * model . OutgoingWebhook ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
if len ( webhook . Id ) > 0 {
result . Err = model . NewAppError ( "SqlWebhookStore.SaveOutgoing" ,
"You cannot overwrite an existing OutgoingWebhook" , "id=" + webhook . Id )
storeChannel <- result
close ( storeChannel )
return
}
webhook . PreSave ( )
if result . Err = webhook . IsValid ( ) ; result . Err != nil {
storeChannel <- result
close ( storeChannel )
return
}
if err := s . GetMaster ( ) . Insert ( webhook ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.SaveOutgoing" , "We couldn't save the OutgoingWebhook" , "id=" + webhook . Id + ", " + err . Error ( ) )
} else {
result . Data = webhook
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetOutgoing ( id string ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
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 {
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoing" , "We couldn't get the webhook" , "id=" + id + ", err=" + err . Error ( ) )
}
result . Data = & webhook
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetOutgoingByCreator ( userId string ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
var webhooks [ ] * model . OutgoingWebhook
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM OutgoingWebhooks WHERE CreatorId = :UserId AND DeleteAt = 0" , map [ string ] interface { } { "UserId" : userId } ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoingByCreator" , "We couldn't get the webhooks" , "userId=" + userId + ", err=" + err . Error ( ) )
}
result . Data = webhooks
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetOutgoingByChannel ( channelId string ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
var webhooks [ ] * model . OutgoingWebhook
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM OutgoingWebhooks WHERE ChannelId = :ChannelId AND DeleteAt = 0" , map [ string ] interface { } { "ChannelId" : channelId } ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoingByChannel" , "We couldn't get the webhooks" , "channelId=" + channelId + ", err=" + err . Error ( ) )
}
result . Data = webhooks
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) GetOutgoingByTeam ( teamId string ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
var webhooks [ ] * model . OutgoingWebhook
2015-10-16 11:17:24 -04:00
if _ , err := s . GetReplica ( ) . Select ( & webhooks , "SELECT * FROM OutgoingWebhooks WHERE TeamId = :TeamId AND DeleteAt = 0" , map [ string ] interface { } { "TeamId" : teamId } ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.GetOutgoingByTeam" , "We couldn't get the webhooks" , "teamId=" + teamId + ", err=" + err . Error ( ) )
2015-10-01 14:07:20 -04:00
}
result . Data = webhooks
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) DeleteOutgoing ( webhookId string , time int64 ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
_ , 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 {
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteOutgoing" , "We couldn't delete the webhook" , "id=" + webhookId + ", err=" + err . Error ( ) )
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) PermanentDeleteOutgoingByUser ( userId string ) StoreChannel {
2015-11-15 18:18:02 -08:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
_ , err := s . GetMaster ( ) . Exec ( "DELETE FROM OutgoingWebhooks WHERE CreatorId = :UserId" , map [ string ] interface { } { "UserId" : userId } )
if err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.DeleteOutgoingByUser" , "We couldn't delete the webhook" , "id=" + userId + ", err=" + err . Error ( ) )
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}
2016-01-20 13:36:16 -06:00
func ( s SqlWebhookStore ) UpdateOutgoing ( hook * model . OutgoingWebhook ) StoreChannel {
2015-10-01 14:07:20 -04:00
storeChannel := make ( StoreChannel )
go func ( ) {
result := StoreResult { }
hook . UpdateAt = model . GetMillis ( )
if _ , err := s . GetMaster ( ) . Update ( hook ) ; err != nil {
result . Err = model . NewAppError ( "SqlWebhookStore.UpdateOutgoing" , "We couldn't update the webhook" , "id=" + hook . Id + ", " + err . Error ( ) )
} else {
result . Data = hook
}
storeChannel <- result
close ( storeChannel )
} ( )
return storeChannel
}