-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
alertRecipient.go
119 lines (106 loc) · 3.77 KB
/
alertRecipient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package model
import (
"encoding/json"
"strings"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/nging/v5/application/dbschema"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/nging/v5/application/registry/alert"
)
func NewAlertRecipient(ctx echo.Context) *AlertRecipient {
m := &AlertRecipient{
NgingAlertRecipient: dbschema.NewNgingAlertRecipient(ctx),
}
return m
}
type AlertRecipient struct {
*dbschema.NgingAlertRecipient
}
func (s *AlertRecipient) check() error {
s.Name = strings.TrimSpace(s.Name)
if len(s.Name) == 0 {
return s.Context().NewError(code.InvalidParameter, `名称不能为空`).SetZone(`name`)
}
if len(s.Account) == 0 && s.Platform != alert.RecipientPlatformWebhookCustom {
return s.Context().NewError(code.InvalidParameter, `账号不能为空`).SetZone(`account`)
}
s.Description = strings.TrimSpace(s.Description)
s.Account = strings.TrimSpace(s.Account)
s.Type = strings.TrimSpace(s.Type)
if len(s.Type) == 0 {
return s.Context().NewError(code.InvalidParameter, `请选择类型`).SetZone(`type`)
}
s.Platform = strings.TrimSpace(s.Platform)
if s.Type == `webhook` {
if len(s.Platform) == 0 {
return s.Context().NewError(code.InvalidParameter, `对于webhook类型,必须选择一个平台`).SetZone(`platform`)
}
if s.Platform == alert.RecipientPlatformWebhookCustom {
s.Extra = strings.TrimSpace(s.Extra)
if len(s.Extra) == 0 {
return s.Context().NewError(code.InvalidParameter, `自定义webhook必须输入“扩展信息”`).SetZone(`extra`)
}
custom := &alert.WebhookCustom{}
extraBytes := []byte(s.Extra)
if err := json.Unmarshal(extraBytes, custom); err != nil {
err = common.JSONBytesParseError(err, extraBytes)
return s.Context().NewError(code.InvalidParameter, err.Error()).SetZone(`extra`)
}
if len(s.Account) > 7 {
switch s.Account[0:7] {
case `https:/`, `http://`:
custom.Url = s.Account
}
}
if err := custom.ToWebhook().Validate(); err != nil {
return s.Context().NewError(code.InvalidParameter, err.Error()).SetZone(`extra`)
}
}
}
return nil
}
func (s *AlertRecipient) Add() (pk interface{}, err error) {
if err = s.check(); err != nil {
return nil, err
}
return s.NgingAlertRecipient.Insert()
}
func (s *AlertRecipient) Edit(mw func(db.Result) db.Result, args ...interface{}) (err error) {
if err = s.check(); err != nil {
return err
}
return s.NgingAlertRecipient.Update(mw, args...)
}
func (s *AlertRecipient) Delete(mw func(db.Result) db.Result, args ...interface{}) (err error) {
m := NewAlertTopic(s.Context())
var rows []*dbschema.NgingAlertRecipient
s.NgingAlertRecipient.ListByOffset(&rows, nil, 0, -1, args...)
recipientIDs := make([]uint, len(rows))
for index, recipient := range rows {
recipientIDs[index] = recipient.Id
}
if len(recipientIDs) == 0 {
return
}
err = m.Delete(nil, db.Cond{`recipient_id`: db.In(recipientIDs)})
if err != nil {
return
}
return s.NgingAlertRecipient.Delete(mw, args...)
}