Add copying task attachments

This commit is contained in:
kolaente 2020-06-29 22:14:50 +02:00
parent 559c2cf0d1
commit 9556bb3918
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 71 additions and 26 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)
}