-
-
Notifications
You must be signed in to change notification settings - Fork 485
Expand file tree
/
Copy pathtask_duplicate.go
More file actions
173 lines (155 loc) · 5.19 KB
/
task_duplicate.go
File metadata and controls
173 lines (155 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"bytes"
"io"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/web"
"xorm.io/xorm"
)
// TaskDuplicate holds everything needed to duplicate a task
type TaskDuplicate struct {
// The task id of the task to duplicate
TaskID int64 `json:"-" param:"projecttask"`
// The duplicated task
Task *Task `json:"duplicated_task,omitempty" readOnly:"true" doc:"The newly created duplicate task, populated by the server in the response."`
web.Permissions `json:"-"`
web.CRUDable `json:"-"`
}
// CanCreate checks if a user has the permission to duplicate a task
func (td *TaskDuplicate) CanCreate(s *xorm.Session, a web.Auth) (canCreate bool, err error) {
// Need read access on the original task
originalTask := &Task{ID: td.TaskID}
canRead, _, err := originalTask.CanRead(s, a)
if err != nil || !canRead {
return canRead, err
}
// Need write access on the project to create tasks in it
p := &Project{ID: originalTask.ProjectID}
return p.CanUpdate(s, a)
}
// Create duplicates a task
// @Summary Duplicate a task
// @Description Copies a task with all its properties (labels, assignees, attachments, reminders) into the same project. Creates a "copied from" relation between the new and original task.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param taskID path int true "The task ID to duplicate"
// @Success 201 {object} models.TaskDuplicate "The duplicated task."
// @Failure 403 {object} web.HTTPError "The user does not have access to the task."
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{taskID}/duplicate [put]
func (td *TaskDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) {
// Get the original task with all details
originalTask := &Task{ID: td.TaskID}
err = originalTask.ReadOne(s, doer)
if err != nil {
return err
}
// Create the new task
newTask := &Task{
Title: originalTask.Title,
Description: originalTask.Description,
Done: false,
DueDate: originalTask.DueDate,
ProjectID: originalTask.ProjectID,
RepeatAfter: originalTask.RepeatAfter,
RepeatMode: originalTask.RepeatMode,
Priority: originalTask.Priority,
StartDate: originalTask.StartDate,
EndDate: originalTask.EndDate,
HexColor: originalTask.HexColor,
PercentDone: originalTask.PercentDone,
Assignees: originalTask.Assignees,
Reminders: originalTask.Reminders,
}
err = createTask(s, newTask, doer, true, true)
if err != nil {
return err
}
// Duplicate labels
labelTasks := []*LabelTask{}
err = s.Where("task_id = ?", td.TaskID).Find(&labelTasks)
if err != nil {
return err
}
for _, lt := range labelTasks {
lt.ID = 0
lt.TaskID = newTask.ID
}
if len(labelTasks) > 0 {
if _, err := s.Insert(&labelTasks); err != nil {
return err
}
}
// Duplicate attachments (copy underlying files)
attachments, err := getTaskAttachmentsByTaskIDs(s, []int64{td.TaskID})
if err != nil {
return err
}
oldToNewAttachmentIDs := make(map[int64]int64)
for _, attachment := range attachments {
oldAttachmentID := attachment.ID
attachment.ID = 0
attachment.TaskID = newTask.ID
attachment.File = &files.File{ID: attachment.FileID}
if err := attachment.File.LoadFileMetaByID(); err != nil {
if files.IsErrFileDoesNotExist(err) {
continue
}
return err
}
if err := attachment.File.LoadFileByID(); err != nil {
return err
}
sourceFile := attachment.File.File
defer sourceFile.Close()
buf, err := io.ReadAll(sourceFile)
if err != nil {
return err
}
err = attachment.NewAttachment(s, bytes.NewReader(buf), attachment.File.Name, uint64(len(buf)), doer)
if err != nil {
return err
}
oldToNewAttachmentIDs[oldAttachmentID] = attachment.ID
}
// Re-set the cover image if the original task had one
if originalTask.CoverImageAttachmentID != 0 {
if newAttachmentID, ok := oldToNewAttachmentIDs[originalTask.CoverImageAttachmentID]; ok {
newTask.CoverImageAttachmentID = newAttachmentID
if _, err := s.Where("id = ?", newTask.ID).
Cols("cover_image_attachment_id").
Update(newTask); err != nil {
return err
}
}
}
// Create "copied from/to" relation
rel := &TaskRelation{
TaskID: newTask.ID,
OtherTaskID: td.TaskID,
RelationKind: RelationKindCopiedFrom,
}
if err := rel.Create(s, doer); err != nil {
return err
}
// Re-read the task to populate all fields for the response
td.Task = newTask
return td.Task.ReadOne(s, doer)
}