mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 06:09:39 +08:00
feat(notify): filter notification by topic_type
之前的模型,消息和Event是一一对应的,一个Event可以和多个Topic对应, 为了支持通过Topic Type过滤消息,现在Event和Topic是一一对应的。
This commit is contained in:
@@ -89,6 +89,7 @@ func init() {
|
||||
|
||||
ContactType string `help:"contact_type"`
|
||||
ReceiverId string `help:"receiver_id"`
|
||||
TopicType string `help:"topic type"`
|
||||
}
|
||||
R(&NotificationListInput{}, "notify-list", "List notify message", func(s *mcclient.ClientSession, args *NotificationListInput) error {
|
||||
params, err := options.ListStructToParams(args)
|
||||
|
||||
@@ -85,6 +85,7 @@ type NotificationListInput struct {
|
||||
ContactType string
|
||||
ReceiverId string
|
||||
Tag string
|
||||
TopicType string
|
||||
}
|
||||
|
||||
type SContact struct {
|
||||
|
||||
@@ -44,13 +44,15 @@ type SEvent struct {
|
||||
Message string
|
||||
Event string `width:"64" nullable:"true"`
|
||||
AdvanceDays int
|
||||
TopicId string `width:"128" nullable:"true" index:"true"`
|
||||
}
|
||||
|
||||
func (e *SEventManager) CreateEvent(ctx context.Context, event, message string, advanceDays int) (*SEvent, error) {
|
||||
func (e *SEventManager) CreateEvent(ctx context.Context, event, topicId, message string, advanceDays int) (*SEvent, error) {
|
||||
eve := &SEvent{
|
||||
Message: message,
|
||||
Event: event,
|
||||
AdvanceDays: advanceDays,
|
||||
TopicId: topicId,
|
||||
}
|
||||
err := e.TableSpec().Insert(ctx, eve)
|
||||
if err != nil {
|
||||
|
||||
@@ -231,34 +231,31 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred
|
||||
}
|
||||
|
||||
// receiver
|
||||
topics, err := TopicManager.TopicsByEvent(input.Event, input.AdvanceDays)
|
||||
topic, err := TopicManager.TopicByEvent(input.Event, input.AdvanceDays)
|
||||
if err != nil {
|
||||
return output, errors.Wrapf(err, "unable fetch subscriptions by event %q", input.Event)
|
||||
}
|
||||
if len(topics) == 0 {
|
||||
if topic == nil {
|
||||
return output, nil
|
||||
}
|
||||
var receiverIds []string
|
||||
for i := range topics {
|
||||
receiverIds1, err := SubscriberManager.getReceiversSent(ctx, topics[i].Id, input.ProjectDomainId, input.ProjectId)
|
||||
if err != nil {
|
||||
return output, errors.Wrap(err, "unable to get receive")
|
||||
}
|
||||
receiverIds = append(receiverIds, receiverIds1...)
|
||||
receiverIds1, err := SubscriberManager.getReceiversSent(ctx, topic.Id, input.ProjectDomainId, input.ProjectId)
|
||||
if err != nil {
|
||||
return output, errors.Wrap(err, "unable to get receive")
|
||||
}
|
||||
receiverIds = append(receiverIds, receiverIds1...)
|
||||
|
||||
// robot
|
||||
var robots []string
|
||||
for i := range topics {
|
||||
_robots, err := SubscriberManager.robot(topics[i].Id, input.ProjectDomainId, input.ProjectId)
|
||||
if err != nil {
|
||||
if errors.Cause(err) != errors.ErrNotFound {
|
||||
return output, errors.Wrapf(err, "unable fetch robot of subscription %q", topics[i].Id)
|
||||
}
|
||||
} else {
|
||||
robots = append(robots, _robots...)
|
||||
_robots, err := SubscriberManager.robot(topic.Id, input.ProjectDomainId, input.ProjectId)
|
||||
if err != nil {
|
||||
if errors.Cause(err) != errors.ErrNotFound {
|
||||
return output, errors.Wrapf(err, "unable fetch robot of subscription %q", topic.Id)
|
||||
}
|
||||
} else {
|
||||
robots = append(robots, _robots...)
|
||||
}
|
||||
|
||||
var webhookRobots []string
|
||||
if len(robots) > 0 {
|
||||
robots = sets.NewString(robots...).UnsortedList()
|
||||
@@ -299,13 +296,12 @@ func (nm *SNotificationManager) PerformEventNotify(ctx context.Context, userCred
|
||||
receiverIds = idSet.UnsortedList()
|
||||
|
||||
// create event
|
||||
event, err := EventManager.CreateEvent(ctx, input.Event, message, input.AdvanceDays)
|
||||
event, err := EventManager.CreateEvent(ctx, input.Event, topic.Id, message, input.AdvanceDays)
|
||||
if err != nil {
|
||||
return output, errors.Wrap(err, "unable to create Event")
|
||||
}
|
||||
|
||||
if nm.needWebconsole(topics) {
|
||||
|
||||
if nm.needWebconsole([]STopic{*topic}) {
|
||||
// webconsole
|
||||
err = nm.create(ctx, userCred, api.WEBCONSOLE, receiverIds, webconsoleContacts.UnsortedList(), input.Priority, event.Id)
|
||||
if err != nil {
|
||||
@@ -777,6 +773,11 @@ func (nm *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchemy.
|
||||
if len(input.Tag) > 0 {
|
||||
q = q.Equals("tag", input.Tag)
|
||||
}
|
||||
if len(input.TopicType) > 0 {
|
||||
topicq := TopicManager.Query("id").Equals("type", input.TopicType).SubQuery()
|
||||
eventq := EventManager.Query("id").In("topic_id", topicq).SubQuery()
|
||||
q = q.In("event_id", eventq)
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -436,6 +436,19 @@ func (s *STopic) getActions() []notify.SAction {
|
||||
return actions
|
||||
}
|
||||
|
||||
func (sm *STopicManager) TopicByEvent(eventStr string, advanceDays int) (*STopic, error) {
|
||||
topics, err := sm.TopicsByEvent(eventStr, advanceDays)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(topics) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// free memory in time
|
||||
topic := topics[0]
|
||||
return &topic, nil
|
||||
}
|
||||
|
||||
func (sm *STopicManager) TopicsByEvent(eventStr string, advanceDays int) ([]STopic, error) {
|
||||
event, err := parseEvent(eventStr)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user