summaryrefslogtreecommitdiffstats
path: root/store/sql_webhook_store.go
diff options
context:
space:
mode:
authorJoramWilander <jwawilander@gmail.com>2015-10-01 14:07:20 -0400
committerJoramWilander <jwawilander@gmail.com>2015-10-19 09:00:30 -0400
commitaf6e2c29eb0a8610fe218e8ec85e739433eac729 (patch)
tree59836ace0d50cc62b99f007916212454bd5c9e99 /store/sql_webhook_store.go
parente308923aeca0a45463aeeeea7b0b3e3bc313f033 (diff)
downloadchat-af6e2c29eb0a8610fe218e8ec85e739433eac729.tar.gz
chat-af6e2c29eb0a8610fe218e8ec85e739433eac729.tar.bz2
chat-af6e2c29eb0a8610fe218e8ec85e739433eac729.zip
Implement outgoing webhooks.
Diffstat (limited to 'store/sql_webhook_store.go')
-rw-r--r--store/sql_webhook_store.go216
1 files changed, 214 insertions, 2 deletions
diff --git a/store/sql_webhook_store.go b/store/sql_webhook_store.go
index 42a91a80e..b85ce4475 100644
--- a/store/sql_webhook_store.go
+++ b/store/sql_webhook_store.go
@@ -5,6 +5,7 @@ package store
import (
"github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
)
type SqlWebhookStore struct {
@@ -20,6 +21,15 @@ func NewSqlWebhookStore(sqlStore *SqlStore) WebhookStore {
table.ColMap("UserId").SetMaxSize(26)
table.ColMap("ChannelId").SetMaxSize(26)
table.ColMap("TeamId").SetMaxSize(26)
+
+ 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)
}
return s
@@ -29,8 +39,11 @@ func (s SqlWebhookStore) UpgradeSchemaIfNeeded() {
}
func (s SqlWebhookStore) CreateIndexesIfNotExists() {
- s.CreateIndexIfNotExists("idx_webhook_user_id", "IncomingWebhooks", "UserId")
- s.CreateIndexIfNotExists("idx_webhook_team_id", "IncomingWebhooks", "TeamId")
+ s.CreateIndexIfNotExists("idx_incoming_webhook_user_id", "IncomingWebhooks", "UserId")
+ s.CreateIndexIfNotExists("idx_incoming_webhook_team_id", "IncomingWebhooks", "TeamId")
+ s.CreateIndexIfNotExists("idx_outgoing_webhook_channel_id", "OutgoingWebhooks", "ChannelId")
+
+ s.CreatePatternIndexIfNotExists("idx_outgoing_webhook_trigger_txt", "OutgoingWebhooks", "TriggerWords")
}
func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) StoreChannel {
@@ -126,3 +139,202 @@ func (s SqlWebhookStore) GetIncomingByUser(userId string) StoreChannel {
return storeChannel
}
+
+func (s SqlWebhookStore) SaveOutgoing(webhook *model.OutgoingWebhook) StoreChannel {
+ 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
+}
+
+func (s SqlWebhookStore) GetOutgoing(id string) StoreChannel {
+ 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
+}
+
+func (s SqlWebhookStore) GetOutgoingByCreator(userId string) StoreChannel {
+ 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
+}
+
+func (s SqlWebhookStore) GetOutgoingByChannel(channelId string) StoreChannel {
+ 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
+}
+
+func (s SqlWebhookStore) GetOutgoingByTriggerWord(teamId, channelId, triggerWord string) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ var webhooks []*model.OutgoingWebhook
+
+ var err error
+
+ if utils.Cfg.SqlSettings.DriverName == "postgres" {
+
+ searchQuery := `SELECT
+ *
+ FROM
+ OutgoingWebhooks
+ WHERE
+ DeleteAt = 0
+ AND TeamId = $1
+ AND $2 LIKE '%' || TriggerWords || '%'`
+
+ if len(channelId) != 0 {
+ searchQuery += " AND (ChannelId = $3 OR ChannelId IS NULL)"
+ _, err = s.GetReplica().Select(&webhooks, searchQuery, teamId, triggerWord, channelId)
+ } else {
+ searchQuery += " AND ChannelId IS NULL"
+ _, err = s.GetReplica().Select(&webhooks, searchQuery, teamId, triggerWord)
+ }
+
+ } else if utils.Cfg.SqlSettings.DriverName == "mysql" {
+ searchQuery := `SELECT
+ *
+ FROM
+ OutgoingWebhooks
+ WHERE
+ DeleteAt = 0
+ AND TeamId = ?
+ AND MATCH (TriggerWords) AGAINST (? IN BOOLEAN MODE)`
+
+ triggerWord = "+" + triggerWord
+
+ if len(channelId) != 0 {
+ searchQuery += " AND (ChannelId = ? OR ChannelId = '')"
+ _, err = s.GetReplica().Select(&webhooks, searchQuery, teamId, triggerWord, channelId)
+ } else {
+ searchQuery += " AND ChannelId = ''"
+ _, err = s.GetReplica().Select(&webhooks, searchQuery, teamId, triggerWord)
+ }
+ }
+
+ if err != nil {
+ result.Err = model.NewAppError("SqlPostStore.GetOutgoingByTriggerWord", "We encounted an error while getting the outgoing webhooks by trigger word", "teamId="+teamId+", channelId="+channelId+", triggerWord="+triggerWord+", err="+err.Error())
+ }
+
+ result.Data = webhooks
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
+func (s SqlWebhookStore) DeleteOutgoing(webhookId string, time int64) StoreChannel {
+ 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
+}
+
+func (s SqlWebhookStore) UpdateOutgoing(hook *model.OutgoingWebhook) StoreChannel {
+ 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
+}