mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
renaming in core/user
This commit is contained in:
+1
-1
@@ -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
@@ -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
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user