2018-11-26 21:17:33 +01:00
// Vikunja is a todo-list application to facilitate your life.
// Copyright 2018 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 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-06-10 15:55:56 +02:00
package models
2018-12-02 01:49:30 +01:00
import (
2019-07-16 16:15:40 +02:00
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/api/pkg/utils"
2018-12-02 01:49:30 +01:00
"code.vikunja.io/web"
2019-07-16 16:15:40 +02:00
"github.com/imdario/mergo"
2018-12-19 20:14:48 +01:00
"sort"
2019-07-16 16:15:40 +02:00
"time"
2018-12-02 01:49:30 +01:00
)
2018-12-01 00:26:56 +01:00
2019-08-14 22:19:04 +02:00
// Task represents an task in a todolist
type Task struct {
2019-01-03 23:22:06 +01:00
// The unique, numeric id of this task.
ID int64 ` xorm:"int(11) autoincr not null unique pk" json:"id" param:"listtask" `
// The task text. This is what you'll see in the list.
2019-03-29 18:54:35 +01:00
Text string ` xorm:"varchar(250) not null" json:"text" valid:"runelength(3|250)" minLength:"3" maxLength:"250" `
2019-01-03 23:22:06 +01:00
// The task description.
2019-07-21 23:57:19 +02:00
Description string ` xorm:"longtext null" json:"description" `
2019-01-14 23:32:56 +01:00
// Whether a task is done or not.
2019-03-29 18:54:35 +01:00
Done bool ` xorm:"INDEX null" json:"done" `
2019-05-22 19:48:48 +02:00
// The unix timestamp when a task was marked as done.
DoneAtUnix int64 ` xorm:"INDEX null" json:"doneAt" `
2019-01-03 23:22:06 +01:00
// A unix timestamp when the task is due.
2019-03-29 18:54:35 +01:00
DueDateUnix int64 ` xorm:"int(11) INDEX null" json:"dueDate" `
2019-01-03 23:22:06 +01:00
// An array of unix timestamps when the user wants to be reminded of the task.
2019-05-25 09:33:57 +02:00
RemindersUnix [ ] int64 ` xorm:"-" json:"reminderDates" `
2019-03-29 18:54:35 +01:00
CreatedByID int64 ` xorm:"int(11) not null" json:"-" ` // ID of the user who put that task on the list
2019-01-03 23:22:06 +01:00
// The list this task belongs to.
2019-04-23 21:53:37 +02:00
ListID int64 ` xorm:"int(11) INDEX not null" json:"listID" param:"list" `
2019-01-03 23:22:06 +01:00
// An amount in seconds this task repeats itself. If this is set, when marking the task as done, it will mark itself as "undone" and then increase all remindes and the due date by its amount.
2019-03-29 18:54:35 +01:00
RepeatAfter int64 ` xorm:"int(11) INDEX null" json:"repeatAfter" `
2019-01-03 23:22:06 +01:00
// If the task is a subtask, this is the id of its parent.
2019-03-29 18:54:35 +01:00
ParentTaskID int64 ` xorm:"int(11) INDEX null" json:"parentTaskID" `
2019-01-03 23:22:06 +01:00
// The task priority. Can be anything you want, it is possible to sort by this later.
2019-03-29 18:54:35 +01:00
Priority int64 ` xorm:"int(11) null" json:"priority" `
2019-01-03 23:22:06 +01:00
// When this task starts.
2019-03-29 18:54:35 +01:00
StartDateUnix int64 ` xorm:"int(11) INDEX null" json:"startDate" query:"-" `
2019-01-03 23:22:06 +01:00
// When this task ends.
2019-03-29 18:54:35 +01:00
EndDateUnix int64 ` xorm:"int(11) INDEX null" json:"endDate" query:"-" `
2019-01-03 23:22:06 +01:00
// An array of users who are assigned to this task
Assignees [ ] * User ` xorm:"-" json:"assignees" `
// An array of labels which are associated with this task.
Labels [ ] * Label ` xorm:"-" json:"labels" `
2019-04-30 11:26:37 +02:00
// The task color in hex
HexColor string ` xorm:"varchar(6) null" json:"hexColor" valid:"runelength(0|6)" maxLength:"6" `
2018-12-01 03:00:57 +01:00
2019-05-22 19:48:48 +02:00
// The UID is currently not used for anything other than caldav, which is why we don't expose it over json
UID string ` xorm:"varchar(250) null" json:"-" `
2019-02-18 19:06:15 +01:00
Sorting string ` xorm:"-" json:"-" query:"sort" ` // Parameter to sort by
StartDateSortUnix int64 ` xorm:"-" json:"-" query:"startdate" `
EndDateSortUnix int64 ` xorm:"-" json:"-" query:"enddate" `
2018-12-22 19:06:14 +01:00
2019-01-03 23:22:06 +01:00
// An array of subtasks.
2019-08-14 22:19:04 +02:00
Subtasks [ ] * Task ` xorm:"-" json:"subtasks" `
2018-11-17 00:17:37 +01:00
2019-01-03 23:22:06 +01:00
// A unix timestamp when this task was created. You cannot change this value.
2019-03-29 18:54:35 +01:00
Created int64 ` xorm:"created not null" json:"created" `
2019-01-03 23:22:06 +01:00
// A unix timestamp when this task was last updated. You cannot change this value.
2019-03-29 18:54:35 +01:00
Updated int64 ` xorm:"updated not null" json:"updated" `
2018-06-10 15:55:56 +02:00
2019-01-03 23:22:06 +01:00
// The user who initially created the task.
2019-08-14 21:59:31 +02:00
CreatedBy * User ` xorm:"-" json:"createdBy" valid:"-" `
2018-07-11 02:13:53 +02:00
2018-12-01 00:26:56 +01:00
web . CRUDable ` xorm:"-" json:"-" `
web . Rights ` xorm:"-" json:"-" `
2018-06-10 15:55:56 +02:00
}
2018-08-30 08:09:17 +02:00
// TableName returns the table name for listtasks
2019-08-14 22:19:04 +02:00
func ( Task ) TableName ( ) string {
2018-08-30 08:09:17 +02:00
return "tasks"
2018-06-10 15:55:56 +02:00
}
2019-05-25 09:33:57 +02:00
// TaskReminder holds a reminder on a task
type TaskReminder struct {
ID int64 ` xorm:"int(11) autoincr not null unique pk" `
TaskID int64 ` xorm:"int(11) not null INDEX" `
ReminderUnix int64 ` xorm:"int(11) not null INDEX" `
Created int64 ` xorm:"created not null" `
}
// TableName returns a pretty table name
func ( TaskReminder ) TableName ( ) string {
return "task_reminders"
}
2019-07-16 16:15:40 +02:00
// SortBy declares constants to sort
type SortBy int
// These are possible sort options
const (
SortTasksByUnsorted SortBy = - 1
SortTasksByDueDateAsc = iota
SortTasksByDueDateDesc
SortTasksByPriorityAsc
SortTasksByPriorityDesc
)
// ReadAll gets all tasks for a user
// @Summary Get tasks
// @Description Returns all tasks on any list the user has access to.
// @tags task
// @Accept json
// @Produce json
// @Param p query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param s query string false "Search tasks by task text."
// @Param sort query string false "The sorting parameter. Possible values to sort by are priority, prioritydesc, priorityasc, duedate, duedatedesc, duedateasc."
// @Param startdate query int false "The start date parameter to filter by. Expects a unix timestamp. If no end date, but a start date is specified, the end date is set to the current time."
// @Param enddate query int false "The end date parameter to filter by. Expects a unix timestamp. If no start date, but an end date is specified, the start date is set to the current time."
// @Security JWTKeyAuth
2019-08-14 22:19:04 +02:00
// @Success 200 {array} models.Task "The tasks"
2019-07-16 16:15:40 +02:00
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/all [get]
2019-08-14 22:19:04 +02:00
func ( t * Task ) ReadAll ( search string , a web . Auth , page int ) ( interface { } , error ) {
2019-07-16 16:15:40 +02:00
var sortby SortBy
switch t . Sorting {
case "priority" :
sortby = SortTasksByPriorityDesc
case "prioritydesc" :
sortby = SortTasksByPriorityDesc
case "priorityasc" :
sortby = SortTasksByPriorityAsc
case "duedate" :
sortby = SortTasksByDueDateDesc
case "duedatedesc" :
sortby = SortTasksByDueDateDesc
case "duedateasc" :
sortby = SortTasksByDueDateAsc
default :
sortby = SortTasksByUnsorted
}
2019-08-31 22:56:41 +02:00
taskopts := & taskOptions {
search : search ,
sortby : sortby ,
startDate : time . Unix ( t . StartDateSortUnix , 0 ) ,
endDate : time . Unix ( t . EndDateSortUnix , 0 ) ,
}
shareAuth , is := a . ( * LinkSharing )
if is {
2019-09-07 15:19:23 +02:00
list := & List { ID : shareAuth . ListID }
err := list . GetSimpleByID ( )
2019-08-31 22:56:41 +02:00
if err != nil {
return nil , err
}
2019-09-07 15:19:23 +02:00
return getTasksForLists ( [ ] * List { list } , taskopts )
2019-08-31 22:56:41 +02:00
}
2019-07-16 16:15:40 +02:00
2019-08-31 22:56:41 +02:00
// Get all lists for the user
lists , err := getRawListsForUser ( "" , & User { ID : a . GetID ( ) } , page )
2019-07-16 16:15:40 +02:00
if err != nil {
return nil , err
}
2019-08-31 22:56:41 +02:00
return getTasksForLists ( lists , taskopts )
}
type taskOptions struct {
search string
sortby SortBy
startDate time . Time
endDate time . Time
}
func getTasksForLists ( lists [ ] * List , opts * taskOptions ) ( tasks [ ] * Task , err error ) {
2019-07-16 16:15:40 +02:00
// Get all list IDs and get the tasks
var listIDs [ ] int64
for _ , l := range lists {
listIDs = append ( listIDs , l . ID )
}
var orderby string
2019-08-31 22:56:41 +02:00
switch opts . sortby {
2019-07-16 16:15:40 +02:00
case SortTasksByPriorityDesc :
orderby = "priority desc"
case SortTasksByPriorityAsc :
orderby = "priority asc"
case SortTasksByDueDateDesc :
orderby = "due_date_unix desc"
case SortTasksByDueDateAsc :
orderby = "due_date_unix asc"
}
2019-08-14 22:19:04 +02:00
taskMap := make ( map [ int64 ] * Task )
2019-07-16 16:15:40 +02:00
// Then return all tasks for that lists
2019-08-31 22:56:41 +02:00
if opts . startDate . Unix ( ) != 0 || opts . endDate . Unix ( ) != 0 {
2019-07-16 16:15:40 +02:00
startDateUnix := time . Now ( ) . Unix ( )
2019-08-31 22:56:41 +02:00
if opts . startDate . Unix ( ) != 0 {
startDateUnix = opts . startDate . Unix ( )
2019-07-16 16:15:40 +02:00
}
endDateUnix := time . Now ( ) . Unix ( )
2019-08-31 22:56:41 +02:00
if opts . endDate . Unix ( ) != 0 {
endDateUnix = opts . endDate . Unix ( )
2019-07-16 16:15:40 +02:00
}
if err := x . In ( "list_id" , listIDs ) .
2019-08-31 22:56:41 +02:00
Where ( "text LIKE ?" , "%" + opts . search + "%" ) .
2019-07-16 16:15:40 +02:00
And ( "((due_date_unix BETWEEN ? AND ?) OR " +
"(start_date_unix BETWEEN ? and ?) OR " +
"(end_date_unix BETWEEN ? and ?))" , startDateUnix , endDateUnix , startDateUnix , endDateUnix , startDateUnix , endDateUnix ) .
And ( "(parent_task_id = 0 OR parent_task_id IS NULL)" ) .
OrderBy ( orderby ) .
Find ( & taskMap ) ; err != nil {
return nil , err
}
} else {
if err := x . In ( "list_id" , listIDs ) .
2019-08-31 22:56:41 +02:00
Where ( "text LIKE ?" , "%" + opts . search + "%" ) .
2019-07-16 16:15:40 +02:00
And ( "(parent_task_id = 0 OR parent_task_id IS NULL)" ) .
OrderBy ( orderby ) .
Find ( & taskMap ) ; err != nil {
return nil , err
}
}
2019-08-31 22:56:41 +02:00
tasks , err = addMoreInfoToTasks ( taskMap )
2019-07-16 16:15:40 +02:00
if err != nil {
return nil , err
}
// Because the list is sorted by id which we don't want (since we're dealing with maps)
// we have to manually sort the tasks again here.
2019-08-31 22:56:41 +02:00
sortTasks ( tasks , opts . sortby )
2019-07-16 16:15:40 +02:00
return tasks , err
}
2019-08-14 22:19:04 +02:00
func sortTasks ( tasks [ ] * Task , by SortBy ) {
2019-07-16 16:15:40 +02:00
switch by {
case SortTasksByPriorityDesc :
sort . Slice ( tasks , func ( i , j int ) bool {
return tasks [ i ] . Priority > tasks [ j ] . Priority
} )
case SortTasksByPriorityAsc :
sort . Slice ( tasks , func ( i , j int ) bool {
return tasks [ i ] . Priority < tasks [ j ] . Priority
} )
case SortTasksByDueDateDesc :
sort . Slice ( tasks , func ( i , j int ) bool {
return tasks [ i ] . DueDateUnix > tasks [ j ] . DueDateUnix
} )
case SortTasksByDueDateAsc :
sort . Slice ( tasks , func ( i , j int ) bool {
return tasks [ i ] . DueDateUnix < tasks [ j ] . DueDateUnix
} )
}
}
2018-08-30 08:09:17 +02:00
// GetTasksByListID gets all todotasks for a list
2019-08-14 22:19:04 +02:00
func GetTasksByListID ( listID int64 ) ( tasks [ ] * Task , err error ) {
2018-12-31 02:18:41 +01:00
// make a map so we can put in a lot of other stuff more easily
2019-08-14 22:19:04 +02:00
taskMap := make ( map [ int64 ] * Task , len ( tasks ) )
2018-12-31 02:18:41 +01:00
err = x . Where ( "list_id = ?" , listID ) . Find ( & taskMap )
2018-06-10 19:49:40 +02:00
if err != nil {
return
}
2019-05-22 19:48:48 +02:00
tasks , err = addMoreInfoToTasks ( taskMap )
2018-06-10 15:55:56 +02:00
return
}
2018-06-12 18:07:47 +02:00
2019-05-22 19:48:48 +02:00
// GetTaskByIDSimple returns a raw task without extra data by the task ID
2019-08-14 22:19:04 +02:00
func GetTaskByIDSimple ( taskID int64 ) ( task Task , err error ) {
2018-12-31 02:18:41 +01:00
if taskID < 1 {
2019-08-14 22:19:04 +02:00
return Task { } , ErrTaskDoesNotExist { taskID }
2018-09-13 20:07:11 +02:00
}
2019-08-14 22:19:04 +02:00
return GetTaskSimple ( & Task { ID : taskID } )
2019-05-22 19:48:48 +02:00
}
// GetTaskSimple returns a raw task without extra data
2019-08-14 22:19:04 +02:00
func GetTaskSimple ( t * Task ) ( task Task , err error ) {
2019-05-22 19:48:48 +02:00
task = * t
exists , err := x . Get ( & task )
2018-06-12 18:35:36 +02:00
if err != nil {
2019-08-14 22:19:04 +02:00
return Task { } , err
2018-06-12 18:35:36 +02:00
}
if ! exists {
2019-08-14 22:19:04 +02:00
return Task { } , ErrTaskDoesNotExist { t . ID }
2018-12-31 02:18:41 +01:00
}
return
}
2019-05-22 19:48:48 +02:00
// GetTaskByID returns all tasks a list has
2019-08-14 22:19:04 +02:00
func GetTaskByID ( listTaskID int64 ) ( listTask Task , err error ) {
2019-05-22 19:48:48 +02:00
listTask , err = GetTaskByIDSimple ( listTaskID )
2018-12-31 02:18:41 +01:00
if err != nil {
return
2018-06-12 18:35:36 +02:00
}
2018-10-31 13:42:38 +01:00
u , err := GetUserByID ( listTask . CreatedByID )
2018-06-12 19:57:38 +02:00
if err != nil {
return
}
2018-10-31 13:42:38 +01:00
listTask . CreatedBy = u
2018-06-12 19:57:38 +02:00
2018-12-29 15:29:50 +01:00
// Get assignees
taskAssignees , err := getRawTaskAssigneesForTasks ( [ ] int64 { listTaskID } )
if err != nil {
return
}
for _ , u := range taskAssignees {
if u != nil {
listTask . Assignees = append ( listTask . Assignees , & u . User )
}
}
2019-01-10 00:08:12 +01:00
// Get task labels
taskLabels , err := getLabelsByTaskIDs ( & LabelByTaskIDsOptions {
TaskIDs : [ ] int64 { listTaskID } ,
} )
if err != nil {
return
}
for _ , label := range taskLabels {
listTask . Labels = append ( listTask . Labels , & label . Label )
}
2018-06-12 18:35:36 +02:00
return
}
2018-12-28 22:49:46 +01:00
// GetTasksByIDs returns all tasks for a list of ids
func ( bt * BulkTask ) GetTasksByIDs ( ) ( err error ) {
for _ , id := range bt . IDs {
if id < 1 {
2019-08-14 22:19:04 +02:00
return ErrTaskDoesNotExist { id }
2018-12-28 22:49:46 +01:00
}
}
2019-08-14 22:19:04 +02:00
taskMap := make ( map [ int64 ] * Task , len ( bt . Tasks ) )
2019-05-22 19:48:48 +02:00
err = x . In ( "id" , bt . IDs ) . Find ( & taskMap )
2018-12-28 22:49:46 +01:00
if err != nil {
2019-05-22 19:48:48 +02:00
return
2018-12-28 22:49:46 +01:00
}
2019-05-22 19:48:48 +02:00
bt . Tasks , err = addMoreInfoToTasks ( taskMap )
return
}
// GetTasksByUIDs gets all tasks from a bunch of uids
2019-08-14 22:19:04 +02:00
func GetTasksByUIDs ( uids [ ] string ) ( tasks [ ] * Task , err error ) {
taskMap := make ( map [ int64 ] * Task )
2019-05-22 19:48:48 +02:00
err = x . In ( "uid" , uids ) . Find ( & taskMap )
if err != nil {
return
2018-12-28 22:49:46 +01:00
}
2019-05-22 19:48:48 +02:00
tasks , err = addMoreInfoToTasks ( taskMap )
return
}
// This function takes a map with pointers and returns a slice with pointers to tasks
// It adds more stuff like assignees/labels/etc to a bunch of tasks
2019-08-14 22:19:04 +02:00
func addMoreInfoToTasks ( taskMap map [ int64 ] * Task ) ( tasks [ ] * Task , err error ) {
2019-05-22 19:48:48 +02:00
// No need to iterate over users and stuff if the list doesn't has tasks
if len ( taskMap ) == 0 {
return
2018-12-28 22:49:46 +01:00
}
2019-05-22 19:48:48 +02:00
// Get all users & task ids and put them into the array
var userIDs [ ] int64
var taskIDs [ ] int64
for _ , i := range taskMap {
taskIDs = append ( taskIDs , i . ID )
userIDs = append ( userIDs , i . CreatedByID )
}
// Get all assignees
taskAssignees , err := getRawTaskAssigneesForTasks ( taskIDs )
2018-12-28 22:49:46 +01:00
if err != nil {
2019-05-22 19:48:48 +02:00
return
}
// Put the assignees in the task map
for _ , a := range taskAssignees {
if a != nil {
2019-08-14 21:59:31 +02:00
a . Email = "" // Obfuscate the email
2019-05-22 19:48:48 +02:00
taskMap [ a . TaskID ] . Assignees = append ( taskMap [ a . TaskID ] . Assignees , & a . User )
}
2018-12-28 22:49:46 +01:00
}
2019-05-22 19:48:48 +02:00
// Get all labels for all the tasks
labels , err := getLabelsByTaskIDs ( & LabelByTaskIDsOptions { TaskIDs : taskIDs } )
if err != nil {
return
}
for _ , l := range labels {
if l != nil {
taskMap [ l . TaskID ] . Labels = append ( taskMap [ l . TaskID ] . Labels , & l . Label )
}
}
// Get all users of a task
// aka the ones who created a task
users := make ( map [ int64 ] * User )
err = x . In ( "id" , userIDs ) . Find ( & users )
if err != nil {
return
}
2019-08-14 21:59:31 +02:00
// Obfuscate all user emails
for _ , u := range users {
u . Email = ""
}
2019-05-25 09:33:57 +02:00
// Get all reminders and put them in a map to have it easier later
reminders := [ ] * TaskReminder { }
err = x . Table ( "task_reminders" ) . In ( "task_id" , taskIDs ) . Find ( & reminders )
if err != nil {
return
}
taskRemindersUnix := make ( map [ int64 ] [ ] int64 )
for _ , r := range reminders {
taskRemindersUnix [ r . TaskID ] = append ( taskRemindersUnix [ r . TaskID ] , r . ReminderUnix )
}
2019-05-22 19:48:48 +02:00
// Add all user objects to the appropriate tasks
for _ , task := range taskMap {
// Make created by user objects
2019-08-14 21:59:31 +02:00
taskMap [ task . ID ] . CreatedBy = users [ task . CreatedByID ]
2019-05-22 19:48:48 +02:00
2019-05-25 09:33:57 +02:00
// Add the reminders
taskMap [ task . ID ] . RemindersUnix = taskRemindersUnix [ task . ID ]
2019-05-22 19:48:48 +02:00
// Reorder all subtasks
if task . ParentTaskID != 0 {
taskMap [ task . ParentTaskID ] . Subtasks = append ( taskMap [ task . ParentTaskID ] . Subtasks , task )
delete ( taskMap , task . ID )
2018-12-28 22:49:46 +01:00
}
}
2019-05-22 19:48:48 +02:00
// make a complete slice from the map
2019-08-14 22:19:04 +02:00
tasks = [ ] * Task { }
2019-05-22 19:48:48 +02:00
for _ , t := range taskMap {
tasks = append ( tasks , t )
}
// Sort the output. In Go, contents on a map are put on that map in no particular order.
// Because of this, tasks are not sorted anymore in the output, this leads to confiusion.
// To avoid all this, we need to sort the slice afterwards
sort . Slice ( tasks , func ( i , j int ) bool {
return tasks [ i ] . ID < tasks [ j ] . ID
} )
2018-12-28 22:49:46 +01:00
return
}
2019-07-16 16:15:40 +02:00
// Create is the implementation to create a list task
// @Summary Create a task
// @Description Inserts a task into a list.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "List ID"
2019-08-14 22:19:04 +02:00
// @Param task body models.Task true "The task object"
// @Success 200 {object} models.Task "The created task object."
2019-07-16 16:15:40 +02:00
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid task object provided."
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the list"
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id} [put]
2019-08-14 22:19:04 +02:00
func ( t * Task ) Create ( a web . Auth ) ( err error ) {
2019-07-16 16:15:40 +02:00
t . ID = 0
// Check if we have at least a text
if t . Text == "" {
2019-08-14 22:19:04 +02:00
return ErrTaskCannotBeEmpty { }
2019-07-16 16:15:40 +02:00
}
// Check if the list exists
l := & List { ID : t . ListID }
if err = l . GetSimpleByID ( ) ; err != nil {
return
}
u , err := GetUserByID ( a . GetID ( ) )
if err != nil {
return err
}
// Generate a uuid if we don't already have one
if t . UID == "" {
t . UID = utils . MakeRandomString ( 40 )
}
t . CreatedByID = u . ID
t . CreatedBy = u
if _ , err = x . Insert ( t ) ; err != nil {
return err
}
// Update the assignees
if err := t . updateTaskAssignees ( t . Assignees ) ; err != nil {
return err
}
// Update the reminders
if err := t . updateReminders ( t . RemindersUnix ) ; err != nil {
return err
}
metrics . UpdateCount ( 1 , metrics . TaskCountKey )
err = updateListLastUpdated ( & List { ID : t . ListID } )
return
}
// Update updates a list task
// @Summary Update a task
// @Description Updates a task. This includes marking it as done. Assignees you pass will be updated, see their individual endpoints for more details on how this is done. To update labels, see the description of the endpoint.
// @tags task
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Task ID"
2019-08-14 22:19:04 +02:00
// @Param task body models.Task true "The task object"
// @Success 200 {object} models.Task "The updated task object."
2019-07-16 16:15:40 +02:00
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid task object provided."
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the task (aka its list)"
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{id} [post]
2019-08-14 22:19:04 +02:00
func ( t * Task ) Update ( ) ( err error ) {
2019-07-16 16:15:40 +02:00
// Check if the task exists
ot , err := GetTaskByID ( t . ID )
if err != nil {
return
}
// Parent task cannot be the same as the current task
if t . ID == t . ParentTaskID {
return ErrParentTaskCannotBeTheSame { TaskID : t . ID }
}
// When a repeating task is marked as done, we update all deadlines and reminders and set it as undone
updateDone ( & ot , t )
// Update the assignees
if err := ot . updateTaskAssignees ( t . Assignees ) ; err != nil {
return err
}
// Update the reminders
if err := ot . updateReminders ( t . RemindersUnix ) ; err != nil {
return err
}
// Update the labels
//
// Maybe FIXME:
// I've disabled this for now, because it requires significant changes in the way we do updates (using the
// Update() function. We need a user object in updateTaskLabels to check if the user has the right to see
// the label it is currently adding. To do this, we'll need to update the webhandler to let it pass the current
// user object (like it's already the case with the create method). However when we change it, that'll break
// a lot of existing code which we'll then need to refactor.
// This is why.
//
//if err := ot.updateTaskLabels(t.Labels); err != nil {
// return err
//}
// set the labels to ot.Labels because our updateTaskLabels function puts the full label objects in it pretty nicely
// We also set this here to prevent it being overwritten later on.
//t.Labels = ot.Labels
// For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand
// Which is why we merge the actual task struct with the one we got from the
// The user struct overrides values in the actual one.
if err := mergo . Merge ( & ot , t , mergo . WithOverride ) ; err != nil {
return err
}
//////
// Mergo does ignore nil values. Because of that, we need to check all parameters and set the updated to
// nil/their nil value in the struct which is inserted.
////
// Done
if ! t . Done {
ot . Done = false
}
// Priority
if t . Priority == 0 {
ot . Priority = 0
}
// Description
if t . Description == "" {
ot . Description = ""
}
// Due date
if t . DueDateUnix == 0 {
ot . DueDateUnix = 0
}
// Repeat after
if t . RepeatAfter == 0 {
ot . RepeatAfter = 0
}
// Parent task
if t . ParentTaskID == 0 {
ot . ParentTaskID = 0
}
// Start date
if t . StartDateUnix == 0 {
ot . StartDateUnix = 0
}
// End date
if t . EndDateUnix == 0 {
ot . EndDateUnix = 0
}
// Color
if t . HexColor == "" {
ot . HexColor = ""
}
_ , err = x . ID ( t . ID ) .
Cols ( "text" ,
"description" ,
"done" ,
"due_date_unix" ,
"repeat_after" ,
"parent_task_id" ,
"priority" ,
"start_date_unix" ,
"end_date_unix" ,
"hex_color" ,
"done_at_unix" ) .
Update ( ot )
* t = ot
if err != nil {
return err
}
err = updateListLastUpdated ( & List { ID : t . ListID } )
return
}
// This helper function updates the reminders and doneAtUnix of the *old* task (since that's the one we're inserting
// with updated values into the db)
2019-08-14 22:19:04 +02:00
func updateDone ( oldTask * Task , newTask * Task ) {
2019-07-16 16:15:40 +02:00
if ! oldTask . Done && newTask . Done && oldTask . RepeatAfter > 0 {
oldTask . DueDateUnix = oldTask . DueDateUnix + oldTask . RepeatAfter // assuming we'll save the old task (merged)
for in , r := range oldTask . RemindersUnix {
oldTask . RemindersUnix [ in ] = r + oldTask . RepeatAfter
}
newTask . Done = false
}
// Update the "done at" timestamp
if ! oldTask . Done && newTask . Done {
oldTask . DoneAtUnix = time . Now ( ) . Unix ( )
}
// When unmarking a task as done, reset the timestamp
if oldTask . Done && ! newTask . Done {
oldTask . DoneAtUnix = 0
}
}
// Creates or deletes all necessary remindes without unneded db operations.
// The parameter is a slice with unix dates which holds the new reminders.
2019-08-14 22:19:04 +02:00
func ( t * Task ) updateReminders ( reminders [ ] int64 ) ( err error ) {
2019-07-16 16:15:40 +02:00
// If we're removing everything, delete all reminders right away
if len ( reminders ) == 0 && len ( t . RemindersUnix ) > 0 {
_ , err = x . Where ( "task_id = ?" , t . ID ) .
Delete ( TaskReminder { } )
t . RemindersUnix = nil
return err
}
// If we didn't change anything (from 0 to zero) don't do anything.
if len ( reminders ) == 0 && len ( t . RemindersUnix ) == 0 {
return nil
}
// Make a hashmap of the new reminders for easier comparison
newReminders := make ( map [ int64 ] * TaskReminder , len ( reminders ) )
for _ , newReminder := range reminders {
newReminders [ newReminder ] = & TaskReminder { ReminderUnix : newReminder }
}
// Get old reminders to delete
var found bool
var remindersToDelete [ ] int64
oldReminders := make ( map [ int64 ] * TaskReminder , len ( t . RemindersUnix ) )
for _ , oldReminder := range t . RemindersUnix {
found = false
// If a new reminder is already in the list with old reminders
if newReminders [ oldReminder ] != nil {
found = true
}
// Put all reminders which are only on the old list to the trash
if ! found {
remindersToDelete = append ( remindersToDelete , oldReminder )
}
oldReminders [ oldReminder ] = & TaskReminder { ReminderUnix : oldReminder }
}
// Delete all reminders not passed
if len ( remindersToDelete ) > 0 {
_ , err = x . In ( "reminder_unix" , remindersToDelete ) .
And ( "task_id = ?" , t . ID ) .
2019-08-14 22:19:04 +02:00
Delete ( TaskAssginee { } )
2019-07-16 16:15:40 +02:00
if err != nil {
return err
}
}
// Loop through our users and add them
for _ , r := range reminders {
// Check if the reminder already exists and only inserts it if not
if oldReminders [ r ] != nil {
// continue outer loop
continue
}
// Add the new reminder
_ , err = x . Insert ( TaskReminder { TaskID : t . ID , ReminderUnix : r } )
if err != nil {
return err
}
}
t . RemindersUnix = reminders
if len ( reminders ) == 0 {
t . RemindersUnix = nil
}
err = updateListLastUpdated ( & List { ID : t . ListID } )
return
}
// Delete implements the delete method for listTask
// @Summary Delete a task
// @Description Deletes a task from a list. This does not mean "mark it done".
// @tags task
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "Task ID"
// @Success 200 {object} models.Message "The created task object."
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid task ID provided."
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the list"
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{id} [delete]
2019-08-14 22:19:04 +02:00
func ( t * Task ) Delete ( ) ( err error ) {
2019-07-16 16:15:40 +02:00
// Check if it exists
_ , err = GetTaskByID ( t . ID )
if err != nil {
return
}
2019-08-14 22:19:04 +02:00
if _ , err = x . ID ( t . ID ) . Delete ( Task { } ) ; err != nil {
2019-07-16 16:15:40 +02:00
return err
}
// Delete assignees
2019-08-14 22:19:04 +02:00
if _ , err = x . Where ( "task_id = ?" , t . ID ) . Delete ( TaskAssginee { } ) ; err != nil {
2019-07-16 16:15:40 +02:00
return err
}
metrics . UpdateCount ( - 1 , metrics . TaskCountKey )
err = updateListLastUpdated ( & List { ID : t . ListID } )
return
}