From 6de3d8b3a1332e05b999448b9fc21ebd3ae24f54 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 2 Mar 2021 18:40:39 +0100 Subject: [PATCH] Explicitly check if there are Ids before trying to get items by a list of Ids --- pkg/log/watermill_logger.go | 2 +- pkg/models/kanban.go | 8 +++++--- pkg/models/label_task.go | 8 +++++--- pkg/models/link_sharing.go | 8 +++++--- pkg/models/list.go | 13 ++++++++++--- pkg/models/namespace.go | 7 +++++++ pkg/models/task_attachment.go | 12 +++++++++--- pkg/models/task_reminder.go | 4 ++++ pkg/models/user_list.go | 12 ++++++++++-- pkg/user/user.go | 5 +++++ 10 files changed, 61 insertions(+), 18 deletions(-) diff --git a/pkg/log/watermill_logger.go b/pkg/log/watermill_logger.go index 4a6c4e4186..8f50c85866 100644 --- a/pkg/log/watermill_logger.go +++ b/pkg/log/watermill_logger.go @@ -60,7 +60,7 @@ func concatFields(fields watermill.LogFields) string { full := "" for key, val := range fields { - full += fmt.Sprintf("%s=%s, ", key, val) + full += fmt.Sprintf("%s=%v, ", key, val) } if full != "" { diff --git a/pkg/models/kanban.go b/pkg/models/kanban.go index 9b6907d837..264114fd1d 100644 --- a/pkg/models/kanban.go +++ b/pkg/models/kanban.go @@ -119,9 +119,11 @@ func (b *Bucket) ReadAll(s *xorm.Session, auth web.Auth, search string, page int // Get all users users := make(map[int64]*user.User) - err = s.In("id", userIDs).Find(&users) - if err != nil { - return + if len(userIDs) > 0 { + err = s.In("id", userIDs).Find(&users) + if err != nil { + return + } } for _, bb := range buckets { diff --git a/pkg/models/label_task.go b/pkg/models/label_task.go index de5be5c1f5..5c94a5c77e 100644 --- a/pkg/models/label_task.go +++ b/pkg/models/label_task.go @@ -218,9 +218,11 @@ func getLabelsByTaskIDs(s *xorm.Session, opts *LabelByTaskIDsOptions) (ls []*lab userids = append(userids, l.CreatedByID) } users := make(map[int64]*user.User) - err = s.In("id", userids).Find(&users) - if err != nil { - return nil, 0, 0, err + if len(userids) > 0 { + err = s.In("id", userids).Find(&users) + if err != nil { + return nil, 0, 0, err + } } // Obfuscate all user emails diff --git a/pkg/models/link_sharing.go b/pkg/models/link_sharing.go index 363897abe6..8b7f4c76ec 100644 --- a/pkg/models/link_sharing.go +++ b/pkg/models/link_sharing.go @@ -182,9 +182,11 @@ func (share *LinkSharing) ReadAll(s *xorm.Session, a web.Auth, search string, pa } users := make(map[int64]*user.User) - err = s.In("id", userIDs).Find(&users) - if err != nil { - return nil, 0, 0, err + if len(userIDs) > 0 { + err = s.In("id", userIDs).Find(&users) + if err != nil { + return nil, 0, 0, err + } } for _, s := range shares { diff --git a/pkg/models/list.go b/pkg/models/list.go index 97cf5d9037..118cbdc0c7 100644 --- a/pkg/models/list.go +++ b/pkg/models/list.go @@ -290,6 +290,11 @@ func GetListSimplByTaskID(s *xorm.Session, taskID int64) (l *List, err error) { // GetListsByIDs returns a map of lists from a slice with list ids func GetListsByIDs(s *xorm.Session, listIDs []int64) (lists map[int64]*List, err error) { lists = make(map[int64]*List, len(listIDs)) + + if len(listIDs) == 0 { + return + } + err = s.In("id", listIDs).Find(&lists) return } @@ -405,9 +410,11 @@ func addListDetails(s *xorm.Session, lists []*List) (err error) { // Get all list owners owners := map[int64]*user.User{} - err = s.In("id", ownerIDs).Find(&owners) - if err != nil { - return + if len(ownerIDs) > 0 { + err = s.In("id", ownerIDs).Find(&owners) + if err != nil { + return + } } var fileIDs []int64 diff --git a/pkg/models/namespace.go b/pkg/models/namespace.go index b61d5509e4..4b4c4c6341 100644 --- a/pkg/models/namespace.go +++ b/pkg/models/namespace.go @@ -657,6 +657,13 @@ func (n *Namespace) Delete(s *xorm.Session, a web.Auth) (err error) { listIDs = append(listIDs, l.ID) } + if len(listIDs) == 0 { + return events.Dispatch(&NamespaceDeletedEvent{ + Namespace: n, + Doer: a, + }) + } + // Delete tasks _, err = s.In("list_id", listIDs).Delete(&Task{}) if err != nil { diff --git a/pkg/models/task_attachment.go b/pkg/models/task_attachment.go index 6d7e70925b..457d16d2f5 100644 --- a/pkg/models/task_attachment.go +++ b/pkg/models/task_attachment.go @@ -128,6 +128,10 @@ func (ta *TaskAttachment) ReadAll(s *xorm.Session, a web.Auth, search string, pa return nil, 0, 0, err } + if len(attachments) == 0 { + return + } + fileIDs := make([]int64, 0, len(attachments)) userIDs := make([]int64, 0, len(attachments)) for _, r := range attachments { @@ -228,9 +232,11 @@ func getTaskAttachmentsByTaskIDs(s *xorm.Session, taskIDs []int64) (attachments } users := make(map[int64]*user.User) - err = s.In("id", userIDs).Find(&users) - if err != nil { - return + if len(userIDs) > 0 { + err = s.In("id", userIDs).Find(&users) + if err != nil { + return + } } // Obfuscate all user emails diff --git a/pkg/models/task_reminder.go b/pkg/models/task_reminder.go index 4746fc797c..f44c706502 100644 --- a/pkg/models/task_reminder.go +++ b/pkg/models/task_reminder.go @@ -49,6 +49,10 @@ type taskUser struct { } func getTaskUsersForTasks(s *xorm.Session, taskIDs []int64) (taskUsers []*taskUser, err error) { + if len(taskIDs) == 0 { + return + } + // Get all creators of tasks creators := make(map[int64]*user.User, len(taskIDs)) err = s. diff --git a/pkg/models/user_list.go b/pkg/models/user_list.go index f155219287..b5d369b08b 100644 --- a/pkg/models/user_list.go +++ b/pkg/models/user_list.go @@ -96,12 +96,20 @@ func ListUsersFromList(s *xorm.Session, l *List, search string) (users []*user.U uids = append(uids, id) } + var cond builder.Cond = builder.Like{"username", "%" + search + "%"} + + if len(uids) > 0 { + cond = builder.And( + builder.In("id", uids), + cond, + ) + } + // Get all users err = s. Table("users"). Select("*"). - In("id", uids). - And("username LIKE ?", "%"+search+"%"). + Where(cond). GroupBy("id"). OrderBy("id"). Find(&users) diff --git a/pkg/user/user.go b/pkg/user/user.go index 5a1364c760..838097e386 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -182,6 +182,11 @@ func GetUserWithEmail(s *xorm.Session, user *User) (userOut *User, err error) { // GetUsersByIDs returns a map of users from a slice of user ids func GetUsersByIDs(s *xorm.Session, userIDs []int64) (users map[int64]*User, err error) { users = make(map[int64]*User) + + if len(userIDs) == 0 { + return users, nil + } + err = s.In("id", userIDs).Find(&users) if err != nil { return