vikunja-api/pkg/models/list_tasks.go

256 lines
7.6 KiB
Go
Raw Normal View History

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 (
"code.vikunja.io/web"
2018-12-19 20:14:48 +01:00
"sort"
2018-12-02 01:49:30 +01:00
)
2018-12-01 00:26:56 +01:00
2018-08-30 08:09:17 +02:00
// ListTask represents an task in a todolist
type ListTask 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.
Description string `xorm:"varchar(250)" json:"description" valid:"runelength(0|250)" maxLength:"250"`
// Whether a task is done or not.
2019-03-29 18:54:35 +01:00
Done bool `xorm:"INDEX null" json:"done"`
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-03-29 18:54:35 +01:00
RemindersUnix []int64 `xorm:"JSON TEXT null" json:"reminderDates"`
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.
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"`
2018-12-01 03:00:57 +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.
2018-12-01 03:00:57 +01:00
Subtasks []*ListTask `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.
2018-11-25 22:38:50 +01:00
CreatedBy User `xorm:"-" json:"createdBy" valid:"-"`
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
func (ListTask) TableName() string {
return "tasks"
2018-06-10 15:55:56 +02:00
}
2018-08-30 08:09:17 +02:00
// GetTasksByListID gets all todotasks for a list
func GetTasksByListID(listID int64) (tasks []*ListTask, 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
taskMap := make(map[int64]*ListTask, len(tasks))
err = x.Where("list_id = ?", listID).Find(&taskMap)
if err != nil {
return
}
2018-12-31 02:18:41 +01:00
// No need to iterate over users and stuff if the list doesn't has tasks
if len(taskMap) == 0 {
2018-07-09 19:49:27 +02:00
return
}
2018-12-29 15:29:50 +01:00
// Get all users & task ids and put them into the array
var userIDs []int64
2018-12-29 15:29:50 +01:00
var taskIDs []int64
2018-12-31 02:18:41 +01:00
for _, i := range taskMap {
2018-12-29 15:29:50 +01:00
taskIDs = append(taskIDs, i.ID)
2018-12-31 02:18:41 +01:00
userIDs = append(userIDs, i.CreatedByID)
}
2018-12-29 15:29:50 +01:00
// Get all assignees
taskAssignees, err := getRawTaskAssigneesForTasks(taskIDs)
if err != nil {
return
}
// Put the assignees in the task map
for _, a := range taskAssignees {
if a != nil {
taskMap[a.TaskID].Assignees = append(taskMap[a.TaskID].Assignees, &a.User)
}
}
2018-12-31 02:18:41 +01:00
// Get all labels for the tasks
2019-01-10 00:08:12 +01:00
labels, err := getLabelsByTaskIDs(&LabelByTaskIDsOptions{TaskIDs: taskIDs})
2018-12-31 02:18:41 +01:00
if err != nil {
return
}
for _, l := range labels {
if l != nil {
taskMap[l.TaskID].Labels = append(taskMap[l.TaskID].Labels, &l.Label)
}
}
users := make(map[int64]*User)
err = x.In("id", userIDs).Find(&users)
if err != nil {
return
}
2018-12-01 03:00:57 +01:00
// Add all user objects to the appropriate tasks
for _, task := range taskMap {
// Make created by user objects
2018-12-31 02:18:41 +01:00
taskMap[task.ID].CreatedBy = *users[task.CreatedByID]
2018-12-01 03:00:57 +01:00
// Reorder all subtasks
if task.ParentTaskID != 0 {
taskMap[task.ParentTaskID].Subtasks = append(taskMap[task.ParentTaskID].Subtasks, task)
delete(taskMap, task.ID)
}
}
// make a complete slice from the map
tasks = []*ListTask{}
for _, t := range taskMap {
tasks = append(tasks, t)
}
2018-12-31 02:18:41 +01:00
// Sort the output. In Go, contents on a map are put on that map in no particular order (saved on heap).
2018-12-19 20:14:48 +01:00
// 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-06-10 15:55:56 +02:00
return
}
2018-12-31 02:18:41 +01:00
func getTaskByIDSimple(taskID int64) (task ListTask, err error) {
if taskID < 1 {
return ListTask{}, ErrListTaskDoesNotExist{taskID}
}
2018-12-31 02:18:41 +01:00
exists, err := x.ID(taskID).Get(&task)
if err != nil {
2018-08-30 08:09:17 +02:00
return ListTask{}, err
}
if !exists {
2018-12-31 02:18:41 +01:00
return ListTask{}, ErrListTaskDoesNotExist{taskID}
}
return
}
// GetListTaskByID returns all tasks a list has
func GetListTaskByID(listTaskID int64) (listTask ListTask, err error) {
listTask, err = getTaskByIDSimple(listTaskID)
if err != nil {
return
}
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)
}
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 {
return ErrListTaskDoesNotExist{id}
}
}
err = x.In("id", bt.IDs).Find(&bt.Tasks)
if err != nil {
return err
}
// We use a map, to avoid looping over two slices at once
var usermapids = make(map[int64]bool) // Bool ist just something, doesn't acutually matter
for _, list := range bt.Tasks {
usermapids[list.CreatedByID] = true
}
// Make a slice from the map
var userids []int64
for uid := range usermapids {
userids = append(userids, uid)
}
// Get all users for the tasks
var users []*User
err = x.In("id", userids).Find(&users)
if err != nil {
return err
}
for in, task := range bt.Tasks {
for _, u := range users {
if task.CreatedByID == u.ID {
bt.Tasks[in].CreatedBy = *u
}
}
}
return
}