Add copying task attachments
This commit is contained in:
parent
559c2cf0d1
commit
9556bb3918
|
@ -83,22 +83,47 @@ func (ld *ListDuplicate) Create(a web.Auth) (err error) {
|
|||
return err
|
||||
}
|
||||
|
||||
taskMap := make(map[int64]int64)
|
||||
// Create + update all tasks
|
||||
oldTaskIDs := make([]int64, len(tasks))
|
||||
for _, t := range tasks {
|
||||
oldID := t.ID
|
||||
t.ID = 0
|
||||
t.ListID = ld.List.ID
|
||||
t.BucketID = bucketMap[t.ID]
|
||||
t.ID = 0
|
||||
t.UID = ""
|
||||
err := createTask(t, a, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
taskMap[oldID] = t.ID
|
||||
oldTaskIDs = append(oldTaskIDs, oldID)
|
||||
}
|
||||
|
||||
// Save all attachments
|
||||
// We also duplicate all underlying files since they could be modified in one list which would result in
|
||||
// file changes in the other list which is not something we want.
|
||||
attachments, err := getTaskAttachmentsByTaskIDs(oldTaskIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, attachment := range attachments {
|
||||
attachment.TaskID = oldTaskIDs[attachment.TaskID]
|
||||
if err := attachment.File.LoadFileByID(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := attachment.NewAttachment(attachment.File.File, attachment.File.Name, attachment.File.Size, a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = attachment.File.File.Close()
|
||||
}
|
||||
|
||||
// * Files
|
||||
// * Label Tasks (not the labels)
|
||||
// * assignees
|
||||
// * attachments
|
||||
// * comments
|
||||
// * relations in that list
|
||||
// * reminders
|
||||
|
|
|
@ -193,3 +193,45 @@ func (ta *TaskAttachment) Delete() error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func getTaskAttachmentsByTaskIDs(taskIDs []int64) (attachments []*TaskAttachment, err error) {
|
||||
attachments = []*TaskAttachment{}
|
||||
err = x.
|
||||
In("task_id", taskIDs).
|
||||
Find(&attachments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fileIDs := []int64{}
|
||||
userIDs := []int64{}
|
||||
for _, a := range attachments {
|
||||
userIDs = append(userIDs, a.CreatedByID)
|
||||
fileIDs = append(fileIDs, a.FileID)
|
||||
}
|
||||
|
||||
// Get all files
|
||||
fs := make(map[int64]*files.File)
|
||||
err = x.In("id", fileIDs).Find(&fs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
users := make(map[int64]*user.User)
|
||||
err = x.In("id", userIDs).Find(&users)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Obfuscate all user emails
|
||||
for _, u := range users {
|
||||
u.Email = ""
|
||||
}
|
||||
|
||||
for _, a := range attachments {
|
||||
a.CreatedBy = users[a.CreatedByID]
|
||||
a.File = fs[a.FileID]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ package models
|
|||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/metrics"
|
||||
"code.vikunja.io/api/pkg/user"
|
||||
"code.vikunja.io/api/pkg/utils"
|
||||
|
@ -440,26 +439,7 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (err error) {
|
|||
}
|
||||
|
||||
// Get task attachments
|
||||
attachments := []*TaskAttachment{}
|
||||
err = x.
|
||||
In("task_id", taskIDs).
|
||||
Find(&attachments)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fileIDs := []int64{}
|
||||
for _, a := range attachments {
|
||||
userIDs = append(userIDs, a.CreatedByID)
|
||||
fileIDs = append(fileIDs, a.FileID)
|
||||
}
|
||||
|
||||
// Get all files
|
||||
fs := make(map[int64]*files.File)
|
||||
err = x.In("id", fileIDs).Find(&fs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
attachments, err := getTaskAttachmentsByTaskIDs(taskIDs)
|
||||
|
||||
// Get all users of a task
|
||||
// aka the ones who created a task
|
||||
|
@ -476,8 +456,6 @@ func addMoreInfoToTasks(taskMap map[int64]*Task) (err error) {
|
|||
|
||||
// Put the users and files in task attachments
|
||||
for _, a := range attachments {
|
||||
a.CreatedBy = users[a.CreatedByID]
|
||||
a.File = fs[a.FileID]
|
||||
taskMap[a.TaskID].Attachments = append(taskMap[a.TaskID].Attachments, a)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue