Make sure to return authors for comments created by link shares

This commit is contained in:
kolaente 2021-04-06 12:08:19 +02:00
parent 2cd2e64a83
commit 9ca9540eba
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 69 additions and 8 deletions

View File

@ -94,3 +94,9 @@
task_id: 36
created: 2020-02-19 18:07:06
updated: 2020-02-19 18:07:06
- id: 17
comment: comment 17
author_id: -2
task_id: 35
created: 2020-02-19 18:07:06
updated: 2020-02-19 18:07:06

View File

@ -17,6 +17,7 @@
package integrations
import (
"code.vikunja.io/api/pkg/db"
"testing"
"code.vikunja.io/api/pkg/models"
@ -298,6 +299,11 @@ func TestTaskComments(t *testing.T) {
rec, err := testHandlerLinkShareWrite.testCreateWithLinkShare(nil, map[string]string{"task": "13"}, `{"comment":"Lorem Ipsum"}`)
assert.NoError(t, err)
assert.Contains(t, rec.Body.String(), `"comment":"Lorem Ipsum"`)
db.AssertExists(t, "task_comments", map[string]interface{}{
"task_id": 13,
"comment": "Lorem Ipsum",
"author_id": -2,
}, false)
})
})
})

View File

@ -280,3 +280,10 @@ func GetLinkShareByID(s *xorm.Session, id int64) (share *LinkSharing, err error)
}
return
}
// GetLinkSharesByIDs returns all link shares from a slice of ids
func GetLinkSharesByIDs(s *xorm.Session, ids []int64) (shares map[int64]*LinkSharing, err error) {
shares = make(map[int64]*LinkSharing)
err = s.In("id", ids).Find(&shares)
return
}

View File

@ -213,14 +213,12 @@ func (tc *TaskComment) ReadAll(s *xorm.Session, auth web.Auth, search string, pa
return
}
// Get all authors
authors := make(map[int64]*user.User)
err = s.
Select("users.*").
Table("task_comments").
Where("task_id = ? AND comment like ?", tc.TaskID, "%"+search+"%").
Join("INNER", "users", "users.id = task_comments.author_id").
Find(&authors)
var authorIDs []int64
for _, comment := range comments {
authorIDs = append(authorIDs, comment.AuthorID)
}
authors, err := getUsersOrLinkSharesFromIDs(s, authorIDs)
if err != nil {
return
}

View File

@ -184,4 +184,18 @@ func TestTaskComment_ReadAll(t *testing.T) {
assert.Error(t, err)
assert.True(t, IsErrGenericForbidden(err))
})
t.Run("comment from link share", func(t *testing.T) {
db.LoadAndAssertFixtures(t)
s := db.NewSession()
defer s.Close()
tc := &TaskComment{TaskID: 35}
u := &user.User{ID: 1}
result, _, _, err := tc.ReadAll(s, u, "", 0, -1)
comments := result.([]*TaskComment)
assert.NoError(t, err)
assert.Len(t, comments, 2)
assert.Equal(t, int64(-2), comments[1].AuthorID)
assert.NotNil(t, comments[1].Author)
})
}

View File

@ -40,3 +40,33 @@ func getUserOrLinkShareUser(s *xorm.Session, a web.Auth) (uu *user.User, err err
return
}
// Returns all users or pseudo link shares from a slice of ids. ids < 0 are considered to be a link share in that case.
func getUsersOrLinkSharesFromIDs(s *xorm.Session, ids []int64) (users map[int64]*user.User, err error) {
var userIDs []int64
var linkShareIDs []int64
for _, id := range ids {
if id < 0 {
linkShareIDs = append(linkShareIDs, id*-1)
continue
}
userIDs = append(userIDs, id)
}
users, err = user.GetUsersByIDs(s, userIDs)
if err != nil {
return
}
shares, err := GetLinkSharesByIDs(s, linkShareIDs)
if err != nil {
return nil, err
}
for _, share := range shares {
users[share.ID*-1] = share.toUser()
}
return
}