api/pkg/models/label_task.go

411 lines
13 KiB
Go
Raw Normal View History

2020-02-07 16:27:45 +00:00
// Vikunja is a to-do list application to facilitate your life.
2021-02-02 19:19:13 +00:00
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
2018-12-31 01:18:41 +00:00
//
// This program is free software: you can redistribute it and/or modify
2020-12-23 15:41:52 +00:00
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
2018-12-31 01:18:41 +00: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
2020-12-23 15:41:52 +00:00
// GNU Affero General Public Licensee for more details.
2018-12-31 01:18:41 +00:00
//
2020-12-23 15:41:52 +00:00
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-12-31 01:18:41 +00:00
package models
import (
"strconv"
"strings"
"time"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/user"
2018-12-31 01:18:41 +00:00
"code.vikunja.io/web"
"xorm.io/builder"
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
"xorm.io/xorm"
2018-12-31 01:18:41 +00:00
)
2019-01-09 23:08:12 +00:00
// LabelTask represents a relation between a label and a task
type LabelTask struct {
// The unique, numeric id of this label.
ID int64 `xorm:"bigint autoincr not null unique pk" json:"-"`
2022-11-13 16:07:01 +00:00
TaskID int64 `xorm:"bigint INDEX not null" json:"-" param:"projecttask"`
2019-01-09 23:08:12 +00:00
// The label id you want to associate with a task.
LabelID int64 `xorm:"bigint INDEX not null" json:"label_id" param:"label"`
// A timestamp when this task was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
2019-01-09 23:08:12 +00:00
web.CRUDable `xorm:"-" json:"-"`
web.Rights `xorm:"-" json:"-"`
}
// TableName makes a pretty table name
func (LabelTask) TableName() string {
return "label_tasks"
2019-01-09 23:08:12 +00:00
}
2018-12-31 01:18:41 +00:00
// Delete deletes a label on a task
// @Summary Remove a label from a task
2022-11-13 16:07:01 +00:00
// @Description Remove a label from a task. The user needs to have write-access to the project to be able do this.
2018-12-31 01:18:41 +00:00
// @tags labels
// @Accept json
// @Produce json
2019-01-03 22:22:06 +00:00
// @Security JWTKeyAuth
2018-12-31 01:18:41 +00:00
// @Param task path int true "Task ID"
// @Param label path int true "Label ID"
2019-03-21 06:40:56 +00:00
// @Success 200 {object} models.Message "The label was successfully removed."
2020-06-28 14:25:46 +00:00
// @Failure 403 {object} web.HTTPError "Not allowed to remove the label."
// @Failure 404 {object} web.HTTPError "Label not found."
2018-12-31 01:18:41 +00:00
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{task}/labels/{label} [delete]
func (lt *LabelTask) Delete(s *xorm.Session, a web.Auth) (err error) {
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
_, err = s.Delete(&LabelTask{LabelID: lt.LabelID, TaskID: lt.TaskID})
2018-12-31 01:18:41 +00:00
return err
}
// Create adds a label to a task
// @Summary Add a label to a task
2022-11-13 16:07:01 +00:00
// @Description Add a label to a task. The user needs to have write-access to the project to be able do this.
2018-12-31 01:18:41 +00:00
// @tags labels
// @Accept json
// @Produce json
2019-01-03 22:22:06 +00:00
// @Security JWTKeyAuth
2018-12-31 01:18:41 +00:00
// @Param task path int true "Task ID"
2019-03-21 06:40:56 +00:00
// @Param label body models.LabelTask true "The label object"
2021-05-26 19:56:31 +00:00
// @Success 201 {object} models.LabelTask "The created label relation object."
2020-06-28 14:25:46 +00:00
// @Failure 400 {object} web.HTTPError "Invalid label object provided."
// @Failure 403 {object} web.HTTPError "Not allowed to add the label."
// @Failure 404 {object} web.HTTPError "The label does not exist."
2018-12-31 01:18:41 +00:00
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{task}/labels [put]
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
func (lt *LabelTask) Create(s *xorm.Session, a web.Auth) (err error) {
2018-12-31 01:18:41 +00:00
// Check if the label is already added
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
exists, err := s.Exist(&LabelTask{LabelID: lt.LabelID, TaskID: lt.TaskID})
2018-12-31 01:18:41 +00:00
if err != nil {
return err
}
if exists {
2019-01-09 23:08:12 +00:00
return ErrLabelIsAlreadyOnTask{lt.LabelID, lt.TaskID}
2018-12-31 01:18:41 +00:00
}
// Insert it
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
_, err = s.Insert(lt)
2019-05-22 17:48:48 +00:00
if err != nil {
return err
}
2022-11-13 16:07:01 +00:00
err = updateProjectByTaskID(s, lt.TaskID)
2019-01-09 23:08:12 +00:00
return
2018-12-31 01:18:41 +00: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"
2019-10-23 21:11:40 +00: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."
2018-12-31 01:18:41 +00:00
// @Param s query string false "Search labels by label text."
2019-01-03 22:22:06 +00:00
// @Security JWTKeyAuth
2018-12-31 01:18:41 +00:00
// @Success 200 {array} models.Label "The labels"
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{task}/labels [get]
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
func (lt *LabelTask) ReadAll(s *xorm.Session, a web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, numberOfTotalItems int64, err error) {
2018-12-31 01:18:41 +00:00
// Check if the user has the right to see the task
2019-08-14 20:19:04 +00:00
task := Task{ID: lt.TaskID}
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
canRead, _, err := task.CanRead(s, a)
2019-03-24 12:35:50 +00:00
if err != nil {
2019-10-23 21:11:40 +00:00
return nil, 0, 0, err
2019-03-24 12:35:50 +00:00
}
if !canRead {
2019-10-23 21:11:40 +00:00
return nil, 0, 0, ErrNoRightToSeeTask{lt.TaskID, a.GetID()}
2018-12-31 01:18:41 +00:00
}
return GetLabelsByTaskIDs(s, &LabelByTaskIDsOptions{
User: &user.User{ID: a.GetID()},
Search: []string{search},
2019-01-09 23:08:12 +00:00
Page: page,
TaskIDs: []int64{lt.TaskID},
})
2018-12-31 01:18:41 +00:00
}
2023-03-05 13:34:34 +00:00
// LabelWithTaskID is a helper struct, contains the label + its task ID
type LabelWithTaskID struct {
TaskID int64 `json:"-"`
2018-12-31 01:18:41 +00:00
Label `xorm:"extends"`
}
2019-01-09 23:08:12 +00:00
// LabelByTaskIDsOptions is a struct to not clutter the function with too many optional parameters.
type LabelByTaskIDsOptions struct {
User *user.User
Search []string
Page int
2019-10-23 21:11:40 +00:00
PerPage int
TaskIDs []int64
GetUnusedLabels bool
GroupByLabelIDsOnly bool
GetForUser int64
2019-01-09 23:08:12 +00:00
}
// GetLabelsByTaskIDs is a helper function to get all labels for a set of tasks
2018-12-31 01:18:41 +00:00
// Used when getting all labels for one task as well when getting all lables
2023-03-05 13:34:34 +00:00
func GetLabelsByTaskIDs(s *xorm.Session, opts *LabelByTaskIDsOptions) (ls []*LabelWithTaskID, resultCount int, totalEntries int64, err error) {
// 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_tasks.task_id"
var selectStmt = "labels.*, label_tasks.task_id"
if opts.GroupByLabelIDsOnly {
groupBy = "labels.id"
selectStmt = "labels.*"
}
// Get all labels associated with these tasks
2023-03-05 13:34:34 +00:00
var labels []*LabelWithTaskID
cond := builder.And(builder.NotNull{"label_tasks.label_id"})
if len(opts.TaskIDs) > 0 && opts.GetForUser == 0 {
cond = builder.And(builder.In("label_tasks.task_id", opts.TaskIDs), cond)
}
if opts.GetForUser != 0 {
cond = builder.And(builder.In("label_tasks.task_id",
builder.
Select("id").
From("tasks").
2022-11-13 16:07:01 +00:00
Where(builder.In("project_id", getUserProjectsStatement(opts.GetForUser).Select("l.id"))),
), cond)
}
if opts.GetUnusedLabels {
cond = builder.Or(cond, builder.Eq{"labels.created_by_id": opts.User.ID})
}
ids := []int64{}
for _, search := range opts.Search {
search = strings.Trim(search, " ")
if search == "" {
continue
}
vals := strings.Split(search, ",")
for _, val := range vals {
v, err := strconv.ParseInt(val, 10, 64)
if err != nil {
log.Debugf("Label search string part '%s' is not a number: %s", val, err)
continue
}
ids = append(ids, v)
}
}
if len(ids) > 0 {
cond = builder.And(cond, builder.In("labels.id", ids))
2023-03-05 13:34:34 +00:00
} else if len(opts.Search) > 0 {
2023-03-05 13:34:34 +00:00
var searchcond builder.Cond
for _, search := range opts.Search {
search = strings.Trim(search, " ")
if search == "" {
continue
}
2023-03-05 13:34:34 +00:00
searchcond = builder.Or(searchcond, db.ILIKE("labels.title", search))
}
2023-03-05 13:34:34 +00:00
cond = builder.And(cond, searchcond)
}
limit, start := getLimitFromPageIndex(opts.Page, opts.PerPage)
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
query := s.Table("labels").
Select(selectStmt).
Join("LEFT", "label_tasks", "label_tasks.label_id = labels.id").
Where(cond).
GroupBy(groupBy).
OrderBy("labels.id ASC")
if limit > 0 {
query = query.Limit(limit, start)
}
err = query.Find(&labels)
2018-12-31 01:18:41 +00:00
if err != nil {
2019-10-23 21:11:40 +00:00
return nil, 0, 0, err
2018-12-31 01:18:41 +00:00
}
if len(labels) == 0 {
return nil, 0, 0, nil
}
2018-12-31 01:18:41 +00:00
// Get all created by users
var userids []int64
for _, l := range labels {
userids = append(userids, l.CreatedByID)
}
users := make(map[int64]*user.User)
if len(userids) > 0 {
err = s.In("id", userids).Find(&users)
if err != nil {
return nil, 0, 0, err
}
2018-12-31 01:18:41 +00:00
}
2019-08-14 19:59:31 +00:00
// Obfuscate all user emails
for _, u := range users {
u.Email = ""
}
2018-12-31 01:18:41 +00:00
// Put it all together
for in, l := range labels {
labels[in].CreatedBy = users[l.CreatedByID]
}
2019-10-23 21:11:40 +00:00
// Get the total number of entries
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
totalEntries, err = s.Table("labels").
Select("count(DISTINCT labels.id)").
Join("LEFT", "label_tasks", "label_tasks.label_id = labels.id").
Where(cond).
2019-10-23 21:11:40 +00:00
Count(&Label{})
if err != nil {
return nil, 0, 0, err
}
return labels, len(labels), totalEntries, err
2018-12-31 01:18:41 +00:00
}
2019-01-09 23:08:12 +00:00
// Create or update a bunch of task labels
func (t *Task) UpdateTaskLabels(s *xorm.Session, creator web.Auth, labels []*Label) (err error) {
2019-01-09 23:08:12 +00: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 {
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
_, err = s.Where("task_id = ?", t.ID).
2019-01-09 23:08:12 +00:00
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 {
2022-11-13 16:07:01 +00:00
found = true // If a new label is already in the project with old labels
2019-01-09 23:08:12 +00:00
}
2022-11-13 16:07:01 +00:00
// Put all labels which are only on the old project to the trash
2019-01-09 23:08:12 +00:00
if !found {
labelsToDelete = append(labelsToDelete, oldLabel.ID)
} else {
t.Labels = append(t.Labels, oldLabel)
}
2022-11-13 16:07:01 +00:00
// Put it in a project with all old labels, just using the loop here
2019-01-09 23:08:12 +00:00
oldLabels[oldLabel.ID] = oldLabel
}
// Delete all labels not passed
if len(labelsToDelete) > 0 {
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
_, err = s.In("label_id", labelsToDelete).
2019-01-09 23:08:12 +00:00
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
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
label, err := getLabelByIDSimple(s, l.ID)
2019-01-09 23:08:12 +00:00
if err != nil {
return err
}
// Check if the user has the rights to see the label he is about to add
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
hasAccessToLabel, _, err := label.hasAccessToLabel(s, creator)
2019-03-24 12:35:50 +00:00
if err != nil {
return err
}
if !hasAccessToLabel {
user, _ := creator.(*user.User)
2019-01-09 23:08:12 +00:00
return ErrUserHasNoAccessToLabel{LabelID: l.ID, UserID: user.ID}
}
// Insert it
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
_, err = s.Insert(&LabelTask{LabelID: l.ID, TaskID: t.ID})
2019-01-09 23:08:12 +00:00
if err != nil {
return err
}
t.Labels = append(t.Labels, label)
}
2019-05-22 17:48:48 +00:00
2022-11-13 16:07:01 +00:00
err = updateProjectLastUpdated(s, &Project{ID: t.ProjectID})
2019-01-09 23:08:12 +00:00
return
}
// LabelTaskBulk is a helper struct to update a bunch of labels at once
type LabelTaskBulk struct {
// All labels you want to update at once.
2019-01-09 23:08:12 +00:00
Labels []*Label `json:"labels"`
2022-11-13 16:07:01 +00:00
TaskID int64 `json:"-" param:"projecttask"`
2019-01-09 23:08:12 +00:00
web.CRUDable `json:"-"`
web.Rights `json:"-"`
}
// Create updates a bunch of labels on a task at once
// @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-09 23:08:12 +00: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"
2021-05-26 19:56:31 +00:00
// @Success 201 {object} models.LabelTaskBulk "The updated labels object."
2020-06-28 14:25:46 +00:00
// @Failure 400 {object} web.HTTPError "Invalid label object provided."
2019-01-09 23:08:12 +00:00
// @Failure 500 {object} models.Message "Internal error"
// @Router /tasks/{taskID}/labels/bulk [post]
Use db sessions everywere (#750) Fix lint Fix lint Fix loading tasks with search Fix loading lists Fix loading task Fix loading lists and namespaces Fix tests Fix user commands Fix upload Fix migration handlers Fix all manual root handlers Fix session in avatar Fix session in list duplication & routes Use sessions in migration code Make sure the openid stuff uses a session Add alias for db type in db package Use sessions for file Use a session for everything in users Use a session for everything in users Make sure to use a session everywhere in models Create new session from db Add session handling for user list Add session handling for unsplash Add session handling for teams and related Add session handling for tasks and related entities Add session handling for task reminders Add session handling for task relations Add session handling for task comments Add session handling for task collections Add session handling for task attachments Add session handling for task assignees Add session handling for saved filters Add session handling for namespace and related types Add session handling for namespace and related types Add session handling for list users Add session handling for list tests Add session handling to list teams and related entities Add session handling for link shares and related entities Add session handling for labels and related entities Add session handling for kanban and related entities Add session handling for bulk task and related entities Add session handling for lists and related entities Add session configuration for web handler Update web handler Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/api/pulls/750 Co-Authored-By: konrad <konrad@kola-entertainments.de> Co-Committed-By: konrad <konrad@kola-entertainments.de>
2020-12-23 15:32:28 +00:00
func (ltb *LabelTaskBulk) Create(s *xorm.Session, a web.Auth) (err error) {
task, err := GetTaskByIDSimple(s, ltb.TaskID)
2019-01-09 23:08:12 +00:00
if err != nil {
return
}
labels, _, _, err := GetLabelsByTaskIDs(s, &LabelByTaskIDsOptions{
TaskIDs: []int64{ltb.TaskID},
})
if err != nil {
return err
}
for _, l := range labels {
task.Labels = append(task.Labels, &l.Label)
}
return task.UpdateTaskLabels(s, a, ltb.Labels)
2019-01-09 23:08:12 +00:00
}