From a0898c86ae1f17ea295ab0de10355248294bf0fd Mon Sep 17 00:00:00 2001 From: Amarjeet Singh Rai Date: Tue, 26 Dec 2023 10:06:55 +0400 Subject: [PATCH] add ordered tags fields --- internal/store/store.go | 15 +++++++++++++++ internal/updater/image/image.go | 3 ++- internal/updater/updater.go | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/internal/store/store.go b/internal/store/store.go index dfc6561..420ac43 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -40,6 +40,21 @@ type Store struct { firestore *firestore.Client } +func (s *Store) GetAll(ctx context.Context) ([]map[string]any, error) { + iter := s.firestore.Collection(s.collection).Documents(ctx) + dsnap, err := iter.GetAll() + if err != nil { + return nil, err + } + + wallpapers := make([]map[string]any, 0, len(dsnap)) + for _, v := range dsnap { + wallpapers = append(wallpapers, v.Data()) + } + + return wallpapers, nil +} + func (s *Store) Get(ctx context.Context, id string) (*WallpaperWithTags, error) { doc, err := s.firestore.Collection(s.collection).Doc(id).Get(ctx) if err != nil { diff --git a/internal/updater/image/image.go b/internal/updater/image/image.go index acbb0d5..9d404e5 100644 --- a/internal/updater/image/image.go +++ b/internal/updater/image/image.go @@ -17,7 +17,8 @@ type Image struct { URL string `json:"url,omitempty"` ThumbURL string `json:"thumbUrl,omitempty"` - Tags map[string]float32 `json:"tags,omitempty"` + Tags map[string]float32 `json:"tags,omitempty"` + TagsOrdered []string `json:"tagsOrdered,omitempty"` } func From(bw bing.Image, market string, bingURL string) (Image, error) { diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 7885173..9093940 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -16,6 +16,7 @@ import ( "io" "net/http" "os" + "sort" "strings" "time" ) @@ -172,6 +173,23 @@ func (u *Updater) Update(ctx context.Context) error { image.Tags[strings.ToLower(v.Description)] = v.Score } + // start: duplicate tags to t + tmp := make([]tag, 0, len(image.Tags)) + for k, v := range image.Tags { + tmp = append(tmp, tag{Name: k, Score: v}) + } + + // sort by score + sort.SliceStable(tmp, func(i, j int) bool { + return tmp[i].Score > tmp[j].Score + }) + + image.TagsOrdered = make([]string, 0, len(tmp)) + for _, v := range tmp { + image.TagsOrdered = append(image.TagsOrdered, strings.ReplaceAll(v.Name, " ", "-")) + } + // end: duplicate tags to t + _, err = u.firestoreClient.Upsert(ctx, image) if err != nil { return err @@ -280,3 +298,8 @@ func (u *Updater) translateText(ctx context.Context, text string) (string, error return resp[0].Text, nil } + +type tag struct { + Name string + Score float32 +}