diff --git a/core/user.go b/core/user.go index 404c87b..2cdb6db 100644 --- a/core/user.go +++ b/core/user.go @@ -77,29 +77,52 @@ type User struct { CountInfo CountInfo } +// GetUserProfile retrieves the user profile, including counts, artworks/bookmarks, and social data. func GetUserProfile(r *http.Request, id string, category UserWorkCategory, page int, getTags bool) (User, error) { var user User + // Fetch user information + userInfoURL := GetUserInformationURL(id) token := session.GetUserToken(r) - - URL := GetUserInformationURL(id) - - resp, err := API_GET_UnwrapJson(r.Context(), URL, token, r.Header) + resp, err := API_GET_UnwrapJson(r.Context(), userInfoURL, token, r.Header) if err != nil { return user, err } resp = session.ProxyImageUrl(r, resp) - - err = json.Unmarshal([]byte(resp), &user) - if err != nil { + if err := json.Unmarshal([]byte(resp), &user); err != nil { return user, err } + // Fetch counts + if err := fetchUserCounts(r, &user, id); err != nil { + return user, err + } + + // Fetch artworks or bookmarks based on the category + if err := fetchUserWorks(r, &user, id, category, page, getTags); err != nil { + return user, err + } + + // Parse social data + if err := user.ParseSocial(); err != nil { + return User{}, err + } + + // Set background image if available + if user.Background != nil { + user.BackgroundImage = user.Background["url"].(string) + } + + return user, nil +} + +// fetchUserCounts retrieves and sets the CountInfo field of the User. +func fetchUserCounts(r *http.Request, user *User, id string) error { // Get counts for illustrations and manga _, allCount, illustCount, mangaCount, _, _, err := getUserArtworksIDAndSeries(r, id, CategoryAny, 1) if err != nil { - return user, err + return err } user.CountInfo.All = allCount user.CountInfo.Illustrations = illustCount @@ -108,75 +131,79 @@ func GetUserProfile(r *http.Request, id string, category UserWorkCategory, page // Get count for novels separately _, _, _, _, novelCount, _, err := getUserArtworksIDAndSeries(r, id, CategoryNovels, 1) if err != nil { - return user, err + return err } user.CountInfo.Novels = novelCount // Get bookmarks count _, bookmarksCount, err := getUserBookmarks(r, id, "show", 1) if err != nil { - return user, err + return err } user.CountInfo.Bookmarks = bookmarksCount + return nil +} + +// fetchUserWorks retrieves and sets artworks or bookmarks based on the category. +// +// It also handles frequent tags and series data. +func fetchUserWorks(r *http.Request, user *User, id string, category UserWorkCategory, page int, getTags bool) error { if category == CategoryBookmarks { - // Bookmarks - works, _, err := getUserBookmarks(r, id, "show", page) + // Fetch bookmarks + works, count, err := getUserBookmarks(r, id, "show", page) if err != nil { - return user, err + return err } user.Artworks = works - user.CategoryItemCount = bookmarksCount + user.CategoryItemCount = count } else { + // Fetch artworks or novels ids, count, _, _, _, series, err := getUserArtworksIDAndSeries(r, id, category, page) if err != nil { - return user, err + return err } if count > 0 { artworks, novels, err := fetchAndProcessArtworks(r, id, ids, category) if err != nil { - return user, err + return err } if category == CategoryNovels { user.Novels = novels } else { user.Artworks = artworks - user.Illustrations = make([]ArtworkBrief, 0) - user.Manga = make([]ArtworkBrief, 0) - for _, artwork := range artworks { - if artwork.IllustType == 0 { - user.Illustrations = append(user.Illustrations, artwork) - } else if artwork.IllustType == 1 { - user.Manga = append(user.Manga, artwork) - } - } + user.Illustrations = filterArtworksByType(artworks, 0) + user.Manga = filterArtworksByType(artworks, 1) } if getTags { user.FrequentTags, err = getUserFrequentTags(r, ids, category) if err != nil { - return user, err + return err } } } + // Handle series data user.NovelSeries, user.MangaSeries = handleSeriesData(series, category) user.CategoryItemCount = count } - err = user.ParseSocial() - if err != nil { - return User{}, err - } + return nil +} - if user.Background != nil { - user.BackgroundImage = user.Background["url"].(string) +// filterArtworksByType filters artworks based on the IllustType. +func filterArtworksByType(artworks []ArtworkBrief, illustType int) []ArtworkBrief { + filtered := make([]ArtworkBrief, 0) + for _, artwork := range artworks { + if artwork.IllustType == illustType { + filtered = append(filtered, artwork) + } } - - return user, nil + return filtered } // Work is a generic type constraint.