From ba5b8ff1825d11f25cc623ddff605eb07686639f Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 13 Feb 2021 22:01:22 +0100 Subject: [PATCH] Send notification if a task was deleted --- pkg/models/listeners.go | 51 +++++++++++++++++++++++++++++++++++++ pkg/models/notifications.go | 18 +++++++++++++ 2 files changed, 69 insertions(+) diff --git a/pkg/models/listeners.go b/pkg/models/listeners.go index 74dd9a9a2..d7f1f5285 100644 --- a/pkg/models/listeners.go +++ b/pkg/models/listeners.go @@ -40,6 +40,7 @@ func RegisterListeners() { events.RegisterListener((&TeamCreatedEvent{}).Name(), &IncreaseTeamCounter{}) events.RegisterListener((&TaskCommentCreatedEvent{}).Name(), &SendTaskCommentNotification{}) events.RegisterListener((&TaskAssigneeCreatedEvent{}).Name(), &SendTaskAssignedNotification{}) + events.RegisterListener((&TaskDeletedEvent{}).Name(), &SendTaskDeletedNotification{}) } ////// @@ -175,6 +176,56 @@ func (s *SendTaskAssignedNotification) Handle(payload message.Payload) (err erro return nil } +// SendTaskDeletedNotification represents a listener +type SendTaskDeletedNotification struct { +} + +// Name defines the name for the SendTaskDeletedNotification listener +func (s *SendTaskDeletedNotification) Name() string { + return "send.task.deleted.notification" +} + +// Handle is executed when the event SendTaskDeletedNotification listens on is fired +func (s *SendTaskDeletedNotification) Handle(payload message.Payload) (err error) { + event := &TaskDeletedEvent{} + err = json.Unmarshal(payload, event) + if err != nil { + return err + } + + doer, is := event.Doer.(*user.User) + if !is { + return + } + + sess := db.NewSession() + defer sess.Close() + + subscribers, err := getSubscribersForEntity(sess, SubscriptionEntityTask, event.Task.ID) + if err != nil { + return err + } + + log.Debugf("Sending task deleted notifications to %d subscribers for task %d", len(subscribers), event.Task.ID) + + for _, subscriber := range subscribers { + if subscriber.UserID == doer.ID { + continue + } + + n := &TaskDeletedNotification{ + Doer: doer, + Task: event.Task, + } + err = notifications.Notify(subscriber.User, n) + if err != nil { + return + } + } + + return nil +} + /////// // List Event Listeners diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index 10c7f7100..bce586a03 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -94,3 +94,21 @@ func (n *TaskAssignedNotification) ToMail() *notifications.Mail { func (n *TaskAssignedNotification) ToDB() interface{} { return n } + +// TaskDeletedNotification represents a TaskDeletedNotification notification +type TaskDeletedNotification struct { + Doer *user.User + Task *Task +} + +// ToMail returns the mail notification for TaskDeletedNotification +func (n *TaskDeletedNotification) ToMail() *notifications.Mail { + return notifications.NewMail(). + Subject(n.Task.Title + "(" + n.Task.GetFullIdentifier() + ")" + " has been delete"). + Line(n.Doer.GetName() + " has deleted the task " + n.Task.Title + "(" + n.Task.GetFullIdentifier() + ")") +} + +// ToDB returns the TaskDeletedNotification notification in a format which can be saved in the db +func (n *TaskDeletedNotification) ToDB() interface{} { + return n +}