From ca4d5000edbd99cea5eed749966db8c03cc2b1c5 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 16 Jun 2020 18:57:08 +0200 Subject: [PATCH] Add list background information when getting all lists --- pkg/models/list.go | 34 +++++++++++++++++++++++++--------- pkg/models/models.go | 1 + 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pkg/models/list.go b/pkg/models/list.go index 2a6b1b776..4d1e9a63a 100644 --- a/pkg/models/list.go +++ b/pkg/models/list.go @@ -329,21 +329,37 @@ func AddListDetails(lists []*List) (err error) { } // Get all list owners - owners := []*user.User{} + owners := map[int64]*user.User{} err = x.In("id", ownerIDs).Find(&owners) if err != nil { return } - // Build it all into the lists slice - for in, list := range lists { - // Owner - for _, owner := range owners { - if list.OwnerID == owner.ID { - lists[in].Owner = owner - break - } + var fileIDs []int64 + for _, l := range lists { + if o, exists := owners[l.OwnerID]; exists { + l.Owner = o } + if l.BackgroundFileID != 0 { + l.BackgroundInformation = &ListBackgroundType{Type: ListBackgroundUpload} + } + fileIDs = append(fileIDs, l.BackgroundFileID) + } + + // Unsplash background file info + us := []*UnsplashPhoto{} + err = x.In("file_id", fileIDs).Find(&us) + if err != nil { + return + } + unsplashPhotos := make(map[int64]*UnsplashPhoto, len(us)) + for _, u := range us { + unsplashPhotos[u.FileID] = u + } + + // Build it all into the lists slice + for _, l := range lists { + l.BackgroundInformation = unsplashPhotos[l.BackgroundFileID] } return diff --git a/pkg/models/models.go b/pkg/models/models.go index 873aa6c50..589b56304 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -52,6 +52,7 @@ func GetTables() []interface{} { &TaskAttachment{}, &TaskComment{}, &Bucket{}, + &UnsplashPhoto{}, } }