Make comments actually work for link shares

This commit is contained in:
kolaente 2021-04-05 18:13:45 +02:00
parent a8750e2fef
commit 2cd2e64a83
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 80 additions and 14 deletions

View File

@ -33,6 +33,20 @@ func TestTaskComments(t *testing.T) {
},
t: t,
}
testHandlerLinkShareWrite := webHandlerTest{
linkShare: &models.LinkSharing{
ID: 2,
Hash: "test2",
ListID: 2,
Right: models.RightWrite,
SharingType: models.SharingTypeWithoutPassword,
SharedByID: 1,
},
strFunc: func() handler.CObject {
return &models.TaskComment{}
},
t: t,
}
// Only run specific nested tests:
// ^TestTaskComments$/^Update$/^Update_task_items$/^Removing_Assignees_null$
t.Run("Update", func(t *testing.T) {
@ -280,6 +294,11 @@ func TestTaskComments(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, rec.Body.String(), `"comment":"Lorem Ipsum"`)
})
t.Run("Link Share", func(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"`)
})
})
})
}

View File

@ -267,3 +267,16 @@ func GetListByShareHash(s *xorm.Session, hash string) (list *List, err error) {
list, err = GetListSimpleByID(s, share.ListID)
return
}
// GetLinkShareByID returns a link share by its id.
func GetLinkShareByID(s *xorm.Session, id int64) (share *LinkSharing, err error) {
share = &LinkSharing{}
has, err := s.Where("id = ?", id).Get(share)
if err != nil {
return
}
if !has {
return share, ErrListShareDoesNotExist{ID: id}
}
return
}

View File

@ -67,30 +67,22 @@ func (tc *TaskComment) Create(s *xorm.Session, a web.Auth) (err error) {
return err
}
tc.AuthorID = a.GetID()
if share, is := a.(*LinkSharing); is {
tc.AuthorID = share.getUserID()
tc.Author, err = getUserOrLinkShareUser(s, a)
if err != nil {
return err
}
tc.AuthorID = tc.Author.ID
_, err = s.Insert(tc)
if err != nil {
return
}
doer, _ := user.GetFromAuth(a)
err = events.Dispatch(&TaskCommentCreatedEvent{
return events.Dispatch(&TaskCommentCreatedEvent{
Task: &task,
Comment: tc,
Doer: doer,
Doer: tc.Author,
})
if err != nil {
return err
}
// TODO: Add a wrapper in models which returns either a user or a share disguised as a share
tc.Author, err = user.GetUserByID(s, tc.AuthorID)
return
}
// Delete removes a task comment

42
pkg/models/users.go Normal file
View File

@ -0,0 +1,42 @@
// Copyright 2021 Vikunja and contriubtors. All rights reserved.
//
// This file is part of Vikunja.
//
// Vikunja is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vikunja is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vikunja. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"code.vikunja.io/api/pkg/user"
"code.vikunja.io/web"
"xorm.io/xorm"
)
// Returns either a user or a link share disguised as a user.
func getUserOrLinkShareUser(s *xorm.Session, a web.Auth) (uu *user.User, err error) {
if u, is := a.(*user.User); is {
uu, err = user.GetUserByID(s, u.ID)
return
}
if ls, is := a.(*LinkSharing); is {
l, err := GetLinkShareByID(s, ls.ID)
if err != nil {
return nil, err
}
return l.toUser(), nil
}
return
}