2020-02-07 17:27:45 +01:00
// Vikunja is a to-do list application to facilitate your life.
2020-01-09 18:33:22 +01:00
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
2018-11-26 21:17:33 +01:00
//
2019-12-04 20:39:56 +01:00
// 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.
2018-11-26 21:17:33 +01:00
//
2019-12-04 20:39:56 +01:00
// 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.
2018-11-26 21:17:33 +01:00
//
2019-12-04 20:39:56 +01:00
// 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-11-26 21:17:33 +01:00
2018-06-10 15:55:56 +02:00
package models
2018-12-02 01:49:30 +01:00
import (
2020-05-15 16:12:20 +02:00
"code.vikunja.io/api/pkg/config"
2019-07-16 16:15:40 +02:00
"code.vikunja.io/api/pkg/metrics"
2020-01-26 18:08:06 +01:00
"code.vikunja.io/api/pkg/user"
2019-07-16 16:15:40 +02:00
"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"
2020-04-24 17:23:03 +02:00
"math"
2020-06-14 13:04:38 +02:00
"sort"
2019-12-07 23:28:45 +01:00
"strconv"
2019-07-16 16:15:40 +02:00
"time"
2020-04-11 16:20:33 +02:00
"xorm.io/builder"
2020-04-24 17:23:03 +02:00
"xorm.io/xorm/schemas"
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.
2020-06-17 18:52:23 +02:00
Title string ` xorm:"varchar(250) not null" json:"title" valid:"runelength(1|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.
2020-04-12 19:29:24 +02:00
Done bool ` xorm:"INDEX null" json:"done" `
2020-02-08 13:48:49 +01:00
// The time when a task was marked as done.
2020-06-27 19:04:01 +02:00
DoneAt time . Time ` xorm:"INDEX null 'done_at'" json:"done_at" `
2020-02-08 13:48:49 +01:00
// The time when the task is due.
2020-06-27 19:04:01 +02:00
DueDate time . Time ` xorm:"DATETIME INDEX null 'due_date'" json:"due_date" `
2020-02-08 13:48:49 +01:00
// An array of datetimes when the user wants to be reminded of the task.
2020-06-27 19:04:01 +02:00
Reminders [ ] time . Time ` xorm:"-" json:"reminder_dates" `
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.
2020-04-12 22:48:46 +02:00
ListID int64 ` xorm:"int(11) INDEX not null" json:"list_id" 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.
2020-04-12 22:48:46 +02:00
RepeatAfter int64 ` xorm:"int(11) INDEX null" json:"repeat_after" `
2020-06-14 13:04:38 +02:00
// If specified, a repeating task will repeat from the current date rather than the last set date.
RepeatFromCurrentDate bool ` xorm:"null" json:"repeat_from_current_date" `
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.
2020-06-27 19:04:01 +02:00
StartDate time . Time ` xorm:"DATETIME INDEX null 'start_date'" json:"start_date" query:"-" `
2019-01-03 23:22:06 +01:00
// When this task ends.
2020-06-27 19:04:01 +02:00
EndDate time . Time ` xorm:"DATETIME INDEX null 'end_date'" json:"end_date" query:"-" `
2019-01-03 23:22:06 +01:00
// An array of users who are assigned to this task
2020-01-26 18:08:06 +01:00
Assignees [ ] * user . User ` xorm:"-" json:"assignees" `
2019-01-03 23:22:06 +01:00
// 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
2020-04-12 22:48:46 +02:00
HexColor string ` xorm:"varchar(6) null" json:"hex_color" valid:"runelength(0|6)" maxLength:"6" `
2019-09-21 12:52:10 +02:00
// Determines how far a task is left from being done
2020-04-12 22:48:46 +02:00
PercentDone float64 ` xorm:"DOUBLE null" json:"percent_done" `
2018-12-01 03:00:57 +01:00
2019-12-07 23:28:45 +01:00
// The task identifier, based on the list identifier and the task's index
Identifier string ` xorm:"-" json:"identifier" `
// The task index, calculated per list
Index int64 ` xorm:"int(11) not null default 0" json:"index" `
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-09-25 20:44:41 +02:00
// All related tasks, grouped by their relation kind
RelatedTasks RelatedTaskMap ` xorm:"-" json:"related_tasks" `
2018-11-17 00:17:37 +01:00
2019-10-16 22:52:29 +02:00
// All attachments this task has
Attachments [ ] * TaskAttachment ` xorm:"-" json:"attachments" `
2020-02-08 13:48:49 +01:00
// A timestamp when this task was created. You cannot change this value.
2020-06-27 19:04:01 +02:00
Created time . Time ` xorm:"created not null" json:"created" `
2020-02-08 13:48:49 +01:00
// A timestamp when this task was last updated. You cannot change this value.
2020-06-27 19:04:01 +02:00
Updated time . Time ` xorm:"updated not null" json:"updated" `
2018-06-10 15:55:56 +02:00
2020-04-19 09:27:28 +02:00
// BucketID is the ID of the kanban bucket this task belongs to.
BucketID int64 ` xorm:"int(11) null" json:"bucket_id" `
2020-04-24 17:23:03 +02:00
// The position of the task - any task list can be sorted as usual by this parameter.
// When accessing tasks via kanban buckets, this is primarily used to sort them based on a range
// We're using a float64 here to make it possible to put any task within any two other tasks (by changing the number).
// You would calculate the new position between two tasks with something like task3.position = (task2.position - task1.position) / 2.
// A 64-Bit float leaves plenty of room to initially give tasks a position with 2^16 difference to the previous task
// which also leaves a lot of room for rearranging and sorting later.
Position float64 ` xorm:"double null" json:"position" `
2019-01-03 23:22:06 +01:00
// The user who initially created the task.
2020-04-12 22:48:46 +02:00
CreatedBy * user . User ` xorm:"-" json:"created_by" 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 {
2020-06-27 19:04:01 +02:00
ID int64 ` xorm:"int(11) autoincr not null unique pk" `
TaskID int64 ` xorm:"int(11) not null INDEX" `
Reminder time . Time ` xorm:"DATETIME not null INDEX 'reminder'" `
Created time . Time ` xorm:"created not null" `
2019-05-25 09:33:57 +02:00
}
// TableName returns a pretty table name
func ( TaskReminder ) TableName ( ) string {
return "task_reminders"
}
2020-05-09 17:48:56 +02:00
type taskFilterConcatinator string
const (
filterConcatAnd = "and"
filterConcatOr = "or"
)
2019-12-01 14:38:11 +01:00
type taskOptions struct {
2020-06-27 19:04:01 +02:00
search string
page int
perPage int
sortby [ ] * sortParam
filters [ ] * taskFilter
filterConcat taskFilterConcatinator
filterIncludeNulls bool
2019-12-01 14:38:11 +01:00
}
// ReadAll is a dummy function to still have that endpoint documented
2019-07-16 16:15:40 +02:00
// @Summary Get tasks
// @Description Returns all tasks on any list the user has access to.
// @tags task
// @Accept json
// @Produce json
2019-10-23 23:11:40 +02:00
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
2019-07-16 16:15:40 +02:00
// @Param s query string false "Search tasks by task text."
2020-06-27 19:04:01 +02:00
// @Param sort_by query string false "The sorting parameter. You can pass this multiple times to get the tasks ordered by multiple different parametes, along with `order_by`. Possible values to sort by are `id`, `text`, `description`, `done`, `done_at`, `due_date`, `created_by_id`, `list_id`, `repeat_after`, `priority`, `start_date`, `end_date`, `hex_color`, `percent_done`, `uid`, `created`, `updated`. Default is `id`."
2020-04-11 16:20:33 +02:00
// @Param order_by query string false "The ordering parameter. Possible values to order by are `asc` or `desc`. Default is `asc`."
// @Param filter_by query string false "The name of the field to filter by. Accepts an array for multiple filters which will be chanied together, all supplied filter must match."
// @Param filter_value query string false "The value to filter for."
// @Param filter_comparator query string false "The comparator to use for a filter. Available values are `equals`, `greater`, `greater_equals`, `less` and `less_equals`. Defaults to `equals`"
2020-05-09 17:48:56 +02:00
// @Param filter_concat query string false "The concatinator to use for filters. Available values are `and` or `or`. Defaults to `or`."
2019-07-16 16:15:40 +02:00
// @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-10-23 23:11:40 +02:00
func ( t * Task ) ReadAll ( a web . Auth , search string , page int , perPage int ) ( result interface { } , resultCount int , totalItems int64 , err error ) {
2019-12-01 14:38:11 +01:00
return nil , 0 , 0 , nil
2019-08-31 22:56:41 +02:00
}
2020-04-24 17:23:03 +02:00
func getRawTasksForLists ( lists [ ] * List , opts * taskOptions ) ( tasks [ ] * Task , resultCount int , totalItems int64 , err error ) {
2019-10-20 17:55:54 +02:00
2020-05-19 17:22:10 +02:00
// If the user does not have any lists, don't try to get any tasks
if len ( lists ) == 0 {
return nil , 0 , 0 , nil
}
2020-05-09 17:48:56 +02:00
// Set the default concatinator of filter variables to or if none was provided
if opts . filterConcat == "" {
opts . filterConcat = filterConcatOr
}
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 )
}
2020-04-24 17:23:03 +02:00
// Add the id parameter as the last parameter to sorty by default, but only if it is not already passed as the last parameter.
if len ( opts . sortby ) == 0 ||
len ( opts . sortby ) > 0 && opts . sortby [ len ( opts . sortby ) - 1 ] . sortBy != taskPropertyID {
opts . sortby = append ( opts . sortby , & sortParam {
sortBy : taskPropertyID ,
orderBy : orderAscending ,
} )
}
2019-12-07 15:30:51 +01:00
// Since xorm does not use placeholders for order by, it is possible to expose this with sql injection if we're directly
// passing user input to the db.
// As a workaround to prevent this, we check for valid column names here prior to passing it to the db.
2019-07-16 16:15:40 +02:00
var orderby string
2019-12-07 15:30:51 +01:00
for i , param := range opts . sortby {
// Validate the params
if err := param . validate ( ) ; err != nil {
return nil , 0 , 0 , err
}
2020-04-24 17:23:03 +02:00
orderby += param . sortBy + " " + param . orderBy . String ( )
// Postgres sorts by default entries with null values after ones with values.
// To make that consistent with the sort order we have and other dbms, we're adding a separate clause here.
if x . Dialect ( ) . URI ( ) . DBType == schemas . POSTGRES {
if param . orderBy == orderAscending {
orderby += " NULLS FIRST"
}
if param . orderBy == orderDescending {
orderby += " NULLS LAST"
}
}
2019-12-07 15:30:51 +01:00
if ( i + 1 ) < len ( opts . sortby ) {
orderby += ", "
}
2019-07-16 16:15:40 +02:00
}
2020-04-11 16:20:33 +02:00
var filters = make ( [ ] builder . Cond , 0 , len ( opts . filters ) )
2020-05-09 17:48:56 +02:00
// To still find tasks with nil values, we exclude 0s when comparing with >/< values.
2020-04-11 16:20:33 +02:00
for _ , f := range opts . filters {
switch f . comparator {
case taskFilterComparatorEquals :
filters = append ( filters , & builder . Eq { f . field : f . value } )
case taskFilterComparatorNotEquals :
filters = append ( filters , & builder . Neq { f . field : f . value } )
case taskFilterComparatorGreater :
2020-06-27 19:04:01 +02:00
if opts . filterIncludeNulls {
filters = append ( filters , builder . Or ( & builder . Gt { f . field : f . value } , & builder . IsNull { f . field } ) )
} else {
filters = append ( filters , & builder . Gt { f . field : f . value } )
}
2020-04-11 16:20:33 +02:00
case taskFilterComparatorGreateEquals :
2020-06-27 19:04:01 +02:00
if opts . filterIncludeNulls {
filters = append ( filters , builder . Or ( & builder . Gte { f . field : f . value } , & builder . IsNull { f . field } ) )
} else {
filters = append ( filters , & builder . Gte { f . field : f . value } )
}
2020-04-11 16:20:33 +02:00
case taskFilterComparatorLess :
2020-06-27 19:04:01 +02:00
if opts . filterIncludeNulls {
filters = append ( filters , builder . Or ( & builder . Lt { f . field : f . value } , & builder . IsNull { f . field } ) )
} else {
filters = append ( filters , & builder . Lt { f . field : f . value } )
}
2020-04-11 16:20:33 +02:00
case taskFilterComparatorLessEquals :
2020-06-27 19:04:01 +02:00
if opts . filterIncludeNulls {
filters = append ( filters , builder . Or ( & builder . Lte { f . field : f . value } , & builder . IsNull { f . field } ) )
} else {
filters = append ( filters , & builder . Lte { f . field : f . value } )
}
2020-04-11 16:20:33 +02:00
}
}
2019-07-16 16:15:40 +02:00
// Then return all tasks for that lists
2020-04-27 19:28:19 +02:00
query := x . NewSession ( ) .
2020-04-12 19:29:24 +02:00
OrderBy ( orderby )
2020-04-27 19:28:19 +02:00
queryCount := x . NewSession ( )
2020-04-12 19:29:24 +02:00
if len ( opts . search ) > 0 {
2020-05-15 16:12:20 +02:00
// Postgres' is case sensitive by default.
// To work around this, we're using ILIKE as opposed to normal LIKE statements.
// ILIKE is preferred over LOWER(text) LIKE for performance reasons.
// See https://stackoverflow.com/q/7005302/10924593
// Seems okay to use that now, we may need to find a better solution overall in the future.
if config . DatabaseType . GetString ( ) == "postgres" {
2020-05-16 12:17:44 +02:00
query = query . Where ( "title ILIKE ?" , "%" + opts . search + "%" )
queryCount = queryCount . Where ( "title ILIKE ?" , "%" + opts . search + "%" )
2020-05-15 16:12:20 +02:00
} else {
2020-05-16 12:17:44 +02:00
query = query . Where ( "title LIKE ?" , "%" + opts . search + "%" )
queryCount = queryCount . Where ( "title LIKE ?" , "%" + opts . search + "%" )
2020-05-15 16:12:20 +02:00
}
2020-04-12 19:29:24 +02:00
}
if len ( listIDs ) > 0 {
query = query . In ( "list_id" , listIDs )
2020-04-27 19:28:19 +02:00
queryCount = queryCount . In ( "list_id" , listIDs )
2020-04-12 19:29:24 +02:00
}
2020-04-11 16:20:33 +02:00
if len ( filters ) > 0 {
2020-05-09 17:48:56 +02:00
if opts . filterConcat == filterConcatOr {
query = query . Where ( builder . Or ( filters ... ) )
queryCount = queryCount . Where ( builder . Or ( filters ... ) )
}
if opts . filterConcat == filterConcatAnd {
query = query . Where ( builder . And ( filters ... ) )
queryCount = queryCount . Where ( builder . And ( filters ... ) )
}
2020-04-12 19:29:24 +02:00
}
2019-07-16 16:15:40 +02:00
2020-04-12 19:29:24 +02:00
limit , start := getLimitFromPageIndex ( opts . page , opts . perPage )
2019-10-23 23:11:40 +02:00
2020-04-12 19:29:24 +02:00
if limit > 0 {
query = query . Limit ( limit , start )
}
2020-04-24 17:23:03 +02:00
tasks = [ ] * Task { }
err = query . Find ( & tasks )
2020-04-12 19:29:24 +02:00
if err != nil {
return nil , 0 , 0 , err
2019-07-16 16:15:40 +02:00
}
2020-04-12 19:29:24 +02:00
2020-04-27 19:28:19 +02:00
totalItems , err = queryCount .
2020-04-12 19:29:24 +02:00
Count ( & Task { } )
if err != nil {
return nil , 0 , 0 , err
}
2020-04-24 17:23:03 +02:00
return tasks , len ( tasks ) , totalItems , nil
2019-10-20 17:55:54 +02:00
}
2019-10-23 23:11:40 +02:00
func getTasksForLists ( lists [ ] * List , opts * taskOptions ) ( tasks [ ] * Task , resultCount int , totalItems int64 , err error ) {
2019-10-20 17:55:54 +02:00
2020-04-24 17:23:03 +02:00
tasks , resultCount , totalItems , err = getRawTasksForLists ( lists , opts )
2019-10-20 17:55:54 +02:00
if err != nil {
2019-10-23 23:11:40 +02:00
return nil , 0 , 0 , err
2019-10-20 17:55:54 +02:00
}
2019-07-16 16:15:40 +02:00
2020-04-24 17:23:03 +02:00
taskMap := make ( map [ int64 ] * Task , len ( tasks ) )
for _ , t := range tasks {
taskMap [ t . ID ] = t
}
err = addMoreInfoToTasks ( taskMap )
2019-07-16 16:15:40 +02:00
if err != nil {
2019-10-23 23:11:40 +02:00
return nil , 0 , 0 , err
2019-07-16 16:15:40 +02:00
}
2019-10-23 23:11:40 +02:00
return tasks , resultCount , totalItems , err
2019-07-16 16:15:40 +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
}
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
}
}
2020-04-24 17:23:03 +02:00
err = x . In ( "id" , bt . IDs ) . Find ( & bt . Tasks )
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
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 ) {
2020-04-24 17:23:03 +02:00
tasks = [ ] * Task { }
err = x . In ( "uid" , uids ) . Find ( & tasks )
2019-05-22 19:48:48 +02:00
if err != nil {
return
2018-12-28 22:49:46 +01:00
}
2020-04-24 17:23:03 +02:00
taskMap := make ( map [ int64 ] * Task , len ( tasks ) )
for _ , t := range tasks {
taskMap [ t . ID ] = t
}
err = addMoreInfoToTasks ( taskMap )
2019-05-22 19:48:48 +02:00
return
}
2019-11-03 23:08:26 +01:00
func getRemindersForTasks ( taskIDs [ ] int64 ) ( reminders [ ] * TaskReminder , err error ) {
reminders = [ ] * TaskReminder { }
2020-02-08 13:48:49 +01:00
err = x . In ( "task_id" , taskIDs ) . Find ( & reminders )
2019-11-03 23:08:26 +01:00
return
}
2020-07-01 11:17:18 +02:00
func ( t * Task ) setIdentifier ( list * List ) {
t . Identifier = list . Identifier + "-" + strconv . FormatInt ( t . Index , 10 )
}
2019-05-22 19:48:48 +02:00
// 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
2020-04-24 17:23:03 +02:00
func addMoreInfoToTasks ( taskMap map [ int64 ] * 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
2019-12-07 23:28:45 +01:00
var listIDs [ ] int64
2019-05-22 19:48:48 +02:00
for _ , i := range taskMap {
taskIDs = append ( taskIDs , i . ID )
userIDs = append ( userIDs , i . CreatedByID )
2019-12-07 23:28:45 +01:00
listIDs = append ( listIDs , i . ListID )
2019-05-22 19:48:48 +02:00
}
// 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
2019-10-23 23:11:40 +02:00
labels , _ , _ , err := getLabelsByTaskIDs ( & LabelByTaskIDsOptions {
2019-10-20 17:56:53 +02:00
TaskIDs : taskIDs ,
Page : - 1 ,
} )
2019-05-22 19:48:48 +02:00
if err != nil {
return
}
for _ , l := range labels {
if l != nil {
taskMap [ l . TaskID ] . Labels = append ( taskMap [ l . TaskID ] . Labels , & l . Label )
}
}
2019-10-16 22:52:29 +02:00
// Get task attachments
2020-06-30 22:53:14 +02:00
attachments , err := getTaskAttachmentsByTaskIDs ( taskIDs )
2019-10-16 22:52:29 +02:00
2019-05-22 19:48:48 +02:00
// Get all users of a task
// aka the ones who created a task
2020-01-26 18:08:06 +01:00
users := make ( map [ int64 ] * user . User )
2019-05-22 19:48:48 +02:00
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-10-16 22:52:29 +02:00
// Put the users and files in task attachments
for _ , a := range attachments {
taskMap [ a . TaskID ] . Attachments = append ( taskMap [ a . TaskID ] . Attachments , a )
}
2019-05-25 09:33:57 +02:00
// Get all reminders and put them in a map to have it easier later
2019-11-03 23:08:26 +01:00
reminders , err := getRemindersForTasks ( taskIDs )
2019-05-25 09:33:57 +02:00
if err != nil {
return
}
2020-06-27 19:04:01 +02:00
taskReminders := make ( map [ int64 ] [ ] time . Time )
2019-05-25 09:33:57 +02:00
for _ , r := range reminders {
2020-06-27 19:04:01 +02:00
taskReminders [ r . TaskID ] = append ( taskReminders [ r . TaskID ] , r . Reminder )
2019-05-25 09:33:57 +02:00
}
2019-12-07 23:28:45 +01:00
// Get all identifiers
lists := make ( map [ int64 ] * List , len ( listIDs ) )
err = x . In ( "id" , listIDs ) . Find ( & lists )
if err != nil {
return
}
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-09-25 20:44:41 +02:00
task . CreatedBy = users [ task . CreatedByID ]
2019-05-22 19:48:48 +02:00
2019-05-25 09:33:57 +02:00
// Add the reminders
2020-06-27 19:04:01 +02:00
task . Reminders = taskReminders [ task . ID ]
2019-05-25 09:33:57 +02:00
2019-09-25 20:44:41 +02:00
// Prepare the subtasks
task . RelatedTasks = make ( RelatedTaskMap )
2019-12-07 23:28:45 +01:00
// Build the task identifier from the list identifier and task index
2020-07-01 11:17:18 +02:00
task . setIdentifier ( lists [ task . ListID ] )
2019-09-25 20:44:41 +02:00
}
// Get all related tasks
relatedTasks := [ ] * TaskRelation { }
err = x . In ( "task_id" , taskIDs ) . Find ( & relatedTasks )
if err != nil {
return
}
// Collect all related task IDs, so we can get all related task headers in one go
var relatedTaskIDs [ ] int64
for _ , rt := range relatedTasks {
relatedTaskIDs = append ( relatedTaskIDs , rt . OtherTaskID )
}
fullRelatedTasks := make ( map [ int64 ] * Task )
err = x . In ( "id" , relatedTaskIDs ) . Find ( & fullRelatedTasks )
if err != nil {
return
}
// NOTE: while it certainly be possible to run this function on fullRelatedTasks again, we don't do this for performance reasons.
// Go through all task relations and put them into the task objects
for _ , rt := range relatedTasks {
taskMap [ rt . TaskID ] . RelatedTasks [ rt . RelationKind ] = append ( taskMap [ rt . TaskID ] . RelatedTasks [ rt . RelationKind ] , fullRelatedTasks [ rt . OtherTaskID ] )
2018-12-28 22:49:46 +01:00
}
return
}
2019-07-16 16:15:40 +02:00
2020-04-19 09:27:28 +02:00
func checkBucketAndTaskBelongToSameList ( fullTask * Task , bucketID int64 ) ( err error ) {
if bucketID != 0 {
b , err := getBucketByID ( bucketID )
if err != nil {
return err
}
if fullTask . ListID != b . ListID {
return ErrBucketDoesNotBelongToList {
ListID : fullTask . ListID ,
BucketID : fullTask . BucketID ,
}
}
}
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."
2020-06-28 16:25:46 +02:00
// @Failure 400 {object} web.HTTPError "Invalid task object provided."
// @Failure 403 {object} web.HTTPError "The user does not have access to the list"
2019-07-16 16:15:40 +02:00
// @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 ) {
2020-06-30 22:53:14 +02:00
return createTask ( t , a , true )
}
func createTask ( t * Task , a web . Auth , updateAssignees bool ) ( err error ) {
2019-07-16 16:15:40 +02:00
t . ID = 0
// Check if we have at least a text
2020-05-16 12:17:44 +02:00
if t . Title == "" {
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
}
2020-05-12 15:51:48 +02:00
if _ , is := a . ( * LinkSharing ) ; is {
// A negative user id indicates user share links
t . CreatedByID = a . GetID ( ) * - 1
} else {
u , err := user . GetUserByID ( a . GetID ( ) )
if err != nil {
return err
}
t . CreatedByID = u . ID
t . CreatedBy = u
2019-07-16 16:15:40 +02:00
}
// Generate a uuid if we don't already have one
if t . UID == "" {
t . UID = utils . MakeRandomString ( 40 )
}
2020-04-19 09:27:28 +02:00
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList ( t , t . BucketID )
if err != nil {
return
}
2020-04-25 22:32:02 +02:00
// Get the default bucket and move the task there
if t . BucketID == 0 {
defaultBucket , err := getDefaultBucket ( t . ListID )
if err != nil {
return err
}
t . BucketID = defaultBucket . ID
}
2019-12-08 16:10:34 +01:00
// Get the index for this task
latestTask := & Task { }
_ , err = x . Where ( "list_id = ?" , t . ListID ) . OrderBy ( "id desc" ) . Get ( latestTask )
if err != nil {
return err
}
t . Index = latestTask . Index + 1
2020-04-24 17:23:03 +02:00
// If no position was supplied, set a default one
if t . Position == 0 {
t . Position = float64 ( latestTask . ID + 1 ) * math . Pow ( 2 , 16 )
}
2019-07-16 16:15:40 +02:00
if _ , err = x . Insert ( t ) ; err != nil {
return err
}
// Update the assignees
2020-06-30 22:53:14 +02:00
if updateAssignees {
if err := t . updateTaskAssignees ( t . Assignees ) ; err != nil {
return err
}
2019-07-16 16:15:40 +02:00
}
// Update the reminders
2020-02-08 13:48:49 +01:00
if err := t . updateReminders ( t . Reminders ) ; err != nil {
2019-07-16 16:15:40 +02:00
return err
}
metrics . UpdateCount ( 1 , metrics . TaskCountKey )
2020-07-01 11:17:18 +02:00
t . setIdentifier ( l )
2019-07-16 16:15:40 +02:00
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."
2020-06-28 16:25:46 +02:00
// @Failure 400 {object} web.HTTPError "Invalid task object provided."
// @Failure 403 {object} web.HTTPError "The user does not have access to the task (aka its list)"
2019-07-16 16:15:40 +02:00
// @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 ) {
2020-02-26 22:09:45 +01:00
// Check if the task exists and get the old values
2019-11-02 21:33:18 +01:00
ot , err := GetTaskByIDSimple ( t . ID )
2019-07-16 16:15:40 +02:00
if err != nil {
return
}
2020-02-26 22:09:45 +01:00
// Get the reminders
reminders , err := getRemindersForTasks ( [ ] int64 { t . ID } )
if err != nil {
return
}
2020-06-27 19:04:01 +02:00
ot . Reminders = make ( [ ] time . Time , len ( reminders ) )
2020-02-26 22:09:45 +01:00
for i , r := range reminders {
ot . Reminders [ i ] = r . Reminder
}
2019-07-16 16:15:40 +02:00
// 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
2020-02-08 13:48:49 +01:00
if err := ot . updateReminders ( t . Reminders ) ; err != nil {
2019-07-16 16:15:40 +02:00
return err
}
2020-04-19 09:27:28 +02:00
// If there is a bucket set, make sure they belong to the same list as the task
err = checkBucketAndTaskBelongToSameList ( & ot , t . BucketID )
if err != nil {
return
}
2020-07-01 16:54:18 +02:00
// All columns to update in a separate variable to be able to add to them
colsToUpdate := [ ] string {
"title" ,
"description" ,
"done" ,
"due_date" ,
"repeat_after" ,
"priority" ,
"start_date" ,
"end_date" ,
"hex_color" ,
"done_at" ,
"percent_done" ,
"list_id" ,
"bucket_id" ,
"position" ,
"repeat_from_current_date" ,
}
// If the task is being moved between lists, make sure to move the bucket + index as well
2020-05-03 22:25:48 +02:00
if t . ListID != 0 && ot . ListID != t . ListID {
2020-05-03 21:33:59 +02:00
b , err := getDefaultBucket ( t . ListID )
if err != nil {
return err
}
t . BucketID = b . ID
2020-07-01 16:54:18 +02:00
latestTask := & Task { }
_ , err = x . Where ( "list_id = ?" , t . ListID ) . OrderBy ( "id desc" ) . Get ( latestTask )
if err != nil {
return err
}
t . Index = latestTask . Index + 1
colsToUpdate = append ( colsToUpdate , "index" )
2020-05-03 21:33:59 +02:00
}
2019-07-16 16:15:40 +02:00
// 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
2020-02-26 22:09:45 +01:00
// Which is why we merge the actual task struct with the one we got from the db
2019-07-16 16:15:40 +02:00
// 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
2020-06-27 19:04:01 +02:00
if t . DueDate . IsZero ( ) {
ot . DueDate = time . Time { }
2019-07-16 16:15:40 +02:00
}
// Repeat after
if t . RepeatAfter == 0 {
ot . RepeatAfter = 0
}
// Start date
2020-06-27 19:04:01 +02:00
if t . StartDate . IsZero ( ) {
ot . StartDate = time . Time { }
2019-07-16 16:15:40 +02:00
}
// End date
2020-06-27 19:04:01 +02:00
if t . EndDate . IsZero ( ) {
ot . EndDate = time . Time { }
2019-07-16 16:15:40 +02:00
}
// Color
if t . HexColor == "" {
ot . HexColor = ""
}
2020-04-20 23:08:38 +02:00
// Percent Done
2019-09-21 12:52:10 +02:00
if t . PercentDone == 0 {
ot . PercentDone = 0
}
2020-04-24 17:23:03 +02:00
// Position
if t . Position == 0 {
ot . Position = 0
}
2020-06-14 13:04:38 +02:00
// Repeat from current date
if ! t . RepeatFromCurrentDate {
ot . RepeatFromCurrentDate = false
}
2019-07-16 16:15:40 +02:00
_ , err = x . ID ( t . ID ) .
2020-07-01 16:54:18 +02:00
Cols ( colsToUpdate ... ) .
2019-07-16 16:15:40 +02:00
Update ( ot )
* t = ot
if err != nil {
return err
}
err = updateListLastUpdated ( & List { ID : t . ListID } )
return
}
2020-06-27 19:04:01 +02:00
// This helper function updates the reminders, doneAt, start and end dates of the *old* task
2020-02-26 22:09:45 +01:00
// and saves the new values in the newTask object.
// We make a few assumtions here:
// 1. Everything in oldTask is the truth - we figure out if we update anything at all if oldTask.RepeatAfter has a value > 0
// 2. Because of 1., this functions should not be used to update values other than Done in the same go
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 {
2020-02-26 22:09:45 +01:00
repeatDuration := time . Duration ( oldTask . RepeatAfter ) * time . Second
2020-06-14 13:04:38 +02:00
// Current time in an extra variable to base all calculations on the same time
now := time . Now ( )
2020-02-26 22:09:45 +01:00
// assuming we'll merge the new task over the old task
2020-06-27 19:04:01 +02:00
if ! oldTask . DueDate . IsZero ( ) {
2020-06-14 13:04:38 +02:00
if oldTask . RepeatFromCurrentDate {
2020-06-27 19:04:01 +02:00
newTask . DueDate = now . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
} else {
// Always add one instance of the repeating interval to catch cases where a due date is already in the future
// but not the repeating interval
2020-06-27 19:04:01 +02:00
newTask . DueDate = oldTask . DueDate . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
// Add the repeating interval until the new due date is in the future
2020-06-27 19:04:01 +02:00
for ! newTask . DueDate . After ( now ) {
newTask . DueDate = newTask . DueDate . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
}
2020-06-13 23:32:31 +02:00
}
2020-02-26 22:09:45 +01:00
}
newTask . Reminders = oldTask . Reminders
2020-06-14 13:04:38 +02:00
// When repeating from the current date, all reminders should keep their difference to each other.
// To make this easier, we sort them first because we can then rely on the fact the first is the smallest
if len ( oldTask . Reminders ) > 0 {
if oldTask . RepeatFromCurrentDate {
sort . Slice ( oldTask . Reminders , func ( i , j int ) bool {
2020-06-27 19:04:01 +02:00
return oldTask . Reminders [ i ] . Unix ( ) < oldTask . Reminders [ j ] . Unix ( )
2020-06-14 13:04:38 +02:00
} )
first := oldTask . Reminders [ 0 ]
for in , r := range oldTask . Reminders {
2020-06-27 19:04:01 +02:00
diff := r . Sub ( first )
newTask . Reminders [ in ] = now . Add ( repeatDuration + diff )
2020-06-14 13:04:38 +02:00
}
} else {
for in , r := range oldTask . Reminders {
2020-06-27 19:04:01 +02:00
newTask . Reminders [ in ] = r . Add ( repeatDuration )
for ! newTask . Reminders [ in ] . After ( now ) {
newTask . Reminders [ in ] = newTask . Reminders [ in ] . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
}
}
2020-06-13 23:32:31 +02:00
}
2020-02-26 22:09:45 +01:00
}
2020-06-14 13:04:38 +02:00
// If a task has a start and end date, the end date should keep the difference to the start date when setting them as new
2020-06-27 19:04:01 +02:00
if oldTask . RepeatFromCurrentDate && ! oldTask . StartDate . IsZero ( ) && ! oldTask . EndDate . IsZero ( ) {
diff := oldTask . EndDate . Sub ( oldTask . StartDate )
newTask . StartDate = now . Add ( repeatDuration )
newTask . EndDate = now . Add ( repeatDuration + diff )
2020-06-14 13:04:38 +02:00
} else {
2020-06-27 19:04:01 +02:00
if ! oldTask . StartDate . IsZero ( ) {
2020-06-14 13:04:38 +02:00
if oldTask . RepeatFromCurrentDate {
2020-06-27 19:04:01 +02:00
newTask . StartDate = now . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
} else {
2020-06-27 19:04:01 +02:00
newTask . StartDate = oldTask . StartDate . Add ( repeatDuration )
for ! newTask . StartDate . After ( now ) {
newTask . StartDate = newTask . StartDate . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
}
}
2020-06-13 23:32:31 +02:00
}
2020-02-26 22:09:45 +01:00
2020-06-27 19:04:01 +02:00
if ! oldTask . EndDate . IsZero ( ) {
2020-06-14 13:04:38 +02:00
if oldTask . RepeatFromCurrentDate {
2020-06-27 19:04:01 +02:00
newTask . EndDate = now . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
} else {
2020-06-27 19:04:01 +02:00
newTask . EndDate = oldTask . EndDate . Add ( repeatDuration )
for ! newTask . EndDate . After ( now ) {
newTask . EndDate = newTask . EndDate . Add ( repeatDuration )
2020-06-14 13:04:38 +02:00
}
}
2020-06-13 23:32:31 +02:00
}
2019-07-16 16:15:40 +02:00
}
newTask . Done = false
}
// Update the "done at" timestamp
if ! oldTask . Done && newTask . Done {
2020-06-27 19:04:01 +02:00
newTask . DoneAt = time . Now ( )
2019-07-16 16:15:40 +02:00
}
// When unmarking a task as done, reset the timestamp
if oldTask . Done && ! newTask . Done {
2020-06-27 19:04:01 +02:00
newTask . DoneAt = time . Time { }
2019-07-16 16:15:40 +02:00
}
}
2020-02-26 22:09:45 +01:00
// Creates or deletes all necessary reminders without unneded db operations.
2019-07-16 16:15:40 +02:00
// The parameter is a slice with unix dates which holds the new reminders.
2020-06-27 19:04:01 +02:00
func ( t * Task ) updateReminders ( reminders [ ] time . Time ) ( err error ) {
2019-07-16 16:15:40 +02:00
2019-11-03 23:08:26 +01:00
// Load the current reminders
taskReminders , err := getRemindersForTasks ( [ ] int64 { t . ID } )
if err != nil {
return err
}
2020-06-27 19:04:01 +02:00
t . Reminders = make ( [ ] time . Time , 0 , len ( taskReminders ) )
2019-11-03 23:08:26 +01:00
for _ , reminder := range taskReminders {
2020-02-08 13:48:49 +01:00
t . Reminders = append ( t . Reminders , reminder . Reminder )
2019-11-03 23:08:26 +01:00
}
2019-07-16 16:15:40 +02:00
// If we're removing everything, delete all reminders right away
2020-02-08 13:48:49 +01:00
if len ( reminders ) == 0 && len ( t . Reminders ) > 0 {
2019-07-16 16:15:40 +02:00
_ , err = x . Where ( "task_id = ?" , t . ID ) .
Delete ( TaskReminder { } )
2020-02-08 13:48:49 +01:00
t . Reminders = nil
2019-07-16 16:15:40 +02:00
return err
}
// If we didn't change anything (from 0 to zero) don't do anything.
2020-02-08 13:48:49 +01:00
if len ( reminders ) == 0 && len ( t . Reminders ) == 0 {
2019-07-16 16:15:40 +02:00
return nil
}
// Make a hashmap of the new reminders for easier comparison
2020-06-27 19:04:01 +02:00
newReminders := make ( map [ time . Time ] * TaskReminder , len ( reminders ) )
2019-07-16 16:15:40 +02:00
for _ , newReminder := range reminders {
2020-02-08 13:48:49 +01:00
newReminders [ newReminder ] = & TaskReminder { Reminder : newReminder }
2019-07-16 16:15:40 +02:00
}
// Get old reminders to delete
var found bool
2020-06-27 19:04:01 +02:00
var remindersToDelete [ ] time . Time
oldReminders := make ( map [ time . Time ] * TaskReminder , len ( t . Reminders ) )
2020-02-08 13:48:49 +01:00
for _ , oldReminder := range t . Reminders {
2019-07-16 16:15:40 +02:00
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 )
}
2020-02-08 13:48:49 +01:00
oldReminders [ oldReminder ] = & TaskReminder { Reminder : oldReminder }
2019-07-16 16:15:40 +02:00
}
// Delete all reminders not passed
if len ( remindersToDelete ) > 0 {
2020-06-27 19:04:01 +02:00
_ , err = x . In ( "reminder" , remindersToDelete ) .
2019-07-16 16:15:40 +02:00
And ( "task_id = ?" , t . ID ) .
2019-11-24 16:13:16 +01:00
Delete ( TaskReminder { } )
2019-07-16 16:15:40 +02:00
if err != nil {
return err
}
}
2020-06-13 23:32:31 +02:00
// Loop through our reminders and add them
2019-07-16 16:15:40 +02:00
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
2020-02-08 13:48:49 +01:00
_ , err = x . Insert ( TaskReminder { TaskID : t . ID , Reminder : r } )
2019-07-16 16:15:40 +02:00
if err != nil {
return err
}
}
2020-02-08 13:48:49 +01:00
t . Reminders = reminders
2019-07-16 16:15:40 +02:00
if len ( reminders ) == 0 {
2020-02-08 13:48:49 +01:00
t . Reminders = nil
2019-07-16 16:15:40 +02:00
}
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."
2020-06-28 16:25:46 +02:00
// @Failure 400 {object} web.HTTPError "Invalid task ID provided."
// @Failure 403 {object} web.HTTPError "The user does not have access to the list"
2019-07-16 16:15:40 +02:00
// @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
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
}
2019-11-02 21:33:18 +01:00
// ReadOne gets one task by its ID
// @Summary Get one task
// @Description Returns one task by its ID
// @tags task
// @Accept json
// @Produce json
// @Param ID path int true "The task ID"
// @Security JWTKeyAuth
// @Success 200 {object} models.Task "The task"
// @Failure 404 {object} models.Message "Task not found"
// @Failure 500 {object} models.Message "Internal error"
2019-12-07 23:28:45 +01:00
// @Router /tasks/{ID} [get]
2019-11-02 21:33:18 +01:00
func ( t * Task ) ReadOne ( ) ( err error ) {
taskMap := make ( map [ int64 ] * Task , 1 )
taskMap [ t . ID ] = & Task { }
* taskMap [ t . ID ] , err = GetTaskByIDSimple ( t . ID )
if err != nil {
return
}
2020-04-24 17:23:03 +02:00
err = addMoreInfoToTasks ( taskMap )
2019-11-02 21:33:18 +01:00
if err != nil {
return
}
2020-04-24 17:23:03 +02:00
if len ( taskMap ) == 0 {
2019-11-02 21:33:18 +01:00
return ErrTaskDoesNotExist { t . ID }
}
2020-04-24 17:23:03 +02:00
* t = * taskMap [ t . ID ]
2019-11-02 21:33:18 +01:00
return
}