2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2019-11-29 12:59:40 +01:00
|
|
|
// See LICENSE.txt for license information.
|
2015-09-21 14:22:23 -04:00
|
|
|
|
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"
|
2020-07-31 09:53:10 -04:00
|
|
|
|
2022-05-10 11:48:59 -03:00
|
|
|
sq "github.com/mattermost/squirrel"
|
2020-07-31 09:53:10 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-03-12 04:10:56 +05:30
|
|
|
|
2023-06-11 10:54:35 +05:30
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
|
"github.com/mattermost/mattermost/server/v8/channels/store"
|
|
|
|
|
"github.com/mattermost/mattermost/server/v8/einterfaces"
|
2015-09-21 14:22:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SqlWebhookStore struct {
|
2020-12-02 12:06:23 +01:00
|
|
|
*SqlStore
|
2017-09-14 12:01:44 -05:00
|
|
|
metrics einterfaces.MetricsInterface
|
2015-09-21 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
2018-03-05 10:35:26 -05:00
|
|
|
func (s SqlWebhookStore) ClearCaches() {
|
2017-01-27 14:07:34 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 12:06:23 +01:00
|
|
|
func newSqlWebhookStore(sqlStore *SqlStore, metrics einterfaces.MetricsInterface) store.WebhookStore {
|
2022-02-15 11:44:04 +05:30
|
|
|
return &SqlWebhookStore{
|
2020-12-02 12:06:23 +01:00
|
|
|
SqlStore: sqlStore,
|
|
|
|
|
metrics: metrics,
|
2017-09-14 12:01:44 -05:00
|
|
|
}
|
2015-09-21 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
2017-01-27 14:07:34 -05:00
|
|
|
func (s SqlWebhookStore) InvalidateWebhookCache(webhookId string) {
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) SaveIncoming(webhook *model.IncomingWebhook) (*model.IncomingWebhook, error) {
|
2021-01-25 11:15:17 +01:00
|
|
|
if webhook.Id != "" {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, store.NewErrInvalidInput("IncomingWebhook", "id", webhook.Id)
|
2019-04-24 04:30:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
webhook.PreSave()
|
|
|
|
|
if err := webhook.IsValid(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if _, err := s.GetMasterX().NamedExec(`INSERT INTO IncomingWebhooks
|
|
|
|
|
(Id, CreateAt, UpdateAt, DeleteAt, UserId, ChannelId, TeamId, DisplayName, Description, Username, IconURL, ChannelLocked)
|
|
|
|
|
VALUES
|
|
|
|
|
(:Id, :CreateAt, :UpdateAt, :DeleteAt, :UserId, :ChannelId, :TeamId, :DisplayName, :Description, :Username, :IconURL, :ChannelLocked)`, webhook); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to save IncomingWebhook with id=%s", webhook.Id)
|
2019-04-24 04:30:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return webhook, nil
|
2015-09-21 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) UpdateIncoming(hook *model.IncomingWebhook) (*model.IncomingWebhook, error) {
|
2019-04-18 14:03:59 +02:00
|
|
|
hook.UpdateAt = model.GetMillis()
|
2017-02-27 00:18:20 +05:30
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().NamedExec(`UPDATE IncomingWebhooks SET
|
|
|
|
|
CreateAt=:CreateAt, UpdateAt=:UpdateAt, DeleteAt=:DeleteAt, ChannelId=:ChannelId, TeamId=:TeamId, DisplayName=:DisplayName,
|
|
|
|
|
Description=:Description, Username=:Username, IconURL=:IconURL, ChannelLocked=:ChannelLocked
|
|
|
|
|
WHERE Id=:Id`, hook)
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to update IncomingWebhook with id=%s", hook.Id)
|
2019-04-18 14:03:59 +02:00
|
|
|
}
|
2021-11-15 16:43:14 +01:00
|
|
|
|
2019-04-18 14:03:59 +02:00
|
|
|
return hook, nil
|
2017-02-27 00:18:20 +05:30
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetIncoming(id string, allowFromCache bool) (*model.IncomingWebhook, error) {
|
2019-04-16 19:16:30 +02:00
|
|
|
var webhook model.IncomingWebhook
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Get(&webhook, "SELECT * FROM IncomingWebhooks WHERE Id = ? AND DeleteAt = 0", id); err != nil {
|
2019-04-16 19:16:30 +02:00
|
|
|
if err == sql.ErrNoRows {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, store.NewErrNotFound("IncomingWebhook", id)
|
2017-01-27 14:07:34 -05:00
|
|
|
}
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to get IncomingWebhook with id=%s", id)
|
2019-04-16 19:16:30 +02:00
|
|
|
}
|
2017-01-27 14:07:34 -05:00
|
|
|
|
2019-04-16 19:16:30 +02:00
|
|
|
return &webhook, nil
|
2015-09-21 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) DeleteIncoming(webhookId string, time int64) error {
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().Exec("UPDATE IncomingWebhooks SET DeleteAt = ?, UpdateAt = ? WHERE Id = ?", time, time, webhookId)
|
2019-04-29 22:34:08 -07:00
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to update IncomingWebhook with id=%s", webhookId)
|
2019-04-29 22:34:08 -07:00
|
|
|
}
|
2015-09-21 14:22:23 -04:00
|
|
|
|
2019-04-29 22:34:08 -07:00
|
|
|
return nil
|
2015-09-21 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) PermanentDeleteIncomingByUser(userId string) error {
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().Exec("DELETE FROM IncomingWebhooks WHERE UserId = ?", userId)
|
2019-04-26 08:34:29 -07:00
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to delete IncomingWebhook with userId=%s", userId)
|
2019-04-26 08:34:29 -07:00
|
|
|
}
|
2015-11-15 18:18:02 -08:00
|
|
|
|
2019-04-26 08:34:29 -07:00
|
|
|
return nil
|
2017-07-31 08:52:45 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) PermanentDeleteIncomingByChannel(channelId string) error {
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().Exec("DELETE FROM IncomingWebhooks WHERE ChannelId = ?", channelId)
|
2019-04-25 15:15:32 +09:00
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to delete IncomingWebhook with channelId=%s", channelId)
|
2019-04-25 15:15:32 +09:00
|
|
|
}
|
2017-07-31 08:52:45 -07:00
|
|
|
|
2019-04-25 15:15:32 +09:00
|
|
|
return nil
|
2015-11-15 18:18:02 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetIncomingList(offset, limit int) ([]*model.IncomingWebhook, error) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return s.GetIncomingListByUser("", offset, limit)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetIncomingListByUser(userId string, offset, limit int) ([]*model.IncomingWebhook, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
webhooks := []*model.IncomingWebhook{}
|
2017-02-21 19:42:34 -05:00
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
query := s.getQueryBuilder().
|
|
|
|
|
Select("*").
|
|
|
|
|
From("IncomingWebhooks").
|
|
|
|
|
Where(sq.Eq{"DeleteAt": int(0)}).Limit(uint64(limit)).Offset(uint64(offset))
|
|
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if userId != "" {
|
2019-07-29 12:32:26 -04:00
|
|
|
query = query.Where(sq.Eq{"UserId": userId})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryString, args, err := query.ToSql()
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "incoming_webhook_tosql")
|
2019-07-29 12:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Select(&webhooks, queryString, args...); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "failed to find IncomingWebhooks")
|
2019-04-24 04:32:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return webhooks, nil
|
2017-02-21 19:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetIncomingByTeamByUser(teamId string, userId string, offset, limit int) ([]*model.IncomingWebhook, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
webhooks := []*model.IncomingWebhook{}
|
2015-09-21 14:22:23 -04:00
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
query := s.getQueryBuilder().
|
|
|
|
|
Select("*").
|
|
|
|
|
From("IncomingWebhooks").
|
|
|
|
|
Where(sq.And{
|
|
|
|
|
sq.Eq{"TeamId": teamId},
|
|
|
|
|
sq.Eq{"DeleteAt": int(0)},
|
|
|
|
|
}).Limit(uint64(limit)).Offset(uint64(offset))
|
|
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if userId != "" {
|
2019-07-29 12:32:26 -04:00
|
|
|
query = query.Where(sq.Eq{"UserId": userId})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryString, args, err := query.ToSql()
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "incoming_webhook_tosql")
|
2019-07-29 12:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Select(&webhooks, queryString, args...); err != nil {
|
2022-04-11 19:31:19 +05:30
|
|
|
return nil, errors.Wrapf(err, "failed to find IncomingWebhook with teamId=%s", teamId)
|
2019-04-22 08:29:55 -07:00
|
|
|
}
|
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
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetIncomingByTeam(teamId string, offset, limit int) ([]*model.IncomingWebhook, error) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return s.GetIncomingByTeamByUser(teamId, "", offset, limit)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetIncomingByChannel(channelId string) ([]*model.IncomingWebhook, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
webhooks := []*model.IncomingWebhook{}
|
2015-10-22 14:27:47 -04:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Select(&webhooks, "SELECT * FROM IncomingWebhooks WHERE ChannelId = ? AND DeleteAt = 0", channelId); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to find IncomingWebhooks with channelId=%s", channelId)
|
2019-04-22 23:28:08 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) SaveOutgoing(webhook *model.OutgoingWebhook) (*model.OutgoingWebhook, error) {
|
2021-01-25 11:15:17 +01:00
|
|
|
if webhook.Id != "" {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, store.NewErrInvalidInput("OutgoingWebhook", "id", webhook.Id)
|
2019-04-23 01:39:59 -07:00
|
|
|
}
|
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
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if _, err := s.GetMasterX().NamedExec(`INSERT INTO OutgoingWebhooks
|
|
|
|
|
(Id, Token, CreateAt, UpdateAt, DeleteAt, CreatorId, ChannelId, TeamId, TriggerWords, TriggerWhen,
|
|
|
|
|
CallbackURLs, DisplayName, Description, ContentType, Username, IconURL)
|
|
|
|
|
VALUES
|
|
|
|
|
(:Id, :Token, :CreateAt, :UpdateAt, :DeleteAt, :CreatorId, :ChannelId, :TeamId, :TriggerWords, :TriggerWhen,
|
|
|
|
|
:CallbackURLs, :DisplayName, :Description, :ContentType, :Username, :IconURL)`, webhook); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to save OutgoingWebhook with id=%s", webhook.Id)
|
2019-04-23 01:39:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return webhook, nil
|
2015-10-01 14:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoing(id string) (*model.OutgoingWebhook, error) {
|
2019-04-25 14:19:38 +08:00
|
|
|
var webhook model.OutgoingWebhook
|
2015-10-01 14:07:20 -04:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Get(&webhook, "SELECT * FROM OutgoingWebhooks WHERE Id = ? AND DeleteAt = 0", id); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
return nil, store.NewErrNotFound("OutgoingWebhook", id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, errors.Wrapf(err, "failed to get OutgoingWebhook with id=%s", id)
|
2019-04-25 14:19:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &webhook, nil
|
2015-10-01 14:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoingListByUser(userId string, offset, limit int) ([]*model.OutgoingWebhook, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
webhooks := []*model.OutgoingWebhook{}
|
2015-10-01 14:07:20 -04:00
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
query := s.getQueryBuilder().
|
|
|
|
|
Select("*").
|
|
|
|
|
From("OutgoingWebhooks").
|
|
|
|
|
Where(sq.And{
|
|
|
|
|
sq.Eq{"DeleteAt": int(0)},
|
|
|
|
|
}).Limit(uint64(limit)).Offset(uint64(offset))
|
|
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if userId != "" {
|
2019-07-29 12:32:26 -04:00
|
|
|
query = query.Where(sq.Eq{"CreatorId": userId})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryString, args, err := query.ToSql()
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "outgoing_webhook_tosql")
|
2019-07-29 12:32:26 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Select(&webhooks, queryString, args...); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "failed to find OutgoingWebhooks")
|
2019-04-25 22:41:45 -07:00
|
|
|
}
|
2017-03-13 10:40:43 -04:00
|
|
|
|
2019-04-25 22:41:45 -07:00
|
|
|
return webhooks, nil
|
2017-03-13 10:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoingList(offset, limit int) ([]*model.OutgoingWebhook, error) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return s.GetOutgoingListByUser("", offset, limit)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoingByChannelByUser(channelId string, userId string, offset, limit int) ([]*model.OutgoingWebhook, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
webhooks := []*model.OutgoingWebhook{}
|
2017-03-13 10:40:43 -04:00
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
query := s.getQueryBuilder().
|
|
|
|
|
Select("*").
|
|
|
|
|
From("OutgoingWebhooks").
|
|
|
|
|
Where(sq.And{
|
|
|
|
|
sq.Eq{"ChannelId": channelId},
|
|
|
|
|
sq.Eq{"DeleteAt": int(0)},
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if userId != "" {
|
2019-07-29 12:32:26 -04:00
|
|
|
query = query.Where(sq.Eq{"CreatorId": userId})
|
|
|
|
|
}
|
|
|
|
|
if limit >= 0 && offset >= 0 {
|
|
|
|
|
query = query.Limit(uint64(limit)).Offset(uint64(offset))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryString, args, err := query.ToSql()
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "outgoing_webhook_tosql")
|
2019-05-07 04:50:03 -07:00
|
|
|
}
|
2015-10-01 14:07:20 -04:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Select(&webhooks, queryString, args...); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "failed to find OutgoingWebhooks")
|
2019-05-07 04:50:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return webhooks, nil
|
2015-10-01 14:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoingByChannel(channelId string, offset, limit int) ([]*model.OutgoingWebhook, error) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return s.GetOutgoingByChannelByUser(channelId, "", offset, limit)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoingByTeamByUser(teamId string, userId string, offset, limit int) ([]*model.OutgoingWebhook, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
webhooks := []*model.OutgoingWebhook{}
|
2015-10-01 14:07:20 -04:00
|
|
|
|
2019-07-29 12:32:26 -04:00
|
|
|
query := s.getQueryBuilder().
|
|
|
|
|
Select("*").
|
|
|
|
|
From("OutgoingWebhooks").
|
|
|
|
|
Where(sq.And{
|
|
|
|
|
sq.Eq{"TeamId": teamId},
|
|
|
|
|
sq.Eq{"DeleteAt": int(0)},
|
|
|
|
|
})
|
|
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if userId != "" {
|
2019-07-29 12:32:26 -04:00
|
|
|
query = query.Where(sq.Eq{"CreatorId": userId})
|
|
|
|
|
}
|
|
|
|
|
if limit >= 0 && offset >= 0 {
|
|
|
|
|
query = query.Limit(uint64(limit)).Offset(uint64(offset))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryString, args, err := query.ToSql()
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "outgoing_webhook_tosql")
|
2019-04-26 12:20:36 -07:00
|
|
|
}
|
2017-03-13 10:40:43 -04:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
if err := s.GetReplicaX().Select(&webhooks, queryString, args...); err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrap(err, "failed to find OutgoingWebhooks")
|
2019-04-26 12:20:36 -07:00
|
|
|
}
|
2015-10-01 14:07:20 -04:00
|
|
|
|
2019-04-26 12:20:36 -07:00
|
|
|
return webhooks, nil
|
2015-10-01 14:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) GetOutgoingByTeam(teamId string, offset, limit int) ([]*model.OutgoingWebhook, error) {
|
2019-07-29 12:32:26 -04:00
|
|
|
return s.GetOutgoingByTeamByUser(teamId, "", offset, limit)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) DeleteOutgoing(webhookId string, time int64) error {
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().Exec("Update OutgoingWebhooks SET DeleteAt = ?, UpdateAt = ? WHERE Id = ?", time, time, webhookId)
|
2019-04-25 22:44:00 -07:00
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to update OutgoingWebhook with id=%s", webhookId)
|
2019-04-25 22:44:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2015-10-01 14:07:20 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) PermanentDeleteOutgoingByUser(userId string) error {
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().Exec("DELETE FROM OutgoingWebhooks WHERE CreatorId = ?", userId)
|
2019-04-26 22:45:02 -07:00
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to delete OutgoingWebhook with creatorId=%s", userId)
|
2019-04-26 22:45:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
2017-07-31 08:52:45 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) PermanentDeleteOutgoingByChannel(channelId string) error {
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().Exec("DELETE FROM OutgoingWebhooks WHERE ChannelId = ?", channelId)
|
2019-04-23 21:40:41 +09:00
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return errors.Wrapf(err, "failed to delete OutgoingWebhook with channelId=%s", channelId)
|
2019-04-23 21:40:41 +09:00
|
|
|
}
|
2017-07-31 08:52:45 -07:00
|
|
|
|
2019-04-23 21:40:41 +09:00
|
|
|
s.ClearCaches()
|
|
|
|
|
|
|
|
|
|
return nil
|
2015-11-15 18:18:02 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) UpdateOutgoing(hook *model.OutgoingWebhook) (*model.OutgoingWebhook, error) {
|
2019-04-26 00:28:04 -07:00
|
|
|
hook.UpdateAt = model.GetMillis()
|
2015-10-01 14:07:20 -04:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
_, err := s.GetMasterX().NamedExec(`UPDATE OutgoingWebhooks SET
|
|
|
|
|
CreateAt = :CreateAt, UpdateAt = :UpdateAt, DeleteAt = :DeleteAt, Token = :Token, CreatorId = :CreatorId,
|
|
|
|
|
ChannelId = :ChannelId, TeamId = :TeamId, TriggerWords = :TriggerWords, TriggerWhen = :TriggerWhen,
|
|
|
|
|
CallbackURLs = :CallbackURLs, DisplayName = :DisplayName, Description = :Description,
|
|
|
|
|
ContentType = :ContentType, Username = :Username, IconURL = :IconURL WHERE Id = :Id`, hook)
|
|
|
|
|
if err != nil {
|
2020-07-31 09:53:10 -04:00
|
|
|
return nil, errors.Wrapf(err, "failed to update OutgoingWebhook with id=%s", hook.Id)
|
2019-04-26 00:28:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hook, nil
|
2015-10-01 14:07:20 -04:00
|
|
|
}
|
2016-02-02 08:41:02 -05:00
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) AnalyticsIncomingCount(teamId string) (int64, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
queryBuilder :=
|
|
|
|
|
s.getQueryBuilder().
|
|
|
|
|
Select("COUNT(*)").
|
|
|
|
|
From("IncomingWebhooks").
|
|
|
|
|
Where("DeleteAt = 0")
|
2019-04-25 15:17:43 +09:00
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if teamId != "" {
|
2021-11-15 16:43:14 +01:00
|
|
|
queryBuilder = queryBuilder.Where("TeamId", teamId)
|
2019-04-25 15:17:43 +09:00
|
|
|
}
|
2016-02-02 08:41:02 -05:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
queryString, args, err := queryBuilder.ToSql()
|
2019-04-25 15:17:43 +09:00
|
|
|
if err != nil {
|
2021-11-15 16:43:14 +01:00
|
|
|
return 0, errors.Wrap(err, "incoming_webhook_tosql")
|
2019-04-25 15:17:43 +09:00
|
|
|
}
|
2016-02-02 08:41:02 -05:00
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
var count int64
|
|
|
|
|
if err := s.GetReplicaX().Get(&count, queryString, args...); err != nil {
|
|
|
|
|
return 0, errors.Wrap(err, "failed to count IncomingWebhooks")
|
|
|
|
|
}
|
|
|
|
|
return count, nil
|
2016-02-02 08:41:02 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-31 09:53:10 -04:00
|
|
|
func (s SqlWebhookStore) AnalyticsOutgoingCount(teamId string) (int64, error) {
|
2021-11-15 16:43:14 +01:00
|
|
|
queryBuilder :=
|
|
|
|
|
s.getQueryBuilder().
|
|
|
|
|
Select("COUNT(*)").
|
|
|
|
|
From("OutgoingWebhooks").
|
|
|
|
|
Where("DeleteAt = 0")
|
2016-02-02 08:41:02 -05:00
|
|
|
|
2021-01-25 11:15:17 +01:00
|
|
|
if teamId != "" {
|
2021-11-15 16:43:14 +01:00
|
|
|
queryBuilder = queryBuilder.Where("TeamId", teamId)
|
2019-05-01 07:37:29 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
queryString, args, err := queryBuilder.ToSql()
|
2019-05-01 07:37:29 -07:00
|
|
|
if err != nil {
|
2021-11-15 16:43:14 +01:00
|
|
|
return 0, errors.Wrap(err, "outgoing_webhook_tosql")
|
2019-05-01 07:37:29 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 16:43:14 +01:00
|
|
|
var count int64
|
|
|
|
|
if err := s.GetReplicaX().Get(&count, queryString, args...); err != nil {
|
|
|
|
|
return 0, errors.Wrap(err, "failed to count OutgoingWebhooks")
|
|
|
|
|
}
|
|
|
|
|
return count, nil
|
2016-02-02 08:41:02 -05:00
|
|
|
}
|