@@ -25,12 +25,12 @@ const (
2525type MessageType = string
2626
2727const (
28- MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed"
29- MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp"
30- MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown"
31- MessageTypeNodeInactive MessageType = "NodeInactive"
32- MessageTypeNodeActive MessageType = "NodeActive"
33- MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed"
28+ MessageTypeHealthCheckFailed MessageType = "HealthCheckFailed" // 节点健康检查失败
29+ MessageTypeHealthCheckNodeUp MessageType = "HealthCheckNodeUp" // 因健康检查节点上线
30+ MessageTypeHealthCheckNodeDown MessageType = "HealthCheckNodeDown" // 因健康检查节点下线
31+ MessageTypeNodeInactive MessageType = "NodeInactive" // 节点不活跃
32+ MessageTypeNodeActive MessageType = "NodeActive" // 节点活跃
33+ MessageTypeClusterDNSSyncFailed MessageType = "ClusterDNSSyncFailed" // DNS同步失败
3434 MessageTypeSSLCertExpiring MessageType = "SSLCertExpiring" // SSL证书即将过期
3535 MessageTypeSSLCertACMETaskFailed MessageType = "SSLCertACMETaskFailed" // SSL证书任务执行失败
3636 MessageTypeSSLCertACMETaskSuccess MessageType = "SSLCertACMETaskSuccess" // SSL证书任务执行成功
@@ -60,7 +60,7 @@ func init() {
6060 })
6161}
6262
63- // 启用条目
63+ // EnableMessage 启用条目
6464func (this * MessageDAO ) EnableMessage (tx * dbs.Tx , id int64 ) error {
6565 _ , err := this .Query (tx ).
6666 Pk (id ).
@@ -69,7 +69,7 @@ func (this *MessageDAO) EnableMessage(tx *dbs.Tx, id int64) error {
6969 return err
7070}
7171
72- // 禁用条目
72+ // DisableMessage 禁用条目
7373func (this * MessageDAO ) DisableMessage (tx * dbs.Tx , id int64 ) error {
7474 _ , err := this .Query (tx ).
7575 Pk (id ).
@@ -78,7 +78,7 @@ func (this *MessageDAO) DisableMessage(tx *dbs.Tx, id int64) error {
7878 return err
7979}
8080
81- // 查找启用中的条目
81+ // FindEnabledMessage 查找启用中的条目
8282func (this * MessageDAO ) FindEnabledMessage (tx * dbs.Tx , id int64 ) (* Message , error ) {
8383 result , err := this .Query (tx ).
8484 Pk (id ).
@@ -90,20 +90,60 @@ func (this *MessageDAO) FindEnabledMessage(tx *dbs.Tx, id int64) (*Message, erro
9090 return result .(* Message ), err
9191}
9292
93- // 创建集群消息
94- func (this * MessageDAO ) CreateClusterMessage (tx * dbs.Tx , clusterId int64 , messageType MessageType , level string , body string , paramsJSON []byte ) error {
95- _ , err := this .createMessage (tx , clusterId , 0 , messageType , level , body , paramsJSON )
96- return err
93+ // CreateClusterMessage 创建集群消息
94+ func (this * MessageDAO ) CreateClusterMessage (tx * dbs.Tx , clusterId int64 , messageType MessageType , level string , subject string , body string , paramsJSON []byte ) error {
95+ _ , err := this .createMessage (tx , clusterId , 0 , messageType , level , subject , body , paramsJSON )
96+ if err != nil {
97+ return err
98+ }
99+
100+ // 发送给媒介接收人
101+ err = SharedMessageTaskDAO .CreateMessageTasks (tx , MessageTaskTarget {
102+ ClusterId : clusterId ,
103+ NodeId : 0 ,
104+ ServerId : 0 ,
105+ }, messageType , subject , body )
106+ if err != nil {
107+ return err
108+ }
109+
110+ return nil
97111}
98112
99- // 创建节点消息
100- func (this * MessageDAO ) CreateNodeMessage (tx * dbs.Tx , clusterId int64 , nodeId int64 , messageType MessageType , level string , body string , paramsJSON []byte ) error {
101- _ , err := this .createMessage (tx , clusterId , nodeId , messageType , level , body , paramsJSON )
102- return err
113+ // CreateNodeMessage 创建节点消息
114+ func (this * MessageDAO ) CreateNodeMessage (tx * dbs.Tx , clusterId int64 , nodeId int64 , messageType MessageType , level string , subject string , body string , paramsJSON []byte ) error {
115+ _ , err := this .createMessage (tx , clusterId , nodeId , messageType , level , subject , body , paramsJSON )
116+ if err != nil {
117+ return err
118+ }
119+
120+ // 发送给媒介接收人 - 集群
121+ err = SharedMessageTaskDAO .CreateMessageTasks (tx , MessageTaskTarget {
122+ ClusterId : clusterId ,
123+ NodeId : 0 ,
124+ ServerId : 0 ,
125+ }, messageType , subject , body )
126+ if err != nil {
127+ return err
128+ }
129+
130+ // 发送给媒介接收人 - 节点
131+ if nodeId > 0 {
132+ err = SharedMessageTaskDAO .CreateMessageTasks (tx , MessageTaskTarget {
133+ ClusterId : clusterId ,
134+ NodeId : nodeId ,
135+ ServerId : 0 ,
136+ }, messageType , subject , body )
137+ if err != nil {
138+ return err
139+ }
140+ }
141+
142+ return nil
103143}
104144
105- // 创建普通消息
106- func (this * MessageDAO ) CreateMessage (tx * dbs.Tx , adminId int64 , userId int64 , messageType MessageType , level string , body string , paramsJSON []byte ) error {
145+ // CreateMessage 创建普通消息
146+ func (this * MessageDAO ) CreateMessage (tx * dbs.Tx , adminId int64 , userId int64 , messageType MessageType , level string , subject string , body string , paramsJSON []byte ) error {
107147 h := md5 .New ()
108148 h .Write ([]byte (body ))
109149 h .Write (paramsJSON )
@@ -114,6 +154,14 @@ func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, m
114154 op .UserId = userId
115155 op .Type = messageType
116156 op .Level = level
157+
158+ subjectRunes := []rune (subject )
159+ if len (subjectRunes ) > 100 {
160+ op .Subject = string (subjectRunes [:100 ]) + "..."
161+ } else {
162+ op .Subject = subject
163+ }
164+
117165 op .Body = body
118166 if len (paramsJSON ) > 0 {
119167 op .Params = paramsJSON
@@ -123,10 +171,14 @@ func (this *MessageDAO) CreateMessage(tx *dbs.Tx, adminId int64, userId int64, m
123171 op .Day = timeutil .Format ("Ymd" )
124172 op .Hash = hash
125173 err := this .Save (tx , op )
126- return err
174+ if err != nil {
175+ return err
176+ }
177+
178+ return nil
127179}
128180
129- // 删除某天之前的消息
181+ // DeleteMessagesBeforeDay 删除某天之前的消息
130182func (this * MessageDAO ) DeleteMessagesBeforeDay (tx * dbs.Tx , dayTime time.Time ) error {
131183 day := timeutil .Format ("Ymd" , dayTime )
132184 _ , err := this .Query (tx ).
@@ -136,7 +188,7 @@ func (this *MessageDAO) DeleteMessagesBeforeDay(tx *dbs.Tx, dayTime time.Time) e
136188 return err
137189}
138190
139- // 计算未读消息数量
191+ // CountUnreadMessages 计算未读消息数量
140192func (this * MessageDAO ) CountUnreadMessages (tx * dbs.Tx , adminId int64 , userId int64 ) (int64 , error ) {
141193 query := this .Query (tx ).
142194 Attr ("isRead" , false )
@@ -149,7 +201,7 @@ func (this *MessageDAO) CountUnreadMessages(tx *dbs.Tx, adminId int64, userId in
149201 return query .Count ()
150202}
151203
152- // 列出单页未读消息
204+ // ListUnreadMessages 列出单页未读消息
153205func (this * MessageDAO ) ListUnreadMessages (tx * dbs.Tx , adminId int64 , userId int64 , offset int64 , size int64 ) (result []* Message , err error ) {
154206 query := this .Query (tx ).
155207 Attr ("isRead" , false )
@@ -168,7 +220,7 @@ func (this *MessageDAO) ListUnreadMessages(tx *dbs.Tx, adminId int64, userId int
168220 return
169221}
170222
171- // 设置消息已读状态
223+ // UpdateMessageRead 设置消息已读状态
172224func (this * MessageDAO ) UpdateMessageRead (tx * dbs.Tx , messageId int64 , b bool ) error {
173225 if messageId <= 0 {
174226 return errors .New ("invalid messageId" )
@@ -180,7 +232,7 @@ func (this *MessageDAO) UpdateMessageRead(tx *dbs.Tx, messageId int64, b bool) e
180232 return err
181233}
182234
183- // 设置一组消息为已读状态
235+ // UpdateMessagesRead 设置一组消息为已读状态
184236func (this * MessageDAO ) UpdateMessagesRead (tx * dbs.Tx , messageIds []int64 , b bool ) error {
185237 // 这里我们一个一个更改,因为In语句不容易Prepare,且效率不高
186238 for _ , messageId := range messageIds {
@@ -192,7 +244,7 @@ func (this *MessageDAO) UpdateMessagesRead(tx *dbs.Tx, messageIds []int64, b boo
192244 return nil
193245}
194246
195- // 设置所有消息为已读
247+ // UpdateAllMessagesRead 设置所有消息为已读
196248func (this * MessageDAO ) UpdateAllMessagesRead (tx * dbs.Tx , adminId int64 , userId int64 ) error {
197249 query := this .Query (tx ).
198250 Attr ("isRead" , false )
@@ -208,7 +260,7 @@ func (this *MessageDAO) UpdateAllMessagesRead(tx *dbs.Tx, adminId int64, userId
208260 return err
209261}
210262
211- // 检查消息权限
263+ // CheckMessageUser 检查消息权限
212264func (this * MessageDAO ) CheckMessageUser (tx * dbs.Tx , messageId int64 , adminId int64 , userId int64 ) (bool , error ) {
213265 if messageId <= 0 || (adminId <= 0 && userId <= 0 ) {
214266 return false , nil
@@ -225,7 +277,7 @@ func (this *MessageDAO) CheckMessageUser(tx *dbs.Tx, messageId int64, adminId in
225277}
226278
227279// 创建消息
228- func (this * MessageDAO ) createMessage (tx * dbs.Tx , clusterId int64 , nodeId int64 , messageType MessageType , level string , body string , paramsJSON []byte ) (int64 , error ) {
280+ func (this * MessageDAO ) createMessage (tx * dbs.Tx , clusterId int64 , nodeId int64 , messageType MessageType , level string , subject string , body string , paramsJSON []byte ) (int64 , error ) {
229281 h := md5 .New ()
230282 h .Write ([]byte (body ))
231283 h .Write (paramsJSON )
@@ -241,6 +293,14 @@ func (this *MessageDAO) createMessage(tx *dbs.Tx, clusterId int64, nodeId int64,
241293 op .NodeId = nodeId
242294 op .Type = messageType
243295 op .Level = level
296+
297+ subjectRunes := []rune (subject )
298+ if len (subjectRunes ) > 100 {
299+ op .Subject = string (subjectRunes [:100 ]) + "..."
300+ } else {
301+ op .Subject = subject
302+ }
303+
244304 op .Body = body
245305 if len (paramsJSON ) > 0 {
246306 op .Params = paramsJSON
0 commit comments