Explicitly check if there are Ids before trying to get items by a list of Ids

This commit is contained in:
kolaente 2021-03-02 18:40:39 +01:00
parent 3999580fe6
commit 6de3d8b3a1
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
10 changed files with 61 additions and 18 deletions

View File

@ -60,7 +60,7 @@ func concatFields(fields watermill.LogFields) string {
full := "" full := ""
for key, val := range fields { for key, val := range fields {
full += fmt.Sprintf("%s=%s, ", key, val) full += fmt.Sprintf("%s=%v, ", key, val)
} }
if full != "" { if full != "" {

View File

@ -119,9 +119,11 @@ func (b *Bucket) ReadAll(s *xorm.Session, auth web.Auth, search string, page int
// Get all users // Get all users
users := make(map[int64]*user.User) users := make(map[int64]*user.User)
err = s.In("id", userIDs).Find(&users) if len(userIDs) > 0 {
if err != nil { err = s.In("id", userIDs).Find(&users)
return if err != nil {
return
}
} }
for _, bb := range buckets { for _, bb := range buckets {

View File

@ -218,9 +218,11 @@ func getLabelsByTaskIDs(s *xorm.Session, opts *LabelByTaskIDsOptions) (ls []*lab
userids = append(userids, l.CreatedByID) userids = append(userids, l.CreatedByID)
} }
users := make(map[int64]*user.User) users := make(map[int64]*user.User)
err = s.In("id", userids).Find(&users) if len(userids) > 0 {
if err != nil { err = s.In("id", userids).Find(&users)
return nil, 0, 0, err if err != nil {
return nil, 0, 0, err
}
} }
// Obfuscate all user emails // Obfuscate all user emails

View File

@ -182,9 +182,11 @@ func (share *LinkSharing) ReadAll(s *xorm.Session, a web.Auth, search string, pa
} }
users := make(map[int64]*user.User) users := make(map[int64]*user.User)
err = s.In("id", userIDs).Find(&users) if len(userIDs) > 0 {
if err != nil { err = s.In("id", userIDs).Find(&users)
return nil, 0, 0, err if err != nil {
return nil, 0, 0, err
}
} }
for _, s := range shares { for _, s := range shares {

View File

@ -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 // 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) { func GetListsByIDs(s *xorm.Session, listIDs []int64) (lists map[int64]*List, err error) {
lists = make(map[int64]*List, len(listIDs)) lists = make(map[int64]*List, len(listIDs))
if len(listIDs) == 0 {
return
}
err = s.In("id", listIDs).Find(&lists) err = s.In("id", listIDs).Find(&lists)
return return
} }
@ -405,9 +410,11 @@ func addListDetails(s *xorm.Session, lists []*List) (err error) {
// Get all list owners // Get all list owners
owners := map[int64]*user.User{} owners := map[int64]*user.User{}
err = s.In("id", ownerIDs).Find(&owners) if len(ownerIDs) > 0 {
if err != nil { err = s.In("id", ownerIDs).Find(&owners)
return if err != nil {
return
}
} }
var fileIDs []int64 var fileIDs []int64

View File

@ -657,6 +657,13 @@ func (n *Namespace) Delete(s *xorm.Session, a web.Auth) (err error) {
listIDs = append(listIDs, l.ID) listIDs = append(listIDs, l.ID)
} }
if len(listIDs) == 0 {
return events.Dispatch(&NamespaceDeletedEvent{
Namespace: n,
Doer: a,
})
}
// Delete tasks // Delete tasks
_, err = s.In("list_id", listIDs).Delete(&Task{}) _, err = s.In("list_id", listIDs).Delete(&Task{})
if err != nil { if err != nil {

View File

@ -128,6 +128,10 @@ func (ta *TaskAttachment) ReadAll(s *xorm.Session, a web.Auth, search string, pa
return nil, 0, 0, err return nil, 0, 0, err
} }
if len(attachments) == 0 {
return
}
fileIDs := make([]int64, 0, len(attachments)) fileIDs := make([]int64, 0, len(attachments))
userIDs := make([]int64, 0, len(attachments)) userIDs := make([]int64, 0, len(attachments))
for _, r := range attachments { for _, r := range attachments {
@ -228,9 +232,11 @@ func getTaskAttachmentsByTaskIDs(s *xorm.Session, taskIDs []int64) (attachments
} }
users := make(map[int64]*user.User) users := make(map[int64]*user.User)
err = s.In("id", userIDs).Find(&users) if len(userIDs) > 0 {
if err != nil { err = s.In("id", userIDs).Find(&users)
return if err != nil {
return
}
} }
// Obfuscate all user emails // Obfuscate all user emails

View File

@ -49,6 +49,10 @@ type taskUser struct {
} }
func getTaskUsersForTasks(s *xorm.Session, taskIDs []int64) (taskUsers []*taskUser, err error) { func getTaskUsersForTasks(s *xorm.Session, taskIDs []int64) (taskUsers []*taskUser, err error) {
if len(taskIDs) == 0 {
return
}
// Get all creators of tasks // Get all creators of tasks
creators := make(map[int64]*user.User, len(taskIDs)) creators := make(map[int64]*user.User, len(taskIDs))
err = s. err = s.

View File

@ -96,12 +96,20 @@ func ListUsersFromList(s *xorm.Session, l *List, search string) (users []*user.U
uids = append(uids, id) 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 // Get all users
err = s. err = s.
Table("users"). Table("users").
Select("*"). Select("*").
In("id", uids). Where(cond).
And("username LIKE ?", "%"+search+"%").
GroupBy("id"). GroupBy("id").
OrderBy("id"). OrderBy("id").
Find(&users) Find(&users)

View File

@ -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 // 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) { func GetUsersByIDs(s *xorm.Session, userIDs []int64) (users map[int64]*User, err error) {
users = make(map[int64]*User) users = make(map[int64]*User)
if len(userIDs) == 0 {
return users, nil
}
err = s.In("id", userIDs).Find(&users) err = s.In("id", userIDs).Find(&users)
if err != nil { if err != nil {
return return