2018-12-31 02:18:41 +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/>.
package models
import (
"code.vikunja.io/web"
"github.com/go-xorm/builder"
)
2019-01-10 00:08:12 +01:00
// LabelTask represents a relation between a label and a task
type LabelTask struct {
// The unique, numeric id of this label.
2019-01-14 23:32:56 +01:00
ID int64 ` xorm:"int(11) autoincr not null unique pk" json:"-" `
2019-01-10 00:08:12 +01:00
TaskID int64 ` xorm:"int(11) INDEX not null" json:"-" param:"listtask" `
// The label id you want to associate with a task.
LabelID int64 ` xorm:"int(11) INDEX not null" json:"label_id" param:"label" `
// 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-10 00:08:12 +01:00
web . CRUDable ` xorm:"-" json:"-" `
web . Rights ` xorm:"-" json:"-" `
}
// TableName makes a pretty table name
func ( LabelTask ) TableName ( ) string {
return "label_task"
}
2018-12-31 02:18:41 +01:00
// Delete deletes a label on a task
// @Summary Remove a label from a task
// @Description Remove a label from a task. The user needs to have write-access to the list to be able do this.
// @tags labels
// @Accept json
// @Produce json
2019-01-03 23:22:06 +01:00
// @Security JWTKeyAuth
2018-12-31 02:18:41 +01:00
// @Param task path int true "Task ID"
// @Param label path int true "Label ID"
2019-03-21 07:40:56 +01:00
// @Success 200 {object} models.Message "The label was successfully removed."
2018-12-31 02:18:41 +01:00
// @Failure 403 {object} code.vikunja.io/web.HTTPError "Not allowed to remove the label."
// @Failure 404 {object} code.vikunja.io/web.HTTPError "Label not found."
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{task}/labels/{label} [delete]
2019-01-10 00:08:12 +01:00
func ( lt * LabelTask ) Delete ( ) ( err error ) {
_ , err = x . Delete ( & LabelTask { LabelID : lt . LabelID , TaskID : lt . TaskID } )
2018-12-31 02:18:41 +01:00
return err
}
// Create adds a label to a task
// @Summary Add a label to a task
// @Description Add a label to a task. The user needs to have write-access to the list to be able do this.
// @tags labels
// @Accept json
// @Produce json
2019-01-03 23:22:06 +01:00
// @Security JWTKeyAuth
2018-12-31 02:18:41 +01:00
// @Param task path int true "Task ID"
2019-03-21 07:40:56 +01:00
// @Param label body models.LabelTask true "The label object"
// @Success 200 {object} models.LabelTask "The created label relation object."
2018-12-31 02:18:41 +01:00
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid label object provided."
// @Failure 403 {object} code.vikunja.io/web.HTTPError "Not allowed to add the label."
// @Failure 404 {object} code.vikunja.io/web.HTTPError "The label does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{task}/labels [put]
2019-01-10 00:08:12 +01:00
func ( lt * LabelTask ) Create ( a web . Auth ) ( err error ) {
2018-12-31 02:18:41 +01:00
// Check if the label is already added
2019-01-10 00:08:12 +01:00
exists , err := x . Exist ( & LabelTask { LabelID : lt . LabelID , TaskID : lt . TaskID } )
2018-12-31 02:18:41 +01:00
if err != nil {
return err
}
if exists {
2019-01-10 00:08:12 +01:00
return ErrLabelIsAlreadyOnTask { lt . LabelID , lt . TaskID }
2018-12-31 02:18:41 +01:00
}
// Insert it
2019-01-10 00:08:12 +01:00
_ , err = x . Insert ( lt )
2019-05-22 19:48:48 +02:00
if err != nil {
return err
}
err = updateListByTaskID ( lt . TaskID )
2019-01-10 00:08:12 +01:00
return
2018-12-31 02:18:41 +01:00
}
// ReadAll gets all labels on a task
// @Summary Get all labels on a task
// @Description Returns all labels which are assicociated with a given task.
// @tags labels
// @Accept json
// @Produce json
// @Param task path int true "Task ID"
// @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 labels by label text."
2019-01-03 23:22:06 +01:00
// @Security JWTKeyAuth
2018-12-31 02:18:41 +01:00
// @Success 200 {array} models.Label "The labels"
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{task}/labels [get]
2019-01-10 00:08:12 +01:00
func ( lt * LabelTask ) ReadAll ( search string , a web . Auth , page int ) ( labels interface { } , err error ) {
2018-12-31 02:18:41 +01:00
// Check if the user has the right to see the task
2019-08-14 22:19:04 +02:00
task := Task { ID : lt . TaskID }
2019-03-24 13:35:50 +01:00
canRead , err := task . CanRead ( a )
if err != nil {
return nil , err
}
if ! canRead {
2019-06-28 10:21:48 +02:00
return nil , ErrNoRightToSeeTask { lt . TaskID , a . GetID ( ) }
2018-12-31 02:18:41 +01:00
}
2019-01-10 00:08:12 +01:00
return getLabelsByTaskIDs ( & LabelByTaskIDsOptions {
2019-06-28 10:21:48 +02:00
User : & User { ID : a . GetID ( ) } ,
2019-01-10 00:08:12 +01:00
Search : search ,
Page : page ,
TaskIDs : [ ] int64 { lt . TaskID } ,
} )
2018-12-31 02:18:41 +01:00
}
2019-01-10 00:08:12 +01:00
// Helper struct, contains the label + its task ID
2018-12-31 02:18:41 +01:00
type labelWithTaskID struct {
2019-09-20 17:52:09 +02:00
TaskID int64 ` json:"-" `
2018-12-31 02:18:41 +01:00
Label ` xorm:"extends" `
}
2019-01-10 00:08:12 +01:00
// LabelByTaskIDsOptions is a struct to not clutter the function with too many optional parameters.
type LabelByTaskIDsOptions struct {
2019-09-20 17:52:09 +02:00
User * User
Search string
Page int
TaskIDs [ ] int64
GetUnusedLabels bool
GroupByLabelIDsOnly bool
2019-01-10 00:08:12 +01:00
}
2018-12-31 02:18:41 +01:00
// Helper function to get all labels for a set of tasks
// Used when getting all labels for one task as well when getting all lables
2019-01-10 00:08:12 +01:00
func getLabelsByTaskIDs ( opts * LabelByTaskIDsOptions ) ( ls [ ] * labelWithTaskID , err error ) {
// Include unused labels. Needed to be able to show a list of all unused labels a user
// has access to.
2018-12-31 02:18:41 +01:00
var uidOrNil interface { }
var requestOrNil interface { }
2019-01-10 00:08:12 +01:00
if opts . GetUnusedLabels {
uidOrNil = opts . User . ID
2018-12-31 02:18:41 +01:00
requestOrNil = "label_task.label_id != null OR labels.created_by_id = ?"
}
2019-09-20 17:52:09 +02:00
// We still need the task ID when we want to get all labels for a task, but because of this, we get the same label
// multiple times when it is associated to more than one task.
// Because of this whole thing, we need this extra switch here to only group by Task IDs if needed.
// Probably not the most ideal solution.
var groupBy = "labels.id,label_task.task_id"
if opts . GroupByLabelIDsOnly {
groupBy = "labels.id"
}
2019-03-22 22:03:41 +01:00
// Get all labels associated with these tasks
2018-12-31 02:18:41 +01:00
var labels [ ] * labelWithTaskID
err = x . Table ( "labels" ) .
Select ( "labels.*, label_task.task_id" ) .
Join ( "LEFT" , "label_task" , "label_task.label_id = labels.id" ) .
Where ( requestOrNil , uidOrNil ) .
2019-01-10 00:08:12 +01:00
Or ( builder . In ( "label_task.task_id" , opts . TaskIDs ) ) .
And ( "labels.title LIKE ?" , "%" + opts . Search + "%" ) .
2019-09-20 17:52:09 +02:00
GroupBy ( groupBy ) .
2019-01-10 00:08:12 +01:00
Limit ( getLimitFromPageIndex ( opts . Page ) ) .
2018-12-31 02:18:41 +01:00
Find ( & labels )
if err != nil {
return nil , err
}
// Get all created by users
var userids [ ] int64
for _ , l := range labels {
userids = append ( userids , l . CreatedByID )
}
users := make ( map [ int64 ] * User )
err = x . In ( "id" , userids ) . Find ( & users )
if err != nil {
return nil , err
}
2019-08-14 21:59:31 +02:00
// Obfuscate all user emails
for _ , u := range users {
u . Email = ""
}
2018-12-31 02:18:41 +01:00
// Put it all together
for in , l := range labels {
labels [ in ] . CreatedBy = users [ l . CreatedByID ]
}
return labels , err
}
2019-01-10 00:08:12 +01:00
// Create or update a bunch of task labels
2019-08-14 22:19:04 +02:00
func ( t * Task ) updateTaskLabels ( creator web . Auth , labels [ ] * Label ) ( err error ) {
2019-01-10 00:08:12 +01:00
// If we don't have any new labels, delete everything right away. Saves us some hassle.
if len ( labels ) == 0 && len ( t . Labels ) > 0 {
_ , err = x . Where ( "task_id = ?" , t . ID ) .
Delete ( LabelTask { } )
return err
}
// If we didn't change anything (from 0 to zero) don't do anything.
if len ( labels ) == 0 && len ( t . Labels ) == 0 {
return nil
}
// Make a hashmap of the new labels for easier comparison
newLabels := make ( map [ int64 ] * Label , len ( labels ) )
for _ , newLabel := range labels {
newLabels [ newLabel . ID ] = newLabel
}
// Get old labels to delete
var found bool
var labelsToDelete [ ] int64
oldLabels := make ( map [ int64 ] * Label , len ( t . Labels ) )
allLabels := t . Labels
t . Labels = [ ] * Label { } // We re-empty our labels struct here because we want it to be fully empty so we can put in all the actual labels.
for _ , oldLabel := range allLabels {
found = false
if newLabels [ oldLabel . ID ] != nil {
found = true // If a new label is already in the list with old labels
}
// Put all labels which are only on the old list to the trash
if ! found {
labelsToDelete = append ( labelsToDelete , oldLabel . ID )
} else {
t . Labels = append ( t . Labels , oldLabel )
}
// Put it in a list with all old labels, just using the loop here
oldLabels [ oldLabel . ID ] = oldLabel
}
// Delete all labels not passed
if len ( labelsToDelete ) > 0 {
_ , err = x . In ( "label_id" , labelsToDelete ) .
And ( "task_id = ?" , t . ID ) .
Delete ( LabelTask { } )
if err != nil {
return err
}
}
// Loop through our labels and add them
for _ , l := range labels {
// Check if the label is already added on the task and only add it if not
if oldLabels [ l . ID ] != nil {
// continue outer loop
continue
}
// Add the new label
label , err := getLabelByIDSimple ( l . ID )
if err != nil {
return err
}
// Check if the user has the rights to see the label he is about to add
2019-03-24 13:35:50 +01:00
hasAccessToLabel , err := label . hasAccessToLabel ( creator )
if err != nil {
return err
}
if ! hasAccessToLabel {
2019-01-10 00:08:12 +01:00
user , _ := creator . ( * User )
return ErrUserHasNoAccessToLabel { LabelID : l . ID , UserID : user . ID }
}
// Insert it
_ , err = x . Insert ( & LabelTask { LabelID : l . ID , TaskID : t . ID } )
if err != nil {
return err
}
t . Labels = append ( t . Labels , label )
}
2019-05-22 19:48:48 +02:00
err = updateListLastUpdated ( & List { ID : t . ListID } )
2019-01-10 00:08:12 +01:00
return
}
// LabelTaskBulk is a helper struct to update a bunch of labels at once
type LabelTaskBulk struct {
2019-03-21 18:42:21 +01:00
// All labels you want to update at once.
2019-01-10 00:08:12 +01:00
Labels [ ] * Label ` json:"labels" `
TaskID int64 ` json:"-" param:"listtask" `
web . CRUDable ` json:"-" `
web . Rights ` json:"-" `
}
// Create updates a bunch of labels on a task at once
2019-03-21 18:42:21 +01:00
// @Summary Update all labels on a task.
// @Description Updates all labels on a task. Every label which is not passed but exists on the task will be deleted. Every label which does not exist on the task will be added. All labels which are passed and already exist on the task won't be touched.
2019-01-10 00:08:12 +01:00
// @tags labels
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param label body models.LabelTaskBulk true "The array of labels"
// @Param taskID path int true "Task ID"
// @Success 200 {object} models.LabelTaskBulk "The updated labels object."
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid label object provided."
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{taskID}/labels/bulk [post]
func ( ltb * LabelTaskBulk ) Create ( a web . Auth ) ( err error ) {
2019-05-22 19:48:48 +02:00
task , err := GetTaskByID ( ltb . TaskID )
2019-01-10 00:08:12 +01:00
if err != nil {
return
}
2019-03-21 19:26:04 +01:00
return task . updateTaskLabels ( a , ltb . Labels )
2019-01-10 00:08:12 +01:00
}