2024-04-28 22:00:56 +07:00
|
|
|
package homeview
|
|
|
|
|
|
|
|
import (
|
2024-04-29 12:27:46 +07:00
|
|
|
"slices"
|
|
|
|
"strings"
|
2024-04-29 21:45:18 +07:00
|
|
|
"time"
|
2024-04-29 12:27:46 +07:00
|
|
|
|
2024-04-28 22:00:56 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api"
|
|
|
|
"github.com/tigorlazuardi/redmage/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
SubredditsList api.ListSubredditsResult
|
|
|
|
RecentlyAddedImages RecentlyAddedImages
|
2024-04-29 21:45:18 +07:00
|
|
|
TotalImages int64
|
2024-04-29 15:18:23 +07:00
|
|
|
Error string
|
2024-04-29 21:45:18 +07:00
|
|
|
Now time.Time
|
2024-04-29 22:47:57 +07:00
|
|
|
SFW bool
|
2024-04-28 22:00:56 +07:00
|
|
|
}
|
|
|
|
|
2024-04-29 12:27:46 +07:00
|
|
|
type RecentlyAddedImages = []RecentlyAddedImage
|
2024-04-28 22:00:56 +07:00
|
|
|
|
2024-04-29 12:27:46 +07:00
|
|
|
type RecentlyAddedImage struct {
|
|
|
|
Device *models.Device
|
|
|
|
Subreddits []Subreddit
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subreddit struct {
|
|
|
|
Subreddit *models.Subreddit
|
|
|
|
Images models.ImageSlice
|
|
|
|
}
|
|
|
|
|
2024-04-28 22:00:56 +07:00
|
|
|
func NewRecentlyAddedImages(images models.ImageSlice) RecentlyAddedImages {
|
2024-04-29 12:27:46 +07:00
|
|
|
r := make(RecentlyAddedImages, 0, len(images))
|
2024-04-29 21:45:18 +07:00
|
|
|
|
|
|
|
var count int
|
|
|
|
|
2024-04-28 22:00:56 +07:00
|
|
|
for _, image := range images {
|
|
|
|
if image.R.Device == nil || image.R.Subreddit == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-29 12:27:46 +07:00
|
|
|
var deviceFound bool
|
|
|
|
for i, ra := range r {
|
2024-04-30 14:12:33 +07:00
|
|
|
if ra.Device.Slug == image.R.Device.Slug {
|
2024-04-29 12:27:46 +07:00
|
|
|
deviceFound = true
|
|
|
|
var subredditFound bool
|
|
|
|
for j, subreddit := range r[i].Subreddits {
|
2024-04-30 14:12:33 +07:00
|
|
|
if subreddit.Subreddit.Name == image.R.Subreddit.Name {
|
2024-04-29 12:27:46 +07:00
|
|
|
subredditFound = true
|
|
|
|
r[i].Subreddits[j].Images = append(r[i].Subreddits[j].Images, image)
|
2024-04-29 21:45:18 +07:00
|
|
|
count++
|
2024-04-29 12:27:46 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !subredditFound {
|
|
|
|
r[i].Subreddits = append(r[i].Subreddits, Subreddit{
|
|
|
|
Subreddit: image.R.Subreddit,
|
|
|
|
Images: models.ImageSlice{image},
|
|
|
|
})
|
2024-04-29 21:45:18 +07:00
|
|
|
count++
|
2024-04-29 12:27:46 +07:00
|
|
|
}
|
2024-04-28 22:00:56 +07:00
|
|
|
}
|
|
|
|
}
|
2024-04-29 12:27:46 +07:00
|
|
|
if !deviceFound {
|
2024-04-29 21:45:18 +07:00
|
|
|
count++
|
2024-04-29 12:27:46 +07:00
|
|
|
r = append(r, RecentlyAddedImage{
|
|
|
|
Device: image.R.Device,
|
|
|
|
Subreddits: []Subreddit{
|
|
|
|
{
|
|
|
|
Subreddit: image.R.Subreddit,
|
|
|
|
Images: models.ImageSlice{image},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2024-04-28 22:00:56 +07:00
|
|
|
}
|
|
|
|
}
|
2024-04-29 12:27:46 +07:00
|
|
|
|
|
|
|
for _, r := range r {
|
|
|
|
slices.SortFunc(r.Subreddits, func(left, right Subreddit) int {
|
|
|
|
leftName := strings.ToLower(left.Subreddit.Name)
|
|
|
|
rightName := strings.ToLower(right.Subreddit.Name)
|
|
|
|
return strings.Compare(leftName, rightName)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.SortFunc(r, func(left, right RecentlyAddedImage) int {
|
|
|
|
leftName := strings.ToLower(left.Device.Name)
|
|
|
|
rightName := strings.ToLower(right.Device.Name)
|
|
|
|
return strings.Compare(leftName, rightName)
|
|
|
|
})
|
|
|
|
|
2024-04-28 22:00:56 +07:00
|
|
|
return r
|
|
|
|
}
|