Add sending the comment notification

This commit is contained in:
kolaente 2021-07-29 13:59:45 +02:00
parent 4c5b95cf26
commit 9c8867ccf8
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 51 additions and 11 deletions

View File

@ -97,6 +97,33 @@ func (s *SendTaskCommentNotification) Handle(msg *message.Message) (err error) {
sess := db.NewSession()
defer sess.Close()
mentionedUsers, err := FindMentionedUsersInText(sess, event.Comment.Comment)
if err != nil {
return err
}
for _, user := range mentionedUsers {
can, _, err := event.Task.CanRead(sess, user)
if err != nil {
return err
}
if !can {
continue
}
n := &TaskCommentNotification{
Doer: event.Doer,
Task: event.Task,
Comment: event.Comment,
Mentioned: true,
}
err = notifications.Notify(user, n)
if err != nil {
return err
}
}
subscribers, err := getSubscribersForEntity(sess, SubscriptionEntityTask, event.Task.ID)
if err != nil {
return err
@ -109,6 +136,10 @@ func (s *SendTaskCommentNotification) Handle(msg *message.Message) (err error) {
continue
}
if _, has := mentionedUsers[subscriber.UserID]; has {
continue
}
n := &TaskCommentNotification{
Doer: event.Doer,
Task: event.Task,

View File

@ -23,7 +23,7 @@ import (
"xorm.io/xorm"
)
func FindMentionedUsersInText(s *xorm.Session, text string) (users []*user.User, err error) {
func FindMentionedUsersInText(s *xorm.Session, text string) (users map[int64]*user.User, err error) {
reg := regexp.MustCompile(`@\w+`)
matches := reg.FindAllString(text, -1)
if matches == nil {

View File

@ -77,9 +77,10 @@ func TestFindMentionedUsersInText(t *testing.T) {
t.Errorf("FindMentionedUsersInText() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i, u := range gotUsers {
if u.ID != tt.wantUsers[i].ID {
t.Errorf("wanted user %d, got %d at position %d", tt.wantUsers[i].ID, u.ID, i)
for _, u := range tt.wantUsers {
_, has := gotUsers[u.ID]
if !has {
t.Errorf("wanted user %d but did not get it", u.ID)
}
}
})

View File

@ -58,17 +58,25 @@ func (n *ReminderDueNotification) Name() string {
// TaskCommentNotification represents a TaskCommentNotification notification
type TaskCommentNotification struct {
Doer *user.User `json:"doer"`
Task *Task `json:"task"`
Comment *TaskComment `json:"comment"`
Doer *user.User `json:"doer"`
Task *Task `json:"task"`
Comment *TaskComment `json:"comment"`
Mentioned bool `json:"mentioned"`
}
// ToMail returns the mail notification for TaskCommentNotification
func (n *TaskCommentNotification) ToMail() *notifications.Mail {
mail := notifications.NewMail().
From(n.Doer.GetNameAndFromEmail()).
Subject("Re: " + n.Task.Title)
From(n.Doer.GetNameAndFromEmail())
subject := "Re: " + n.Task.Title
if n.Mentioned {
subject = n.Doer.GetName() + " mentioned you in a comment at " + n.Task.Title
mail.Line(n.Doer.GetName() + " mentioned you in a comment:")
}
mail.Subject(subject)
lines := bufio.NewScanner(strings.NewReader(n.Comment.Comment))
for lines.Scan() {

View File

@ -198,12 +198,12 @@ func GetUserByUsername(s *xorm.Session, username string) (user *User, err error)
}
// GetUsersByUsername returns a slice of users with the provided usernames
func GetUsersByUsername(s *xorm.Session, usernames []string, withEmails bool) (users []*User, err error) {
func GetUsersByUsername(s *xorm.Session, usernames []string, withEmails bool) (users map[int64]*User, err error) {
if len(usernames) == 0 {
return
}
users = []*User{}
users = make(map[int64]*User)
err = s.In("username", usernames).Find(&users)
if err != nil {
return