Send notification if a task was deleted

This commit is contained in:
kolaente 2021-02-13 22:01:22 +01:00
parent 4d8dfecc3c
commit ba5b8ff182
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 69 additions and 0 deletions

View File

@ -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

View File

@ -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
}