From e1d38e6f3dc9675acdee41d53d3c2cbcb4e85a1a Mon Sep 17 00:00:00 2001 From: perennial Date: Sun, 20 Oct 2024 00:51:52 +1100 Subject: [PATCH] renaming in core/user --- assets/views/user.jet.html | 18 +++--- core/artwork.go | 2 +- core/endpoints.go | 2 +- core/user.go | 125 +++++++++++++++++++------------------ 4 files changed, 76 insertions(+), 71 deletions(-) diff --git a/assets/views/user.jet.html b/assets/views/user.jet.html index 835b72f..719bb41 100644 --- a/assets/views/user.jet.html +++ b/assets/views/user.jet.html @@ -111,7 +111,7 @@ {{- baseURL := "/users/" + .User.ID + "/" }} {{- paths := slice("" + "#checkpoint", "illustrations" + "#checkpoint", "manga" + "#checkpoint", "novels" + "#checkpoint", "bookmarks" + "#checkpoint") }} {{- names := slice("Home", "Illustrations", "Manga", "Novels", "Bookmarks") }} - {{- categoryCounts := slice(.User.CountInfo.All, .User.CountInfo.Illustrations, .User.CountInfo.Manga, .User.CountInfo.Novels, .User.CountInfo.Bookmarks) }} + {{- categoryCounts := slice(.User.WorkCounts.All, .User.WorkCounts.Illustrations, .User.WorkCounts.Manga, .User.WorkCounts.Novels, .User.WorkCounts.Bookmarks) }} {{- yield UnderlineNavUser(baseURL=baseURL, paths=paths, names=names, categoryCounts=categoryCounts, activeState=.Category + "#checkpoint") }} @@ -121,11 +121,11 @@ {{- if .Category == "" || .Category == "artworks" }}
{* NOTE: using sentence case here to be more consistent with the rest of our UI, even though pixiv uses title case *} -

Works{{ .User.CountInfo.All }}

+

Works{{ .User.WorkCounts.All }}

{{ if len(.User.Illustrations) != 0 }} -

Illustrations{{ .User.CountInfo.Illustrations }}

+

Illustrations{{ .User.WorkCounts.Illustrations }}

{{- include "fragments/small-tn" .User.Illustrations }}
@@ -133,7 +133,7 @@ {{ if len(.User.Manga) != 0 }}
-

Manga{{ .User.CountInfo.Manga }}

+

Manga{{ .User.WorkCounts.Manga }}

{{- include "fragments/small-tn" .User.Manga }}
@@ -141,7 +141,7 @@ {{ if len(.User.Novels) != 0 }}
-

Novels{{ .User.CountInfo.Novels }}

+

Novels{{ .User.WorkCounts.Novels }}

{{- range .User.Novels }}
@@ -158,7 +158,7 @@ {{- if .Category == "illustrations" }}
-

Illustrations{{ .User.CountInfo.Illustrations }}

+

Illustrations{{ .User.WorkCounts.Illustrations }}

{{- combinedUrl := "/artworks-multi/" + joinArtworkIds(.User.Illustrations) }} View all
@@ -170,7 +170,7 @@ {{- if .Category == "manga" }}
-

Manga{{ .User.CountInfo.Manga }}

+

Manga{{ .User.WorkCounts.Manga }}

{{- combinedUrl := "/artworks-multi/" + joinArtworkIds(.User.Manga) }} View all
@@ -182,7 +182,7 @@ {{- if .Category == "novels" }}
-

Novels{{ .User.CountInfo.Novels }}

+

Novels{{ .User.WorkCounts.Novels }}

{{- range .User.Novels }} @@ -199,7 +199,7 @@ {{- if .Category == "bookmarks" }}
-

Bookmarks{{ .User.CountInfo.Bookmarks }}

+

Bookmarks{{ .User.WorkCounts.Bookmarks }}

