renaming in core/user

This commit is contained in:
perennial
2024-10-20 00:51:52 +11:00
parent 292fd774f7
commit e1d38e6f3d
4 changed files with 76 additions and 71 deletions
+9 -9
View File
@@ -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") }}
</div>
@@ -121,11 +121,11 @@
{{- if .Category == "" || .Category == "artworks" }}
<div class="d-flex justify-content-between align-items-center mb-4">
{* NOTE: using sentence case here to be more consistent with the rest of our UI, even though pixiv uses title case *}
<h2 class="d-flex align-items-baseline mb-0">Works<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.All }}</span></h2>
<h2 class="d-flex align-items-baseline mb-0">Works<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.All }}</span></h2>
</div>
{{ if len(.User.Illustrations) != 0 }}
<h2 class="d-flex align-items-baseline">Illustrations<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Illustrations }}</span></h2>
<h2 class="d-flex align-items-baseline">Illustrations<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Illustrations }}</span></h2>
<div class="row row-cols-2 row-cols-md-4 row-cols-lg-6 g-4">
{{- include "fragments/small-tn" .User.Illustrations }}
</div>
@@ -133,7 +133,7 @@
{{ if len(.User.Manga) != 0 }}
<hr class="my-4">
<h2 class="d-flex align-items-baseline">Manga<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Manga }}</span></h2>
<h2 class="d-flex align-items-baseline">Manga<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Manga }}</span></h2>
<div class="row row-cols-2 row-cols-md-4 row-cols-lg-6 g-4">
{{- include "fragments/small-tn" .User.Manga }}
</div>
@@ -141,7 +141,7 @@
{{ if len(.User.Novels) != 0 }}
<hr class="my-4">
<h2 class="d-flex align-items-baseline">Novels<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Novels }}</span></h2>
<h2 class="d-flex align-items-baseline">Novels<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Novels }}</span></h2>
<div class="row row-cols-1 row-cols-lg-2 g-4">
{{- range .User.Novels }}
<div class="col mt-1">
@@ -158,7 +158,7 @@
<!-- Illustrations category -->
{{- if .Category == "illustrations" }}
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="d-flex align-items-baseline mb-0">Illustrations<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Illustrations }}</span></h2>
<h2 class="d-flex align-items-baseline mb-0">Illustrations<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Illustrations }}</span></h2>
{{- combinedUrl := "/artworks-multi/" + joinArtworkIds(.User.Illustrations) }}
<a href="{{combinedUrl}}" class="custom-btn-secondary">View all</a>
</div>
@@ -170,7 +170,7 @@
<!-- Manga category -->
{{- if .Category == "manga" }}
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="d-flex align-items-baseline mb-0">Manga<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Manga }}</span></h2>
<h2 class="d-flex align-items-baseline mb-0">Manga<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Manga }}</span></h2>
{{- combinedUrl := "/artworks-multi/" + joinArtworkIds(.User.Manga) }}
<a href="{{combinedUrl}}" class="custom-btn-secondary">View all</a>
</div>
@@ -182,7 +182,7 @@
<!-- Novels category -->
{{- if .Category == "novels" }}
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="d-flex align-items-baseline mb-0">Novels<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Novels }}</span></h2>
<h2 class="d-flex align-items-baseline mb-0">Novels<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Novels }}</span></h2>
</div>
<div class="row row-cols-1 row-cols-lg-2 g-4">
{{- range .User.Novels }}
@@ -199,7 +199,7 @@
<!-- Bookmarks category -->
{{- if .Category == "bookmarks" }}
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="d-flex align-items-baseline mb-0">Bookmarks<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.CountInfo.Bookmarks }}</span></h2>
<h2 class="d-flex align-items-baseline mb-0">Bookmarks<span class="fs-6 bg-charcoal-surface1 rounded px-2 py-1 ms-2">{{ .User.WorkCounts.Bookmarks }}</span></h2>
</div>
{* NOTE: using .User.Artworks since there's no need to create a separate bookmarks slice; the category handles it *}
<div class="row row-cols-2 row-cols-md-4 row-cols-lg-6 g-4">
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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)
+65 -60
View File
@@ -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)