Revert "Use Work interface when retrieving user artworks and novels"

This reverts commit 27749085da.

Didn't check for other calls to GetUserArtworks
This commit is contained in:
perennial
2024-10-08 00:03:12 +11:00
parent 27749085da
commit ca5f041013
+110 -82
View File
@@ -62,6 +62,23 @@ type User struct {
MangaSeries []MangaSeries
}
// Utility function to compute slice bounds safely
func computeSliceBounds(page int, worksPerPage float64, totalItems int) (start, end int, err error) {
if totalItems == 0 {
return 0, 0, i18n.Error("No items available.")
}
maxPages := int(math.Ceil(float64(totalItems) / worksPerPage))
if page < 1 || page > maxPages {
return 0, 0, i18n.Error("Invalid page number.")
}
start = (page - 1) * int(worksPerPage)
end = min(start+int(worksPerPage), totalItems)
return start, end, nil
}
func (s *User) ParseSocial() error {
if string(s.SocialRaw[:]) == "[]" {
// Fuck Pixiv
@@ -98,19 +115,10 @@ func GetFrequentTags(r *http.Request, ids string, category UserArtCategory) ([]F
return tags, nil
}
type Work interface {
GetID() string
}
func GetUserArtworks(r *http.Request, id, ids string) ([]ArtworkBrief, error) {
var works []ArtworkBrief
func GetUserWorks(r *http.Request, id, ids string, category UserArtCategory) ([]Work, error) {
var works []Work
var URL string
if category == UserArt_Novel {
URL = GetUserFullNovelURL(id, ids)
} else {
URL = GetUserFullArtworkURL(id, ids)
}
URL := GetUserFullArtworkURL(id, ids)
resp, err := API_GET_UnwrapJson(r.Context(), URL, "")
if err != nil {
@@ -119,7 +127,7 @@ func GetUserWorks(r *http.Request, id, ids string, category UserArtCategory) ([]
resp = session.ProxyImageUrl(r, resp)
var body struct {
Works map[int]json.RawMessage `json:"works"`
Illusts map[int]json.RawMessage `json:"works"`
}
err = json.Unmarshal([]byte(resp), &body)
@@ -127,22 +135,50 @@ func GetUserWorks(r *http.Request, id, ids string, category UserArtCategory) ([]
return nil, err
}
for _, v := range body.Works {
if category == UserArt_Novel {
var novel NovelBrief
err = json.Unmarshal(v, &novel)
if err != nil {
return nil, err
}
works = append(works, novel)
} else {
var illust ArtworkBrief
err = json.Unmarshal(v, &illust)
if err != nil {
return nil, err
}
works = append(works, illust)
for _, v := range body.Illusts {
var illust ArtworkBrief
err = json.Unmarshal(v, &illust)
if err != nil {
return nil, err
}
works = append(works, illust)
}
return works, nil
}
func GetUserNovels(r *http.Request, id, ids string) ([]NovelBrief, error) {
// VnPower: we can merge this function into GetUserArtworks, but I want to make things simple for now
var works []NovelBrief
URL := GetUserFullNovelURL(id, ids)
resp, err := API_GET_UnwrapJson(r.Context(), URL, "")
if err != nil {
return nil, err
}
resp = session.ProxyImageUrl(r, resp)
var body struct {
Novels map[int]json.RawMessage `json:"works"`
}
err = json.Unmarshal([]byte(resp), &body)
if err != nil {
return nil, err
}
for _, v := range body.Novels {
var novel NovelBrief
err = json.Unmarshal(v, &novel)
if err != nil {
return nil, err
}
works = append(works, novel)
}
return works, nil
@@ -264,37 +300,26 @@ func GetUserArtwork(r *http.Request, id string, category UserArtCategory, page i
// Public bookmarks count
user.ArtworksCount = count
} else {
} else if category == UserArt_Novel {
ids, count, series, err := GetUserArtworksIDAndSeries(r, id, category, page)
if err != nil {
return user, err
}
if count > 0 {
// Check if the user has works available or not
works, err := GetUserWorks(r, id, ids, category)
// Check if the user has artworks available or not
works, err := GetUserNovels(r, id, ids)
if err != nil {
return user, err
}
// IDK but the order got shuffled even though Pixiv sorted the IDs in the response
sort.Slice(works[:], func(i, j int) bool {
left := works[i].GetID()
right := works[j].GetID()
left := works[i].ID
right := works[j].ID
return numberGreaterThan(left, right)
})
if category == UserArt_Novel {
user.Novels = make([]NovelBrief, len(works))
for i, w := range works {
user.Novels[i] = w.(NovelBrief)
}
} else {
user.Artworks = make([]ArtworkBrief, len(works))
for i, w := range works {
user.Artworks[i] = w.(ArtworkBrief)
}
}
user.Novels = works
if getTags {
user.FrequentTags, err = GetFrequentTags(r, ids, category)
@@ -304,19 +329,48 @@ func GetUserArtwork(r *http.Request, id string, category UserArtCategory, page i
}
}
if category == UserArt_Novel {
var novelSeries []NovelSeries
if series != nil {
if err = json.Unmarshal(series, &novelSeries); err == nil {
user.NovelSeries = novelSeries
var novelSeries []NovelSeries
if series != nil {
if err = json.Unmarshal(series, &novelSeries); err == nil {
user.NovelSeries = novelSeries
}
}
// Artworks count
user.ArtworksCount = count
} else {
ids, count, series, err := GetUserArtworksIDAndSeries(r, id, category, page)
if err != nil {
return user, err
}
if count > 0 {
// Check if the user has artworks available or not
works, err := GetUserArtworks(r, id, ids)
if err != nil {
return user, err
}
// IDK but the order got shuffled even though Pixiv sorted the IDs in the response
sort.Slice(works[:], func(i, j int) bool {
left := works[i].ID
right := works[j].ID
return numberGreaterThan(left, right)
})
user.Artworks = works
if getTags {
user.FrequentTags, err = GetFrequentTags(r, ids, category)
if err != nil {
return user, err
}
}
} else {
var mangaSeries []MangaSeries
if series != nil {
if err = json.Unmarshal(series, &mangaSeries); err == nil {
user.MangaSeries = mangaSeries
}
}
var mangaSeries []MangaSeries
if series != nil {
if err = json.Unmarshal(series, &mangaSeries); err == nil {
user.MangaSeries = mangaSeries
}
}
@@ -386,29 +440,3 @@ func numberGreaterThan(l, r string) bool {
}
return l > r
}
// Utility function to compute slice bounds safely
func computeSliceBounds(page int, worksPerPage float64, totalItems int) (start, end int, err error) {
if totalItems == 0 {
return 0, 0, i18n.Error("No items available.")
}
maxPages := int(math.Ceil(float64(totalItems) / worksPerPage))
if page < 1 || page > maxPages {
return 0, 0, i18n.Error("Invalid page number.")
}
start = (page - 1) * int(worksPerPage)
end = min(start+int(worksPerPage), totalItems)
return start, end, nil
}
// Methods to satisfy the Work interface
func (a ArtworkBrief) GetID() string {
return a.ID
}
func (n NovelBrief) GetID() string {
return n.ID
}