{* NOTE: using .User.Artworks since there's no need to create a separate bookmarks slice; the category handles it *}
diff --git a/core/artwork.go b/core/artwork.go index 266545b..dc33dd7 100644 --- a/core/artwork.go +++ b/core/artwork.go @@ -420,7 +420,7 @@ func GetArtworkByID(r *http.Request, id string, full bool) (*Illust, error) { } // Fetch the user's recent artworks for display. - recent, err := getUserArtworkIDs(r, illust.UserID, idsString) + recent, err := fetchArtworkIDs(r, illust.UserID, idsString) if err != nil { cerr <- err return diff --git a/core/endpoints.go b/core/endpoints.go index 7493363..b21a395 100644 --- a/core/endpoints.go +++ b/core/endpoints.go @@ -43,7 +43,7 @@ func GetUserInformationURL(id string) string { return fmt.Sprintf(base, id) } -func GetUserArtworksURL(id string) string { +func GetUserWorksURL(id string) string { base := "https://www.pixiv.net/ajax/user/%s/profile/all" return fmt.Sprintf(base, id) diff --git a/core/user.go b/core/user.go index 37e3122..7970ae5 100644 --- a/core/user.go +++ b/core/user.go @@ -50,7 +50,7 @@ type FrequentTag struct { TranslatedName string `json:"tag_translation"` } -type CountInfo struct { +type WorkCounts struct { All int Illustrations int Manga int @@ -59,15 +59,17 @@ type CountInfo struct { } type User struct { - ID string `json:"userId"` - Name string `json:"name"` - Avatar string `json:"imageBig"` - Following int `json:"following"` - MyPixiv int `json:"mypixivCount"` - Comment HTML `json:"commentHtml"` - Webpage string `json:"webpage"` - SocialRaw json.RawMessage `json:"social"` - Artworks []ArtworkBrief `json:"artworks"` // this slice includes both illustrations and manga, but not novels + ID string `json:"userId"` + Name string `json:"name"` + Avatar string `json:"imageBig"` + Following int `json:"following"` + MyPixiv int `json:"mypixivCount"` + Comment HTML `json:"commentHtml"` + Webpage string `json:"webpage"` + SocialRaw json.RawMessage `json:"social"` + // the Artworks slice includes both illustrations and manga, but not novels + // it's also used to hold the []ArtworkBrief when viewing a user's bookmarks (TODO to separate?) + Artworks []ArtworkBrief `json:"artworks"` Illustrations []ArtworkBrief Manga []ArtworkBrief Novels []NovelBrief `json:"novels"` @@ -81,8 +83,8 @@ type User struct { IsFollowed bool `json:"isFollowed"` // Denotes whether the logged in user currently following the given user - // The following fields are internal to PixivFE, used to display the number of works for a given category - CountInfo CountInfo + // Used to display the number of works for a given category + WorkCounts WorkCounts } // GetUserProfile retrieves the user profile, including counts, artworks/bookmarks, and social data. @@ -102,13 +104,13 @@ func GetUserProfile(r *http.Request, id string, category UserWorkCategory, page return user, err } - // Fetch counts - if err := fetchUserCounts(r, &user, id); err != nil { + // Populate work counts + if err := populateWorkCounts(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 { + // Get populated works or bookmarks based on the category + if err := getPopulatedWorks(r, &user, id, category, page, getTags); err != nil { return user, err } @@ -125,32 +127,32 @@ func GetUserProfile(r *http.Request, id string, category UserWorkCategory, page return user, nil } -// fetchUserCounts retrieves and sets the CountInfo field of the User. -func fetchUserCounts(r *http.Request, user *User, id string) error { +// populateWorkCounts populates the WorkCounts struct. +func populateWorkCounts(r *http.Request, user *User, id string) error { // Get counts for illustrations and manga, as well as novels - _, countInfo, _, err := getUserArtworksIDAndSeries(r, id, CategoryAny, 1) + _, countInfo, _, err := fetchWorkIDsAndSeriesData(r, id, CategoryAny, 1) if err != nil { return err } - user.CountInfo = countInfo + user.WorkCounts = countInfo // Get count for bookmarks - _, bookmarksCount, err := getUserBookmarks(r, id, "show", 1) + _, bookmarksCount, err := fetchBookmarks(r, id, "show", 1) if err != nil { return err } - user.CountInfo.Bookmarks = bookmarksCount + user.WorkCounts.Bookmarks = bookmarksCount return nil } -// fetchUserWorks retrieves and sets artworks or bookmarks based on the category. +// getPopulatedWorks fetches then populates information for a user's works 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 { +// It also handles a user's frequently used tags and series data. +func getPopulatedWorks(r *http.Request, user *User, id string, category UserWorkCategory, page int, getTags bool) error { if category == CategoryBookmarks { // Fetch bookmarks - works, count, err := getUserBookmarks(r, id, "show", page) + works, count, err := fetchBookmarks(r, id, "show", page) if err != nil { return err } @@ -162,8 +164,11 @@ func fetchUserWorks(r *http.Request, user *User, id string, category UserWorkCat return nil } - // Fetch artworks or novels - userWorkIDs, countInfo, series, err := getUserArtworksIDAndSeries(r, id, category, page) + // Fetch work IDs and series data + // + // At this stage, these are only simple string slices that are not populated, + // as pixiv only returns the work IDs at the GetUserWorksURL endpoint. + userWorkIDs, countInfo, series, err := fetchWorkIDsAndSeriesData(r, id, category, page) if err != nil { return err } @@ -198,11 +203,11 @@ func fetchUserWorks(r *http.Request, user *User, id string, category UserWorkCat println(illustrationIDs == "") - // Fetch and process artworks if they exist + // Fetch and populate []ArtworkBrief for the IDs if illustrationIDs != "" || mangaIDs != "" { - // Combine Illustration and Manga IDs + // Combine illustration and manga IDs ids := illustrationIDs + mangaIDs - artworks, err := fetchAndProcessItems[ArtworkBrief](r, id, ids, getUserArtworkIDs) + artworks, err := fetchPopulatedBriefs[ArtworkBrief](r, id, ids, fetchArtworkIDs) if err != nil { return err } @@ -212,17 +217,17 @@ func fetchUserWorks(r *http.Request, user *User, id string, category UserWorkCat user.Manga = filterArtworksByType(artworks, 1) } - // Fetch and process novels if they exist + // Fetch and populate []NovelBrief for the IDs if novelIDs != "" { ids := novelIDs - novels, err := fetchAndProcessItems[NovelBrief](r, id, ids, getUserNovelIDs) + novels, err := fetchPopulatedBriefs[NovelBrief](r, id, ids, fetchNovelIDs) if err != nil { return err } user.Novels = novels } - // Fetch frequent tags if requested + // Fetch a user's frequently used tags if requested if getTags { var tagsIDs []string @@ -250,7 +255,7 @@ func fetchUserWorks(r *http.Request, user *User, id string, category UserWorkCat } else { // Concatenate IDs form a single string ids := strings.Join(tagsIDs, "") - user.FrequentTags, err = getUserFrequentTags(r, ids, category) + user.FrequentTags, err = fetchFrequentTags(r, ids, category) if err != nil { return fmt.Errorf("failed to get user frequent tags: %w", err) } @@ -280,9 +285,9 @@ func (n NovelBrief) GetID() string { return n.ID } -// fetchAndProcessItems is a generic function that fetches and processes items of type T, +// fetchPopulatedBriefs is a generic function that fetches and processes items of type T, // where T must implement the HasID interface. -func fetchAndProcessItems[T HasID]( +func fetchPopulatedBriefs[T HasID]( r *http.Request, id, ids string, fetchFunc func(*http.Request, string, string) ([]T, error), @@ -347,15 +352,15 @@ func (m *IntStringMap) UnmarshalJSON(data []byte) error { return nil } -// getUserArtworksIDAndSeries retrieves artwork IDs and series information for a user. +// fetchWorkIDsAndSeriesData fetches work IDs and series data for a user. // // It returns separate ID strings for illustrations, manga, and novels encapsulated in UserWorkIDs. -func getUserArtworksIDAndSeries(r *http.Request, id string, category UserWorkCategory, page int) (userWorkIDs UserWorkIDs, countInfo CountInfo, series json.RawMessage, err error) { - URL := GetUserArtworksURL(id) +func fetchWorkIDsAndSeriesData(r *http.Request, id string, category UserWorkCategory, page int) (userWorkIDs UserWorkIDs, countInfo WorkCounts, series json.RawMessage, err error) { + URL := GetUserWorksURL(id) resp, err := API_GET_UnwrapJson(r.Context(), URL, "", r.Header) if err != nil { - return UserWorkIDs{}, CountInfo{}, nil, err + return UserWorkIDs{}, WorkCounts{}, nil, err } resp = session.ProxyImageUrl(r, resp) @@ -370,10 +375,10 @@ func getUserArtworksIDAndSeries(r *http.Request, id string, category UserWorkCat err = json.Unmarshal([]byte(resp), &body) if err != nil { - return UserWorkIDs{}, CountInfo{}, nil, fmt.Errorf("failed to unmarshal response body: %w", err) + return UserWorkIDs{}, WorkCounts{}, nil, fmt.Errorf("failed to unmarshal response body: %w", err) } - countInfo = CountInfo{} + countInfo = WorkCounts{} var illusts IntStringMap var manga IntStringMap @@ -438,17 +443,17 @@ func getUserArtworksIDAndSeries(r *http.Request, id string, category UserWorkCat // Compute slice bounds for each category startIllust, endIllust, err := computeSliceBounds(page, worksPerPage, len(illustIDs)) if err != nil { - return UserWorkIDs{}, CountInfo{}, nil, err + return UserWorkIDs{}, WorkCounts{}, nil, err } startManga, endManga, err := computeSliceBounds(page, worksPerPage, len(mangaIDs)) if err != nil { - return UserWorkIDs{}, CountInfo{}, nil, err + return UserWorkIDs{}, WorkCounts{}, nil, err } startNovel, endNovel, err := computeSliceBounds(page, worksPerPage, len(novelIDs)) if err != nil { - return UserWorkIDs{}, CountInfo{}, nil, err + return UserWorkIDs{}, WorkCounts{}, nil, err } // Build the ID strings for each category @@ -489,8 +494,8 @@ func handleSeriesData(series json.RawMessage, category UserWorkCategory) ([]Nove return novelSeries, mangaSeries } -// getUserFrequentTags retrieves frequent tags for a user based on category. -func getUserFrequentTags(r *http.Request, ids string, category UserWorkCategory) ([]FrequentTag, error) { +// fetchFrequentTags fetches a user's frequently used tags, based on category. +func fetchFrequentTags(r *http.Request, ids string, category UserWorkCategory) ([]FrequentTag, error) { var tags []FrequentTag var URL string @@ -525,8 +530,8 @@ func getUserFrequentTags(r *http.Request, ids string, category UserWorkCategory) // Work is a generic type constraint. type Work interface{} -// getWorkIDs is a generic helper function to fetch work IDs. -func getWorkIDs[T Work](r *http.Request, url string) ([]T, error) { +// fetchWorkIDs is a generic helper function to fetch work IDs. +func fetchWorkIDs[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 @@ -559,11 +564,11 @@ func getWorkIDs[T Work](r *http.Request, url string) ([]T, error) { return works, nil } -// getUserArtworkIDs fetches the list of artwork IDs for a user (without metadata). -func getUserArtworkIDs(r *http.Request, id, ids string) ([]ArtworkBrief, error) { +// fetchArtworkIDs fetches the list of artwork IDs for a user (without other data). +func fetchArtworkIDs(r *http.Request, id, ids string) ([]ArtworkBrief, error) { URL := GetUserFullArtworkURL(id, ids) - works, err := getWorkIDs[ArtworkBrief](r, URL) + works, err := fetchWorkIDs[ArtworkBrief](r, URL) if err != nil { return nil, err } @@ -571,11 +576,11 @@ func getUserArtworkIDs(r *http.Request, id, ids string) ([]ArtworkBrief, error) return works, nil } -// getUserNovelIDs fetches the list of novel IDs for a user (without metadata). -func getUserNovelIDs(r *http.Request, id, ids string) ([]NovelBrief, error) { +// fetchNovelIDs fetches the list of novel IDs for a user (without other data). +func fetchNovelIDs(r *http.Request, id, ids string) ([]NovelBrief, error) { URL := GetUserFullNovelURL(id, ids) - works, err := getWorkIDs[NovelBrief](r, URL) + works, err := fetchWorkIDs[NovelBrief](r, URL) if err != nil { return nil, err } @@ -583,11 +588,11 @@ func getUserNovelIDs(r *http.Request, id, ids string) ([]NovelBrief, error) { return works, nil } -// getUserBookmarks fetches the list of bookmarks for a user (with metadata). +// fetchBookmarks fetches the list of bookmarks for a user (with other data). // -// This function cannot be neatly refactored to use getWorkIDs due -// to having a different API response structure -func getUserBookmarks(r *http.Request, id, mode string, page int) ([]ArtworkBrief, int, error) { +// This function cannot be neatly refactored to use getWorkIDs due to having +// a different API response structure. +func fetchBookmarks(r *http.Request, id, mode string, page int) ([]ArtworkBrief, int, error) { page-- URL := GetUserBookmarksURL(id, mode, page)