reorganise core/user

This commit is contained in:
perennial
2024-10-19 18:31:07 +11:00
parent c5bd4fe797
commit a4a5100b5b
+115 -115
View File
@@ -77,121 +77,6 @@ type User struct {
CountInfo CountInfo
}
// 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, nil
}
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
return nil
}
err := json.Unmarshal(s.SocialRaw, &s.Social)
if err != nil {
return err
}
return nil
}
// Work is a generic type constraint.
type Work interface{}
// GetUserWorks is a generic helper function to fetch user works.
func GetUserWorks[T Work](r *http.Request, url string) ([]T, error) {
resp, err := API_GET_UnwrapJson(r.Context(), url, "", r.Header)
if err != nil {
return nil, err
}
resp = session.ProxyImageUrl(r, resp)
// Define a generic body structure.
var body struct {
Works map[int]json.RawMessage `json:"works"`
}
// Unmarshal the response into the body.
if err := json.Unmarshal([]byte(resp), &body); err != nil {
return nil, err
}
// Initialize the slice to hold the works.
var works []T
// Iterate over each work and unmarshal into the specific type.
for _, v := range body.Works {
var work T
if err := json.Unmarshal(v, &work); err != nil {
return nil, err
}
works = append(works, work)
}
return works, nil
}
// GetUserFrequentTags retrieves frequent tags for a user based on category.
func GetUserFrequentTags(r *http.Request, ids string, category UserArtCategory) ([]FrequentTag, error) {
var tags []FrequentTag
var URL string
if category != "novels" {
URL = GetUserFrequentArtworkTagsURL(ids)
} else {
URL = GetUserFrequentNovelTagsURL(ids)
}
response, err := API_GET_UnwrapJson(r.Context(), URL, "", r.Header)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(response), &tags)
if err != nil {
return nil, err
}
return tags, nil
}
// GetUserArtworkList fetches the list of artworks for a user.
func GetUserArtworkList(r *http.Request, id, ids string) ([]ArtworkBrief, error) {
URL := GetUserFullArtworkURL(id, ids)
works, err := GetUserWorks[ArtworkBrief](r, URL)
if err != nil {
return nil, err
}
return works, nil
}
// GetUserNovels fetches the list of novels for a user.
func GetUserNovels(r *http.Request, id, ids string) ([]NovelBrief, error) {
URL := GetUserFullNovelURL(id, ids)
works, err := GetUserWorks[NovelBrief](r, URL)
if err != nil {
return nil, err
}
return works, nil
}
// IntStringMap is a custom type that can handle both JSON objects and empty arrays.
//
// Required as the Pixiv API returns empty arrays for work types that
@@ -472,6 +357,91 @@ func GetUserProfile(r *http.Request, id string, category UserArtCategory, page i
return user, nil
}
// Work is a generic type constraint.
type Work interface{}
// GetUserWorks is a generic helper function to fetch user works.
func GetUserWorks[T Work](r *http.Request, url string) ([]T, error) {
resp, err := API_GET_UnwrapJson(r.Context(), url, "", r.Header)
if err != nil {
return nil, err
}
resp = session.ProxyImageUrl(r, resp)
// Define a generic body structure.
var body struct {
Works map[int]json.RawMessage `json:"works"`
}
// Unmarshal the response into the body.
if err := json.Unmarshal([]byte(resp), &body); err != nil {
return nil, err
}
// Initialize the slice to hold the works.
var works []T
// Iterate over each work and unmarshal into the specific type.
for _, v := range body.Works {
var work T
if err := json.Unmarshal(v, &work); err != nil {
return nil, err
}
works = append(works, work)
}
return works, nil
}
// GetUserFrequentTags retrieves frequent tags for a user based on category.
func GetUserFrequentTags(r *http.Request, ids string, category UserArtCategory) ([]FrequentTag, error) {
var tags []FrequentTag
var URL string
if category != "novels" {
URL = GetUserFrequentArtworkTagsURL(ids)
} else {
URL = GetUserFrequentNovelTagsURL(ids)
}
response, err := API_GET_UnwrapJson(r.Context(), URL, "", r.Header)
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(response), &tags)
if err != nil {
return nil, err
}
return tags, nil
}
// GetUserArtworkList fetches the list of artworks for a user.
func GetUserArtworkList(r *http.Request, id, ids string) ([]ArtworkBrief, error) {
URL := GetUserFullArtworkURL(id, ids)
works, err := GetUserWorks[ArtworkBrief](r, URL)
if err != nil {
return nil, err
}
return works, nil
}
// GetUserNovels fetches the list of novels for a user.
func GetUserNovels(r *http.Request, id, ids string) ([]NovelBrief, error) {
URL := GetUserFullNovelURL(id, ids)
works, err := GetUserWorks[NovelBrief](r, URL)
if err != nil {
return nil, err
}
return works, nil
}
func GetUserBookmarks(r *http.Request, id, mode string, page int) ([]ArtworkBrief, int, error) {
page--
@@ -513,6 +483,36 @@ func GetUserBookmarks(r *http.Request, id, mode string, page int) ([]ArtworkBrie
return artworks, body.Total, nil
}
func (s *User) ParseSocial() error {
if string(s.SocialRaw[:]) == "[]" {
// Fuck Pixiv
return nil
}
err := json.Unmarshal(s.SocialRaw, &s.Social)
if err != nil {
return err
}
return nil
}
// computeSliceBounds is a 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, nil
}
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 numberGreaterThan(l, r string) bool {
if len(l) > len(r) {
return true