Send notification if a task was assigned to someone

This commit is contained in:
kolaente 2021-02-13 21:57:34 +01:00
parent aa765ac3dd
commit 4d8dfecc3c
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 86 additions and 1 deletions

View File

@ -39,6 +39,7 @@ func RegisterListeners() {
events.RegisterListener((&TeamDeletedEvent{}).Name(), &DecreaseTeamCounter{})
events.RegisterListener((&TeamCreatedEvent{}).Name(), &IncreaseTeamCounter{})
events.RegisterListener((&TaskCommentCreatedEvent{}).Name(), &SendTaskCommentNotification{})
events.RegisterListener((&TaskAssigneeCreatedEvent{}).Name(), &SendTaskAssignedNotification{})
}
//////
@ -123,6 +124,57 @@ func (s *SendTaskCommentNotification) Handle(payload message.Payload) (err error
return
}
// SendTaskAssignedNotification represents a listener
type SendTaskAssignedNotification struct {
}
// Name defines the name for the SendTaskAssignedNotification listener
func (s *SendTaskAssignedNotification) Name() string {
return "send.task.assigned.notification"
}
// Handle is executed when the event SendTaskAssignedNotification listens on is fired
func (s *SendTaskAssignedNotification) Handle(payload message.Payload) (err error) {
event := &TaskAssigneeCreatedEvent{}
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 assigned notifications to %d subscribers for task %d", len(subscribers), event.Task.ID)
for _, subscriber := range subscribers {
if subscriber.UserID == doer.ID {
continue
}
n := &TaskAssignedNotification{
Doer: doer,
Task: event.Task,
Assignee: event.Assignee,
}
err = notifications.Notify(subscriber.User, n)
if err != nil {
return
}
}
return nil
}
///////
// List Event Listeners

View File

@ -67,10 +67,30 @@ func (n *TaskCommentNotification) ToMail() *notifications.Mail {
}
return mail.
Action("View Task", config.ServiceFrontendurl.GetString()+"tasks/"+strconv.FormatInt(n.Task.ID, 10))
Action("View Task", n.Task.GetFrontendURL())
}
// ToDB returns the TaskCommentNotification notification in a format which can be saved in the db
func (n *TaskCommentNotification) ToDB() interface{} {
return n
}
// TaskAssignedNotification represents a TaskAssignedNotification notification
type TaskAssignedNotification struct {
Doer *user.User
Task *Task
Assignee *user.User
}
// ToMail returns the mail notification for TaskAssignedNotification
func (n *TaskAssignedNotification) ToMail() *notifications.Mail {
return notifications.NewMail().
Subject(n.Task.Title+"("+n.Task.GetFullIdentifier()+")"+" has been assigned to "+n.Assignee.GetName()).
Line(n.Doer.GetName()+" has assigned this task to "+n.Assignee.GetName()).
Action("View Task", n.Task.GetFrontendURL())
}
// ToDB returns the TaskAssignedNotification notification in a format which can be saved in the db
func (n *TaskAssignedNotification) ToDB() interface{} {
return n
}

View File

@ -123,6 +123,19 @@ func (Task) TableName() string {
return "tasks"
}
// GetFullIdentifier returns the task identifier if the task has one and the index prefixed with # otherwise.
func (t *Task) GetFullIdentifier() string {
if t.Identifier != "" {
return t.Identifier
}
return "#" + strconv.FormatInt(t.Index, 10)
}
func (t *Task) GetFrontendURL() string {
return config.ServiceFrontendurl.GetString() + "tasks/" + strconv.FormatInt(t.ID, 10)
}
type taskFilterConcatinator string
const (