-
Notifications
You must be signed in to change notification settings - Fork 182
/
addNoticeReceivers.go
105 lines (92 loc) · 2.37 KB
/
addNoticeReceivers.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
package settings
import (
"github.com/TeaWeb/build/internal/teaconfigs/agents"
"github.com/TeaWeb/build/internal/teaconfigs/notices"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
)
type AddNoticeReceiversAction actions.Action
// 添加通知接收人
func (this *AddNoticeReceiversAction) Run(params struct {
AgentId string
Level notices.NoticeLevel
}) {
this.Data["selectedTab"] = "noticeSetting"
agent := agents.NewAgentConfigFromId(params.AgentId)
if agent == nil {
this.Fail("找不到Agent")
}
this.Data["agent"] = agent
level := notices.FindNoticeLevel(params.Level)
if level == nil {
this.Fail("Level不存在")
}
this.Data["level"] = level
// 媒介
setting := notices.SharedNoticeSetting()
mediaMaps := []maps.Map{}
for _, media := range setting.Medias {
if !media.On {
continue
}
mediaType := notices.FindNoticeMediaType(media.Type)
if mediaType == nil {
continue
}
mediaMaps = append(mediaMaps, maps.Map{
"id": media.Id,
"name": media.Name,
"typeName": mediaType["name"],
"type": media.Type,
"mediaDescription": mediaType["description"],
"userDescription": mediaType["user"],
})
}
this.Data["medias"] = mediaMaps
this.Show()
}
// 提交保存
func (this *AddNoticeReceiversAction) RunPost(params struct {
AgentId string
Level notices.NoticeLevel
On bool
Name string
MediaId string
User string
Must actions.Must
}) {
agent := agents.NewAgentConfigFromId(params.AgentId)
if agent == nil {
this.Fail("找不到Agent")
}
params.Must.
Field("name", params.Name).
Require("请输入接收人名称").
Field("mediaId", params.MediaId).
Require("请选择使用的媒介")
// 是否校验接收人
media := notices.SharedNoticeSetting().FindMedia(params.MediaId)
if media == nil {
this.Fail("找不到媒介")
}
rawMedia, err := media.Raw()
if err != nil {
this.Fail("找不到媒介:" + err.Error())
}
if rawMedia.RequireUser() {
params.Must.
Field("name", params.Name).
Require("请输入接收人名称")
}
receiver := notices.NewNoticeReceiver()
receiver.On = params.On
receiver.Name = params.Name
receiver.MediaId = params.MediaId
receiver.User = params.User
agent.AddNoticeReceiver(params.Level, receiver)
err = agent.Save()
if err != nil {
this.Fail("保存失败:" + err.Error())
}
this.Success()
}